diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2022-02-16 17:56:11 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-16 17:56:11 +0100 |
commit | 508bd66a3519226e4a6dc34cabb218d4527c47f0 (patch) | |
tree | e1cde15a50c0e83e5ef5739c9df4f157e0051fa6 | |
parent | 2f6ed10a4f7c4dcea1d1aebec5c781f60199fc65 (diff) | |
parent | e0469912686e967b27e14431c83dae3610ee19ab (diff) |
Merge pull request #21232 from vespa-engine/vekterli/make-countdownlatch-debug-function-thread-safev7.545.20
Make CountDownLatch debug function thread safe
-rw-r--r-- | vespalib/src/vespa/vespalib/util/count_down_latch.h | 13 |
1 files changed, 8 insertions, 5 deletions
diff --git a/vespalib/src/vespa/vespalib/util/count_down_latch.h b/vespalib/src/vespa/vespalib/util/count_down_latch.h index d543d773909..613a60e90c5 100644 --- a/vespalib/src/vespa/vespalib/util/count_down_latch.h +++ b/vespalib/src/vespa/vespalib/util/count_down_latch.h @@ -22,7 +22,7 @@ namespace vespalib { class CountDownLatch { private: - std::mutex _lock; + mutable std::mutex _lock; std::condition_variable _cond; uint32_t _count; @@ -44,7 +44,7 @@ public: * blocked in the await method will be unblocked. **/ void countDown() { - std::lock_guard<std::mutex> guard(_lock); + std::lock_guard guard(_lock); if (_count != 0) { --_count; if (_count == 0) { @@ -59,7 +59,7 @@ public: * reduce the count to 0. **/ void await() { - std::unique_lock<std::mutex> guard(_lock); + std::unique_lock guard(_lock); _cond.wait(guard, [this]() { return (_count == 0); }); } @@ -72,7 +72,7 @@ public: * @return true if the counter reached 0, false if we timed out **/ bool await(vespalib::duration maxwait) { - std::unique_lock<std::mutex> guard(_lock); + std::unique_lock guard(_lock); return _cond.wait_for(guard, maxwait, [this]() { return (_count == 0); }); } @@ -82,7 +82,10 @@ public: * * @return current count **/ - uint32_t getCount() const { return _count; } + [[nodiscard]] uint32_t getCount() const noexcept { + std::lock_guard guard(_lock); + return _count; + } /** * Empty. Needs to be virtual to reduce compiler warnings. |