2020-12-09 08:47:45 +01:00
|
|
|
#include "cond.h"
|
2017-01-24 21:19:52 +01:00
|
|
|
#include "sync.h"
|
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
|
2020-12-09 08:47:45 +01:00
|
|
|
ensure(_old);
|
2019-09-09 10:09:30 +02:00
|
|
|
|
|
|
|
// Wait with timeout
|
2020-10-26 21:32:40 +01:00
|
|
|
m_value.wait(_old, c_signal_mask, 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
|
2020-03-04 15:08:40 +01:00
|
|
|
value += c_signal_mask & (0 - c_signal_mask);
|
2019-09-09 10:09:30 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-03-04 15:08:40 +01:00
|
|
|
if (_count > 1 || ((_old + (c_signal_mask & (0 - 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
|
2020-11-05 16:59:01 +01:00
|
|
|
m_value.notify_all(c_signal_mask);
|
2019-09-09 10:09:30 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-05 16:59:01 +01:00
|
|
|
m_value.notify_one(c_signal_mask);
|
2019-09-09 10:09:30 +02:00
|
|
|
}
|
2017-01-24 21:19:52 +01:00
|
|
|
}
|