Digitalisierte Elektroverteilung zur permanenten Verbraucherüberwachung
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

net_dequeue_ts.h 3.9KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. MMO Client/Server Framework using ASIO
  3. "Happy Birthday Mrs Javidx9!" - javidx9
  4. Videos:
  5. Part #1: https://youtu.be/2hNdkYInj4g
  6. Part #2: https://youtu.be/UbjxGvrDrbw
  7. License (OLC-3)
  8. ~~~~~~~~~~~~~~~
  9. Copyright 2018 - 2020 OneLoneCoder.com
  10. Redistribution and use in source and binary forms, with or without
  11. modification, are permitted provided that the following conditions
  12. are met:
  13. 1. Redistributions or derivations of source code must retain the above
  14. copyright notice, this list of conditions and the following disclaimer.
  15. 2. Redistributions or derivative works in binary form must reproduce
  16. the above copyright notice. This list of conditions and the following
  17. disclaimer must be reproduced in the documentation and/or other
  18. materials provided with the distribution.
  19. 3. Neither the name of the copyright holder nor the names of its
  20. contributors may be used to endorse or promote products derived
  21. from this software without specific prior written permission.
  22. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  25. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  26. HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  27. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  28. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  30. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. Links
  34. ~~~~~
  35. YouTube: https://www.youtube.com/javidx9
  36. Discord: https://discord.gg/WhwHUMV
  37. Twitter: https://www.twitter.com/javidx9
  38. Twitch: https://www.twitch.tv/javidx9
  39. GitHub: https://www.github.com/onelonecoder
  40. Homepage: https://www.onelonecoder.com
  41. Author
  42. ~~~~~~
  43. David Barr, aka javidx9, ©OneLoneCoder 2019, 2020
  44. */
  45. #pragma once
  46. #include "net_common.h"
  47. namespace net
  48. {
  49. template<typename T>
  50. class ts_dequeue
  51. {
  52. public:
  53. ts_dequeue() = default;
  54. ts_dequeue(const ts_dequeue<T>&) = delete;
  55. virtual ~ts_dequeue() { clear(); }
  56. public:
  57. // Returns and maintains item at front of Queue
  58. const T& front()
  59. {
  60. std::scoped_lock lock(muxQueue);
  61. return deqQueue.front();
  62. }
  63. // Returns and maintains item at back of Queue
  64. const T& back()
  65. {
  66. std::scoped_lock lock(muxQueue);
  67. return deqQueue.back();
  68. }
  69. // Removes and returns item from front of Queue
  70. T pop_front()
  71. {
  72. std::scoped_lock lock(muxQueue);
  73. auto t = std::move(deqQueue.front());
  74. deqQueue.pop_front();
  75. return t;
  76. }
  77. // Removes and returns item from back of Queue
  78. T pop_back()
  79. {
  80. std::scoped_lock lock(muxQueue);
  81. auto t = std::move(deqQueue.back());
  82. deqQueue.pop_back();
  83. return t;
  84. }
  85. // Adds an item to back of Queue
  86. void push_back(const T& item)
  87. {
  88. std::scoped_lock lock(muxQueue);
  89. deqQueue.emplace_back(std::move(item));
  90. std::unique_lock<std::mutex> ul(muxBlocking);
  91. cvBlocking.notify_one();
  92. }
  93. // Adds an item to front of Queue
  94. void push_front(const T& item)
  95. {
  96. std::scoped_lock lock(muxQueue);
  97. deqQueue.emplace_front(std::move(item));
  98. std::unique_lock<std::mutex> ul(muxBlocking);
  99. cvBlocking.notify_one();
  100. }
  101. // Returns true if Queue has no items
  102. bool empty()
  103. {
  104. std::scoped_lock lock(muxQueue);
  105. return deqQueue.empty();
  106. }
  107. // Returns number of items in Queue
  108. size_t count()
  109. {
  110. std::scoped_lock lock(muxQueue);
  111. return deqQueue.size();
  112. }
  113. // Clears Queue
  114. void clear()
  115. {
  116. std::scoped_lock lock(muxQueue);
  117. deqQueue.clear();
  118. }
  119. void wait()
  120. {
  121. while (empty())
  122. {
  123. std::unique_lock<std::mutex> ul(muxBlocking);
  124. cvBlocking.wait(ul);
  125. }
  126. }
  127. protected:
  128. std::mutex muxQueue;
  129. std::deque<T> deqQueue;
  130. std::condition_variable cvBlocking;
  131. std::mutex muxBlocking;
  132. };
  133. }