public void ConstructorWithLengthSetExpectedLength()
        {
            const int length = 2;
            _volatile = new Volatile4.LongArray(length);

            Assert.AreEqual(length, _volatile.Length);
        }
        public void ConstructorWithLengthSetExpectedLength()
        {
            const int length = 2;

            _volatile = new Volatile4.LongArray(length);

            Assert.AreEqual(length, _volatile.Length);
        }
Example #3
0
 /// <summary>
 /// Construct a new multi-threaded publisher <see cref="IClaimStrategy"/> for a given buffer size.
 /// </summary>
 /// <param name="bufferSize">bufferSize for the underlying data structure.</param>
 /// <param name="pendingBufferSize">pendingBufferSize number of item that can be pending for serialization</param>
 public MultiThreadedClaimStrategy(int bufferSize, int pendingBufferSize = 1024) : base(bufferSize)
 {
     if (!pendingBufferSize.IsPowerOf2())
     {
         throw new ArgumentException("must be power of 2", "pendingBufferSize");
     }
     _bufferSize         = bufferSize;
     _pendingPublication = new Volatile4.LongArray(pendingBufferSize);
     _pendingMask        = pendingBufferSize - 1;
 }
 /// <summary>
 /// Construct a new multi-threaded publisher <see cref="IClaimStrategy"/> for a given buffer size.
 /// </summary>
 /// <param name="bufferSize">bufferSize for the underlying data structure.</param>
 /// <param name="pendingBufferSize">pendingBufferSize number of item that can be pending for serialization</param>
 public MultiThreadedClaimStrategy(int bufferSize, int pendingBufferSize = 1024) : base(bufferSize)
 {
     
     if (!pendingBufferSize.IsPowerOf2())
     {
         throw new ArgumentException("must be power of 2", "pendingBufferSize");
     }
     _bufferSize = bufferSize;
     _pendingPublication = new Volatile4.LongArray(pendingBufferSize);
     _pendingMask = pendingBufferSize - 1;
 }
        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);
                Volatile4.LongArray claimed       = new Volatile4.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 SetUp()
 {
     _volatile = new Volatile4.LongArray(InitialValues);
 }
 public void SetUp()
 {
     _volatile = new Volatile4.LongArray(InitialValues);
 }
 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);
         Volatile4.LongArray claimed = new Volatile4.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);
         }
     }
 }