예제 #1
0
        public void Dispose()
        {
            var lockSlot = Interlocked.Exchange(ref _lockSlot, null);

            if (lockSlot == null)
            {
                return;
            }
            var context = Interlocked.Exchange(ref _context, null);

            if (context != null)
            {
                context.Slot = _parent;
            }
            var needleLocks = Interlocked.Exchange(ref _needleLocks, null);

            if (needleLocks != null)
            {
                foreach (var needleLock in needleLocks)
                {
                    needleLock.Release(lockSlot);
                    needleLock.Release();
                }
                needleLocks.Clear();
            }
            lockSlot.Close();
            ThreadingHelper.MemoryBarrier();
            _parent = null;
            GC.SuppressFinalize(this);
        }
예제 #2
0
 public void Dispose()
 {
     var lockslot = Interlocked.Exchange(ref _lockSlot, null);
     if (lockslot == null)
     {
         return;
     }
     var context = Interlocked.Exchange(ref _context, null);
     if (context != null)
     {
         context.Slot = _parent;
     }
     var needleLocks = Interlocked.Exchange(ref _needleLocks, null);
     if (needleLocks != null)
     {
         foreach (var needleLock in needleLocks)
         {
             needleLock.Uncapture(lockslot);
             needleLock.Release();
         }
         needleLocks.Clear();
     }
     lockslot.Close();
     Thread.MemoryBarrier();
     _parent = null;
     GC.SuppressFinalize(this);
 }
예제 #3
0
        private void CaptureAndWait(LockableSlot slot)
        {
            Capture(slot);
            var thread = Thread.CurrentThread;

            // The reason while I cannot make an smarter wait function:
            // If another thread changed _needleLock.Value after the check but before the starting to wait, the wait will not finish.
            ThreadingHelper.SpinWaitUntil(() => _needleLock.Value == thread);
        }
예제 #4
0
        private void Capture(LockableSlot slot)
        {
            var lockslot = slot.LockSlot;

            if (ReferenceEquals(lockslot, null))
            {
                throw new InvalidOperationException("The current thread has not entered the LockableContext of this LockableNeedle.");
            }
            _needleLock.Capture(lockslot);
            slot.Add(_needleLock);
        }
예제 #5
0
 internal LockableSlot(LockableContext context)
 {
     _context = context;
     _parent = _context.Slot;
     _context.Slot = this;
     // --
     LockSlot<Thread> lockSlot = null;
     ThreadingHelper.SpinWaitUntil(() => _context.Context.ClaimSlot(out lockSlot));
     lockSlot.Value = Thread.CurrentThread;
     _lockSlot = lockSlot;
     _needleLocks = new List<NeedleLock<Thread>>();
 }
예제 #6
0
        internal LockableSlot(LockableContext context)
        {
            _context      = context;
            _parent       = _context.Slot;
            _context.Slot = this;
            // --
            LockSlot <Thread> lockSlot = null;

            ThreadingHelper.SpinWaitUntil(() => _context.Context.ClaimSlot(out lockSlot));
            lockSlot.Value = Thread.CurrentThread;
            _lockSlot      = lockSlot;
            _needleLocks   = new List <NeedleLock <Thread> >();
        }
예제 #7
0
 internal bool TryGetSlot(out LockableSlot slot)
 {
     return(_slots.Value.TryGetValue(Thread.CurrentThread, out slot) && slot != null);
 }