diff options
Diffstat (limited to 'Swiften/EventLoop')
-rw-r--r-- | Swiften/EventLoop/DummyEventLoop.cpp | 3 | ||||
-rw-r--r-- | Swiften/EventLoop/DummyEventLoop.h | 27 |
2 files changed, 26 insertions, 4 deletions
diff --git a/Swiften/EventLoop/DummyEventLoop.cpp b/Swiften/EventLoop/DummyEventLoop.cpp index 4ea425e..b8e631e 100644 --- a/Swiften/EventLoop/DummyEventLoop.cpp +++ b/Swiften/EventLoop/DummyEventLoop.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010 Isode Limited. + * Copyright (c) 2010-2015 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ @@ -14,6 +14,7 @@ DummyEventLoop::DummyEventLoop() { } DummyEventLoop::~DummyEventLoop() { + boost::lock_guard<boost::mutex> lock(eventsMutex_); if (!events_.empty()) { std::cerr << "DummyEventLoop: Unhandled events at destruction time" << std::endl; } diff --git a/Swiften/EventLoop/DummyEventLoop.h b/Swiften/EventLoop/DummyEventLoop.h index 6ee1b77..297549e 100644 --- a/Swiften/EventLoop/DummyEventLoop.h +++ b/Swiften/EventLoop/DummyEventLoop.h @@ -8,6 +8,9 @@ #include <deque> +#include <boost/thread/mutex.hpp> +#include <boost/thread/locks.hpp> + #include <Swiften/Base/API.h> #include <Swiften/EventLoop/EventLoop.h> @@ -18,21 +21,39 @@ namespace Swift { virtual ~DummyEventLoop(); void processEvents() { - while (!events_.empty()) { - handleEvent(events_[0]); - events_.pop_front(); + while (hasEvents()) { + /* + Creating a copy of the to-be-handled Event object because handling + it can result in a DummyEventLoop::post() call. + This call would also try to lock the eventsMutex_, resulting in a + deadlock. + */ + + eventsMutex_.lock(); + Event eventCopy = events_[0]; + eventsMutex_.unlock(); + + handleEvent(eventCopy); + + { + boost::lock_guard<boost::mutex> lock(eventsMutex_); + events_.pop_front(); + } } } bool hasEvents() { + boost::lock_guard<boost::mutex> lock(eventsMutex_); return !events_.empty(); } virtual void post(const Event& event) { + boost::lock_guard<boost::mutex> lock(eventsMutex_); events_.push_back(event); } private: + boost::mutex eventsMutex_; std::deque<Event> events_; }; } |