1
0
forked from Alepha/Alepha

Interlock for mailbox.

This commit is contained in:
2023-12-12 17:37:03 -05:00
parent bf207244b6
commit 94596d0d7d
3 changed files with 146 additions and 16 deletions

View File

@ -27,11 +27,56 @@ namespace Alepha::Hydrogen ::detail:: Thread_m
class ConditionVariable;
using condition_variable= ConditionVariable;
/*!
* An `Interlock` is kind of like a condition variable, for two cooperating threads.
*
* An `Interlock` object represents the state which permits two cooperating threads to
* perform a synchronization action. Each side enters `Interlock::interlock`, and
* when both of them have entered, the synchronization function will be executed.
*
* In some sense it is similar to a `std::barrier` with a capacity to be interrupted
* and accepts parameter to pass as the interlock-synchronization action.
*/
class Interlock;
using std::mutex;
using std::lock_guard;
using std::unique_lock;
}
class exports::Interlock
{
private:
struct Impl;
std::unique_ptr< Impl > pimpl;
auto &impl() { return *pimpl; }
const auto &impl() const { return *pimpl; }
bool waiter( unique_lock< mutex > & );
void interlock( unique_lock< mutex > &lock );
void noblock( unique_lock< mutex > &lock );
public:
~Interlock();
Interlock();
void
wait( unique_lock< mutex > &lock, auto &&synchronize )
{
if( waiter( lock ) ) synchronize();
interlock( lock );
}
void
checkpoint( unique_lock< mutex > &lock, auto &&synchronize )
{
if( waiter( lock ) ) synchronize();
noblock( lock );
}
};
class exports::ConditionVariable
{
private: