# HG changeset patch # User cin # Date 2015-01-29 02:09:31 # Node ID 6241bff0cd64e284346ea70c208272528be53d54 # Parent d86da8d2d4c3220c4663461b78bb9115af5f6bc0 Added Signal class a lightweight alternative to ManualResetEvent diff --git a/Implab/Implab.csproj b/Implab/Implab.csproj --- a/Implab/Implab.csproj +++ b/Implab/Implab.csproj @@ -155,6 +155,7 @@ + diff --git a/Implab/Parallels/AsyncQueue.cs b/Implab/Parallels/AsyncQueue.cs --- a/Implab/Parallels/AsyncQueue.cs +++ b/Implab/Parallels/AsyncQueue.cs @@ -454,7 +454,7 @@ namespace Implab.Parallels { } while(true); } - T[] ReadChunks(Chunk chunk, object last) { + static T[] ReadChunks(Chunk chunk, object last) { var result = new List(); var buffer = new T[DEFAULT_CHUNK_SIZE]; int actual; diff --git a/Implab/Parallels/Signal.cs b/Implab/Parallels/Signal.cs new file mode 100644 --- /dev/null +++ b/Implab/Parallels/Signal.cs @@ -0,0 +1,31 @@ +using System; +using System.Threading; + +namespace Implab.Parallels { + /// + /// Implements simple signalling logic using . + /// + public class Signal { + readonly object m_lock = new object(); + bool m_state; + + public void Set() { + lock(m_lock) { + m_state = true; + Monitor.PulseAll(m_lock); + } + } + + public void Wait() { + lock (m_lock) + if (!m_state) + Monitor.Wait(m_lock); + } + + public bool Wait(int timeout) { + lock (m_lock) + return m_state || Monitor.Wait(m_lock, timeout); + } + } +} + diff --git a/Implab/Safe.cs b/Implab/Safe.cs --- a/Implab/Safe.cs +++ b/Implab/Safe.cs @@ -36,13 +36,26 @@ namespace Implab throw new ArgumentOutOfRangeException(paramName); } - public static void Dispose(params IDisposable[] objects) - { - foreach(var d in objects) + public static void Dispose(params IDisposable[] objects) { + foreach (var d in objects) if (d != null) d.Dispose(); } + public static void Dispose(params object[] objects) { + foreach (var obj in objects) { + var d = obj as IDisposable; + if (d != null) + d.Dispose(); + } + } + + public static void Dispose(object obj) { + var d = obj as IDisposable; + if (d != null) + d.Dispose(); + } + [DebuggerStepThrough] public static IPromise InvokePromise(Func action) { ArgumentNotNull(action, "action");