예제 #1
0
 public static void Enter(object obj, ref bool taken)
 {
     Monitor.Enter(obj);
     GC.KeepAlive(taken);
     ThreadingHelper.MemoryBarrier();
     taken = true;
 }
예제 #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.Release(lockSlot);
                    needleLock.Release();
                }
                needleLocks.Clear();
            }
            lockSlot.Close();
            ThreadingHelper.MemoryBarrier();
            _parent = null;
            GC.SuppressFinalize(this);
        }
예제 #3
0
 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);
 }
예제 #4
0
        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);
        }
예제 #5
0
        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);
        }
예제 #6
0
        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);
            }
        }
예제 #7
0
 public static void MemoryBarrier()
 {
     ThreadingHelper.MemoryBarrier();
 }