From 944433cb8c85b052687d26aff4dab1e3aa727e5f Mon Sep 17 00:00:00 2001 From: Tor Brede Vekterli Date: Wed, 16 Feb 2022 16:47:54 +0000 Subject: Make CountDownLatch debug function thread safe --- vespalib/src/vespa/vespalib/util/count_down_latch.h | 13 ++++++++----- 1 file 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..ee299ef2d21 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 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 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 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::unique_lock guard(_lock); + return _count; + } /** * Empty. Needs to be virtual to reduce compiler warnings. -- cgit v1.2.3 From e0469912686e967b27e14431c83dae3610ee19ab Mon Sep 17 00:00:00 2001 From: Tor Brede Vekterli Date: Wed, 16 Feb 2022 16:54:34 +0000 Subject: Use `lock_guard` instead of `unique_lock` --- vespalib/src/vespa/vespalib/util/count_down_latch.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vespalib/src/vespa/vespalib/util/count_down_latch.h b/vespalib/src/vespa/vespalib/util/count_down_latch.h index ee299ef2d21..613a60e90c5 100644 --- a/vespalib/src/vespa/vespalib/util/count_down_latch.h +++ b/vespalib/src/vespa/vespalib/util/count_down_latch.h @@ -83,7 +83,7 @@ public: * @return current count **/ [[nodiscard]] uint32_t getCount() const noexcept { - std::unique_lock guard(_lock); + std::lock_guard guard(_lock); return _count; } -- cgit v1.2.3