Skip to content

Commit

Permalink
small refactor of AtomicNotificationQueue, move Node out of Queue
Browse files Browse the repository at this point in the history
Reviewed By: andriigrynenko

Differential Revision: D24880761

fbshipit-source-id: 643da2b0fd461955ea33b4ba48f377c30720d442
  • Loading branch information
mshneer authored and dotconnor committed Mar 18, 2021
1 parent 70797a0 commit f07fd71
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
7 changes: 3 additions & 4 deletions folly/io/async/AtomicNotificationQueue-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ ssize_t AtomicNotificationQueue<Task, Consumer>::Queue::size() const {
}

template <typename Task, typename Consumer>
typename AtomicNotificationQueue<Task, Consumer>::Queue::Node&
typename AtomicNotificationQueue<Task, Consumer>::Node&
AtomicNotificationQueue<Task, Consumer>::Queue::front() {
return *head_;
}
Expand Down Expand Up @@ -104,8 +104,7 @@ AtomicNotificationQueue<Task, Consumer>::AtomicQueue::~AtomicQueue() {
template <typename Task, typename Consumer>
template <typename T>
bool AtomicNotificationQueue<Task, Consumer>::AtomicQueue::push(T&& value) {
std::unique_ptr<typename Queue::Node> node(
new typename Queue::Node(std::forward<T>(value)));
std::unique_ptr<Node> node(new Node(std::forward<T>(value)));
auto head = head_.load(std::memory_order_relaxed);
while (true) {
node->next =
Expand Down Expand Up @@ -147,7 +146,7 @@ AtomicNotificationQueue<Task, Consumer>::AtomicQueue::arm() {
if (!head &&
head_.compare_exchange_strong(
head,
reinterpret_cast<typename Queue::Node*>(kQueueArmedTag),
reinterpret_cast<Node*>(kQueueArmedTag),
std::memory_order_relaxed,
std::memory_order_relaxed)) {
++successfulArmCount_;
Expand Down
26 changes: 12 additions & 14 deletions folly/io/async/AtomicNotificationQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,21 @@ class AtomicNotificationQueue : private EventBase::LoopCallback,
noexcept(std::declval<Consumer>()(std::declval<Task&&>())),
"Consumer::operator()(Task&&) should be noexcept.");
class AtomicQueue;
class Queue {
public:
struct Node {
Task task;
std::shared_ptr<RequestContext> rctx{RequestContext::saveContext()};
struct Node {
Task task;
std::shared_ptr<RequestContext> rctx{RequestContext::saveContext()};

private:
friend class AtomicNotificationQueue::AtomicQueue;
friend class Queue;
private:
friend class AtomicNotificationQueue;

template <typename T>
explicit Node(T&& t) : task(std::forward<T>(t)) {}
template <typename T>
explicit Node(T&& t) : task(std::forward<T>(t)) {}

Node* next{};
};
Node* next{};
};

class Queue {
public:
Queue() {}
Queue(Queue&& other) noexcept;
Queue& operator=(Queue&& other) noexcept;
Expand Down Expand Up @@ -174,8 +173,7 @@ class AtomicNotificationQueue : private EventBase::LoopCallback,
}

private:
alignas(
folly::cacheline_align_v) std::atomic<typename Queue::Node*> head_{};
alignas(folly::cacheline_align_v) std::atomic<Node*> head_{};
alignas(folly::cacheline_align_v) ssize_t successfulArmCount_{0};
ssize_t consumerDisarmCount_{0};
static constexpr intptr_t kQueueArmedTag = 1;
Expand Down

0 comments on commit f07fd71

Please sign in to comment.