コード例 #1
0
        public void TestLongPredicateEstimatesOnceWithSmallTimeout()
        {
            object syncObj = new object();

            using (var testInst = new ConditionVariable(syncObj))
            {
                int result     = 0;
                int estimCount = 0;
                var task       = Task.Run(() =>
                {
                    using (var waiter = testInst.Enter(200))
                    {
                        if (waiter.Wait(_ => { Interlocked.Increment(ref estimCount); Thread.Sleep(500); return(false); }, (object)null))
                        {
                            Interlocked.Exchange(ref result, 1);
                        }
                        else
                        {
                            Interlocked.Exchange(ref result, 2);
                        }
                    }
                });

                lock (syncObj)
                {
                    testInst.Pulse();
                }
                TimingAssert.AreEqual(10000, 2, () => Volatile.Read(ref result));
                Assert.AreEqual(1, Volatile.Read(ref estimCount));
            }
        }
コード例 #2
0
        public void TestTimeoutWorks()
        {
            object syncObj = new object();

            using (var testInst = new ConditionVariable(syncObj))
            {
                int result = 0;
                var task   = Task.Run(() =>
                {
                    using (var waiter = testInst.Enter(500))
                    {
                        if (waiter.Wait(_ => false, (object)null))
                        {
                            Interlocked.Exchange(ref result, 1);
                        }
                        else
                        {
                            Interlocked.Exchange(ref result, 2);
                        }
                    }
                });

                lock (syncObj)
                {
                    testInst.Pulse();
                }
                TimingAssert.AreEqual(10000, 2, () => Volatile.Read(ref result));
            }
        }
コード例 #3
0
        public void TestAllThreadWakeUpOnSignalAll()
        {
            object syncObj = new object();

            using (var testInst = new ConditionVariable(syncObj))
            {
                int         exitCount = 0;
                int         state     = 0;
                List <Task> tasks     = new List <Task>();
                for (int i = 0; i < 6; i++)
                {
                    var task = Task.Run(() =>
                    {
                        using (var waiter = testInst.Enter())
                        {
                            waiter.Wait(_ => { return(Volatile.Read(ref state) > 0); }, (object)null);
                            Interlocked.Increment(ref exitCount);
                        }
                    });
                    tasks.Add(task);
                }


                TimingAssert.AreEqual(10000, 6, () => testInst.WaiterCount);
                Interlocked.Increment(ref state);

                lock (syncObj)
                {
                    testInst.PulseAll();
                }
                TimingAssert.AreEqual(10000, 0, () => testInst.WaiterCount);
                TimingAssert.AreEqual(10000, 6, () => Volatile.Read(ref exitCount));
            }
        }
コード例 #4
0
        public void TestInterruptOnDispose()
        {
            object syncObj = new object();

            using (var testInst = new ConditionVariable(syncObj))
            {
                int exitCount = 0;
                var task      = Task.Run(() =>
                {
                    try
                    {
                        using (var waiter = testInst.Enter())
                        {
                            waiter.Wait(_ => false, (object)null);
                        }
                    }
                    catch (OperationInterruptedException) { }
                    Interlocked.Increment(ref exitCount);
                });


                TimingAssert.AreEqual(10000, 1, () => testInst.WaiterCount);

                testInst.Dispose();
                TimingAssert.AreEqual(10000, 1, () => Volatile.Read(ref exitCount));

                task.Wait();
            }
        }
コード例 #5
0
        public void TestExceptionPassedFromPredicate()
        {
            object syncObj = new object();

            using (var testInst = new ConditionVariable(syncObj))
            {
                try
                {
                    using (var waiter = testInst.Enter(100000))
                    {
                        try
                        {
                            object state  = new object();
                            bool   result = waiter.Wait((s) => { throw new ApplicationException("test"); }, state);
                            Assert.IsTrue(result);
                        }
                        catch (Exception)
                        {
                            Assert.AreEqual(1, testInst.WaiterCount);
                            throw;
                        }
                    }
                }
                catch (Exception)
                {
                    Assert.AreEqual(0, testInst.WaiterCount);
                    throw;
                }
            }
        }
コード例 #6
0
        public void TestPredicateCalledOnTimeout()
        {
            object syncObj = new object();

            using (var testInst = new ConditionVariable(syncObj))
            {
                int result = 0;
                int state  = 0;
                int called = 0;
                var task   = Task.Run(() =>
                {
                    using (var waiter = testInst.Enter(100))
                    {
                        if (waiter.Wait((s) => { Interlocked.Increment(ref called); return(Volatile.Read(ref state) > 0); }, new object()))
                        {
                            Interlocked.Exchange(ref result, 1);
                        }
                        else
                        {
                            Interlocked.Exchange(ref result, 2);
                        }
                    }
                });

                TimingAssert.AreEqual(10000, 1, () => testInst.WaiterCount);
                TimingAssert.AreEqual(10000, 2, () => Volatile.Read(ref result));
                TimingAssert.AreEqual(10000, 2, () => Volatile.Read(ref called));
            }
        }
コード例 #7
0
        public void TestNotificationReceived()
        {
            object syncObj = new object();

            using (var testInst = new ConditionVariable(syncObj))
            {
                int result = 0;
                var task   = Task.Run(() =>
                {
                    using (var waiter = testInst.Enter(60000))
                    {
                        if (waiter.Wait())
                        {
                            Interlocked.Exchange(ref result, 1);
                        }
                        else
                        {
                            Interlocked.Exchange(ref result, 2);
                        }
                    }
                });

                TimingAssert.AreEqual(10000, 1, () => testInst.WaiterCount);
                Assert.AreEqual(0, Volatile.Read(ref result));

                lock (syncObj)
                {
                    testInst.Pulse();
                }
                TimingAssert.AreEqual(10000, 1, () => Volatile.Read(ref result));
            }
        }
コード例 #8
0
 static Program()
 {
     conditions = new ConditionVariable[10];
     for (int i = 0; i < conditions.Length; i++)
     {
         conditions[i] = new ConditionVariable();
     }
 }
コード例 #9
0
        public void TestPulseWorksWithoutExternalLock()
        {
            object syncObj = new object();

            using (var testInst = new ConditionVariable(syncObj))
            {
                testInst.Pulse();
            }
        }
コード例 #10
0
        public void Test___Method_GetValue___Condition_False()
        {
            var testee = new ConditionVariable()
            {
                Condition = new FalseCondition()
            };

            Assert.IsFalse(testee.GetValue());
        }
コード例 #11
0
 public CollectionBase(Exchange exchange, Func <TValue, TKey> key_from)
 {
     Exchange = exchange;
     Updated  = new ConditionVariable <DateTimeOffset>(DateTimeOffset.MinValue);
     m_data   = new BindingDict <TKey, TValue> {
         KeyFrom = key_from
     };
     m_data.CollectionChanged += (s, a) => CollectionChanged?.Invoke(this, a);
     m_data.ListChanging      += (s, a) => ListChanging?.Invoke(this, a);
 }
コード例 #12
0
        public MailBox(int N)
        {
            clientMessages = new Queue <MessageFromServer> [N];
            emptyClient    = new ConditionVariable[N];

            for (int i = 0; i < N; i++)
            {
                clientMessages[i] = new Queue <MessageFromServer>();
                emptyClient[i]    = new ConditionVariable();
            }
        }
コード例 #13
0
 public Quest DeepCopy()
 {
     return(new Quest
     {
         Name = Name,
         ConditionSwitch = ConditionSwitch,
         ConditionVariable = ConditionVariable.DeepCopy(),
         Message = CloneMessage(),
         Reward = CloneReward(),
     });
 }
コード例 #14
0
        public void TestPredicateCalledInsideLock()
        {
            object syncObj = new object();

            using (var testInst = new ConditionVariable(syncObj))
            {
                int result         = 0;
                int estimCount     = 0;
                int inMonitorCount = 0;
                int stopEstim      = 0;
                var task           = Task.Run(() =>
                {
                    using (var waiter = testInst.Enter(60000))
                    {
                        if (waiter.Wait(_ =>
                        {
                            if (Monitor.IsEntered(syncObj))
                            {
                                Interlocked.Increment(ref inMonitorCount);
                            }
                            Interlocked.Increment(ref estimCount);
                            return(Volatile.Read(ref stopEstim) != 0);
                        }, (object)null))
                        {
                            Interlocked.Exchange(ref result, 1);
                        }
                        else
                        {
                            Interlocked.Exchange(ref result, 2);
                        }
                    }
                });

                TimingAssert.AreEqual(10000, 1, () => testInst.WaiterCount);
                for (int i = 0; i < 20; i++)
                {
                    lock (syncObj)
                    {
                        testInst.Pulse();
                    }
                    Thread.Sleep(10);
                }
                Interlocked.Increment(ref stopEstim);
                lock (syncObj)
                {
                    testInst.Pulse();
                }

                TimingAssert.AreEqual(10000, 1, () => Volatile.Read(ref result));
                Assert.IsTrue(Volatile.Read(ref estimCount) > 1);
                Assert.AreEqual(Volatile.Read(ref inMonitorCount), Volatile.Read(ref estimCount));
            }
        }
コード例 #15
0
 public QueryExecutor(DatabaseProxy db)
 {
     ScoreProvider       = new ChessDBCNScoreProvider();
     Database            = db;
     QueryQueueMutex     = new Mutex();
     QueryQueue          = new QueryQueue();
     QueryCacheLock      = new object();
     EndQueryThread      = false;
     AnyOutstandingQuery = new ConditionVariable();
     QueryCache          = new LRUCache <QueryQueueEntry, QueryCacheEntry>(queryCacheSize);
     QueryThread         = new Thread(new ThreadStart(RunQueryThread));
     QueryThread.Start();
 }
コード例 #16
0
        internal override IExpression GetConditionExpression()
        {
            IExpression expr = ConditionVariable.GetExpression();

            if (ConditionValue)
            {
                return(expr);
            }
            else
            {
                return(Builder.NotExpr(expr));
            }
        }
コード例 #17
0
        private static void CreateVar(ConditionVariableValue.Type type)
        {
            var newVar = new ConditionVariable
            {
                name  = tempVarName,
                value = new ConditionVariableValue()
                {
                    type = type
                }
            };

            tempVarName = "";
            MusicTreeEditorManager.Instance.TreeAsset.vars.Add(newVar);
        }
コード例 #18
0
        public BookWithConditionVariable(int maxReaders, bool wakeUpAllReaders)
        {
            _maxReaders       = maxReaders;
            _wakeUpAllReaders = wakeUpAllReaders;

            _lock    = new object();
            _reading = new ConditionVariable();
            _writing = new ConditionVariable();

            _readersNumber = 0;
            _isWriting     = false;

            _readersInARowCounter = 0;
        }
コード例 #19
0
        public void TestStatePassedCorrectly()
        {
            object syncObj = new object();

            using (var testInst = new ConditionVariable(syncObj))
            {
                using (var waiter = testInst.Enter(100000))
                {
                    object state  = new object();
                    bool   result = waiter.Wait((s) => { Assert.AreEqual(state, s); return(true); }, state);
                    Assert.IsTrue(result);
                }
            }
        }
コード例 #20
0
        public void TestAlwaysPositivePredicate()
        {
            object syncObj = new object();

            using (var testInst = new ConditionVariable(syncObj))
            {
                using (var waiter = testInst.Enter(100000))
                {
                    int  called = 0;
                    bool result = waiter.Wait((s) => { called++; return(true); }, new object());
                    Assert.IsTrue(result);
                    Assert.AreEqual(1, called);
                }
            }
        }
コード例 #21
0
        public void TestWaitWithPredicateThrowsIfExternalLockTakenRecursively()
        {
            object syncObj = new object();

            using (var testInst = new ConditionVariable(syncObj))
            {
                lock (syncObj)
                {
                    using (var waiter = testInst.Enter(10))
                    {
                        waiter.Wait(_ => false, (object)null);
                    }
                }
            }
        }
コード例 #22
0
        public void TestWaitThrowsIfRecursiveEnter()
        {
            object syncObj = new object();

            using (var testInst = new ConditionVariable(syncObj))
            {
                using (var waiter2 = testInst.Enter(10))
                {
                    using (var waiter = testInst.Enter(10))
                    {
                        waiter.Wait();
                    }
                }
            }
        }
コード例 #23
0
        public TellingManager(List <Knight> _knights)
        {
            knights           = _knights;
            n                 = knights.Count;
            isListening       = new bool[n];
            nListening        = new int[n];
            isTelling         = new bool[n];
            conditionVariable = new ConditionVariable[n];
            for (int i = 0; i < n; i++)
            {
                conditionVariable[i] = new ConditionVariable();
            }

            QueuePulse           = new Queue <ConditionVariable>();
            waitForQueuePulse    = new ConditionVariable();
            isManagerHelperDoing = false;
            helperQueue          = new ConditionVariable();
        }
コード例 #24
0
        public void Test1()
        {
            var  mutex     = new object();
            var  cv        = new ConditionVariable <int>();
            bool ready     = false;
            bool processed = false;

            // Main
            {
                var worker = new Thread(new ThreadStart(WorkerThread)).Run();

                // send data to the worker thread
                lock (mutex)
                {
                    ready = true;
                    cv.NotifyOne(mutex, 1);
                }

                // wait for the worker
                lock (mutex)
                {
                    var value = cv.Wait(mutex, x => processed);
                    Assert.Equal(value, 42);
                }

                worker.Join();
            }

            void WorkerThread()
            {
                // Wait until main() sends data
                lock (mutex)
                {
                    var value = cv.Wait(mutex, x => ready);
                    Assert.Equal(value, 1);

                    // after the wait, we own the lock.
                    cv.NotifyOne(mutex, 42);

                    // Send data back to main()
                    processed = true;
                }
            }
        }
コード例 #25
0
        public void TestCancellationWorks()
        {
            object syncObj = new object();

            using (var testInst = new ConditionVariable(syncObj))
            {
                int result = 0;
                CancellationTokenSource tokenSrc = new CancellationTokenSource();
                var task = Task.Run(() =>
                {
                    try
                    {
                        using (var waiter = testInst.Enter(60000, tokenSrc.Token))
                        {
                            if (waiter.Wait(_ => false, (object)null))
                            {
                                Interlocked.Exchange(ref result, 1);
                            }
                            else
                            {
                                Interlocked.Exchange(ref result, 2);
                            }
                        }
                    }
                    catch (OperationCanceledException)
                    {
                        Interlocked.Exchange(ref result, 3);
                    }
                });


                TimingAssert.AreEqual(10000, 1, () => testInst.WaiterCount);
                lock (syncObj)
                {
                    testInst.Pulse();
                }
                Assert.AreEqual(0, Volatile.Read(ref result));

                Thread.Sleep(100);
                tokenSrc.Cancel();
                TimingAssert.AreEqual(10000, 3, () => Volatile.Read(ref result));
            }
        }
コード例 #26
0
        public async void Test2()
        {
            var cv        = new ConditionVariable();
            var data      = (string?)null;
            var ready     = false;
            var processed = false;

            // Main
            {
                var worker = new Thread(new ThreadStart(WorkerThread)).Run();

                data = "Example data";

                // send data to the worker thread
                ready = true;
                cv.NotifyOne();

                // wait for the worker
                await cv.WaitAsync(() => processed);

                // "Back in main(), data = " << data << '\n';
                Assert.Equal(data, "Changed data");

                worker.Join();
            }

            async void WorkerThread()
            {
                // Wait until main() sends data
                await cv.WaitAsync(() => ready);

                // after the wait, we own the lock.
                data = "Changed data";

                // Send data back to main()
                processed = true;

                // Should be notifying outside of the lock but C# Monitor.Pulse is
                // not quite the same as std::condition_variable.notifiy_all()
                cv.NotifyOne();
            }
        }
コード例 #27
0
        public DrinkingManager(Table _table)
        {
            table             = _table;
            n                 = Const.n;
            conditionVariable = new ConditionVariable[n];
            for (int i = 0; i < n; i++)
            {
                conditionVariable[i] = new ConditionVariable();
            }

            isWaitingForWine     = new bool[n];
            isWaitingForCucumber = new bool[n];
            isWaitingForPlate    = new bool[n];
            isWaitingForCup      = new bool[n];

            QueuePulse           = new Queue <ConditionVariable>();
            waitForQueuePulse    = new ConditionVariable();
            isManagerHelperDoing = false;
            helperQueue          = new ConditionVariable();
        }
コード例 #28
0
        public void TestNotificationWithPredicate()
        {
            object syncObj = new object();

            using (var testInst = new ConditionVariable(syncObj))
            {
                int result = 0;
                int state  = 0;
                var task   = Task.Run(() =>
                {
                    using (var waiter = testInst.Enter(60000))
                    {
                        if (waiter.Wait((s) => Volatile.Read(ref state) > 0, new object()))
                        {
                            Interlocked.Exchange(ref result, 1);
                        }
                        else
                        {
                            Interlocked.Exchange(ref result, 2);
                        }
                    }
                });

                Thread.Sleep(100);
                Assert.AreEqual(0, Volatile.Read(ref result));

                lock (syncObj)
                {
                    testInst.Pulse();
                }
                Thread.Sleep(100);
                Assert.AreEqual(0, Volatile.Read(ref result));

                Interlocked.Increment(ref state);
                lock (syncObj)
                {
                    testInst.Pulse();
                }
                TimingAssert.AreEqual(10000, 1, () => Volatile.Read(ref result));
            }
        }
コード例 #29
0
        public MonitorForDrinking(int N, int w, int c)
        {
            this.N         = N;
            KnightsAlive   = N;
            wineState      = w;
            cucumbersState = new int[N / 2];
            initialC       = c;
            initialW       = w;

            isWaiting           = new bool[N];
            isBusy              = new bool[N];
            hisTurnCond         = new ConditionVariable[N];
            cucumbersRefillCond = new ConditionVariable();
            wineRefillCond      = new ConditionVariable();

            for (int i = 0; i < N; i++)
            {
                cucumbersState[i / 2] = c;
                isBusy[i]             = false;
                hisTurnCond[i]        = new ConditionVariable();
            }
        }
コード例 #30
0
        public void AddConditionVariableTest()
        {
            var var1 = new ConditionVariable()
            {
                form_id           = 1,
                variable_name_chs = "标的编码",
                data_type         = DataType.String,
                default_value     = "标的编码",
                condition_list    = new List <Condition>()
                {
                    new Condition {
                        condition_expr = "@楼盘编码 != ''", condition_item = new ConditionItem()
                        {
                            inner_value  = "@楼盘编码 ",
                            value_method = ValueMethod.Formula,
                        }
                    },
                    new Condition()
                    {
                        condition_expr = "@楼栋编码 != ''", condition_item = new ConditionItem()
                        {
                            inner_value  = "@楼栋编码 ",
                            value_method = ValueMethod.Formula,
                        }
                    },
                    new Condition()
                    {
                        condition_expr = "@房号编码 != ''", condition_item = new ConditionItem()
                        {
                            inner_value  = "@房号编码 ",
                            value_method = ValueMethod.Formula,
                        }
                    }
                }
            };

            _formVariableService.AddFormVariabel(var1, 0);
        }