mirror of
https://github.com/oxen-io/lokinet.git
synced 2024-11-15 12:13:24 +00:00
273270916e
This commit reflects changes to clang-format rules. Unfortunately, these rule changes create a massive change to the codebase, which causes an apparent rewrite of git history. Git blame's --ignore-rev flag can be used to ignore this commit when attempting to `git blame` some code.
47 lines
1.0 KiB
C++
47 lines
1.0 KiB
C++
#pragma once
|
|
#include <mutex>
|
|
#include <condition_variable>
|
|
|
|
namespace llarp
|
|
{
|
|
namespace util
|
|
{
|
|
/// Barrier class that blocks all threads until the high water mark of
|
|
/// threads (set during construction) is reached, then releases them all.
|
|
class Barrier
|
|
{
|
|
std::mutex mutex;
|
|
std::condition_variable cv;
|
|
unsigned pending;
|
|
|
|
public:
|
|
Barrier(unsigned threads) : pending{threads}
|
|
{
|
|
}
|
|
|
|
/// Returns true if *this* Block call is the one that releases all of
|
|
/// them; returns false (i.e. after unblocking) if some other thread
|
|
/// triggered the released.
|
|
bool
|
|
Block()
|
|
{
|
|
std::unique_lock<std::mutex> lock{mutex};
|
|
if (pending == 1)
|
|
{
|
|
pending = 0;
|
|
lock.unlock();
|
|
cv.notify_all();
|
|
return true;
|
|
}
|
|
else if (pending > 1)
|
|
{
|
|
pending--;
|
|
}
|
|
cv.wait(lock, [this] { return !pending; });
|
|
return false;
|
|
}
|
|
};
|
|
|
|
} // namespace util
|
|
} // namespace llarp
|