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.

mpmc_bounded_q.h 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. A modified version of Bounded MPMC queue by Dmitry Vyukov.
  3. Original code from:
  4. http://www.1024cores.net/home/lock-free-algorithms/queues/bounded-mpmc-queue
  5. licensed by Dmitry Vyukov under the terms below:
  6. Simplified BSD license
  7. Copyright (c) 2010-2011 Dmitry Vyukov. All rights reserved.
  8. Redistribution and use in source and binary forms, with or without modification,
  9. are permitted provided that the following conditions are met:
  10. 1. Redistributions of source code must retain the above copyright notice, this list of
  11. conditions and the following disclaimer.
  12. 2. Redistributions in binary form must reproduce the above copyright notice, this list
  13. of conditions and the following disclaimer in the documentation and/or other materials
  14. provided with the distribution.
  15. THIS SOFTWARE IS PROVIDED BY DMITRY VYUKOV "AS IS" AND ANY EXPRESS OR IMPLIED
  16. WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  17. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  18. SHALL DMITRY VYUKOV OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  20. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  21. OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  22. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  23. OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  24. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. The views and conclusions contained in the software and documentation are those of the authors and
  26. should not be interpreted as representing official policies, either expressed or implied, of Dmitry Vyukov.
  27. */
  28. /*
  29. The code in its current form adds the license below:
  30. Copyright(c) 2015 Gabi Melman.
  31. Distributed under the MIT License (http://opensource.org/licenses/MIT)
  32. */
  33. #pragma once
  34. #include <spdlog/common.h>
  35. #include <atomic>
  36. #include <utility>
  37. namespace spdlog
  38. {
  39. namespace details
  40. {
  41. template<typename T>
  42. class mpmc_bounded_queue
  43. {
  44. public:
  45. using item_type = T;
  46. mpmc_bounded_queue(size_t buffer_size)
  47. :max_size_(buffer_size),
  48. buffer_(new cell_t [buffer_size]),
  49. buffer_mask_(buffer_size - 1)
  50. {
  51. //queue size must be power of two
  52. if(!((buffer_size >= 2) && ((buffer_size & (buffer_size - 1)) == 0)))
  53. throw spdlog_ex("async logger queue size must be power of two");
  54. for (size_t i = 0; i != buffer_size; i += 1)
  55. buffer_[i].sequence_.store(i, std::memory_order_relaxed);
  56. enqueue_pos_.store(0, std::memory_order_relaxed);
  57. dequeue_pos_.store(0, std::memory_order_relaxed);
  58. }
  59. ~mpmc_bounded_queue()
  60. {
  61. delete [] buffer_;
  62. }
  63. bool enqueue(T&& data)
  64. {
  65. cell_t* cell;
  66. size_t pos = enqueue_pos_.load(std::memory_order_relaxed);
  67. for (;;)
  68. {
  69. cell = &buffer_[pos & buffer_mask_];
  70. size_t seq = cell->sequence_.load(std::memory_order_acquire);
  71. intptr_t dif = (intptr_t)seq - (intptr_t)pos;
  72. if (dif == 0)
  73. {
  74. if (enqueue_pos_.compare_exchange_weak(pos, pos + 1, std::memory_order_relaxed))
  75. break;
  76. }
  77. else if (dif < 0)
  78. {
  79. return false;
  80. }
  81. else
  82. {
  83. pos = enqueue_pos_.load(std::memory_order_relaxed);
  84. }
  85. }
  86. cell->data_ = std::move(data);
  87. cell->sequence_.store(pos + 1, std::memory_order_release);
  88. return true;
  89. }
  90. bool dequeue(T& data)
  91. {
  92. cell_t* cell;
  93. size_t pos = dequeue_pos_.load(std::memory_order_relaxed);
  94. for (;;)
  95. {
  96. cell = &buffer_[pos & buffer_mask_];
  97. size_t seq =
  98. cell->sequence_.load(std::memory_order_acquire);
  99. intptr_t dif = (intptr_t)seq - (intptr_t)(pos + 1);
  100. if (dif == 0)
  101. {
  102. if (dequeue_pos_.compare_exchange_weak(pos, pos + 1, std::memory_order_relaxed))
  103. break;
  104. }
  105. else if (dif < 0)
  106. return false;
  107. else
  108. pos = dequeue_pos_.load(std::memory_order_relaxed);
  109. }
  110. data = std::move(cell->data_);
  111. cell->sequence_.store(pos + buffer_mask_ + 1, std::memory_order_release);
  112. return true;
  113. }
  114. size_t approx_size()
  115. {
  116. size_t first_pos = dequeue_pos_.load(std::memory_order_relaxed);
  117. size_t last_pos = enqueue_pos_.load(std::memory_order_relaxed);
  118. if (last_pos <= first_pos)
  119. return 0;
  120. auto size = last_pos - first_pos;
  121. return size < max_size_ ? size : max_size_;
  122. }
  123. private:
  124. struct cell_t
  125. {
  126. std::atomic<size_t> sequence_;
  127. T data_;
  128. };
  129. size_t const max_size_;
  130. static size_t const cacheline_size = 64;
  131. typedef char cacheline_pad_t [cacheline_size];
  132. cacheline_pad_t pad0_;
  133. cell_t* const buffer_;
  134. size_t const buffer_mask_;
  135. cacheline_pad_t pad1_;
  136. std::atomic<size_t> enqueue_pos_;
  137. cacheline_pad_t pad2_;
  138. std::atomic<size_t> dequeue_pos_;
  139. cacheline_pad_t pad3_;
  140. mpmc_bounded_queue(mpmc_bounded_queue const&) = delete;
  141. void operator= (mpmc_bounded_queue const&) = delete;
  142. };
  143. } // ns details
  144. } // ns spdlog