diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2020-10-14 20:23:37 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-14 20:23:37 +0200 |
commit | bf738b79b217814f11bc3dc57b5d0e6bad8cd1a2 (patch) | |
tree | a2c41ed5c0c963a0e0538212feb85e03ca9ccb64 /vespalib | |
parent | 89ae1507eb41fc760fc93e3d341db24babccbe44 (diff) | |
parent | 2ec50fb20f5288ac7f40097cbfb74da6c893f8f2 (diff) |
Merge pull request #14878 from vespa-engine/balder/modernize-barrier
vespalib::Monitor -> std:.mutex/std::condition_variable
Diffstat (limited to 'vespalib')
-rw-r--r-- | vespalib/src/vespa/vespalib/util/barrier.cpp | 19 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/util/barrier.h | 14 |
2 files changed, 22 insertions, 11 deletions
diff --git a/vespalib/src/vespa/vespalib/util/barrier.cpp b/vespalib/src/vespa/vespalib/util/barrier.cpp index d20298ffada..32f63424ae1 100644 --- a/vespalib/src/vespa/vespalib/util/barrier.cpp +++ b/vespalib/src/vespa/vespalib/util/barrier.cpp @@ -4,10 +4,19 @@ namespace vespalib { +Barrier::Barrier(size_t n) + : _n(n), + _lock(), + _cond(), + _count(0), + _next(0) +{} +Barrier::~Barrier() = default; + bool Barrier::await() { - MonitorGuard guard(_monitor); + std::unique_lock guard(_lock); if (_n == 0) { return false; } @@ -15,14 +24,14 @@ Barrier::await() _next += _n; } if (++_count == _next) { - guard.broadcast(); + _cond.notify_all(); } else { size_t limit = _next; while ((_count - limit) > _n) { if (_n == 0) { return false; } - guard.wait(); + _cond.wait(guard); } } return true; @@ -31,9 +40,9 @@ Barrier::await() void Barrier::destroy() { - MonitorGuard guard(_monitor); + std::lock_guard guard(_lock); _n = 0; - guard.broadcast(); + _cond.notify_all(); } } // namespace vespalib diff --git a/vespalib/src/vespa/vespalib/util/barrier.h b/vespalib/src/vespa/vespalib/util/barrier.h index 20768c3abcf..3eeb0cb5837 100644 --- a/vespalib/src/vespa/vespalib/util/barrier.h +++ b/vespalib/src/vespa/vespalib/util/barrier.h @@ -2,7 +2,7 @@ #pragma once -#include "sync.h" +#include <condition_variable> namespace vespalib { @@ -12,10 +12,11 @@ namespace vespalib { class Barrier { private: - size_t _n; - Monitor _monitor; - size_t _count; - size_t _next; + size_t _n; + std::mutex _lock; + std::condition_variable _cond; + size_t _count; + size_t _next; public: /** @@ -23,7 +24,8 @@ public: * * @param n number of participants **/ - Barrier(size_t n) : _n(n), _monitor(), _count(0), _next(0) {} + Barrier(size_t n); + ~Barrier(); /** * Wait for the (n - 1) other participants to call this |