Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions libgo/coroutine.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#define __const__
#include <atomic>
#include "common/config.h"
#include "common/pp.h"
#include "common/syntax_helper.h"
Expand Down Expand Up @@ -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;

Expand All @@ -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<co_mutex> lck(mu);
mFlyingCount += i;
}

void Done() {
std::unique_lock<co_mutex> lck(mu);
if (--mFlyingCount == 0) {
cv.notify_all();
}
}
void Wait() {
std::unique_lock<co_mutex> 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;
};