diff options
Diffstat (limited to 'osdep/BlockingQueue.hpp')
| -rw-r--r-- | osdep/BlockingQueue.hpp | 28 |
1 files changed, 25 insertions, 3 deletions
diff --git a/osdep/BlockingQueue.hpp b/osdep/BlockingQueue.hpp index 351a095a..9e2f73cb 100644 --- a/osdep/BlockingQueue.hpp +++ b/osdep/BlockingQueue.hpp @@ -32,6 +32,8 @@ #include <condition_variable> #include <chrono> +#include "Thread.hpp" + namespace ZeroTier { /** @@ -52,11 +54,27 @@ public: c.notify_one(); } + inline void postLimit(T t,const unsigned long limit) + { + std::unique_lock<std::mutex> lock(m); + for(;;) { + if (q.size() < limit) { + q.push(t); + c.notify_one(); + break; + } + if (!r) + break; + gc.wait(lock); + } + } + inline void stop(void) { std::lock_guard<std::mutex> lock(m); r = false; c.notify_all(); + gc.notify_all(); } inline bool get(T &value) @@ -65,10 +83,14 @@ public: if (!r) return false; while (q.empty()) { c.wait(lock); - if (!r) return false; + if (!r) { + gc.notify_all(); + return false; + } } value = q.front(); q.pop(); + gc.notify_all(); return true; } @@ -98,8 +120,8 @@ public: private: volatile bool r; std::queue<T> q; - std::mutex m; - std::condition_variable c; + mutable std::mutex m; + mutable std::condition_variable c,gc; }; } // namespace ZeroTier |
