diff options
author | leak <leak@bitmx.net> | 2014-07-08 20:55:25 +0200 |
---|---|---|
committer | leak <leak@bitmx.net> | 2014-07-08 20:55:25 +0200 |
commit | d1594998f80762fa58f64cf123f9bf9cb77036e4 (patch) | |
tree | 11b289988ddf931333c3b043c4b06692c07d0ce0 | |
parent | c24bf2f0287fb004b5b231e2cfa7e0b7ebec3bec (diff) |
Replaced the LogWorker thread with Boost ASIO
-rw-r--r-- | src/server/shared/Database/DatabaseWorker.h | 1 | ||||
-rw-r--r-- | src/server/shared/Logging/Log.cpp | 17 | ||||
-rw-r--r-- | src/server/shared/Logging/Log.h | 16 | ||||
-rw-r--r-- | src/server/shared/Logging/LogWorker.cpp | 56 | ||||
-rw-r--r-- | src/server/shared/Logging/LogWorker.h | 46 | ||||
-rw-r--r-- | src/server/shared/Networking/AsyncAcceptor.h | 4 | ||||
-rw-r--r-- | src/server/worldserver/Main.cpp | 6 |
7 files changed, 31 insertions, 115 deletions
diff --git a/src/server/shared/Database/DatabaseWorker.h b/src/server/shared/Database/DatabaseWorker.h index 91aef2c7194..6f452c767f6 100644 --- a/src/server/shared/Database/DatabaseWorker.h +++ b/src/server/shared/Database/DatabaseWorker.h @@ -18,6 +18,7 @@ #ifndef _WORKERTHREAD_H #define _WORKERTHREAD_H +#include <thread> #include "ProducerConsumerQueue.h" class MySQLConnection; diff --git a/src/server/shared/Logging/Log.cpp b/src/server/shared/Logging/Log.cpp index 57d8797e61e..65cf930a634 100644 --- a/src/server/shared/Logging/Log.cpp +++ b/src/server/shared/Logging/Log.cpp @@ -29,7 +29,7 @@ #include <cstdio> #include <sstream> -Log::Log() : worker(NULL) +Log::Log() : _ioService(nullptr), _strand(nullptr) { m_logsTimestamp = "_" + GetTimestampStr(); LoadFromConfig(); @@ -37,6 +37,7 @@ Log::Log() : worker(NULL) Log::~Log() { + delete _strand; Close(); } @@ -272,8 +273,13 @@ void Log::write(LogMessage* msg) const Logger const* logger = GetLoggerByType(msg->type); msg->text.append("\n"); - if (worker) - worker->Enqueue(new LogOperation(logger, msg)); + if (_ioService) + { + auto logOperation = std::shared_ptr<LogOperation>(new LogOperation(logger, msg)); + + _ioService->post(_strand->wrap([logOperation](){ logOperation->call(); })); + + } else { logger->write(*msg); @@ -375,8 +381,6 @@ void Log::SetRealmId(uint32 id) void Log::Close() { - delete worker; - worker = NULL; loggers.clear(); for (AppenderMap::iterator it = appenders.begin(); it != appenders.end(); ++it) { @@ -390,9 +394,6 @@ void Log::LoadFromConfig() { Close(); - if (sConfigMgr->GetBoolDefault("Log.Async.Enable", false)) - worker = new LogWorker(); - AppenderId = 0; m_logsDir = sConfigMgr->GetStringDefault("LogsDir", ""); if (!m_logsDir.empty()) diff --git a/src/server/shared/Logging/Log.h b/src/server/shared/Logging/Log.h index 8d2fd33d886..e739c9eaf4e 100644 --- a/src/server/shared/Logging/Log.h +++ b/src/server/shared/Logging/Log.h @@ -22,8 +22,9 @@ #include "Define.h" #include "Appender.h" #include "Logger.h" -#include "LogWorker.h" #include <stdarg.h> +#include <boost/asio/io_service.hpp> +#include <boost/asio/strand.hpp> #include <unordered_map> #include <string> @@ -39,9 +40,17 @@ class Log ~Log(); public: - static Log* instance() + + static Log* instance(boost::asio::io_service* ioService = nullptr) { static Log* instance = new Log(); + + if (ioService != nullptr) + { + instance->_ioService = ioService; + instance->_strand = new boost::asio::strand(*ioService); + } + return instance; } @@ -77,7 +86,8 @@ class Log std::string m_logsDir; std::string m_logsTimestamp; - LogWorker* worker; + boost::asio::io_service* _ioService; + boost::asio::strand* _strand; }; inline Logger const* Log::GetLoggerByType(std::string const& type) const diff --git a/src/server/shared/Logging/LogWorker.cpp b/src/server/shared/Logging/LogWorker.cpp deleted file mode 100644 index ab0f41bf105..00000000000 --- a/src/server/shared/Logging/LogWorker.cpp +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright (C) 2008-2014 TrinityCore <http://www.trinitycore.org/> - * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation; either version 2 of the License, or (at your - * option) any later version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for - * more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see <http://www.gnu.org/licenses/>. - */ - -#include "LogWorker.h" -#include <thread> - -LogWorker::LogWorker() -{ - _cancelationToken = false; - _workerThread = std::thread(&LogWorker::WorkerThread, this); -} - -LogWorker::~LogWorker() -{ - _cancelationToken = true; - - _queue.Cancel(); - - _workerThread.join(); -} - -void LogWorker::Enqueue(LogOperation* op) -{ - return _queue.Push(op); -} - -void LogWorker::WorkerThread() -{ - while (1) - { - LogOperation* operation = nullptr; - - _queue.WaitAndPop(operation); - - if (_cancelationToken) - return; - - operation->call(); - - delete operation; - } -} diff --git a/src/server/shared/Logging/LogWorker.h b/src/server/shared/Logging/LogWorker.h deleted file mode 100644 index b2680b12c34..00000000000 --- a/src/server/shared/Logging/LogWorker.h +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright (C) 2008-2014 TrinityCore <http://www.trinitycore.org/> - * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation; either version 2 of the License, or (at your - * option) any later version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for - * more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see <http://www.gnu.org/licenses/>. - */ - -#ifndef LOGWORKER_H -#define LOGWORKER_H - -#include <atomic> -#include <thread> - -#include "LogOperation.h" -#include "ProducerConsumerQueue.h" - -class LogWorker -{ - public: - LogWorker(); - ~LogWorker(); - - void Enqueue(LogOperation *op); - - private: - ProducerConsumerQueue<LogOperation*> _queue; - - void WorkerThread(); - std::thread _workerThread; - - std::atomic_bool _cancelationToken; -}; - - - -#endif diff --git a/src/server/shared/Networking/AsyncAcceptor.h b/src/server/shared/Networking/AsyncAcceptor.h index 1b65282527a..789bd9c3a74 100644 --- a/src/server/shared/Networking/AsyncAcceptor.h +++ b/src/server/shared/Networking/AsyncAcceptor.h @@ -37,9 +37,9 @@ public: _socket(ioService), _acceptor(ioService, tcp::endpoint(boost::asio::ip::address::from_string(bindIp), port)) { - AsyncAccept(); + _acceptor.set_option(boost::asio::ip::tcp::no_delay(tcpNoDelay)); - _socket.set_option(boost::asio::ip::tcp::no_delay(tcpNoDelay)); + AsyncAccept(); }; private: diff --git a/src/server/worldserver/Main.cpp b/src/server/worldserver/Main.cpp index 15a282b70ea..05ae31e8780 100644 --- a/src/server/worldserver/Main.cpp +++ b/src/server/worldserver/Main.cpp @@ -145,6 +145,12 @@ extern int main(int argc, char** argv) return 1; } + if (sConfigMgr->GetBoolDefault("Log.Async.Enable", false)) + { + // If logs are supposed to be handled async then we need to pass the io_service into the Log singleton + Log::instance(&_ioService); + } + TC_LOG_INFO("server.worldserver", "%s (worldserver-daemon)", _FULLVERSION); TC_LOG_INFO("server.worldserver", "<Ctrl-C> to stop.\n"); TC_LOG_INFO("server.worldserver", " ______ __"); |