Skip to content
Snippets Groups Projects
Commit 67a3299d authored by Tomasz Grabiec's avatar Tomasz Grabiec Committed by Avi Kivity
Browse files

core: add 'latch' synchronizer


The synchronizer allows any thread to block on it until it is
unlocked. It is unlocked once count_down() has been called given
number of times.

Signed-off-by: default avatarTomasz Grabiec <tgrabiec@cloudius-systems.com>
Signed-off-by: default avatarAvi Kivity <avi@cloudius-systems.com>
parent bc8b01b6
No related branches found
No related tags found
No related merge requests found
#ifndef _OSV_LATCH_HH
#define _OSV_LATCH_HH
#include <condition_variable>
#include <mutex>
class latch
{
private:
int _count;
std::mutex _mutex;
std::condition_variable _condvar;
public:
latch(int count)
: _count(count)
{
}
void count_down()
{
std::lock_guard<std::mutex> l(_mutex);
if (--_count == 0) {
_condvar.notify_all();
}
}
void await()
{
std::unique_lock<std::mutex> l(_mutex);
while (_count > 0) {
_condvar.wait(l);
}
}
};
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment