BlockingQueue.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2019 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #ifndef ZT_BLOCKINGQUEUE_HPP
  27. #define ZT_BLOCKINGQUEUE_HPP
  28. #include <queue>
  29. #include <mutex>
  30. #include <condition_variable>
  31. #include <chrono>
  32. #include "Thread.hpp"
  33. namespace ZeroTier {
  34. /**
  35. * Simple C++11 thread-safe queue
  36. *
  37. * Do not use in node/ since we have not gone C++11 there yet.
  38. */
  39. template <class T>
  40. class BlockingQueue
  41. {
  42. public:
  43. BlockingQueue(void) : r(true) {}
  44. inline void post(T t)
  45. {
  46. std::lock_guard<std::mutex> lock(m);
  47. q.push(t);
  48. c.notify_one();
  49. }
  50. inline void postLimit(T t,const unsigned long limit)
  51. {
  52. std::unique_lock<std::mutex> lock(m);
  53. for(;;) {
  54. if (q.size() < limit) {
  55. q.push(t);
  56. c.notify_one();
  57. break;
  58. }
  59. if (!r)
  60. break;
  61. gc.wait(lock);
  62. }
  63. }
  64. inline void stop(void)
  65. {
  66. std::lock_guard<std::mutex> lock(m);
  67. r = false;
  68. c.notify_all();
  69. gc.notify_all();
  70. }
  71. inline bool get(T &value)
  72. {
  73. std::unique_lock<std::mutex> lock(m);
  74. if (!r) return false;
  75. while (q.empty()) {
  76. c.wait(lock);
  77. if (!r) {
  78. gc.notify_all();
  79. return false;
  80. }
  81. }
  82. value = q.front();
  83. q.pop();
  84. gc.notify_all();
  85. return true;
  86. }
  87. enum TimedWaitResult
  88. {
  89. OK,
  90. TIMED_OUT,
  91. STOP
  92. };
  93. inline TimedWaitResult get(T &value,const unsigned long ms)
  94. {
  95. const std::chrono::milliseconds ms2{ms};
  96. std::unique_lock<std::mutex> lock(m);
  97. if (!r) return STOP;
  98. while (q.empty()) {
  99. if (c.wait_for(lock,ms2) == std::cv_status::timeout)
  100. return ((r) ? TIMED_OUT : STOP);
  101. else if (!r)
  102. return STOP;
  103. }
  104. value = q.front();
  105. q.pop();
  106. return OK;
  107. }
  108. private:
  109. volatile bool r;
  110. std::queue<T> q;
  111. mutable std::mutex m;
  112. mutable std::condition_variable c,gc;
  113. };
  114. } // namespace ZeroTier
  115. #endif