2017-01-24 20:19:52 +00:00
|
|
|
#include "cond.h"
|
|
|
|
#include "sync.h"
|
2019-01-11 17:44:10 +00:00
|
|
|
#include "lockless.h"
|
2017-01-24 20:19:52 +00:00
|
|
|
|
2019-09-09 08:29:14 +00:00
|
|
|
#include <climits>
|
2017-02-24 15:48:53 +00:00
|
|
|
|
2019-09-09 08:09:30 +00:00
|
|
|
// use constants, increase signal space
|
|
|
|
|
|
|
|
void cond_variable::imp_wait(u32 _old, u64 _timeout) noexcept
|
2017-01-24 20:19:52 +00:00
|
|
|
{
|
2019-09-09 08:09:30 +00:00
|
|
|
// Not supposed to fail
|
|
|
|
verify(HERE), _old;
|
|
|
|
|
|
|
|
// Wait with timeout
|
|
|
|
m_value.wait(_old, atomic_wait_timeout{_timeout > max_timeout ? UINT64_MAX : _timeout * 1000});
|
2017-01-24 20:19:52 +00:00
|
|
|
|
2019-09-09 08:09:30 +00:00
|
|
|
// Cleanup
|
|
|
|
m_value.atomic_op([](u32& value)
|
2017-01-24 20:19:52 +00:00
|
|
|
{
|
2019-09-09 08:09:30 +00:00
|
|
|
value -= c_waiter_mask & -c_waiter_mask;
|
|
|
|
|
|
|
|
if ((value & c_waiter_mask) == 0)
|
2017-01-24 20:19:52 +00:00
|
|
|
{
|
2019-09-09 08:09:30 +00:00
|
|
|
// Last waiter removed, clean signals
|
|
|
|
value = 0;
|
2017-01-24 20:19:52 +00:00
|
|
|
}
|
2019-09-09 08:09:30 +00:00
|
|
|
});
|
|
|
|
}
|
2017-01-24 20:19:52 +00:00
|
|
|
|
2019-09-09 08:09:30 +00:00
|
|
|
void cond_variable::imp_wake(u32 _count) noexcept
|
|
|
|
{
|
|
|
|
const auto [_old, ok] = m_value.fetch_op([](u32& value)
|
|
|
|
{
|
|
|
|
if (!value || (value & c_signal_mask) == c_signal_mask)
|
2017-01-24 20:19:52 +00:00
|
|
|
{
|
2019-09-09 08:09:30 +00:00
|
|
|
return false;
|
2017-01-24 20:19:52 +00:00
|
|
|
}
|
|
|
|
|
2019-09-09 08:09:30 +00:00
|
|
|
// Add signal
|
|
|
|
value += c_signal_mask & -c_signal_mask;
|
|
|
|
return true;
|
2018-11-26 15:55:22 +00:00
|
|
|
});
|
|
|
|
|
2019-09-09 08:09:30 +00:00
|
|
|
if (!ok || !_count)
|
2018-11-26 15:55:22 +00:00
|
|
|
{
|
2019-09-09 08:09:30 +00:00
|
|
|
return;
|
2017-01-24 20:19:52 +00:00
|
|
|
}
|
|
|
|
|
2019-09-09 08:09:30 +00:00
|
|
|
if (_count > 1 || ((_old + (c_signal_mask & -c_signal_mask)) & c_signal_mask) == c_signal_mask)
|
2017-01-24 20:19:52 +00:00
|
|
|
{
|
2019-09-09 08:09:30 +00:00
|
|
|
// Resort to notify_all if signal count reached max
|
|
|
|
m_value.notify_all();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_value.notify_one();
|
|
|
|
}
|
2017-01-24 20:19:52 +00:00
|
|
|
}
|