Increment() public static method

public static Increment ( int &location ) : int
location int
return int
コード例 #1
0
        public void Exit(bool useMemoryBarrier)
        {
            RuntimeHelpers.PrepareConstrainedRegions();
            try { }
            finally
            {
                if (isThreadOwnerTrackingEnabled && !IsHeldByCurrentThread)
                {
                    throw new SynchronizationLockException("Current thread is not the owner of this lock");
                }

                threadWhoTookLock = int.MinValue;
                do
                {
                    if (useMemoryBarrier)
                    {
                        ClientInterlocked.Increment(ref ticket.Value);
                    }
                    else
                    {
                        ticket.Value++;
                    }
                } while (stallTickets != null && ((ClientConcurrentOrderedList <int>)stallTickets).TryRemove(ticket.Value));
            }
        }
コード例 #2
0
        public void Enter(ref bool lockTaken)
        {
            if (lockTaken)
            {
                throw new ArgumentException("lockTaken", "lockTaken must be initialized to false");
            }
            if (isThreadOwnerTrackingEnabled && IsHeldByCurrentThread)
            {
                throw new LockRecursionException();
            }

            int slot = -1;

            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                slot = ClientInterlocked.Increment(ref ticket.Users) - 1;

                ClientSpinWait wait = new ClientSpinWait();
                while (slot != ticket.Value)
                {
                    wait.SpinOnce();

                    while (stallTickets != null && ((ClientConcurrentOrderedList <int>)stallTickets).TryRemove(ticket.Value))
                    {
                        ++ticket.Value;
                    }
                }
            }
            finally
            {
                if (slot == ticket.Value)
                {
                    lockTaken         = true;
                    threadWhoTookLock = Thread.CurrentThread.ManagedThreadId;
                }
                else if (slot != -1)
                {
                    // We have been interrupted, initialize stallTickets
                    if (stallTickets == null)
                    {
                        ClientInterlocked.CompareExchange(ref stallTickets, new ClientConcurrentOrderedList <int>(), null);
                    }
                    ((ClientConcurrentOrderedList <int>)stallTickets).TryAdd(slot);
                }
            }
        }