annotate Implab/Parallels/Signal.cs @ 266:254d1f255d87 v3

Добавлена метка v3.0.10 для набора изменений 74e048cbaac8
author cin
date Mon, 16 Apr 2018 19:45:18 +0300
parents 471f596b2603
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
128
6241bff0cd64 Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
diff changeset
1 using System;
6241bff0cd64 Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
diff changeset
2 using System.Threading;
6241bff0cd64 Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
diff changeset
3
6241bff0cd64 Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
diff changeset
4 namespace Implab.Parallels {
6241bff0cd64 Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
diff changeset
5 /// <summary>
129
471f596b2603 Added SharedLock to synchronization routines
cin
parents: 128
diff changeset
6 /// Implements a simple signalling logic using <see cref="Monitor.PulseAll(object)"/>.
128
6241bff0cd64 Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
diff changeset
7 /// </summary>
6241bff0cd64 Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
diff changeset
8 public class Signal {
6241bff0cd64 Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
diff changeset
9 readonly object m_lock = new object();
6241bff0cd64 Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
diff changeset
10 bool m_state;
6241bff0cd64 Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
diff changeset
11
6241bff0cd64 Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
diff changeset
12 public void Set() {
6241bff0cd64 Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
diff changeset
13 lock(m_lock) {
6241bff0cd64 Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
diff changeset
14 m_state = true;
6241bff0cd64 Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
diff changeset
15 Monitor.PulseAll(m_lock);
6241bff0cd64 Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
diff changeset
16 }
6241bff0cd64 Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
diff changeset
17 }
6241bff0cd64 Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
diff changeset
18
6241bff0cd64 Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
diff changeset
19 public void Wait() {
6241bff0cd64 Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
diff changeset
20 lock (m_lock)
6241bff0cd64 Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
diff changeset
21 if (!m_state)
6241bff0cd64 Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
diff changeset
22 Monitor.Wait(m_lock);
6241bff0cd64 Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
diff changeset
23 }
6241bff0cd64 Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
diff changeset
24
6241bff0cd64 Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
diff changeset
25 public bool Wait(int timeout) {
6241bff0cd64 Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
diff changeset
26 lock (m_lock)
6241bff0cd64 Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
diff changeset
27 return m_state || Monitor.Wait(m_lock, timeout);
6241bff0cd64 Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
diff changeset
28 }
6241bff0cd64 Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
diff changeset
29 }
6241bff0cd64 Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
diff changeset
30 }
6241bff0cd64 Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
diff changeset
31