Exemplo n.º 1
0
        public void EnterWriteLock()
        {
            ClientSpinWait sw = new ClientSpinWait();

            do
            {
                int state = rwlock;
                if (state < RwWrite)
                {
                    if (ClientInterlocked.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 && ClientInterlocked.CompareExchange(ref rwlock, state | RwWait, state) != state)
                {
                    state = rwlock;
                }
                // Before falling to sleep
                while (rwlock > RwWait)
                {
                    sw.SpinOnce();
                }
            } while (true);
        }
Exemplo n.º 2
0
        public static void SpinUntil(MyFunc <bool> condition)
        {
            ClientSpinWait sw = new ClientSpinWait();

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

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

            return true;
        }
 static public int constructor(IntPtr l)
 {
     try {
         System.Threading.ClientSpinWait o;
         o = new System.Threading.ClientSpinWait();
         pushValue(l, true);
         pushValue(l, o);
         return(2);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
        public void EnterReadLock()
        {
            ClientSpinWait sw = new ClientSpinWait();
              do {
            while ((rwlock & (RwWrite | RwWait)) > 0)
              sw.SpinOnce();

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

            ClientInterlocked.Add(ref rwlock, -RwRead);
              } while (true);
        }
Exemplo n.º 6
0
        public static bool SpinUntil(MyFunc <bool> condition, int millisecondsTimeout)
        {
            ClientSpinWait sw    = new ClientSpinWait();
            ClientWatch    watch = ClientWatch.StartNew();

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

            return(true);
        }
Exemplo n.º 7
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);
                }
            }
        }
 public void EnterWriteLock()
 {
     ClientSpinWait sw = new ClientSpinWait();
       do {
     int state = rwlock;
     if (state < RwWrite) {
       if (ClientInterlocked.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 && ClientInterlocked.CompareExchange(ref rwlock, state | RwWait, state) != state)
       state = rwlock;
     // Before falling to sleep
     while (rwlock > RwWait)
       sw.SpinOnce();
       } while (true);
 }
Exemplo n.º 9
0
        public void EnterReadLock()
        {
            ClientSpinWait sw = new ClientSpinWait();

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

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

                ClientInterlocked.Add(ref rwlock, -RwRead);
            } while (true);
        }
 public static void SpinUntil(MyFunc<bool> condition)
 {
     ClientSpinWait sw = new ClientSpinWait ();
     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 = 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);
                }
            }
        }