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