Mercurial > pub > ImplabNet
annotate Implab/Parallels/Signal.cs @ 186:75103928da09 ref20160224
working on cancelation and error handling
author | cin |
---|---|
date | Tue, 19 Apr 2016 00:50:14 +0300 |
parents | 471f596b2603 |
children |
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 | 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 |