Skip to content

Instantly share code, notes, and snippets.

@benjamn
Created July 18, 2009 01:58
Show Gist options
  • Save benjamn/149380 to your computer and use it in GitHub Desktop.
Save benjamn/149380 to your computer and use it in GitHub Desktop.
class EventQueue {
Queue<nsIRunnable> mQueue;
PRBool mWaiting;
mozilla::Monitor mRarelyUsedMonitor;
public:
EventQueue()
: mQueue()
, mWaiting(0)
, mRarelyUsedMonitor("EventQueue::mRarelyUsedMonitor")
{}
PRBool GetEvent(PRBool mayWait, nsIRunnable** result) {
if (!mQueue.dequeue(*result)) {
if (!mayWait)
return PR_FALSE;
mozilla::MonitorAutoEnter mon(mRarelyUsedMonitor);
while (!mQueue.dequeue(*result)) {
mWaiting = PR_TRUE;
mon.Wait();
}
}
return PR_TRUE;
}
void PutEvent(nsIRunnable* event) {
NS_IF_ADDREF(event);
mQueue.enqueue(event);
if (mWaiting) {
mozilla::MonitorAutoEnter mon(mRarelyUsedMonitor);
mWaiting = PR_FALSE;
mon.NotifyAll();
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment