diff options
author | Tor Egge <Tor.Egge@oath.com> | 2017-10-30 11:06:11 +0000 |
---|---|---|
committer | Tor Egge <Tor.Egge@oath.com> | 2017-10-30 11:06:11 +0000 |
commit | c46c12fe27cbcb0f3ff8105e42cb490aab60921c (patch) | |
tree | c415cd8cacb49edcbcd21b61d809767894dc5196 /fastos/src | |
parent | ed5f7e9d6db0b5a88f7eb8c8a95429b502bb97f8 (diff) |
Use std::mutex instead of FastOS_Mutex to protect process information
contained in FastOS_Application.
Diffstat (limited to 'fastos/src')
-rw-r--r-- | fastos/src/vespa/fastos/app.cpp | 2 | ||||
-rw-r--r-- | fastos/src/vespa/fastos/app.h | 7 | ||||
-rw-r--r-- | fastos/src/vespa/fastos/unix_app.cpp | 10 | ||||
-rw-r--r-- | fastos/src/vespa/fastos/unix_ipc.cpp | 46 | ||||
-rw-r--r-- | fastos/src/vespa/fastos/unix_process.cpp | 36 |
5 files changed, 50 insertions, 51 deletions
diff --git a/fastos/src/vespa/fastos/app.cpp b/fastos/src/vespa/fastos/app.cpp index 824d009591f..822683540f7 100644 --- a/fastos/src/vespa/fastos/app.cpp +++ b/fastos/src/vespa/fastos/app.cpp @@ -65,7 +65,7 @@ bool FastOS_ApplicationInterface::Init () if(errorMsg == nullptr) { - _processListMutex = new FastOS_Mutex(); + _processListMutex = new std::mutex; _threadPool = new FastOS_ThreadPool(128 * 1024); rc = true; } diff --git a/fastos/src/vespa/fastos/app.h b/fastos/src/vespa/fastos/app.h index 283db64985c..9560d1ced6a 100644 --- a/fastos/src/vespa/fastos/app.h +++ b/fastos/src/vespa/fastos/app.h @@ -15,7 +15,7 @@ class FastOS_ProcessInterface; class FastOS_ThreadPool; -#include <vespa/fastos/mutex.h> +#include <mutex> /** * FastOS application wrapper class. @@ -143,7 +143,7 @@ protected: FastOS_ThreadPool *_threadPool; FastOS_ProcessInterface *_processList; - FastOS_Mutex *_processListMutex; + std::mutex *_processListMutex; bool _disableLeakReporting; virtual bool PreThreadInit () { return true; } @@ -248,8 +248,7 @@ public: void AddChildProcess (FastOS_ProcessInterface *node); void RemoveChildProcess (FastOS_ProcessInterface *node); - void ProcessLock () { _processListMutex->Lock(); } - void ProcessUnlock() { _processListMutex->Unlock(); } + std::unique_lock<std::mutex> getProcessGuard() { return std::unique_lock<std::mutex>(*_processListMutex); } FastOS_ProcessInterface *GetProcessList () { return _processList; } FastOS_ThreadPool *GetThreadPool (); diff --git a/fastos/src/vespa/fastos/unix_app.cpp b/fastos/src/vespa/fastos/unix_app.cpp index 7682b2d5b8f..c60035aa5ab 100644 --- a/fastos/src/vespa/fastos/unix_app.cpp +++ b/fastos/src/vespa/fastos/unix_app.cpp @@ -162,9 +162,13 @@ void FastOS_UNIX_Application::Cleanup () _ipcHelper->Exit(); if (_processStarter != nullptr) { - if (_processListMutex) ProcessLock(); - _processStarter->Stop(); - if (_processListMutex) ProcessUnlock(); + { + std::unique_lock<std::mutex> guard; + if (_processListMutex) { + guard = getProcessGuard(); + } + _processStarter->Stop(); + } delete _processStarter; _processStarter = nullptr; } diff --git a/fastos/src/vespa/fastos/unix_ipc.cpp b/fastos/src/vespa/fastos/unix_ipc.cpp index 695d395674f..2eec1ab93a4 100644 --- a/fastos/src/vespa/fastos/unix_ipc.cpp +++ b/fastos/src/vespa/fastos/unix_ipc.cpp @@ -474,27 +474,27 @@ Run(FastOS_ThreadInterface *thisThread, void *arg) for(;;) { // Deliver messages to from child processes and parent. - _app->ProcessLock(); - for(node = _app->GetProcessList(); node != nullptr; node = node->_next) { - FastOS_UNIX_Process *xproc = static_cast<FastOS_UNIX_Process *>(node); - FastOS_UNIX_Process::DescriptorHandle &desc = - xproc->GetDescriptorHandle(FastOS_UNIX_Process::TYPE_IPC); - DeliverMessages(desc._readBuffer.get()); - PipeData(xproc, FastOS_UNIX_Process::TYPE_STDOUT); - PipeData(xproc, FastOS_UNIX_Process::TYPE_STDERR); - } - DeliverMessages(_appParentIPCDescriptor._readBuffer.get()); - - // Setup file descriptor sets for the next select() call - BuildPollChecks(); + auto guard = _app->getProcessGuard(); + for(node = _app->GetProcessList(); node != nullptr; node = node->_next) + { + FastOS_UNIX_Process *xproc = static_cast<FastOS_UNIX_Process *>(node); + FastOS_UNIX_Process::DescriptorHandle &desc = + xproc->GetDescriptorHandle(FastOS_UNIX_Process::TYPE_IPC); + DeliverMessages(desc._readBuffer.get()); + PipeData(xproc, FastOS_UNIX_Process::TYPE_STDOUT); + PipeData(xproc, FastOS_UNIX_Process::TYPE_STDERR); + } + DeliverMessages(_appParentIPCDescriptor._readBuffer.get()); - // Close and signal closing processes - RemoveClosingProcesses(); + // Setup file descriptor sets for the next select() call + BuildPollChecks(); - BuildPollArray(&fds, &nfds, &allocnfds); + // Close and signal closing processes + RemoveClosingProcesses(); - _app->ProcessUnlock(); + BuildPollArray(&fds, &nfds, &allocnfds); + } _lock.Lock(); bool exitFlag(_exitFlag); @@ -546,11 +546,13 @@ Run(FastOS_ThreadInterface *thisThread, void *arg) break; } - _app->ProcessLock(); - bool woken = SavePollArray(fds, nfds); - // Do actual IO (based on file descriptor sets and buffer contents) - PerformAsyncIO(); - _app->ProcessUnlock(); + bool woken = false; + { + auto guard = _app->getProcessGuard(); + woken = SavePollArray(fds, nfds); + // Do actual IO (based on file descriptor sets and buffer contents) + PerformAsyncIO(); + } PerformAsyncIPCIO(); // Did someone want to wake us up from the poll() call? diff --git a/fastos/src/vespa/fastos/unix_process.cpp b/fastos/src/vespa/fastos/unix_process.cpp index df32cb935ff..80ad0605f78 100644 --- a/fastos/src/vespa/fastos/unix_process.cpp +++ b/fastos/src/vespa/fastos/unix_process.cpp @@ -805,9 +805,10 @@ FastOS_UNIX_Process (const char *cmdLine, bool pipeStdin, if (stderrListener != nullptr) _descriptor[TYPE_STDERR]._readBuffer.reset(new FastOS_RingBuffer(bufferSize)); - _app->ProcessLock(); - _app->AddChildProcess(this); - _app->ProcessUnlock(); + { + auto guard = _app->getProcessGuard(); + _app->AddChildProcess(this); + } // App::AddToIPCComm() is performed when the process is started } @@ -825,9 +826,8 @@ FastOS_UNIX_Process::~FastOS_UNIX_Process () static_cast<FastOS_UNIX_Application *>(_app)->RemoveFromIPCComm(this); } else { // No IPC descriptor, do it ourselves - _app->ProcessLock(); + auto guard = _app->getProcessGuard(); _app->RemoveChildProcess(this); - _app->ProcessUnlock(); } for(int i=0; i<int(TYPE_COUNT); i++) { @@ -897,7 +897,7 @@ bool FastOS_UNIX_Process::Signal(int sig) bool rc = false; pid_t pid; - _app->ProcessLock(); + auto guard = _app->getProcessGuard(); pid = GetProcessId(); if (pid == 0) { /* Do nothing */ @@ -908,7 +908,6 @@ bool FastOS_UNIX_Process::Signal(int sig) _killed = true; rc = true; } - _app->ProcessUnlock(); return rc; } @@ -1722,7 +1721,7 @@ CreateProcess (FastOS_UNIX_Process *process, const char *cmdLine = process->GetCommandLine(); - process->_app->ProcessLock(); + auto guard = _app->getProcessGuard(); if (process->GetDirectChild()) { _hasDirectChildren = true; @@ -1770,7 +1769,7 @@ CreateProcess (FastOS_UNIX_Process *process, "Forkandexec %s failed\n", cmdLine); } - process->_app->ProcessUnlock(); + guard.unlock(); delete rprocess; FreeEnvironmentVariables(env); return rc; @@ -1847,8 +1846,6 @@ CreateProcess (FastOS_UNIX_Process *process, } } } - process->_app->ProcessUnlock(); - return rc; } @@ -1926,13 +1923,13 @@ FastOS_UNIX_ProcessStarter::Wait(FastOS_UNIX_Process *process, *pollStillRunning = true; for (;;) { - process->_app->ProcessLock(); - - if (_hasDirectChildren) PollReapDirectChildren(); + { + auto guard = process->_app->getProcessGuard(); - if (_hasProxiedChildren) PollReapProxiedChildren(); + if (_hasDirectChildren) PollReapDirectChildren(); - process->_app->ProcessUnlock(); + if (_hasProxiedChildren) PollReapProxiedChildren(); + } if (process->GetDeathFlag()) { if (pollStillRunning != nullptr) @@ -1971,16 +1968,14 @@ bool FastOS_UNIX_ProcessStarter::Detach(FastOS_UNIX_Process *process) bool rc = true; pid_t pid; - process->_app->ProcessLock(); + auto guard = process->_app->getProcessGuard(); pid = process->GetProcessId(); if (pid == 0) { - process->_app->ProcessUnlock(); return false; // Cannot detach nonstarted process. } if (process->GetDeathFlag()) { - process->_app->ProcessUnlock(); return true; } @@ -2004,7 +1999,6 @@ bool FastOS_UNIX_ProcessStarter::Detach(FastOS_UNIX_Process *process) ReadBytes(_mainSocket, &returnCode, sizeof(int)); process->DeathNotification(returnCode); } - process->_app->ProcessUnlock(); return rc; } @@ -2044,4 +2038,4 @@ FastOS_UNIX_Process::DescriptorHandle::CloseHandleDirectChild() close(_fd); _fd = -1; } -}
\ No newline at end of file +} |