summaryrefslogtreecommitdiffstats
path: root/vespalib
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2020-10-14 18:27:50 +0000
committerHenning Baldersheim <balder@yahoo-inc.com>2020-10-14 18:27:50 +0000
commit8387d5383f4c14dfb31112856d6656d435bb8fef (patch)
tree18802dc1f78e1e5d570372ac36139bb466209c5d /vespalib
parenta9b2923859dff064aa5a976cf3cbf61674f806bd (diff)
Use c++11 primitives for synchronization
Diffstat (limited to 'vespalib')
-rw-r--r--vespalib/src/vespa/vespalib/util/child_process.cpp27
-rw-r--r--vespalib/src/vespa/vespalib/util/child_process.h15
2 files changed, 20 insertions, 22 deletions
diff --git a/vespalib/src/vespa/vespalib/util/child_process.cpp b/vespalib/src/vespa/vespalib/util/child_process.cpp
index b804d4ca87c..33ea4cce9e5 100644
--- a/vespalib/src/vespa/vespalib/util/child_process.cpp
+++ b/vespalib/src/vespa/vespalib/util/child_process.cpp
@@ -61,17 +61,17 @@ void
ChildProcess::Reader::OnReceiveData(const void *data, size_t length)
{
const char *buf = (const char *) data;
- MonitorGuard lock(_cond);
- if (_gotEOF || (buf != NULL && length == 0)) { // ignore special cases
+ std::unique_lock lock(_lock);
+ if (_gotEOF || (buf != nullptr && length == 0)) { // ignore special cases
return;
}
- if (buf == NULL) { // EOF
+ if (buf == nullptr) { // EOF
_gotEOF = true;
} else {
_queue.push(std::string(buf, length));
}
if (_waitCnt > 0) {
- lock.signal();
+ _cond.notify_one();
}
}
@@ -85,12 +85,12 @@ ChildProcess::Reader::hasData()
bool
-ChildProcess::Reader::waitForData(child_process::Timer &timer, MonitorGuard &lock)
+ChildProcess::Reader::waitForData(child_process::Timer &timer, std::unique_lock<std::mutex> &guard)
{
// NB: caller has lock on _cond
CounterGuard count(_waitCnt);
while (!timer.update().timeOut() && !hasData() && !_gotEOF) {
- lock.wait(timer.waitTime());
+ _cond.wait_for(guard, std::chrono::milliseconds(timer.waitTime()));
}
return hasData();
}
@@ -107,7 +107,8 @@ ChildProcess::Reader::updateEOF()
ChildProcess::Reader::Reader()
- : _cond(),
+ : _lock(),
+ _cond(),
_queue(),
_data(),
_gotEOF(false),
@@ -117,9 +118,7 @@ ChildProcess::Reader::Reader()
}
-ChildProcess::Reader::~Reader()
-{
-}
+ChildProcess::Reader::~Reader() = default;
uint32_t
@@ -129,8 +128,8 @@ ChildProcess::Reader::read(char *buf, uint32_t len, int msTimeout)
return 0;
}
child_process::Timer timer(msTimeout);
- MonitorGuard lock(_cond);
- waitForData(timer, lock);
+ std::unique_lock guard(_lock);
+ waitForData(timer, guard);
uint32_t bytes = 0;
while (bytes < len && hasData()) {
if (_data.empty()) {
@@ -160,8 +159,8 @@ ChildProcess::Reader::readLine(std::string &line, int msTimeout)
return false;
}
child_process::Timer timer(msTimeout);
- MonitorGuard lock(_cond);
- while (waitForData(timer, lock)) {
+ std::unique_lock guard(_lock);
+ while (waitForData(timer, guard)) {
while (hasData()) {
if (_data.empty()) {
_data = _queue.front();
diff --git a/vespalib/src/vespa/vespalib/util/child_process.h b/vespalib/src/vespa/vespalib/util/child_process.h
index 0ae7206ac48..2edf0e03638 100644
--- a/vespalib/src/vespa/vespalib/util/child_process.h
+++ b/vespalib/src/vespa/vespalib/util/child_process.h
@@ -6,7 +6,7 @@
#ifndef FASTOS_NO_THREADS
#include <string>
#include <queue>
-#include "sync.h"
+#include <condition_variable>
namespace vespalib::child_process { class Timer; }
@@ -24,7 +24,8 @@ private:
class Reader : public FastOS_ProcessRedirectListener
{
private:
- Monitor _cond;
+ std::mutex _lock;
+ std::condition_variable _cond;
std::queue<std::string> _queue;
std::string _data;
bool _gotEOF;
@@ -33,12 +34,12 @@ private:
void OnReceiveData(const void *data, size_t length) override;
bool hasData();
- bool waitForData(child_process::Timer &timer, MonitorGuard &lock);
+ bool waitForData(child_process::Timer &timer, std::unique_lock<std::mutex> &lock);
void updateEOF();
public:
Reader();
- ~Reader();
+ ~Reader() override;
uint32_t read(char *buf, uint32_t len, int msTimeout);
bool readLine(std::string &line, int msTimeout);
@@ -105,8 +106,7 @@ public:
* @param len number of bytes to try to read
* @param msTimeout number of milliseconds to wait for data
**/
- uint32_t read(char *buf, uint32_t len,
- int msTimeout = 10000);
+ uint32_t read(char *buf, uint32_t len, int msTimeout = 10000);
/**
* @brief read a line of program output
@@ -116,8 +116,7 @@ public:
* @param msTimeout number of milliseconds to wait for data
* @return true if successful
**/
- bool readLine(std::string &line,
- int msTimeout = 10000);
+ bool readLine(std::string &line, int msTimeout = 10000);
/**
* @brief check if the program has finished writing output