diff --git a/libgo/coroutine.h b/libgo/coroutine.h index 946f43e0..c67d34d9 100644 --- a/libgo/coroutine.h +++ b/libgo/coroutine.h @@ -1,5 +1,6 @@ #pragma once #define __const__ +#include #include "common/config.h" #include "common/pp.h" #include "common/syntax_helper.h" @@ -49,6 +50,9 @@ using ::co::co_rwmutex; using ::co::co_rmutex; using ::co::co_wmutex; +// co_condition_variable +typedef ::co::ConditionVariableAny co_condition_variable; + // co_chan using ::co::co_chan; @@ -71,3 +75,34 @@ typedef ::co::CoTimer::TimerId co_timer_id; #define co_last_defer() ::co::GetLastDefer() #define co_defer_scope co_defer [&] +class CountDownLatch { +public: + explicit CountDownLatch(size_t n = 1) : mFlyingCount(n) {} + + void Add(size_t i) { + std::unique_lock lck(mu); + mFlyingCount += i; + } + + void Done() { + std::unique_lock lck(mu); + if (--mFlyingCount == 0) { + cv.notify_all(); + } + } + void Wait() { + std::unique_lock lck(mu); + while (mFlyingCount > 0) { + cv.wait(lck); + } + } +private: + size_t mFlyingCount; + co_mutex mu; + co_condition_variable cv; + + CountDownLatch(CountDownLatch const &) = delete; + CountDownLatch(CountDownLatch &&) = delete; + CountDownLatch& operator=(CountDownLatch const &) = delete; + CountDownLatch& operator=(CountDownLatch &&) = delete; +};