lockfree_queue.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * Tencent is pleased to support the open source community by making wwsearch
  3. * available.
  4. *
  5. * Copyright (C) 2018-present Tencent. All Rights Reserved.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  8. * use this file except in compliance with the License. You may obtain a copy of
  9. * the License at
  10. *
  11. * https://opensource.org/licenses/Apache-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OF ANY KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations under the License.
  17. */
  18. #include <atomic>
  19. #include <cassert>
  20. #include <cstdlib>
  21. #include <limits>
  22. #include <memory>
  23. #include <stdexcept>
  24. #include <string>
  25. #include <type_traits>
  26. #include <utility>
  27. namespace wwsearch {
  28. template <typename T>
  29. class LockFreeQueue {
  30. private:
  31. template <typename D>
  32. struct node {
  33. D data;
  34. node* next;
  35. explicit node(D&& d) : data(std::forward<D>(d)), next(nullptr) {}
  36. };
  37. std::atomic<node<T>*> head_;
  38. std::atomic<uint64_t> count_;
  39. std::atomic<uint64_t> times_;
  40. public:
  41. // Constructor
  42. LockFreeQueue() : head_(nullptr), count_(0), times_(0) {}
  43. ~LockFreeQueue() {
  44. T data;
  45. while (Pop(&data) != false)
  46. ;
  47. }
  48. // Push one item without lock
  49. void Push(T&& data) {
  50. node<T>* new_node = new node<T>(std::forward<T>(data));
  51. // put the current value of head into new_node->next
  52. new_node->next = head_.load(std::memory_order_acquire);
  53. // now make new_node the new head, but if the head
  54. // is no longer what's stored in new_node->next
  55. // (some other thread must have inserted a node just now)
  56. // then put that new head into new_node->next and try again
  57. while (!head_.compare_exchange_weak(new_node->next, new_node,
  58. std::memory_order_release,
  59. std::memory_order_relaxed)) {
  60. new_node->next = head_.load(std::memory_order_acquire);
  61. }
  62. count_.fetch_add(1);
  63. }
  64. // Pop one item without lock
  65. bool Pop(T* data) {
  66. assert(data != nullptr);
  67. while (1) {
  68. auto result = head_.load(std::memory_order_acquire);
  69. if (result == nullptr) {
  70. return false;
  71. }
  72. if (head_.compare_exchange_weak(result, result->next,
  73. std::memory_order_release,
  74. std::memory_order_relaxed)) {
  75. *data = std::move(result->data);
  76. count_.fetch_sub(1);
  77. return true;
  78. }
  79. }
  80. }
  81. // Check is empty ?
  82. bool Empty() {
  83. auto result = head_.load(std::memory_order_acquire);
  84. if (result == nullptr) {
  85. return true;
  86. } else {
  87. return false;
  88. }
  89. }
  90. // Get queue's szie
  91. uint64_t Count() { return count_.load(); }
  92. };
  93. // Fix size queue.
  94. template <typename T>
  95. class FixLenLockFreeQueue {
  96. private:
  97. // inner api
  98. constexpr size_t idx(size_t i) const noexcept { return i % capacity_; }
  99. constexpr size_t turn(size_t i) const noexcept { return i / capacity_; }
  100. static constexpr size_t kCacheLineSize = 128;
  101. struct Slot {
  102. ~Slot() noexcept {
  103. if (occupy & 1) {
  104. Destroy();
  105. }
  106. }
  107. template <typename... Args>
  108. void Construct(Args&&... args) noexcept {
  109. new (&storage) T(std::forward<Args>(args)...);
  110. }
  111. void Destroy() noexcept { reinterpret_cast<T*>(&storage)->~T(); }
  112. T&& Move() noexcept { return reinterpret_cast<T&&>(storage); }
  113. // Align to avoid false sharing between adjacent slots
  114. alignas(kCacheLineSize) std::atomic<size_t> occupy{0};
  115. typename std::aligned_storage<sizeof(T), alignof(T)>::type storage;
  116. };
  117. inline void* align(std::size_t alignment, std::size_t size, void*& ptr,
  118. std::size_t& space) {
  119. std::uintptr_t pn = reinterpret_cast<std::uintptr_t>(ptr);
  120. std::uintptr_t aligned = (pn + alignment - 1) & -alignment;
  121. std::size_t padding = aligned - pn;
  122. if (space < size + padding) return nullptr;
  123. space -= padding;
  124. return ptr = reinterpret_cast<void*>(aligned);
  125. }
  126. private:
  127. alignas(kCacheLineSize) std::atomic<size_t> head_;
  128. alignas(kCacheLineSize) std::atomic<size_t> tail_;
  129. std::atomic<uint64_t> count_;
  130. std::atomic<uint64_t> capacity_;
  131. void* buf_;
  132. Slot* slots_;
  133. public:
  134. FixLenLockFreeQueue(const size_t capacity)
  135. : head_(0), tail_(0), count_(0), capacity_(capacity) {
  136. size_t space = capacity * sizeof(Slot) + kCacheLineSize - 1;
  137. buf_ = std::malloc(space);
  138. void* buf = buf_;
  139. slots_ = reinterpret_cast<Slot*>(
  140. align(kCacheLineSize, capacity * sizeof(Slot), buf, space));
  141. for (size_t i = 0; i < capacity_; ++i) {
  142. new (&slots_[i]) Slot();
  143. }
  144. }
  145. ~FixLenLockFreeQueue() {
  146. for (size_t i = 0; i < capacity_; ++i) {
  147. slots_[i].~Slot();
  148. }
  149. free(buf_);
  150. }
  151. // emplace try
  152. template <typename... Args>
  153. bool TryEmplace(Args&&... args) noexcept {
  154. auto head = head_.load(std::memory_order_acquire);
  155. for (;;) {
  156. auto& slot = slots_[idx(head)];
  157. if (turn(head) * 2 == slot.occupy.load(std::memory_order_acquire)) {
  158. if (head_.compare_exchange_strong(head, head + 1)) {
  159. slot.Construct(std::forward<Args>(args)...);
  160. slot.occupy.store(turn(head) * 2 + 1, std::memory_order_release);
  161. return true;
  162. }
  163. } else {
  164. auto const prev_head = head;
  165. head = head_.load(std::memory_order_acquire);
  166. if (head == prev_head) {
  167. return false;
  168. }
  169. }
  170. };
  171. }
  172. // Push one item
  173. void Push(T&& data) {
  174. while (false == TryEmplace(std::forward<T>(data)))
  175. ;
  176. }
  177. // Pop one item
  178. bool Pop(T* data) {
  179. assert(data != nullptr);
  180. auto tail = tail_.load(std::memory_order_acquire);
  181. for (;;) {
  182. auto& slot = slots_[idx(tail)];
  183. if (turn(tail) * 2 + 1 == slot.occupy.load(std::memory_order_acquire)) {
  184. if (tail_.compare_exchange_strong(tail, tail + 1)) {
  185. *data = slot.Move();
  186. slot.Destroy();
  187. slot.occupy.store(turn(tail) * 2 + 2, std::memory_order_release);
  188. return true;
  189. }
  190. } else {
  191. auto const prev_tail = tail;
  192. tail = tail_.load(std::memory_order_acquire);
  193. if (tail == prev_tail) {
  194. return false;
  195. }
  196. }
  197. };
  198. }
  199. bool Empty() { return count_.load() == 0; }
  200. uint64_t Count() { return count_.load(); }
  201. };
  202. } // namespace wwsearch