0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-18 07:50:57 +01:00
construct/include/ircd/ctx/latch.h

91 lines
1.4 KiB
C
Raw Normal View History

2018-08-19 04:28:39 +02:00
// Matrix Construct
//
// Copyright (C) Matrix Construct Developers, Authors & Contributors
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice is present in all copies. The
// full license for this software is available in the LICENSE file.
#pragma once
#define HAVE_IRCD_CTX_LATCH_H
namespace ircd::ctx
{
class latch;
}
class ircd::ctx::latch
{
mutable dock d;
2018-08-19 04:28:39 +02:00
size_t count {0};
public:
bool is_ready() const noexcept;
void count_down(const size_t &n = 1);
void count_down_and_wait();
void wait() const;
latch(const size_t &count);
latch() = default;
latch(latch &&);
2018-08-19 04:28:39 +02:00
latch(const latch &) = delete;
~latch() noexcept;
};
inline
ircd::ctx::latch::latch(const size_t &count)
:count{count}
{}
inline
ircd::ctx::latch::latch(latch &&o)
:d{std::move(o.d)}
,count{std::move(o.count)}
{
o.count = 0;
}
2018-08-19 04:28:39 +02:00
inline
ircd::ctx::latch::~latch()
noexcept
{
2018-12-05 00:19:56 +01:00
assert(d.empty());
2018-08-19 04:28:39 +02:00
}
inline void
ircd::ctx::latch::wait()
const
{
d.wait([this]
2018-08-19 04:28:39 +02:00
{
return is_ready();
});
}
inline void
ircd::ctx::latch::count_down_and_wait()
{
if(--count == 0)
d.notify_all();
2018-08-19 04:28:39 +02:00
else
wait();
}
inline void
ircd::ctx::latch::count_down(const size_t &n)
{
count -= n;
if(is_ready())
d.notify_all();
2018-08-19 04:28:39 +02:00
}
inline bool
ircd::ctx::latch::is_ready()
const noexcept
{
return count == 0;
}