public void ConcurrentSetsAndWaits__3()
        {
            var autoResSlim = new AutoResetSuperSlimLock(false);

            var obtainedEv = new AutoResetEvent(true);
            var barrier = new ManualResetEvent(false);
            var semaphore = new SemaphoreSlim(0, 4);

            int countOfObtained = 0;
            int countOfMissed = 0;
            var tasks = new List<Task>();

            for (int i = 0; i < 4; i++)
            {
                var t = Task.Factory.StartNew((p) =>
                {
                    barrier.WaitOne();

                    for (int j = 0; j < 1000; j++)
                    {
                        if (autoResSlim.Wait())
                        {
                            Interlocked.Increment(ref countOfObtained);
            //							Console.WriteLine(countOfObtained + " _ " + p);
                            obtainedEv.Set();
                        }
                        else
                        {
                            Interlocked.Increment(ref countOfMissed);
                        }
                    }

                    semaphore.Release();
                }, i);
                tasks.Add(t);
            }

            var t1 = Task.Factory.StartNew(() =>
            {
                barrier.Set();
                for (int i = 0; i < 4000; i++)
                {
                    obtainedEv.WaitOne();
                    autoResSlim.Set();
            //					Console.WriteLine("*\t" + i);
                }
            });
            tasks.Add(t1);

            Task.WaitAll(tasks.ToArray());

            for (int i = 0; i < 4; i++)
                semaphore.Wait();

            countOfObtained.Should().Be(4000);

            autoResSlim.IsSet.Should().BeFalse();
        }
        public void ConcurrentWaits_2()
        {
            var autoResSlim = new AutoResetSuperSlimLock(false);

            var barrier = new ManualResetEvent(false);

            int countOfObtained = 0;
            var tasks           = new List <Task>();
            var _run            = true;

            for (int i = 0; i < 4; i++)
            {
                var t = Task.Factory.StartNew(() =>
                {
                    barrier.WaitOne();

                    while (_run)
                    {
                        if (autoResSlim.Wait(100))
                        {
                            Interlocked.Increment(ref countOfObtained);
                            break;
                        }
                    }
                });
                tasks.Add(t);
            }

            barrier.Set();

            Thread.Sleep(500);

            autoResSlim.Set();

            Thread.Sleep(1);
            _run = false;

            Task.WaitAll(tasks.ToArray());

            countOfObtained.Should().Be(1);
            autoResSlim.IsSet.Should().BeFalse();
        }
示例#3
0
        //
        public void RemoveReadingGate(ReadingGate gate)
        {
            lock (_gateLocker)
            {
                lock (gate)
                {
                    if (gate.inEffect == false)
                    {
                        return;
                    }

                    gate.inEffect = false;
                    AtomicRemoveAtIndex(gate.index);
                }

                _abaPrevention++;
            }
            _gateSemaphoreSlim.Release();

            //			Console.WriteLine("RemoveReadingGate for " + gate.gpos + " len " + gate.length + " at " + gate.index);

            // if waiting for write coz of gate, given them another chance
            _readLock.Set();
        }
        public void ConcurrentSetsAndWaits__3()
        {
            var autoResSlim = new AutoResetSuperSlimLock(false);

            var obtainedEv = new AutoResetEvent(true);
            var barrier    = new ManualResetEvent(false);
            var semaphore  = new SemaphoreSlim(0, 4);

            int countOfObtained = 0;
            int countOfMissed   = 0;
            var tasks           = new List <Task>();

            for (int i = 0; i < 4; i++)
            {
                var t = Task.Factory.StartNew((p) =>
                {
                    barrier.WaitOne();

                    for (int j = 0; j < 1000; j++)
                    {
                        if (autoResSlim.Wait())
                        {
                            Interlocked.Increment(ref countOfObtained);
//							Console.WriteLine(countOfObtained + " _ " + p);
                            obtainedEv.Set();
                        }
                        else
                        {
                            Interlocked.Increment(ref countOfMissed);
                        }
                    }

                    semaphore.Release();
                }, i);
                tasks.Add(t);
            }

            var t1 = Task.Factory.StartNew(() =>
            {
                barrier.Set();
                for (int i = 0; i < 4000; i++)
                {
                    obtainedEv.WaitOne();
                    autoResSlim.Set();
//					Console.WriteLine("*\t" + i);
                }
            });

            tasks.Add(t1);

            Task.WaitAll(tasks.ToArray());

            for (int i = 0; i < 4; i++)
            {
                semaphore.Wait();
            }

            countOfObtained.Should().Be(4000);

            autoResSlim.IsSet.Should().BeFalse();
        }
示例#5
0
 public override void SignalWriteDone()
 {
     _write.Set();
 }
示例#6
0
 public override void SignalReadDone()
 {
     _read.Set();
 }
        public void ConcurrentWaits_2()
        {
            var autoResSlim = new AutoResetSuperSlimLock(false);

            var barrier = new ManualResetEvent(false);

            int countOfObtained = 0;
            var tasks = new List<Task>();
            var _run = true;

            for (int i = 0; i < 4; i++)
            {
                var t = Task.Factory.StartNew(() =>
                {
                    barrier.WaitOne();

                    while (_run)
                    {
                        if (autoResSlim.Wait(100))
                        {
                            Interlocked.Increment(ref countOfObtained);
                            break;
                        }
                    }
                });
                tasks.Add(t);
            }

            barrier.Set();

            Thread.Sleep(500);

            autoResSlim.Set();

            Thread.Sleep(1);
            _run = false;

            Task.WaitAll(tasks.ToArray());

            countOfObtained.Should().Be(1);
            autoResSlim.IsSet.Should().BeFalse();
        }