From 9bce26254aa3c2feffdd751b59cdee5e903fd2bc Mon Sep 17 00:00:00 2001 From: Kevin Smith Date: Thu, 17 Sep 2015 19:21:44 +0100 Subject: Add event loop for integration in Boost ASIO This allows execution of events inside an existing io_service if an application is already using Boost ASIO for other things and can share the io_service. Test-Information: Builds on OS X 10.11.2. Change-Id: I092ed7a25b24ef95d4664bae98ed84cc0f149073 diff --git a/Swiften/EventLoop/BoostASIOEventLoop.cpp b/Swiften/EventLoop/BoostASIOEventLoop.cpp new file mode 100644 index 0000000..f3c5618 --- /dev/null +++ b/Swiften/EventLoop/BoostASIOEventLoop.cpp @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2015 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#include + +#include + +namespace Swift { + +BoostASIOEventLoop::BoostASIOEventLoop(boost::shared_ptr ioService) : ioService_(ioService) { + +} + +BoostASIOEventLoop::~BoostASIOEventLoop() { + +} + +void BoostASIOEventLoop::handleASIOEvent() { + { + boost::recursive_mutex::scoped_lock lock(isEventInASIOEventLoopMutex_); + isEventInASIOEventLoop_ = false; + } + handleNextEvent(); +} + +void BoostASIOEventLoop::eventPosted() { + boost::recursive_mutex::scoped_lock lock(isEventInASIOEventLoopMutex_); + if (!isEventInASIOEventLoop_) { + isEventInASIOEventLoop_ = true; + ioService_->post(boost::bind(&BoostASIOEventLoop::handleASIOEvent, this)); + } +} + +} diff --git a/Swiften/EventLoop/BoostASIOEventLoop.h b/Swiften/EventLoop/BoostASIOEventLoop.h new file mode 100644 index 0000000..a093199 --- /dev/null +++ b/Swiften/EventLoop/BoostASIOEventLoop.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2015 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#pragma once + +#include +#include +#include + +#include +#include +#include + +namespace Swift { + class SWIFTEN_API BoostASIOEventLoop : public EventLoop { + public: + BoostASIOEventLoop(boost::shared_ptr ioService); + virtual ~BoostASIOEventLoop(); + + protected: + void handleASIOEvent(); + + virtual void eventPosted(); + + private: + boost::shared_ptr ioService_; + + bool isEventInASIOEventLoop_; + boost::recursive_mutex isEventInASIOEventLoopMutex_; + }; +} diff --git a/Swiften/EventLoop/SConscript b/Swiften/EventLoop/SConscript index d26661c..b810e8c 100644 --- a/Swiften/EventLoop/SConscript +++ b/Swiften/EventLoop/SConscript @@ -1,11 +1,12 @@ Import("swiften_env") sources = [ + "BoostASIOEventLoop.cpp", + "DummyEventLoop.cpp", + "Event.cpp", "EventLoop.cpp", "EventOwner.cpp", - "Event.cpp", "SimpleEventLoop.cpp", - "DummyEventLoop.cpp", "SingleThreadedEventLoop.cpp", ] diff --git a/Swiften/Network/BoostIOServiceThread.cpp b/Swiften/Network/BoostIOServiceThread.cpp index 6a89c9a..57b2cb4 100644 --- a/Swiften/Network/BoostIOServiceThread.cpp +++ b/Swiften/Network/BoostIOServiceThread.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. */ @@ -10,18 +10,27 @@ namespace Swift { -BoostIOServiceThread::BoostIOServiceThread() { - ioService_ = boost::make_shared(); - thread_ = new boost::thread(boost::bind(&BoostIOServiceThread::doRun, this)); +BoostIOServiceThread::BoostIOServiceThread(boost::shared_ptr ioService) { + if (!!ioService) { + ioService_ = ioService; + thread_ = NULL; + } + else { + ioService_ = boost::make_shared(); + thread_ = new boost::thread(boost::bind(&BoostIOServiceThread::doRun, this)); + } } BoostIOServiceThread::~BoostIOServiceThread() { - ioService_->stop(); - thread_->join(); - delete thread_; + if (thread_) { + ioService_->stop(); + thread_->join(); + delete thread_; + } } void BoostIOServiceThread::doRun() { + assert(thread_); boost::asio::io_service::work work(*ioService_); ioService_->run(); } diff --git a/Swiften/Network/BoostIOServiceThread.h b/Swiften/Network/BoostIOServiceThread.h index 37047d1..428837b 100644 --- a/Swiften/Network/BoostIOServiceThread.h +++ b/Swiften/Network/BoostIOServiceThread.h @@ -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. */ @@ -15,7 +15,15 @@ namespace Swift { class SWIFTEN_API BoostIOServiceThread { public: - BoostIOServiceThread(); + /** + * Construct the object. + * @param ioService If this optional parameter is provided, the behaviour + * of this class changes completely - it no longer spawns its own thread + * and instead acts as a simple wrapper of the io_service. Use this if + * you are re-using an io_service from elsewhere (particularly if you + * are using the BoostASIOEventLoop). + */ + BoostIOServiceThread(boost::shared_ptr ioService = boost::shared_ptr()); ~BoostIOServiceThread(); boost::shared_ptr getIOService() const { diff --git a/Swiften/Network/BoostNetworkFactories.cpp b/Swiften/Network/BoostNetworkFactories.cpp index 4ab42d9..871a38b 100644 --- a/Swiften/Network/BoostNetworkFactories.cpp +++ b/Swiften/Network/BoostNetworkFactories.cpp @@ -1,24 +1,24 @@ /* - * Copyright (c) 2010-2013 Isode Limited. + * Copyright (c) 2010-2015 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include -#include -#include +#include +#include +#include +#include +#include #include -#include -#include +#include #include +#include #include -#include #include -#include -#include -#include -#include +#include +#include #ifdef USE_UNBOUND #include @@ -28,7 +28,7 @@ namespace Swift { -BoostNetworkFactories::BoostNetworkFactories(EventLoop* eventLoop) : eventLoop(eventLoop){ +BoostNetworkFactories::BoostNetworkFactories(EventLoop* eventLoop, boost::shared_ptr ioService) : ioServiceThread(ioService), eventLoop(eventLoop) { timerFactory = new BoostTimerFactory(ioServiceThread.getIOService(), eventLoop); connectionFactory = new BoostConnectionFactory(ioServiceThread.getIOService(), eventLoop); connectionServerFactory = new BoostConnectionServerFactory(ioServiceThread.getIOService(), eventLoop); diff --git a/Swiften/Network/BoostNetworkFactories.h b/Swiften/Network/BoostNetworkFactories.h index 32fdd93..12755b9 100644 --- a/Swiften/Network/BoostNetworkFactories.h +++ b/Swiften/Network/BoostNetworkFactories.h @@ -8,8 +8,8 @@ #include #include -#include #include +#include namespace Swift { class EventLoop; @@ -18,7 +18,12 @@ namespace Swift { class SWIFTEN_API BoostNetworkFactories : public NetworkFactories { public: - BoostNetworkFactories(EventLoop* eventLoop); + /** + * Construct the network factories, using the provided EventLoop. + * @param ioService If this optional parameter is provided, it will be + * used for the construction of the BoostIOServiceThread. + */ + BoostNetworkFactories(EventLoop* eventLoop, boost::shared_ptr ioService = boost::shared_ptr()); virtual ~BoostNetworkFactories(); virtual TimerFactory* getTimerFactory() const SWIFTEN_OVERRIDE { -- cgit v0.10.2-6-g49f6