From 31e14990dc515f1a86de12fbf555a3b874422afd Mon Sep 17 00:00:00 2001 From: Trigve Date: Fri, 19 May 2017 09:04:35 +0200 Subject: [PATCH] Server: Support for running the server in the main thread in WT_THREADED build --- src/Wt/WIOService | 4 ++++ src/Wt/WIOService.C | 10 ++++++++++ src/Wt/WServer | 8 +++++++- src/Wt/WServer.C | 19 +++++++++++++++++++ src/http/WServer.C | 31 ++++++++++++++++++++++--------- src/web/WebController.C | 8 ++++++-- 6 files changed, 68 insertions(+), 12 deletions(-) diff --git a/src/Wt/WIOService b/src/Wt/WIOService index b8c2d3b..833d87e 100644 --- a/src/Wt/WIOService +++ b/src/Wt/WIOService @@ -62,6 +62,10 @@ public: */ void start(); + /*! \brief Starts the I/O service in the main thread. + */ + void start_mt(); + /*! \brief Stops the I/O service. * * This will stop the internal thread pool. The method will block until diff --git a/src/Wt/WIOService.C b/src/Wt/WIOService.C index bc48e4d..93d35f8 100644 --- a/src/Wt/WIOService.C +++ b/src/Wt/WIOService.C @@ -109,6 +109,16 @@ void WIOService::start() } } +void WIOService::start_mt() +{ + if(!impl_->work_) + { + impl_->work_ = new boost::asio::io_service::work(*this); + + run(); + } +} + void WIOService::stop() { delete impl_->work_; diff --git a/src/Wt/WServer b/src/Wt/WServer index 27928dd..1371124 100644 --- a/src/Wt/WServer +++ b/src/Wt/WServer @@ -261,7 +261,10 @@ public: * \sa isRunning(), stop() */ WTCONNECTOR_API bool start(); - + + /*! \brief Sets whatever a server should be run in the main thread. + */ + WT_API void setrunInMainThread(bool Enable); /*! \brief Stops the server. * * All active application sessions are terminated cleanly, and the @@ -500,6 +503,8 @@ public: WT_API static void terminate(); #endif // WT_WIN32 + WT_API bool runInMainThread() const; + private: WebController *webController_; @@ -515,6 +520,7 @@ private: WIOService *ioService_; bool dedicatedProcessEnabled_; + bool runInMainThreadEnabled; struct Impl; Impl *impl_; diff --git a/src/Wt/WServer.C b/src/Wt/WServer.C index c414cb2..2053856 100644 --- a/src/Wt/WServer.C +++ b/src/Wt/WServer.C @@ -45,6 +45,7 @@ void WServer::init(const std::string& wtApplicationPath, ownsIOService_ = true; dedicatedProcessEnabled_ = false; + runInMainThreadEnabled = false; ioService_ = 0; webController_ = 0; configuration_ = 0; @@ -172,6 +173,16 @@ void WServer::initLogger(const std::string& logFile, LOG_INFO("initializing " << description_); } +void WServer::setrunInMainThread(bool Enable) +{ + runInMainThreadEnabled = Enable; +} + +bool WServer::runInMainThread() const +{ + return runInMainThreadEnabled; +} + Configuration& WServer::configuration() { if (!configuration_) { @@ -398,11 +409,19 @@ bool WServer::expireSessions() void WServer::scheduleStop() { #ifdef WT_THREADED + if(runInMainThread()) + { + if(!stopCallback_.empty()) + stopCallback_(); + } + else + { #ifndef WT_WIN32 kill(getpid(), SIGTERM); #else // WT_WIN32 terminate(); #endif // WT_WIN32 + } #else // !WT_THREADED if (!stopCallback_.empty()) stopCallback_(); diff --git a/src/http/WServer.C b/src/http/WServer.C index 5aeb138..23671f9 100644 --- a/src/http/WServer.C +++ b/src/http/WServer.C @@ -174,10 +174,20 @@ bool WServer::start() LOG_WARN("No boost thread support, running in main thread."); #endif // WT_THREADED - webController_->start(); + webController_->start(); - ioService().start(); + if(runInMainThread()) + { + ioService().start_mt(); + delete impl_->server_; + impl_->server_ = 0; + + ioService().stop(); + } + else + ioService().start(); + #ifndef WT_THREADED delete impl_->server_; impl_->server_ = 0; @@ -186,7 +196,7 @@ bool WServer::start() return false; #else - return true; + return runInMainThread() ? false : true; #endif // WT_THREADED } catch (asio_system_error& e) { @@ -220,18 +230,21 @@ void WServer::stop() #ifdef WT_THREADED try { - // Stop the Wt application server (cleaning up all sessions). - webController_->shutdown(); + // Stop the Wt application server (cleaning up all sessions). + webController_->shutdown(); - LOG_INFO("Shutdown: stopping web server."); + LOG_INFO("Shutdown: stopping web server."); // Stop the server. impl_->server_->stop(); - ioService().stop(); + ioService().stop(); - delete impl_->server_; - impl_->server_ = 0; + if(!runInMainThread()) + { + delete impl_->server_; + impl_->server_ = 0; + } } catch (asio_system_error& e) { throw Exception(std::string("Error (asio): ") + e.what()); } catch (std::exception& e) { diff --git a/src/web/WebController.C b/src/web/WebController.C index 396a6e9..0c12296 100644 --- a/src/web/WebController.C +++ b/src/web/WebController.C @@ -158,8 +158,12 @@ void WebController::shutdown() } #ifdef WT_THREADED - while (zombieSessions_ > 0) { - boost::this_thread::sleep(boost::posix_time::milliseconds(10)); + if(!server_.runInMainThread()) + { + while(zombieSessions_ > 0) + { + boost::this_thread::sleep(boost::posix_time::milliseconds(10)); + } } #endif } -- 2.10.1.windows.1