public void MonitorSimpleWaitPulse() { object locker = new object(); bool go = false; bool gotPulse = false; var thread = new Thread(() => { lock (locker) while (!go) { Monitor2.Wait(locker); } gotPulse = true; }); thread.Start(); lock (locker) { go = true; Monitor2.Pulse(locker); } Assert.IsTrue(thread.Join(250)); Assert.IsTrue(gotPulse); }
public void EnqueueItem(Action item) { lock (_locker) { _itemQ.Enqueue(item); // We must pulse because we're Monitor2.Pulse(_locker); // changing a blocking condition. } }
public void Dispose() { _done = true; lock (_cancel) { Monitor2.Pulse(_cancel); } }
public void Run() { while (!_done) { var timeout = Scheduler.Normalize(_next - _stopwatch.Elapsed); lock (_cancel) { if (Monitor2.Wait(_cancel, timeout)) { return; } } _state = _action(_state); _next += _period; } }
private void AcquireLocks(int fromInclusive, int toExclusive, ref int locksAcquired) { for (int i = fromInclusive; i < toExclusive; i++) { bool flag = false; try { Monitor2.Enter(m_locks[i], ref flag); } finally { if (flag) { locksAcquired++; } } } }
public void MonitorTryEnterTimeout() { object lockObj = new object(); var thread = new Thread(() => { lock (lockObj) Thread.Sleep(10); }); thread.Start(); Thread.Sleep(1); Assert.IsFalse(Monitor.TryEnter(lockObj)); //Assert.IsTrue(Monitor2.TryEnter(lockObj, 15)); Assert.IsTrue(Monitor2.TryEnter(lockObj, 30)); Monitor.Exit(lockObj); Assert.IsTrue(thread.Join(250)); }
void Consume() { while (true) // Keep consuming until { // told otherwise. Action item; lock (_locker) { while (_itemQ.Count == 0) { Monitor2.Wait(_locker); } item = _itemQ.Dequeue(); } if (item == null) { return; // This signals our exit. } item(); // Execute item. } }
/// <summary> /// local helper method to initialize the value /// </summary> /// <returns>The inititialized T value</returns> private T LazyInitValue() { Boxed boxed = null; LazyThreadSafetyMode mode = Mode; if (mode == LazyThreadSafetyMode.None) { boxed = CreateValue(); m_boxed = boxed; } else if (mode == LazyThreadSafetyMode.PublicationOnly) { boxed = CreateValue(); if (boxed == null || Interlocked.CompareExchange(ref m_boxed, boxed, null) != null) { // If CreateValue returns null, it means another thread successfully invoked the value factory // and stored the result, so we should just take what was stored. If CreateValue returns non-null // but another thread set the value we should just take what was stored. boxed = (Boxed)m_boxed; } else { // We successfully created and stored the value. At this point, the value factory delegate is // no longer needed, and we don't want to hold onto its resources. m_valueFactory = ALREADY_INVOKED_SENTINEL; } } else { object threadSafeObj = m_threadSafeObj; bool lockTaken = false; try { if (threadSafeObj != (object)ALREADY_INVOKED_SENTINEL) { Monitor2.Enter(threadSafeObj, ref lockTaken); } else { Debug.Assert(m_boxed != null); } if (m_boxed == null) { boxed = CreateValue(); m_boxed = boxed; m_threadSafeObj = ALREADY_INVOKED_SENTINEL; } else // got the lock but the value is not null anymore, check if it is created by another thread or faulted and throw if so { boxed = m_boxed as Boxed; if (boxed == null) // it is not Boxed, so it is a LazyInternalExceptionHolder { LazyInternalExceptionHolder exHolder = m_boxed as LazyInternalExceptionHolder; Debug.Assert(exHolder != null); exHolder.m_edi.Throw(); } } } finally { if (lockTaken) { Monitor.Exit(threadSafeObj); } } } Debug.Assert(boxed != null); return(boxed.m_value); }
/// <summary> /// Default Constructor /// </summary> public EventQueuePC() { queue = new Queue <Event>(); itemCount = 0; monitor = new Monitor2(); }
/// <summary> /// Default Constructor /// </summary> public MessageMonitoredQueue() { queue = new Queue <Message>(); itemCount = 0; monitor = new Monitor2(); }
/// <summary> /// Constructor vacío /// </summary> public NetMessageQueue() { queue = new Queue <NetMessage>(); itemCount = 0; monitor = new Monitor2(); }
private bool TryAddInternal(TKey key, TValue value, bool updateIfExists, bool acquireLock, out TValue resultingValue) { int hashCode = m_comparer.GetHashCode(key); ConcurrentDictionary <TKey, TValue> .Node[] local; bool flag; while (true) { local = m_buckets; int num; int num2; GetBucketAndLockNo(hashCode, out num, out num2, local.Length); flag = false; bool flag2 = false; try { if (acquireLock) { Monitor2.Enter(m_locks[num2], ref flag2); } if (local != m_buckets) { continue; } ConcurrentDictionary <TKey, TValue> .Node node = null; for (ConcurrentDictionary <TKey, TValue> .Node node2 = local[num]; node2 != null; node2 = node2.m_next) { if (m_comparer.Equals(node2.m_key, key)) { if (updateIfExists) { ConcurrentDictionary <TKey, TValue> .Node node3 = new ConcurrentDictionary <TKey, TValue> .Node(node2.m_key, value, hashCode, node2.m_next); if (node == null) { local[num] = node3; } else { node.m_next = node3; } resultingValue = value; } else { resultingValue = node2.m_value; } return(false); } node = node2; } local[num] = new ConcurrentDictionary <TKey, TValue> .Node(key, value, hashCode, local[num]); m_countPerLock[num2]++; if (m_countPerLock[num2] > local.Length / m_locks.Length) { flag = true; } } finally { if (flag2) { Monitor.Exit(m_locks[num2]); } } break; } if (flag) { GrowTable(local); } resultingValue = value; return(true); }