Skip to content

Commit

Permalink
chore: use RoundUpPowerOf2() in LockFreeQueue Init() (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
ypingcn authored Dec 18, 2023
1 parent c95fdcb commit 1cea869
Showing 1 changed file with 5 additions and 13 deletions.
18 changes: 5 additions & 13 deletions trpc/util/queue/lockfree_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include <utility>
#include <vector>

#include "trpc/util/algorithm/power_of_two.h"

namespace trpc {

/// @brief Implementation of a thread-safe lock-free queue that supports multiple producers and multiple
Expand Down Expand Up @@ -54,21 +56,11 @@ class alignas(64) LockFreeQueue {
return RT_OK;
}

bool size_is_power_of_2 = (size >= 2) && ((size & (size - 1)) == 0);
if (!size_is_power_of_2) {
uint64_t tmp = 1;
while (tmp <= size) {
tmp <<= 1;
}

size = tmp;
}

mask_ = size - 1;
capacity_ = size;
capacity_ = RoundUpPowerOf2(size);
mask_ = capacity_ - 1;

elements_ = std::make_unique<Element[]>(capacity_);
for (size_t i = 0; i < size; ++i) {
for (size_t i = 0; i < capacity_; ++i) {
elements_[i].sequence = i;
}

Expand Down

0 comments on commit 1cea869

Please sign in to comment.