示例#1
0
        public TrackedLockResult TryBlockingEnter(out TrackedLockCollection.DeadlockInfo deadlock, bool recursive = false)
        {
            TrackedLockCollection.Wait wait;
            deadlock = null;

            while (true)
            {
                var result = TryEnter(recursive);
                if (result.FailureReason != TrackedLockFailureReason.HeldByOtherThread)
                {
                    return(result);
                }

                if (Collection.TryCreateWait(this, out deadlock, out wait))
                {
                    using (wait) {
                        result = TryEnter(recursive);

                        if (result.FailureReason == TrackedLockFailureReason.HeldByOtherThread)
                        {
                            wait.Block();
                        }
                        else
                        {
                            return(result);
                        }
                    }
                }
                else
                {
                    return(new TrackedLockResult(TrackedLockFailureReason.Deadlock));
                }
            }
        }
示例#2
0
        public TrackedLockResult TryBlockingEnter(out TrackedLockCollection.DeadlockInfo deadlock, bool recursive = false, int timeoutMs = -1)
        {
            bool iterating = true;

            TrackedLockCollection.Wait wait;
            deadlock = null;

            while (iterating)
            {
                var result = TryEnter(recursive);
                if (result.FailureReason != TrackedLockFailureReason.HeldByOtherThread)
                {
                    return(result);
                }

                EnsureTracked();

                if (Collection.TryCreateWait(WeakSelf, out deadlock, out wait))
                {
                    using (wait) {
                        result = TryEnter(recursive);

                        if (result.FailureReason == TrackedLockFailureReason.HeldByOtherThread)
                        {
                            var finishedWaiting = wait.Block(timeoutMs);

                            if (!finishedWaiting)
                            {
                                iterating = false;
                            }
                        }
                        else
                        {
                            return(result);
                        }
                    }
                }
                else
                {
                    return(new TrackedLockResult(TrackedLockFailureReason.Deadlock));
                }
            }

            return(new TrackedLockResult(TrackedLockFailureReason.HeldByOtherThread));
        }