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); }
private void CheckRunning() { if (!_stat.CompareAndSet(STAT_RUNNING, STAT_RUNNING)) { throw new SpiderExceptoin("Already closed!"); } }
/** * 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); })) ); }
// 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); }
internal void SubtractFlushedNumDocs(int numFlushed) { int oldValue = NumDocsInRAM.Get(); while (!NumDocsInRAM.CompareAndSet(oldValue, oldValue - numFlushed)) { oldValue = NumDocsInRAM.Get(); } }
private int NextIdx() { while (true) { int current = currentIdx.Get(); int next = (current + 1) % providers.Length; if (currentIdx.CompareAndSet(current, next)) { return(current); } } }
internal void IncRef() { int count; while ((count = @ref.Get()) > 0) { if (@ref.CompareAndSet(count, count + 1)) { return; } } throw new AlreadyClosedException("SegmentCoreReaders is already closed"); }
/// <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); }
public int GetAndIncrement() { for (; ;) { int current = i.Value; int next = (current >= int.MaxValue ? 0 : current + 1); if (i.CompareAndSet(current, next)) { return(current); } } }
/// <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)); } } }
public void Close() { if (!closed.CompareAndSet(0, 1)) { return; } try { socket.Shutdown(SocketShutdown.Both); socket.Close(); } catch { } try { manager.DelSession(this); } catch { } }
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); }