public static void Enter(object obj, ref bool taken) { Monitor.Enter(obj); GC.KeepAlive(taken); ThreadingHelper.MemoryBarrier(); taken = true; }
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 bool TryUpdate(T newValue, T expectedValue, IEqualityComparer <T> comparer) { CaptureAndWait(); if (!comparer.Equals(base.Value, expectedValue)) { return(false); } ThreadingHelper.MemoryBarrier(); base.Value = newValue; ThreadingHelper.MemoryBarrier(); return(true); }
public T Update(Func <T, T> updateValueFactory) { if (updateValueFactory == null) { throw new ArgumentNullException("updateValueFactory"); } CaptureAndWait(); var result = updateValueFactory(base.Value); base.Value = result; ThreadingHelper.MemoryBarrier(); return(result); }
public static void TaskThreadIsBackgroundWhileLooping() { var signal = new[] { 0 }; Thread thread = null; TaskEx.Run ( () => { thread = Thread.CurrentThread; var spinWait = new SpinWait(); while (Volatile.Read(ref signal[0]) == 0) { spinWait.SpinOnce(); } } ); ThreadingHelper.SpinWaitWhileNull(ref thread); Assert.IsTrue((thread.ThreadState & ThreadState.Background) != 0); ThreadingHelper.MemoryBarrier(); Volatile.Write(ref signal[0], 1); }
public bool Commit() { if (_currentTransaction != this) { throw new InvalidOperationException("Cannot commit a non-current transaction."); } ThreadingHelper.MemoryBarrier(); try { if (!CheckValue()) { //the resources has been modified by another thread return(false); } try { ThreadingHelper.SpinWaitUntil(() => Context.ClaimSlot(out LockSlot)); LockSlot.Value = Thread.CurrentThread; if (!Capture()) { //Nothing to commit return(true); } ThreadingHelper.MemoryBarrier(); if (!CheckCapture() || !CheckValue()) { //the resources has been claimed by another thread return(false); } var written = false; foreach (var resource in WriteLog) { if (resource.Key.Commit()) { written = true; } else { //unexpected if (written) { // TODO - the transaction was partially written, this should not be possible. System.Diagnostics.Debug.Fail("unexpected - partially committed transaction"); } return(false); } } return(true); } finally { if (LockSlot != null) { LockSlot.Close(); LockSlot = null; } } } finally { Release(false); } }
public static void MemoryBarrier() { ThreadingHelper.MemoryBarrier(); }