atomic_t<>: extend fetch_op to support cancellation

Use std::invoke inside atomic_op/fetch_op
Remove op_fetch because it's easily replaced
Add fetch_dec_sat algorithm (conditional decrement)
This commit is contained in:
Nekotekina 2018-09-05 22:28:37 +03:00
parent ed9fb8405b
commit fb5cdf9769
10 changed files with 80 additions and 60 deletions

View file

@ -26,7 +26,7 @@ void semaphore_base::imp_wait()
while (true)
{
// Try hard way
const s32 value = m_value.op_fetch([](s32& value)
const s32 value = m_value.atomic_op([](s32& value)
{
// Use sign bit to acknowledge waiter presence
if (value && value > INT32_MIN)
@ -44,6 +44,8 @@ void semaphore_base::imp_wait()
// Set sign bit
value = INT32_MIN;
}
return value;
});
if (value >= 0)
@ -69,20 +71,6 @@ void semaphore_base::imp_post(s32 _old)
#endif
}
bool semaphore_base::try_wait()
{
// Conditional decrement
const s32 value = m_value.fetch_op([](s32& value)
{
if (value > 0)
{
value -= 1;
}
});
return value > 0;
}
bool semaphore_base::try_post(s32 _max)
{
// Conditional increment