Esempio n. 1
0
        public TrackedLockResult TryEnter(out Thread previousOwner, bool recursive = false)
        {
            var currentThread = Thread.CurrentThread;

            previousOwner = Interlocked.CompareExchange(ref _HeldBy, currentThread, null);
            var acquired = previousOwner == null;

            TrackedLockFailureReason?failureReason = null;

            if (!acquired)
            {
                failureReason = (previousOwner == currentThread)
                    ? TrackedLockFailureReason.HeldByCurrentThread
                    : TrackedLockFailureReason.HeldByOtherThread;
            }

            if (recursive && (failureReason == TrackedLockFailureReason.HeldByCurrentThread))
            {
                _RecursionCount += 1;
                return(new TrackedLockResult());
            }

            var result = new TrackedLockResult(failureReason);

#if LOCK_TRACING
            Console.WriteLine(
                "TryEnter {0} => {1}; result = {2}",
                previousOwner != null ? previousOwner.Name : "<null>",
                _HeldBy != null ? _HeldBy.Name : "<null>",
                result
                );
#endif

            return(result);
        }
Esempio n. 2
0
        public TrackedLockResult TryEnter (out Thread previousOwner, bool recursive = false) {
            var currentThread = Thread.CurrentThread;
            previousOwner = Interlocked.CompareExchange(ref _HeldBy, currentThread, null);
            var acquired = previousOwner == null;

            TrackedLockFailureReason? failureReason = null;

            if (!acquired) {
                failureReason = (previousOwner == currentThread)
                    ? TrackedLockFailureReason.HeldByCurrentThread
                    : TrackedLockFailureReason.HeldByOtherThread;
            }

            if (recursive && (failureReason == TrackedLockFailureReason.HeldByCurrentThread)) {
                _RecursionCount += 1;
                return new TrackedLockResult();
            }

            var result = new TrackedLockResult(failureReason);

#if LOCK_TRACING
            Console.WriteLine(
                "TryEnter {0} => {1}; result = {2}", 
                previousOwner != null ? previousOwner.Name : "<null>",
                _HeldBy != null ? _HeldBy.Name : "<null>", 
                result
            );
#endif

            return result;
        }