Mercurial > pub > ImplabNet
changeset 128:6241bff0cd64 v2
Added Signal class a lightweight alternative to ManualResetEvent
author | cin |
---|---|
date | Thu, 29 Jan 2015 05:09:31 +0300 |
parents | d86da8d2d4c3 |
children | 471f596b2603 |
files | Implab/Implab.csproj Implab/Parallels/AsyncQueue.cs Implab/Parallels/Signal.cs Implab/Safe.cs |
diffstat | 4 files changed, 49 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- a/Implab/Implab.csproj Tue Jan 27 18:18:29 2015 +0300 +++ b/Implab/Implab.csproj Thu Jan 29 05:09:31 2015 +0300 @@ -155,6 +155,7 @@ <Compile Include="AbstractPromise.cs" /> <Compile Include="Promise.cs" /> <Compile Include="PromiseTransientException.cs" /> + <Compile Include="Parallels\Signal.cs" /> </ItemGroup> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <ItemGroup />
--- a/Implab/Parallels/AsyncQueue.cs Tue Jan 27 18:18:29 2015 +0300 +++ b/Implab/Parallels/AsyncQueue.cs Thu Jan 29 05:09:31 2015 +0300 @@ -454,7 +454,7 @@ } while(true); } - T[] ReadChunks(Chunk chunk, object last) { + static T[] ReadChunks(Chunk chunk, object last) { var result = new List<T>(); var buffer = new T[DEFAULT_CHUNK_SIZE]; int actual;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Implab/Parallels/Signal.cs Thu Jan 29 05:09:31 2015 +0300 @@ -0,0 +1,31 @@ +using System; +using System.Threading; + +namespace Implab.Parallels { + /// <summary> + /// Implements simple signalling logic using <see cref="Monitor.PulseAll(object)"/>. + /// </summary> + 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); + } + } +} +
--- a/Implab/Safe.cs Tue Jan 27 18:18:29 2015 +0300 +++ b/Implab/Safe.cs Thu Jan 29 05:09:31 2015 +0300 @@ -36,13 +36,26 @@ 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<T> InvokePromise<T>(Func<T> action) { ArgumentNotNull(action, "action");