2020-03-04 14:08:40 +00:00
|
|
|
|
#include "cond.h"
|
2017-01-24 20:19:52 +00:00
|
|
|
|
#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
|
2020-10-26 20:32:40 +00:00
|
|
|
|
m_value.wait(_old, c_signal_mask, 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-20 12:40:50 +00:00
|
|
|
|
// Remove waiter (c_waiter_mask)
|
|
|
|
|
value -= 1;
|
2019-09-09 08:09:30 +00:00
|
|
|
|
|
|
|
|
|
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
|
2020-03-04 14:08:40 +00:00
|
|
|
|
value += c_signal_mask & (0 - c_signal_mask);
|
2019-09-09 08:09:30 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2020-03-04 14:08:40 +00:00
|
|
|
|
if (_count > 1 || ((_old + (c_signal_mask & (0 - 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
|
|
|
|
}
|