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); }
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); }
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); }
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); }
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>>(); }
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> >(); }
internal bool TryGetSlot(out LockableSlot slot) { return(_slots.Value.TryGetValue(Thread.CurrentThread, out slot) && slot != null); }