SpinOnce() public method

public SpinOnce ( ) : void
return void
示例#1
0
        public void EnterWriteLock()
        {
            ServerSpinWait sw = new ServerSpinWait();

            do
            {
                int state = rwlock;
                if (state < RwWrite)
                {
                    if (Interlocked.CompareExchange(ref rwlock, RwWrite, state) == state)
                    {
                        return;
                    }
                    state = rwlock;
                }
                // We register our interest in taking the Write lock (if upgradeable it's already done)
                while ((state & RwWait) == 0 && Interlocked.CompareExchange(ref rwlock, state | RwWait, state) != state)
                {
                    state = rwlock;
                }
                // Before falling to sleep
                while (rwlock > RwWait)
                {
                    sw.SpinOnce();
                }
            } while (true);
        }
示例#2
0
        public static void SpinUntil(MyFunc <bool> condition)
        {
            ServerSpinWait sw = new ServerSpinWait();

            while (!condition())
            {
                sw.SpinOnce();
            }
        }
        public static bool SpinUntil(MyFunc<bool> condition, int millisecondsTimeout)
        {
            ServerSpinWait sw = new ServerSpinWait ();
            ServerWatch watch = ServerWatch.StartNew ();

            while (!condition ()) {
                if (watch.ElapsedMilliseconds > millisecondsTimeout)
                    return false;
                sw.SpinOnce ();
            }

            return true;
        }
        public void EnterReadLock()
        {
            ServerSpinWait sw = new ServerSpinWait();
              do {
            while ((rwlock & (RwWrite | RwWait)) > 0)
              sw.SpinOnce();

            if ((Interlocked.Add(ref rwlock, RwRead) & (RwWait | RwWait)) == 0)
              return;

            Interlocked.Add(ref rwlock, -RwRead);
              } while (true);
        }
示例#5
0
        public static bool SpinUntil(MyFunc <bool> condition, int millisecondsTimeout)
        {
            ServerSpinWait sw    = new ServerSpinWait();
            ServerWatch    watch = ServerWatch.StartNew();

            while (!condition())
            {
                if (watch.ElapsedMilliseconds > millisecondsTimeout)
                {
                    return(false);
                }
                sw.SpinOnce();
            }

            return(true);
        }
示例#6
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 = Interlocked.Increment(ref ticket.Users) - 1;

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

                    while (stallTickets != null && 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)
                    {
                        Interlocked.CompareExchange(ref stallTickets, new ServerConcurrentOrderedList <int>(), null);
                    }
                    stallTickets.TryAdd(slot);
                }
            }
        }
 public void EnterWriteLock()
 {
     ServerSpinWait sw = new ServerSpinWait();
       do {
     int state = rwlock;
     if (state < RwWrite) {
       if (Interlocked.CompareExchange(ref rwlock, RwWrite, state) == state)
     return;
       state = rwlock;
     }
     // We register our interest in taking the Write lock (if upgradeable it's already done)
     while ((state & RwWait) == 0 && Interlocked.CompareExchange(ref rwlock, state | RwWait, state) != state)
       state = rwlock;
     // Before falling to sleep
     while (rwlock > RwWait)
       sw.SpinOnce();
       } while (true);
 }
示例#8
0
        public void EnterReadLock()
        {
            ServerSpinWait sw = new ServerSpinWait();

            do
            {
                while ((rwlock & (RwWrite | RwWait)) > 0)
                {
                    sw.SpinOnce();
                }

                if ((Interlocked.Add(ref rwlock, RwRead) & (RwWait | RwWait)) == 0)
                {
                    return;
                }

                Interlocked.Add(ref rwlock, -RwRead);
            } while (true);
        }
 public static void SpinUntil(MyFunc<bool> condition)
 {
     ServerSpinWait sw = new ServerSpinWait ();
     while (!condition ())
         sw.SpinOnce ();
 }
        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 = Interlocked.Increment (ref ticket.Users) - 1;

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

                    while (stallTickets != null && 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)
                        Interlocked.CompareExchange (ref stallTickets, new ServerConcurrentOrderedList<int> (), null);
                    stallTickets.TryAdd (slot);
                }
            }
        }