diff Core/MultiThreading/Semaphore.cpp @ 4064:7176ebf08765

Semaphore: allow acquiring/releasing multiple resources
author Alain Mazy
date Thu, 11 Jun 2020 16:42:27 +0200
parents 94f4a18a79cc
children
line wrap: on
line diff
--- a/Core/MultiThreading/Semaphore.cpp	Wed Jun 10 18:49:21 2020 +0200
+++ b/Core/MultiThreading/Semaphore.cpp	Thu Jun 11 16:42:27 2020 +0200
@@ -20,7 +20,7 @@
  * you do not wish to do so, delete this exception statement from your
  * version. If you delete this exception statement from all source files
  * in the program, then also delete it here.
- * 
+ *
  * This program is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
@@ -48,36 +48,36 @@
     }
   }
 
-  void Semaphore::Release()
+  void Semaphore::Release(unsigned int resourceCount)
   {
     boost::mutex::scoped_lock lock(mutex_);
 
-    availableResources_++;
-    condition_.notify_one(); 
+    availableResources_ += resourceCount;
+    condition_.notify_one();
   }
 
-  void Semaphore::Acquire()
+  void Semaphore::Acquire(unsigned int resourceCount)
   {
     boost::mutex::scoped_lock lock(mutex_);
 
-    while (availableResources_ == 0)
+    while (availableResources_ < resourceCount)
     {
       condition_.wait(lock);
     }
 
-    availableResources_--;
+    availableResources_ -= resourceCount;
   }
 
-  bool Semaphore::TryAcquire()
+  bool Semaphore::TryAcquire(unsigned int resourceCount)
   {
     boost::mutex::scoped_lock lock(mutex_);
 
-    if (availableResources_ == 0)
+    if (availableResources_ < resourceCount)
     {
       return false;
     }
 
-    availableResources_--;
+    availableResources_ -= resourceCount;
     return true;
   }
 }