Ejemplo n.º 1
0
        public TracedLock(TraceableLock traceableLock, TimeSpan?timeout)
        {
            if (timeout == null)
            {
                timeout = TimeSpan.FromSeconds(30);
            }

            _status        = TracedLockStatus.Acquiring;      //useful for detecting dead-lock
            _traceableLock = traceableLock;

            LockAnalyzer.Lock(traceableLock);

            //collect useful information about the context such
            //as stacktrace, time to acquire the lock(T1)
            if (Monitor.TryEnter(_traceableLock, timeout.Value))
            {
                _status = TracedLockStatus.Acquired;
            }
            else
            {
                _status = TracedLockStatus.Timedout;
            }
            //lock is acuired, so collect acquired-time(T2)
            //[T2-T1 = time taken to acquire lock]
        }
Ejemplo n.º 2
0
        public static void Release(TraceableLock traceableLock)
        {
            lock (Sync)
            {
                var threadId = Thread.CurrentThread.ManagedThreadId;

                if (ActiveLockChains[threadId].Count > 0 && ActiveLockChains[threadId].Last() == traceableLock)
                    ActiveLockChains[threadId].Remove(traceableLock);
            }
        }
Ejemplo n.º 3
0
 static IEnumerable<TraceableLock> RequiredLocks(TraceableLock traceableLock)
 {
     if (Requires.ContainsKey(traceableLock))
         foreach (var tl in Requires[traceableLock])
         {
             yield return tl;
             foreach (TraceableLock tl2 in RequiredLocks(tl))
             {
                 yield return tl2;
             }
         }
 }
Ejemplo n.º 4
0
        public static void Release(TraceableLock traceableLock)
        {
            lock (Sync)
            {
                var threadId = Thread.CurrentThread.ManagedThreadId;

                if (ActiveLockChains[threadId].Count > 0 && ActiveLockChains[threadId].Last() == traceableLock)
                {
                    ActiveLockChains[threadId].Remove(traceableLock);
                }
            }
        }
Ejemplo n.º 5
0
        static IEnumerable <TraceableLock> RequiredLocks(TraceableLock traceableLock)
        {
            if (Requires.ContainsKey(traceableLock))
            {
                foreach (var tl in Requires[traceableLock])
                {
                    yield return(tl);

                    foreach (TraceableLock tl2 in RequiredLocks(tl))
                    {
                        yield return(tl2);
                    }
                }
            }
        }
Ejemplo n.º 6
0
        public static void Lock(TraceableLock traceableLock)
        {
            lock (Sync)
            {
                var threadId = Thread.CurrentThread.ManagedThreadId;
                if (!ActiveLockChains.ContainsKey(threadId))
                    ActiveLockChains[threadId] = new List<TraceableLock>();

                if (ActiveLockChains[threadId].Contains(traceableLock))
                    return; // Same thread using the same lock

                ActiveLockChains[threadId].Add(traceableLock);
                if (!VerifyLockChain(ActiveLockChains[threadId]))
                {
                    var copy = ActiveLockChains[threadId].ToList();
                    ActiveLockChains[threadId].Remove(ActiveLockChains[threadId].Last());
                    PossibleDeadlock(copy);
                }
            }
        }
Ejemplo n.º 7
0
        private static bool VerifyLockChain(IEnumerable <TraceableLock> thisLockChain)
        {
            var           chainRequredLocks = new List <TraceableLock>();
            TraceableLock prevLock          = null;

            foreach (TraceableLock traceableLock in thisLockChain)
            {
                if (chainRequredLocks.Contains(traceableLock))
                {
                    return(false);
                }

                if (Requires.ContainsKey(traceableLock))
                {
                    chainRequredLocks.AddRange(RequiredLocks(traceableLock));
                }

                if (prevLock != null)
                {
                    if (Requires.ContainsKey(traceableLock))
                    {
                        if (!Requires[traceableLock].Contains(prevLock))
                        {
                            Requires[traceableLock].Add(prevLock);
                        }
                    }
                    else
                    {
                        Requires[traceableLock] = new List <TraceableLock> {
                            prevLock
                        };
                    }
                }
                prevLock = traceableLock;
            }

            return(true);
        }
Ejemplo n.º 8
0
        public static void Lock(TraceableLock traceableLock)
        {
            lock (Sync)
            {
                var threadId = Thread.CurrentThread.ManagedThreadId;
                if (!ActiveLockChains.ContainsKey(threadId))
                {
                    ActiveLockChains[threadId] = new List <TraceableLock>();
                }

                if (ActiveLockChains[threadId].Contains(traceableLock))
                {
                    return;                     // Same thread using the same lock
                }
                ActiveLockChains[threadId].Add(traceableLock);
                if (!VerifyLockChain(ActiveLockChains[threadId]))
                {
                    var copy = ActiveLockChains[threadId].ToList();
                    ActiveLockChains[threadId].Remove(ActiveLockChains[threadId].Last());
                    PossibleDeadlock(copy);
                }
            }
        }
Ejemplo n.º 9
0
        public TracedLock(TraceableLock traceableLock, TimeSpan? timeout)
        {
            if (timeout == null)
                timeout = TimeSpan.FromSeconds(30);

            _status = TracedLockStatus.Acquiring; //useful for detecting dead-lock
            _traceableLock = traceableLock;

            LockAnalyzer.Lock(traceableLock);

            //collect useful information about the context such
            //as stacktrace, time to acquire the lock(T1)
            if (Monitor.TryEnter(_traceableLock, timeout.Value))
            {
                _status = TracedLockStatus.Acquired;
            }
            else
            {
                _status = TracedLockStatus.Timedout;
            }
            //lock is acuired, so collect acquired-time(T2)
            //[T2-T1 = time taken to acquire lock]
        }
Ejemplo n.º 10
0
 public static TracedLock Lock(TraceableLock traceableLock, TimeSpan?maxWaitTime = null)
 {
     return(new TracedLock(traceableLock, maxWaitTime));
 }
Ejemplo n.º 11
0
 public static TracedLock Lock(TraceableLock traceableLock, TimeSpan? maxWaitTime = null)
 {
     return new TracedLock(traceableLock, maxWaitTime);
 }