public void ShouldOnlyClaimWhatsAvailable() { Sequence dependentSequence = new Sequence(); Sequence[] dependentSequences = { dependentSequence }; for (int j = 0; j < 1000; j++) { int numThreads = BufferSize * 2; IClaimStrategy claimStrategy = new MultiThreadedClaimStrategy(BufferSize); Volatile.LongArray claimed = new Volatile.LongArray(numThreads); Barrier barrier = new Barrier(numThreads); Thread[] ts = new Thread[numThreads]; for (int i = 0; i < numThreads; i++) { ts[i] = new Thread(() => { try { barrier.SignalAndWait(); long next = claimStrategy.CheckAndIncrement(1, 1, dependentSequences); claimed.AtomicIncrementAndGet((int)next); } catch (Exception e) { } }); } foreach (Thread t in ts) { t.Start(); } foreach (Thread t in ts) { t.Join(); } for (int i = 0; i < BufferSize; i++) { Assert.AreEqual(1L, claimed.ReadFullFence(i), "j = " + j + ", i = " + i); } for (int i = BufferSize; i < numThreads; i++) { Assert.AreEqual(0L, claimed.ReadFullFence(i), "j = " + j + ", i = " + i); } } }
public void AtomicIncrementAndGetReturnsNewValue() { Assert.AreEqual(InitialValue + 1, _volatile.AtomicIncrementAndGet(0)); }
public void ShouldOnlyClaimWhatsAvailable() { Sequence dependentSequence = new Sequence(); Sequence[] dependentSequences = { dependentSequence }; for (int j = 0; j < 1000; j++) { int numThreads = BufferSize * 2; IClaimStrategy claimStrategy = new MultiThreadedClaimStrategy(BufferSize); Volatile.LongArray claimed = new Volatile.LongArray(numThreads); Barrier barrier = new Barrier(numThreads); Thread[] ts = new Thread[numThreads]; for (int i = 0; i < numThreads; i++) { ts[i] = new Thread(() => { try { barrier.SignalAndWait(); long next = claimStrategy.CheckAndIncrement(1, 1, dependentSequences); claimed.AtomicIncrementAndGet((int) next); } catch (Exception e) { } }); } foreach (Thread t in ts) { t.Start(); } foreach (Thread t in ts) { t.Join(); } for (int i = 0; i < BufferSize; i++) { Assert.AreEqual(1L, claimed.ReadFullFence(i), "j = " + j + ", i = " + i); } for (int i = BufferSize; i < numThreads; i++) { Assert.AreEqual(0L, claimed.ReadFullFence(i), "j = " + j + ", i = " + i); } } }