job/semaphore: add multi-acquire

This commit is contained in:
Danny Robson 2018-03-22 13:37:28 +11:00
parent 804b2263b8
commit 848b9951e8
2 changed files with 19 additions and 8 deletions

View File

@ -32,6 +32,7 @@ namespace util::job {
semaphore& operator= (semaphore&&) = delete; semaphore& operator= (semaphore&&) = delete;
int acquire (void); int acquire (void);
int acquire (int count);
int release (void); int release (void);
auto lock (void) { return acquire (); } auto lock (void) { return acquire (); }

View File

@ -50,16 +50,18 @@ semaphore::semaphore (int initial):
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
int int
semaphore::acquire (void) semaphore::acquire (int count)
{ {
CHECK_GE (count, 0);
do { do {
int now = m_value; int now = m_value;
// if our value is positive then attempt to decrement it and return, // if our value is positive then attempt to decrement it and return,
// else retry because someone interfered with us. // else retry because someone interfered with us.
if (now > 0) { if (now - count >= 0) {
if (m_value.compare_exchange_weak (now, now - 1)) if (m_value.compare_exchange_weak (now, now - count))
return now - 1; return now - count;
continue; continue;
} }
@ -77,6 +79,14 @@ semaphore::acquire (void)
} }
//-----------------------------------------------------------------------------
int
semaphore::acquire (void)
{
return acquire (1);
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
int int
semaphore::release (void) semaphore::release (void)