File tree Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Original file line number Diff line number Diff line change 1
1
#pragma once
2
2
#define __const__
3
+ #include < atomic>
3
4
#include " common/config.h"
4
5
#include " common/pp.h"
5
6
#include " common/syntax_helper.h"
@@ -71,3 +72,29 @@ typedef ::co::CoTimer::TimerId co_timer_id;
71
72
#define co_last_defer () ::co::GetLastDefer()
72
73
#define co_defer_scope co_defer [&]
73
74
75
+ class CountDownLatch {
76
+ public:
77
+ explicit CountDownLatch (size_t n = 1 ) : mFlyingCount(n) {}
78
+
79
+ void Done () {
80
+ --mFlyingCount ;
81
+ }
82
+ void Wait () {
83
+ uint64_t usec = 100 * 1000 ;
84
+ while (mFlyingCount ) {
85
+ if (co_sched.TaskCount () <= co_sched.ProcessCount ()) {
86
+ usleep (usec);
87
+ usec <<= 1 ;
88
+ } else {
89
+ co_yield ;
90
+ }
91
+ }
92
+ }
93
+ private:
94
+ std::atomic<size_t > mFlyingCount ;
95
+
96
+ CountDownLatch (CountDownLatch const &) = delete ;
97
+ CountDownLatch (CountDownLatch &&) = delete ;
98
+ CountDownLatch& operator =(CountDownLatch const &) = delete ;
99
+ CountDownLatch& operator =(CountDownLatch &&) = delete ;
100
+ };
Original file line number Diff line number Diff line change @@ -56,6 +56,9 @@ class Scheduler
56
56
// 使用独立的定时器线程
57
57
void UseAloneTimerThread ();
58
58
59
+ // 当前调度器中的线程数量
60
+ size_t ProcessCount () { return processers_.size (); };
61
+
59
62
// 当前调度器中的协程数量
60
63
uint32_t TaskCount ();
61
64
You can’t perform that action at this time.
0 commit comments