annotate Implab/Parallels/Signal.cs @ 259:7d52dc684bbd v3

PollingComponent: implemented correct stopping
author cin
date Fri, 13 Apr 2018 03:57:39 +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