Ejemplo n.º 1
0
        public void Test()
        {
            AtomicInteger m = new AtomicInteger(0);

            Assert.IsTrue(m == 0);

            Assert.IsFalse(m.CompareAndSet(3, 2));
            Assert.IsTrue(m == 0);

            Assert.IsTrue(m.CompareAndSet(0, 2));
            Assert.IsTrue(m == 2);

            var rs = m + 3;// 2+3

            Assert.AreEqual(5, rs);

            Assert.AreEqual(4, m--);

            Assert.AreEqual(2, m - 2);

            Assert.AreEqual(6, m + 4);

            Assert.AreEqual(6, m.GetAndAdd(4));
            Assert.AreEqual(10, m);
        }
Ejemplo n.º 2
0
 private void CheckRunning()
 {
     if (!_stat.CompareAndSet(STAT_RUNNING, STAT_RUNNING))
     {
         throw new SpiderExceptoin("Already closed!");
     }
 }
Ejemplo n.º 3
0
 /**
  * Start the manager
  */
 public void start()
 {
     if (state.CompareAndSet(LATENT, STARTED) != LATENT)
     {
         throw new InvalidOperationException("Cannot be started more than once");
     }
     service.submit
     (
         new FutureTask <object>(CallableUtils.FromFunc <object>(() =>
     {
         processEvents();
         return(null);
     }))
     );
 }
Ejemplo n.º 4
0
 // wait for whatever we wait on
 /// <summary>Transmit the current buffer to bookkeeper.</summary>
 /// <remarks>
 /// Transmit the current buffer to bookkeeper.
 /// Synchronised at the FSEditLog level. #write() and #setReadyToFlush()
 /// are never called at the same time.
 /// </remarks>
 /// <exception cref="System.IO.IOException"/>
 private void Transmit()
 {
     if (!transmitResult.CompareAndSet(BKException.Code.Ok, BKException.Code.Ok))
     {
         throw new IOException("Trying to write to an errored stream;" + " Error code : ("
                               + transmitResult.Get() + ") " + BKException.GetMessage(transmitResult.Get()));
     }
     if (bufCurrent.GetLength() > 0)
     {
         byte[] entry = Arrays.CopyOf(bufCurrent.GetData(), bufCurrent.GetLength());
         lh.AsyncAddEntry(entry, this, null);
         bufCurrent.Reset();
         outstandingRequests.IncrementAndGet();
     }
 }
 public void Run()
 {
     try
     {
         barrier.Await();
         long i    = iters;
         int  y    = 0;
         int  succ = 0;
         while (i > 0)
         {
             for (int k = 0; k < CASLoops.innerPerOuter; ++k)
             {
                 int x = obj.Value;
                 int z = y + LoopHelpers.compute6(x);
                 if (obj.CompareAndSet(x, z))
                 {
                     ++succ;
                 }
                 y = LoopHelpers.compute7(z);
             }
             i -= CASLoops.innerPerOuter;
         }
         CASLoops.sum.AddDeltaAndReturnPreviousValue(obj.Value);
         CASLoops.successes.AddDeltaAndReturnPreviousValue(succ);
         barrier.Await();
     }
     catch (System.Exception ie)
     {
         return;
     }
 }
 public bool CheckAndSetPartitionCount(int newPartitionCount)
 {
     if (_partitionCount.CompareAndSet(0, newPartitionCount))
     {
         return(true);
     }
     return(_partitionCount.Get() == newPartitionCount);
 }
Ejemplo n.º 7
0
        internal void SubtractFlushedNumDocs(int numFlushed)
        {
            int oldValue = NumDocsInRAM.Get();

            while (!NumDocsInRAM.CompareAndSet(oldValue, oldValue - numFlushed))
            {
                oldValue = NumDocsInRAM.Get();
            }
        }
Ejemplo n.º 8
0
 private int NextIdx()
 {
     while (true)
     {
         int current = currentIdx.Get();
         int next    = (current + 1) % providers.Length;
         if (currentIdx.CompareAndSet(current, next))
         {
             return(current);
         }
     }
 }
Ejemplo n.º 9
0
        internal void IncRef()
        {
            int count;

            while ((count = @ref.Get()) > 0)
            {
                if (@ref.CompareAndSet(count, count + 1))
                {
                    return;
                }
            }
            throw new AlreadyClosedException("SegmentCoreReaders is already closed");
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Expert: increments the refCount of this IndexReader
        /// instance only if the IndexReader has not been closed yet
        /// and returns <code>true</code> iff the refCount was
        /// successfully incremented, otherwise <code>false</code>.
        /// If this method returns <code>false</code> the reader is either
        /// already closed or is currently being closed. Either way this
        /// reader instance shouldn't be used by an application unless
        /// <code>true</code> is returned.
        /// <p>
        /// RefCounts are used to determine when a
        /// reader can be closed safely, i.e. as soon as there are
        /// no more references.  Be sure to always call a
        /// corresponding <seealso cref="#decRef"/>, in a finally clause;
        /// otherwise the reader may never be closed.  Note that
        /// <seealso cref="#close"/> simply calls decRef(), which means that
        /// the IndexReader will not really be closed until {@link
        /// #decRef} has been called for all outstanding
        /// references.
        /// </summary>
        /// <seealso cref= #decRef </seealso>
        /// <seealso cref= #incRef </seealso>
        public bool TryIncRef()
        {
            int count;

            while ((count = refCount.Get()) > 0)
            {
                if (refCount.CompareAndSet(count, count + 1))
                {
                    return(true);
                }
            }
            return(false);
        }
Ejemplo n.º 11
0
        public int GetAndIncrement()
        {
            for (; ;)
            {
                int current = i.Value;
                int next    = (current >= int.MaxValue ? 0 : current + 1);

                if (i.CompareAndSet(current, next))
                {
                    return(current);
                }
            }
        }
Ejemplo n.º 12
0
 /// <summary>Mark the status as closed.</summary>
 /// <remarks>
 /// Mark the status as closed.
 /// Once the status is closed, it cannot be reopened.
 /// </remarks>
 /// <returns>The current reference count.</returns>
 /// <exception cref="ClosedChannelException">
 /// If someone else closes the object
 /// before we do.
 /// </exception>
 public virtual int SetClosed()
 {
     while (true)
     {
         int curBits = status.Get();
         if ((curBits & StatusClosedMask) != 0)
         {
             throw new ClosedChannelException();
         }
         if (status.CompareAndSet(curBits, curBits | StatusClosedMask))
         {
             return(curBits & (~StatusClosedMask));
         }
     }
 }
Ejemplo n.º 13
0
        public void Test()
        {
            AtomicInteger m = new AtomicInteger(0);
            Assert.IsTrue(m == 0);

            Assert.IsFalse(m.CompareAndSet(3, 2));
            Assert.IsTrue(m == 0);

            Assert.IsTrue(m.CompareAndSet(0, 2));
            Assert.IsTrue(m == 2);

            var rs = m + 3;// 2+3
            Assert.AreEqual(5, rs);

            Assert.AreEqual(4, m--);

            Assert.AreEqual(2, m - 2);

            Assert.AreEqual(6, m + 4);

            Assert.AreEqual(6, m.GetAndAdd(4));
            Assert.AreEqual(10, m);
           
        }
Ejemplo n.º 14
0
        public void Close()
        {
            if (!closed.CompareAndSet(0, 1))
            {
                return;
            }

            try
            {
                socket.Shutdown(SocketShutdown.Both);
                socket.Close();
            }
            catch
            {
            }

            try
            {
                manager.DelSession(this);
            }
            catch
            {
            }
        }
Ejemplo n.º 15
0
        public void AtomicInteger_CompareAndSet_ThreadSafe_Test()
        {
            var integer   = new AtomicInteger(0);
            var casResult = new ConcurrentDictionary <int, bool>();
            var tasks     = Enumerable.Range(1, 50).Select(key => Task.Factory.StartNew(() => casResult.TryAdd(key, integer.CompareAndSet(0, key))));

            Task.WaitAll(tasks.ToArray());
            Assert.IsTrue(casResult.Values.Count(val => val == true) == 1);
        }