예제 #1
0
        // ===================

        private void RunComplexTest(MutuallyExclusivePrimitive inst, int workCount, int workSpin, int sleepProbability)
        {
            Barrier barrier = new Barrier(4);
            CancellationTokenSource globalCancellation = new CancellationTokenSource();
            ManualResetEventSlim    alwaysNotSet       = new ManualResetEventSlim(false);
            int workPerformGate1   = 0;
            int workPerformGate2   = 0;
            int workCompletedCount = 0;

            int gate1Executed = 0;
            int gate2Executed = 0;

            Action <Random, CancellationToken, int> doWork = (Random rnd, CancellationToken token, int gate) =>
            {
                try
                {
                    if (gate == 1)
                    {
                        Interlocked.Increment(ref workPerformGate1);
                        if (Volatile.Read(ref workPerformGate2) != 0)
                        {
                            throw new Exception("Mutual exclusive logic is broken");
                        }

                        Interlocked.Increment(ref gate1Executed);
                    }
                    else
                    {
                        Interlocked.Increment(ref workPerformGate2);
                        if (Volatile.Read(ref workPerformGate1) != 0)
                        {
                            throw new Exception("Mutual exclusive logic is broken");
                        }

                        Interlocked.Increment(ref gate2Executed);
                    }

                    if (rnd.Next(sleepProbability) == 0)
                    {
                        alwaysNotSet.Wait(1, token);
                    }
                    else
                    {
                        int spin = rnd.Next(0, workSpin);
                        Thread.SpinWait(spin);
                    }
                }
                finally
                {
                    if (gate == 1)
                    {
                        Interlocked.Decrement(ref workPerformGate1);
                    }
                    else
                    {
                        Interlocked.Decrement(ref workPerformGate2);
                    }
                }
            };


            Action <int> worker = (int gate) =>
            {
                var    token = globalCancellation.Token;
                Random rnd   = new Random(Environment.TickCount + Thread.CurrentThread.ManagedThreadId);

                barrier.SignalAndWait();
                while (!token.IsCancellationRequested)
                {
                    try
                    {
                        using (var guard = gate == 1 ? inst.EnterMain(Timeout.Infinite, token) : inst.EnterBackground(Timeout.Infinite, token))
                        {
                            using (var linked = CancellationTokenSource.CreateLinkedTokenSource(token, guard.Token))
                            {
                                doWork(rnd, token, gate);
                            }
                        }

                        int spin = rnd.Next(0, workSpin);
                        Thread.SpinWait(spin);
                    }
                    catch (OperationCanceledException) { }

                    int localWorkCompl = Interlocked.Increment(ref workCompletedCount);
                    if (localWorkCompl > workCount)
                    {
                        globalCancellation.Cancel();
                    }
                }
            };

            Action switcher = () =>
            {
                var    token = globalCancellation.Token;
                Random rnd   = new Random(Environment.TickCount + Thread.CurrentThread.ManagedThreadId);

                barrier.SignalAndWait();
                while (!token.IsCancellationRequested)
                {
                    int sleep = rnd.Next(workSpin);

                    Thread.Sleep(sleep);
                    inst.AllowBackgroundGate();

                    sleep = rnd.Next(workSpin);
                    Thread.Sleep(sleep);
                    inst.DisallowBackgroundGate();
                }
            };

            Task workerThread1  = Task.Factory.StartNew(() => worker(1), TaskCreationOptions.LongRunning);
            Task workerThread2  = Task.Factory.StartNew(() => worker(2), TaskCreationOptions.LongRunning);
            Task workerThread3  = Task.Factory.StartNew(() => worker(2), TaskCreationOptions.LongRunning);
            Task switcherThread = Task.Factory.StartNew(() => switcher(), TaskCreationOptions.LongRunning);

            Task.WaitAll(workerThread1, workerThread2, workerThread3, switcherThread);

            Assert.IsTrue(gate1Executed > 0);
            Assert.IsTrue(gate2Executed > 0);
        }
예제 #2
0
        // ====================== Background ===============


        /// <summary>
        /// Transfers data from LowLevelQueue to HighLevelQueue in background
        /// </summary>
        private void BackgroundTransferProc(object state, CancellationToken token)
        {
            while (!token.IsCancellationRequested)
            {
                using (var gateGuard = _backgoundTransfererExclusive.EnterBackground(Timeout.Infinite, token))
                {
                    TurboContract.Assert(gateGuard.IsAcquired, conditionString: "gateGuard.IsAcquired");

                    T    item         = default(T);
                    bool itemTaken    = false;
                    bool needSlowPath = true;

                    // Lightweight pipeline
                    while (!token.IsCancellationRequested && !gateGuard.IsCancellationRequested)
                    {
                        if (itemTaken = _lowLevelQueue.TryTake(out item, 0, default(CancellationToken)))
                        {
                            if (!_highLevelQueue.TryAdd(item, 0, default(CancellationToken)))
                            {
                                break;
                            }

                            item      = default(T);
                            itemTaken = false;
                        }
                        else
                        {
                            _backgoundTransfererExclusive.DisallowBackgroundGate(); // Nothing to do. Stop attempts
                            needSlowPath = false;
                            break;
                        }
                    }

                    if (!needSlowPath || token.IsCancellationRequested || gateGuard.IsCancellationRequested)
                    {
                        if (itemTaken)
                        {
                            _highLevelQueue.AddForced(item); // Prevent item lost
                            itemTaken = false;
                        }
                        continue;
                    }

                    using (var linkedCancellation = CancellationTokenSource.CreateLinkedTokenSource(token, gateGuard.Token))
                    {
                        try
                        {
                            if (itemTaken)
                            {
                                bool itemAdded = _highLevelQueue.TryAdd(item, Timeout.Infinite, linkedCancellation.Token);
                                TurboContract.Assert(itemAdded, conditionString: "itemAdded");
                                itemTaken = false;
                            }

                            while (!linkedCancellation.IsCancellationRequested)
                            {
                                if (itemTaken = _lowLevelQueue.TryTake(out item, 0, default(CancellationToken)))
                                {
                                    if (!_highLevelQueue.TryAdd(item, 0, default(CancellationToken))) // Fast path to ignore cancellation
                                    {
                                        bool itemAdded = _highLevelQueue.TryAdd(item, Timeout.Infinite, linkedCancellation.Token);
                                        TurboContract.Assert(itemAdded, conditionString: "itemAdded");
                                    }

                                    item      = default(T);
                                    itemTaken = false;
                                }
                                else
                                {
                                    _backgoundTransfererExclusive.DisallowBackgroundGate(); // Nothing to do. Stop attempts
                                    break;
                                }
                            }
                        }
                        catch (OperationCanceledException)
                        {
                            if (itemTaken)
                            {
                                _highLevelQueue.AddForced(item); // Prevent item lost
                                itemTaken = false;
                            }


                            //_bacgoundTransfererExclusive.DisallowBackgroundGate(); // Exclusivity contention. Stop attmpts
                            if (!linkedCancellation.IsCancellationRequested)
                            {
                                throw;
                            }
                        }
                    }
                }
            }
        }