job/semaphore: add multi-acquire
This commit is contained in:
parent
804b2263b8
commit
848b9951e8
@ -32,6 +32,7 @@ namespace util::job {
|
||||
semaphore& operator= (semaphore&&) = delete;
|
||||
|
||||
int acquire (void);
|
||||
int acquire (int count);
|
||||
int release (void);
|
||||
|
||||
auto lock (void) { return acquire (); }
|
||||
|
@ -50,16 +50,18 @@ semaphore::semaphore (int initial):
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
int
|
||||
semaphore::acquire (void)
|
||||
semaphore::acquire (int count)
|
||||
{
|
||||
CHECK_GE (count, 0);
|
||||
|
||||
do {
|
||||
int now = m_value;
|
||||
|
||||
// if our value is positive then attempt to decrement it and return,
|
||||
// else retry because someone interfered with us.
|
||||
if (now > 0) {
|
||||
if (m_value.compare_exchange_weak (now, now - 1))
|
||||
return now - 1;
|
||||
if (now - count >= 0) {
|
||||
if (m_value.compare_exchange_weak (now, now - count))
|
||||
return now - count;
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -77,6 +79,14 @@ semaphore::acquire (void)
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
int
|
||||
semaphore::acquire (void)
|
||||
{
|
||||
return acquire (1);
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
int
|
||||
semaphore::release (void)
|
||||
|
Loading…
Reference in New Issue
Block a user