Skip to content
Permalink
Browse files
  • Loading branch information
Timothy A. Bish committed Jan 21, 2011
1 parent 15a09fa commit ad6f8906a5b25c1b918634892036cae4a1e1fb81
Showing 1 changed file with 13 additions and 9 deletions.
@@ -25,11 +25,18 @@ namespace Apache.NMS.Stomp.Util
public class MessageDispatchChannel
{
private readonly Mutex mutex = new Mutex();
private readonly ManualResetEvent waiter = new ManualResetEvent(false);
private readonly ManualResetEvent wakeAll = new ManualResetEvent(false);
private readonly AutoResetEvent waiter = new AutoResetEvent(false);
private WaitHandle[] waiters;
private bool closed;
private bool running;
private readonly LinkedList<MessageDispatch> channel = new LinkedList<MessageDispatch>();

public MessageDispatchChannel()
{
this.waiters = new WaitHandle[] { this.waiter, this.wakeAll };
}

#region Properties

public object SyncRoot
@@ -106,8 +113,7 @@ public void Start()
if(!Closed)
{
this.running = true;
this.waiter.Set();
this.waiter.Reset();
this.wakeAll.Reset();
}
}
}
@@ -117,8 +123,7 @@ public void Stop()
lock(mutex)
{
this.running = false;
this.waiter.Set();
this.waiter.Reset();
this.wakeAll.Set();
}
}

@@ -132,7 +137,7 @@ public void Close()
this.closed = true;
}

this.waiter.Set();
this.wakeAll.Set();
}
}

@@ -142,7 +147,6 @@ public void Enqueue(MessageDispatch dispatch)
{
this.channel.AddLast(dispatch);
this.waiter.Set();
this.waiter.Reset();
}
}

@@ -152,7 +156,6 @@ public void EnqueueFirst(MessageDispatch dispatch)
{
this.channel.AddFirst(dispatch);
this.waiter.Set();
this.waiter.Reset();
}
}

@@ -166,7 +169,8 @@ public MessageDispatch Dequeue(TimeSpan timeout)
if( timeout != TimeSpan.Zero && !Closed && ( Empty || !Running ) )
{
this.mutex.ReleaseMutex();
this.waiter.WaitOne((int)timeout.TotalMilliseconds, false);
this.waiter.Reset();
WaitHandle.WaitAny(this.waiters, (int)timeout.TotalMilliseconds, false);
this.mutex.WaitOne();
}

0 comments on commit ad6f890

Please sign in to comment.