diff options
Diffstat (limited to 'cpp/src/client/IncomingMessage.cpp')
-rw-r--r-- | cpp/src/client/IncomingMessage.cpp | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/cpp/src/client/IncomingMessage.cpp b/cpp/src/client/IncomingMessage.cpp index 05c4bc2378..eb5f2b6fae 100644 --- a/cpp/src/client/IncomingMessage.cpp +++ b/cpp/src/client/IncomingMessage.cpp @@ -32,6 +32,44 @@ using sys::Mutex; IncomingMessage::Destination::~Destination() {} + +IncomingMessage::WaitableDestination::WaitableDestination() + : shutdownFlag(false) {} + +void IncomingMessage::WaitableDestination::message(const Message& msg) { + Mutex::ScopedLock l(monitor); + queue.push(msg); + monitor.notify(); +} + +void IncomingMessage::WaitableDestination::empty() { + Mutex::ScopedLock l(monitor); + queue.push(Empty()); + monitor.notify(); +} + +bool IncomingMessage::WaitableDestination::wait(Message& msgOut) { + Mutex::ScopedLock l(monitor); + while (queue.empty() && !shutdownFlag) + monitor.wait(); + if (shutdownFlag) + return false; + Message* msg = boost::get<Message>(&queue.front()); + bool success = msg; + if (success) + msgOut=*msg; + queue.pop(); + if (!queue.empty()) + monitor.notify(); // Wake another waiter. + return success; +} + +void IncomingMessage::WaitableDestination::shutdown() { + Mutex::ScopedLock l(monitor); + shutdownFlag = true; + monitor.notifyAll(); +} + void IncomingMessage::openReference(const std::string& name) { Mutex::ScopedLock l(lock); if (references.find(name) != references.end()) |