From fb90317434d0f157cb256d3e9df4e64819622e9c Mon Sep 17 00:00:00 2001 From: Tor Egge Date: Fri, 27 Oct 2017 03:02:56 +0200 Subject: Use std::mutex and std::condition_variable instead of FastOS_Cond. --- fastlib/src/vespa/fastlib/net/httpserver.cpp | 83 ++++++++++++++-------------- fastlib/src/vespa/fastlib/net/httpserver.h | 8 ++- 2 files changed, 46 insertions(+), 45 deletions(-) (limited to 'fastlib/src') diff --git a/fastlib/src/vespa/fastlib/net/httpserver.cpp b/fastlib/src/vespa/fastlib/net/httpserver.cpp index 807b169323d..a9bba95a8ff 100644 --- a/fastlib/src/vespa/fastlib/net/httpserver.cpp +++ b/fastlib/src/vespa/fastlib/net/httpserver.cpp @@ -338,6 +338,7 @@ Fast_HTTPServer::Fast_HTTPServer(int portNumber, int stackSize, int maxThreads, int clientReadTimeout /* = -1 no timeout */) : _connections(10), + _connectionLock(), _connectionCond(), _threadPool(NULL), _acceptThread(NULL), @@ -365,22 +366,23 @@ int Fast_HTTPServer::Start(void) { int retCode = FASTLIB_SUCCESS; - _runningMutex.Lock(); - if (!_isRunning) { - // Try listening - retCode = Listen(); - - // Start worker thread - if (retCode == FASTLIB_SUCCESS) { - _acceptThread = static_cast(_threadPool->NewThread(this)); - if (_acceptThread == NULL) { - retCode = FASTLIB_HTTPSERVER_NEWTHREADFAILED; + { + std::unique_lock runningGuard(_runningMutex); + if (!_isRunning) { + // Try listening + retCode = Listen(); + + // Start worker thread + if (retCode == FASTLIB_SUCCESS) { + _acceptThread = static_cast(_threadPool->NewThread(this)); + if (_acceptThread == NULL) { + retCode = FASTLIB_HTTPSERVER_NEWTHREADFAILED; + } } + } else { + retCode = FASTLIB_HTTPSERVER_ALREADYSTARTED; } - } else { - retCode = FASTLIB_HTTPSERVER_ALREADYSTARTED; } - _runningMutex.Unlock(); return retCode; } @@ -388,12 +390,13 @@ int Fast_HTTPServer::Start(void) void Fast_HTTPServer::Stop(void) { - _runningMutex.Lock(); - _stopSignalled = true; - if (_acceptThread) { - _acceptThread->SetBreakFlag(); + { + std::unique_lock runningGuard(_runningMutex); + _stopSignalled = true; + if (_acceptThread) { + _acceptThread->SetBreakFlag(); + } } - _runningMutex.Unlock(); if (_acceptThread) { _acceptThread->Join(); } @@ -404,9 +407,8 @@ Fast_HTTPServer::Stop(void) { bool Fast_HTTPServer::StopSignalled(void) { bool retVal; - _runningMutex.Lock(); + std::unique_lock runningGuard(_runningMutex); retVal = _stopSignalled; - _runningMutex.Unlock(); return retVal; } @@ -416,15 +418,16 @@ Fast_HTTPServer::~Fast_HTTPServer(void) { Stop(); - _connectionCond.Lock(); + { + std::unique_lock connectionGuard(_connectionLock); - for (Fast_BagIterator i(_connections); !i.End(); i.Next()) - i.GetCurrent()->Interrupt(); + for (Fast_BagIterator i(_connections); !i.End(); i.Next()) + i.GetCurrent()->Interrupt(); - while (_connections.NumberOfElements() > 0) - _connectionCond.Wait(); - - _connectionCond.Unlock(); + while (_connections.NumberOfElements() > 0) { + _connectionCond.wait(connectionGuard); + } + } delete _threadPool; } @@ -454,11 +457,11 @@ void Fast_HTTPServer::Run(FastOS_ThreadInterface *thisThread, void *params) (void) params; Fast_Socket *mySocket; - - _runningMutex.Lock(); - _isRunning = true; - _stopSignalled = false; - _runningMutex.Unlock(); + { + std::unique_lock runningGuard(_runningMutex); + _isRunning = true; + _stopSignalled = false; + } if (Listen() == FASTLIB_SUCCESS) { FastOS_SocketEvent socketEvent; @@ -513,9 +516,8 @@ void Fast_HTTPServer::Run(FastOS_ThreadInterface *thisThread, void *params) _serverSocket.SetSocketEvent(NULL); } - _runningMutex.Lock(); + std::unique_lock runningGuard(_runningMutex); _isRunning = false; - _runningMutex.Unlock(); } void Fast_HTTPConnection::Run(FastOS_ThreadInterface *thisThread, void *params) @@ -1038,7 +1040,7 @@ void Fast_HTTPServer::HandleFileRequest(const string & url, Fast_HTTPConnection& void Fast_HTTPServer::SetBaseDir(const char *baseDir) { - _runningMutex.Lock(); + std::unique_lock runningGuard(_runningMutex); if (!_isRunning) { _baseDir = baseDir; @@ -1051,7 +1053,6 @@ void Fast_HTTPServer::SetBaseDir(const char *baseDir) } else { fprintf(stderr, "HTTPServer: Tried to set base dir after the server had been started. Request denied.\r\n"); } - _runningMutex.Unlock(); } void @@ -1177,19 +1178,17 @@ void Fast_HTTPServer::OutputNotFound(Fast_HTTPConnection& conn, void Fast_HTTPServer::AddConnection(Fast_HTTPConnection* connection) { - _connectionCond.Lock(); + std::unique_lock connectionGuard(_connectionLock); _connections.Insert(connection); - _connectionCond.Unlock(); } void Fast_HTTPServer::RemoveConnection(Fast_HTTPConnection* connection) { - _connectionCond.Lock(); + std::unique_lock connectionGuard(_connectionLock); _connections.RemoveElement(connection); - _connectionCond.Signal(); - _connectionCond.Unlock(); -} + _connectionCond.notify_one(); + } void Fast_HTTPConnection::Interrupt() diff --git a/fastlib/src/vespa/fastlib/net/httpserver.h b/fastlib/src/vespa/fastlib/net/httpserver.h index 571a6108181..54803fe86d4 100644 --- a/fastlib/src/vespa/fastlib/net/httpserver.h +++ b/fastlib/src/vespa/fastlib/net/httpserver.h @@ -14,8 +14,9 @@ #include #include #include -#include #include +#include +#include class FastOS_FileInterface; class Fast_HTTPServer; @@ -128,7 +129,8 @@ private: Fast_HTTPServer& operator=(const Fast_HTTPServer&); Fast_Bag _connections; - FastOS_Cond _connectionCond; + std::mutex _connectionLock; + std::condition_variable _connectionCond; protected: typedef vespalib::string string; @@ -143,7 +145,7 @@ protected: bool _isRunning; bool _isListening; bool _stopSignalled; - FastOS_Mutex _runningMutex; + std::mutex _runningMutex; /** Max number of concurrent threads */ int _maxThreads; -- cgit v1.2.3