queue.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #define MPMC_CACHE_LINE_SIZE 64
  2. typedef std::atomic<i32> MPMCQueueAtomicIdx;
  3. // Multiple Producer Multiple Consumer Queue
  4. template <typename T>
  5. struct MPMCQueue {
  6. static size_t const PAD0_OFFSET = (sizeof(T *) + sizeof(MPMCQueueAtomicIdx *) + sizeof(gbAllocator) + sizeof(BlockingMutex) + sizeof(i32) + sizeof(i32));
  7. T * nodes;
  8. MPMCQueueAtomicIdx *indices;
  9. gbAllocator allocator;
  10. BlockingMutex mutex;
  11. MPMCQueueAtomicIdx count;
  12. i32 mask; // capacity-1, because capacity must be a power of 2
  13. char pad0[(MPMC_CACHE_LINE_SIZE*2 - PAD0_OFFSET) % MPMC_CACHE_LINE_SIZE];
  14. MPMCQueueAtomicIdx head_idx;
  15. char pad1[MPMC_CACHE_LINE_SIZE - sizeof(i32)];
  16. MPMCQueueAtomicIdx tail_idx;
  17. };
  18. void mpmc_internal_init_indices(MPMCQueueAtomicIdx *indices, i32 offset, i32 size) {
  19. GB_ASSERT(offset % 8 == 0);
  20. GB_ASSERT(size % 8 == 0);
  21. // NOTE(bill): pretend it's not atomic for performance
  22. auto *raw_data = cast(i32 *)indices;
  23. for (i32 i = offset; i < size; i += 8) {
  24. raw_data[i+0] = i+0;
  25. raw_data[i+1] = i+1;
  26. raw_data[i+2] = i+2;
  27. raw_data[i+3] = i+3;
  28. raw_data[i+4] = i+4;
  29. raw_data[i+5] = i+5;
  30. raw_data[i+6] = i+6;
  31. raw_data[i+7] = i+7;
  32. }
  33. }
  34. template <typename T>
  35. void mpmc_init(MPMCQueue<T> *q, gbAllocator a, isize size_i) {
  36. if (size_i < 8) {
  37. size_i = 8;
  38. }
  39. GB_ASSERT(size_i < I32_MAX);
  40. i32 size = cast(i32)size_i;
  41. size = next_pow2(size);
  42. GB_ASSERT(gb_is_power_of_two(size));
  43. mutex_init(&q->mutex);
  44. q->mask = size-1;
  45. q->allocator = a;
  46. q->nodes = gb_alloc_array(a, T, size);
  47. q->indices = gb_alloc_array(a, MPMCQueueAtomicIdx, size);
  48. mpmc_internal_init_indices(q->indices, 0, q->mask+1);
  49. }
  50. template <typename T>
  51. void mpmc_destroy(MPMCQueue<T> *q) {
  52. mutex_destroy(&q->mutex);
  53. gb_free(q->allocator, q->nodes);
  54. gb_free(q->allocator, q->indices);
  55. }
  56. template <typename T>
  57. i32 mpmc_enqueue(MPMCQueue<T> *q, T const &data) {
  58. GB_ASSERT(q->mask != 0);
  59. i32 head_idx = q->head_idx.load(std::memory_order_relaxed);
  60. for (;;) {
  61. auto node = &q->nodes[head_idx & q->mask];
  62. auto node_idx_ptr = &q->indices[head_idx & q->mask];
  63. i32 node_idx = node_idx_ptr->load(std::memory_order_acquire);
  64. i32 diff = node_idx - head_idx;
  65. if (diff == 0) {
  66. i32 next_head_idx = head_idx+1;
  67. if (q->head_idx.compare_exchange_weak(head_idx, next_head_idx)) {
  68. *node = data;
  69. node_idx_ptr->store(next_head_idx, std::memory_order_release);
  70. return q->count.fetch_add(1, std::memory_order_release);
  71. }
  72. } else if (diff < 0) {
  73. mutex_lock(&q->mutex);
  74. i32 old_size = q->mask+1;
  75. i32 new_size = old_size*2;
  76. resize_array_raw(&q->nodes, q->allocator, old_size, new_size);
  77. if (q->nodes == nullptr) {
  78. GB_PANIC("Unable to resize enqueue: %td -> %td", old_size, new_size);
  79. mutex_unlock(&q->mutex);
  80. return -1;
  81. }
  82. resize_array_raw(&q->indices, q->allocator, old_size, new_size);
  83. if (q->indices == nullptr) {
  84. GB_PANIC("Unable to resize enqueue: %td -> %td", old_size, new_size);
  85. mutex_unlock(&q->mutex);
  86. return -1;
  87. }
  88. mpmc_internal_init_indices(q->indices, old_size, new_size);
  89. q->mask = new_size-1;
  90. mutex_unlock(&q->mutex);
  91. } else {
  92. head_idx = q->head_idx.load(std::memory_order_relaxed);
  93. }
  94. }
  95. }
  96. template <typename T>
  97. bool mpmc_dequeue(MPMCQueue<T> *q, T *data_) {
  98. if (q->mask == 0) {
  99. return false;
  100. }
  101. i32 tail_idx = q->tail_idx.load(std::memory_order_relaxed);
  102. for (;;) {
  103. auto node_ptr = &q->nodes[tail_idx & q->mask];
  104. auto node_idx_ptr = &q->indices[tail_idx & q->mask];
  105. i32 node_idx = node_idx_ptr->load(std::memory_order_acquire);
  106. i32 diff = node_idx - (tail_idx+1);
  107. if (diff == 0) {
  108. i32 next_tail_idx = tail_idx+1;
  109. if (q->tail_idx.compare_exchange_weak(tail_idx, next_tail_idx)) {
  110. if (data_) *data_ = *node_ptr;
  111. node_idx_ptr->store(tail_idx + q->mask + 1, std::memory_order_release);
  112. q->count.fetch_sub(1, std::memory_order_release);
  113. return true;
  114. }
  115. } else if (diff < 0) {
  116. return false;
  117. } else {
  118. tail_idx = q->tail_idx.load(std::memory_order_relaxed);
  119. }
  120. }
  121. }