rpcs3/Utilities/cond.cpp
Nekotekina 0a96497e13 Remove shared_cond and simplify reservation waiting
Use atomic wait for reservations
Cleanup some obsolete code
2019-09-10 19:25:39 +03:00

59 lines
1.1 KiB
C++

#include "cond.h"
#include "sync.h"
#include "lockless.h"
#include <climits>
// use constants, increase signal space
void cond_variable::imp_wait(u32 _old, u64 _timeout) noexcept
{
// Not supposed to fail
verify(HERE), _old;
// Wait with timeout
m_value.wait(_old, atomic_wait_timeout{_timeout > max_timeout ? UINT64_MAX : _timeout * 1000});
// Cleanup
m_value.atomic_op([](u32& value)
{
value -= c_waiter_mask & -c_waiter_mask;
if ((value & c_waiter_mask) == 0)
{
// Last waiter removed, clean signals
value = 0;
}
});
}
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)
{
return false;
}
// Add signal
value += c_signal_mask & -c_signal_mask;
return true;
});
if (!ok || !_count)
{
return;
}
if (_count > 1 || ((_old + (c_signal_mask & -c_signal_mask)) & c_signal_mask) == c_signal_mask)
{
// Resort to notify_all if signal count reached max
m_value.notify_all();
}
else
{
m_value.notify_one();
}
}