diff --git a/libgo/coroutine.h b/libgo/coroutine.h index 946f43e0..d2751c7c 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" @@ -71,3 +72,29 @@ 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) { + mFlyingCount += i; + } + + void Done() { + if (--mFlyingCount == 0) { + ch_0.Close(); + } + } + void Wait() { + int done; + ch_0 >> done; + } +private: + std::atomic mFlyingCount; + co_chan ch_0; + + CountDownLatch(CountDownLatch const &) = delete; + CountDownLatch(CountDownLatch &&) = delete; + CountDownLatch& operator=(CountDownLatch const &) = delete; + CountDownLatch& operator=(CountDownLatch &&) = delete; +};