]> git.sesse.net Git - casparcg/blobdiff - common/semaphore.h
[executor] changed default to unbounded like in 2.0.7 and fixed a deadlock when capac...
[casparcg] / common / semaphore.h
index c50394f852e488feefb1c4629a918314218c988b..04e36426045c48c9603807a9c7f0a90d1806c9f8 100644 (file)
@@ -25,7 +25,7 @@
 
 #include <boost/noncopyable.hpp>
 #include <boost/thread/mutex.hpp>
-#include <boost/thread/condition.hpp>
+#include <boost/thread/condition_variable.hpp>
 
 namespace caspar {
 
@@ -45,14 +45,14 @@ class semaphore : boost::noncopyable
 {
        mutable boost::mutex mutex_;
        unsigned int permits_;
-       boost::condition_variable permits_available_;
+       boost::condition_variable_any permits_available_;
 public:
        /**
         * Constructor.
         *
         * @param permits The initial number of permits.
         */
-       semaphore(unsigned int permits)
+       explicit semaphore(unsigned int permits)
                : permits_(permits)
        {
        }
@@ -62,7 +62,7 @@ public:
         */
        void release()
        {
-               boost::mutex::scoped_lock lock(mutex_);
+               boost::unique_lock<boost::mutex> lock(mutex_);
 
                ++permits_;
 
@@ -76,7 +76,7 @@ public:
         */
        void release(unsigned int permits)
        {
-               boost::mutex::scoped_lock lock(mutex_);
+               boost::unique_lock<boost::mutex> lock(mutex_);
 
                permits_ += permits;
 
@@ -89,7 +89,7 @@ public:
         */
        void acquire()
        {
-               boost::mutex::scoped_lock lock(mutex_);
+               boost::unique_lock<boost::mutex> lock(mutex_);
 
                while (permits_ == 0u)
                {
@@ -107,7 +107,7 @@ public:
         */
        void acquire(unsigned int permits)
        {
-               boost::mutex::scoped_lock lock(mutex_);
+               boost::unique_lock<boost::mutex> lock(mutex_);
                auto num_acquired = 0u;
 
                while (true)
@@ -125,6 +125,44 @@ public:
                }
        }
 
+       /**
+        * Acquire a number of permits. Will block until the given number of
+        * permits has been acquired if not enough permits are currently available
+        * or the timeout has passed.
+        *
+        * @param permits The number of permits to acquire.
+        * @param timeout The timeout (will be used for each permit).
+        *
+        * @return whether successfully acquired within timeout or not.
+        */
+       template <typename Rep, typename Period>
+       bool try_acquire(unsigned int permits, const boost::chrono::duration<Rep, Period>& timeout)
+       {
+               boost::unique_lock<boost::mutex> lock(mutex_);
+               auto num_acquired = 0u;
+
+               while (true)
+               {
+                       auto num_wanted = permits - num_acquired;
+                       auto to_drain = std::min(num_wanted, permits_);
+
+                       permits_ -= to_drain;
+                       num_acquired += to_drain;
+
+                       if (num_acquired == permits)
+                               break;
+
+                       if (permits_available_.wait_for(lock, timeout) == boost::cv_status::timeout)
+                       {
+                               lock.unlock();
+                               release(num_acquired);
+                               return false;
+                       }
+               }
+
+               return true;
+       }
+
        /**
         * Acquire one permits if permits are currently available. Does not block
         * until one is available, but returns immediately if unavailable.
@@ -134,7 +172,7 @@ public:
         */
        bool try_acquire()
        {
-               boost::mutex::scoped_lock lock(mutex_);
+               boost::unique_lock<boost::mutex> lock(mutex_);
 
                if (permits_ == 0u)
                        return false;
@@ -152,7 +190,7 @@ public:
         */
        unsigned int permits() const
        {
-               boost::mutex::scoped_lock lock(mutex_);
+               boost::unique_lock<boost::mutex> lock(mutex_);
 
                return permits_;
        }