diff options
| author | Grant Limberg <grant.limberg@zerotier.com> | 2018-11-13 16:00:17 -0800 |
|---|---|---|
| committer | Grant Limberg <grant.limberg@zerotier.com> | 2018-11-13 16:00:17 -0800 |
| commit | 01e6df4d46df280b3ad2ae158cb87b5ee98f10d7 (patch) | |
| tree | 231f4ef754a3871456f72895a9ae4899bb4588b8 /osdep/BlockingQueue.hpp | |
| parent | 882b03436d7b40a788f060e0d83a4027f86e73ef (diff) | |
| parent | 690bd933d52c7dbbcddde7c0aff30f7fee91a6d9 (diff) | |
| download | infinitytier-01e6df4d46df280b3ad2ae158cb87b5ee98f10d7.tar.gz infinitytier-01e6df4d46df280b3ad2ae158cb87b5ee98f10d7.zip | |
Merge branch 'dev' of http://git.int.zerotier.com/ZeroTier/ZeroTierOne into dev
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 |
