public void FillBottle() { lock (o) { while (isManagerHelperDoing) { waitForQueuePulse.Wait(o); } table.FillBottleOfWine(); for (int i = 0; i < n; i++) { if (isWaitingForWine[i]) { QueuePulse.Enqueue(conditionVariable[i]); } } if (QueuePulse.Count != 0) { isManagerHelperDoing = true; helperQueue.Pulse(); } } }
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)); } }
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)); } }
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)); } }
public void EndTellingStory(int knightTelling, List <int> knightListening) { lock (o) { isTelling[knightTelling] = false; for (int i = 0; i < knightListening.Count; i++) { int index = knightListening[i]; if (!isTelling[index]) { if (nListening[index] != 0) { nListening[index]--; } if (nListening[index] == 0) { isListening[index] = false; QueuePulse.Enqueue(conditionVariable[index]); } } } ConsoleWriter.WriteMessage($"Ending telling Story by Knight : {knightTelling}", ConsoleColor.DarkRed); if (QueuePulse.Count != 0) { isManagerHelperDoing = true; helperQueue.Pulse(); } } }
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)); } }
public void TestPulseWorksWithoutExternalLock() { object syncObj = new object(); using (var testInst = new ConditionVariable(syncObj)) { testInst.Pulse(); } }
public void ClientSend(MessageFromClient msg) { lock (obj) { messages.Enqueue(msg); if (messages.Count == 1) { empty.Pulse(); } } }
private int LeaveWriting() { lock (_lock) { _isWriting = false; var tmpCounter = _readersInARowCounter; if (_wakeUpAllReaders) { _reading.PulseAll(); } else { _reading.Pulse(); } _writing.Pulse(); return(tmpCounter); } }
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)); } }
//ENTRIES public void DecreaseCounter(bool B, string queuename) { lock (jankielLock) { bCount = bCount || B; //System.Console.WriteLine($"{queuename}: decrease counter from {counter}."); counter--; if (counter == 0) { // System.Console.WriteLine($"{queuename} counter :{counter} pulsam"); mainThreadQueue.Pulse(); } } }
//ENTRIES public void NotifyEndPlay() { lock (jankielPlayLock) { if (nPlaying == -1) { throw new System.Exception("pizda"); } if (--nPlaying == 0) { mainThreadQueue.Pulse(); } } }
public bool TryAdd(T value, int timeout, CancellationToken token) { using (var waiter = VarFull.Enter(timeout, token)) { if (waiter.Wait(s => { Assert.IsTrue(Monitor.IsEntered(s.SharedSyncObj)); return(s.Queue.Count < s.MaxCount); }, this)) { Assert.IsTrue(Monitor.IsEntered(SharedSyncObj)); Queue.Enqueue(value); VarEmpty.Pulse(); return(true); } Assert.IsTrue(Monitor.IsEntered(SharedSyncObj)); return(false); } }
public bool TryTake(out T value, int timeout, CancellationToken token) { using (var waiter = VarEmpty.Enter(timeout, token)) { if (waiter.Wait(s => { Assert.IsTrue(Monitor.IsEntered(s.SharedSyncObj)); return(s.Queue.Count > 0); }, this)) { Assert.IsTrue(Monitor.IsEntered(SharedSyncObj)); value = Queue.Dequeue(); VarFull.Pulse(); return(true); } Assert.IsTrue(Monitor.IsEntered(SharedSyncObj)); value = default(T); return(false); } }
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)); } }
public void TestSingleThreadWakeUpOnSignal() { 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); for (int i = 0; i < 6; i++) { lock (syncObj) { testInst.Pulse(); } TimingAssert.AreEqual(10000, 5 - i, () => testInst.WaiterCount); Thread.Sleep(50); TimingAssert.AreEqual(10000, i + 1, () => Volatile.Read(ref exitCount)); } } }
public void TestPredicateCalledTwiceOnDelayedSuccess() { 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(60000)) { 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); Assert.AreEqual(0, Volatile.Read(ref result)); TimingAssert.AreEqual(10000, 1, () => Volatile.Read(ref called)); Assert.AreEqual(0, Volatile.Read(ref result)); Interlocked.Increment(ref state); lock (syncObj) { testInst.Pulse(); } TimingAssert.AreEqual(10000, 1, () => Volatile.Read(ref result)); TimingAssert.AreEqual(10000, 2, () => Volatile.Read(ref called)); } }