Commit 7e06aa20 authored by Misha Shneerson's avatar Misha Shneerson Committed by Facebook GitHub Bot

small refactor of AtomicNotificationQueue, move Node out of Queue

Reviewed By: andriigrynenko

Differential Revision: D24880761

fbshipit-source-id: 643da2b0fd461955ea33b4ba48f377c30720d442
parent ff2ecd36
...@@ -54,7 +54,7 @@ ssize_t AtomicNotificationQueue<Task, Consumer>::Queue::size() const { ...@@ -54,7 +54,7 @@ ssize_t AtomicNotificationQueue<Task, Consumer>::Queue::size() const {
} }
template <typename Task, typename Consumer> template <typename Task, typename Consumer>
typename AtomicNotificationQueue<Task, Consumer>::Queue::Node& typename AtomicNotificationQueue<Task, Consumer>::Node&
AtomicNotificationQueue<Task, Consumer>::Queue::front() { AtomicNotificationQueue<Task, Consumer>::Queue::front() {
return *head_; return *head_;
} }
...@@ -104,8 +104,7 @@ AtomicNotificationQueue<Task, Consumer>::AtomicQueue::~AtomicQueue() { ...@@ -104,8 +104,7 @@ AtomicNotificationQueue<Task, Consumer>::AtomicQueue::~AtomicQueue() {
template <typename Task, typename Consumer> template <typename Task, typename Consumer>
template <typename T> template <typename T>
bool AtomicNotificationQueue<Task, Consumer>::AtomicQueue::push(T&& value) { bool AtomicNotificationQueue<Task, Consumer>::AtomicQueue::push(T&& value) {
std::unique_ptr<typename Queue::Node> node( std::unique_ptr<Node> node(new Node(std::forward<T>(value)));
new typename Queue::Node(std::forward<T>(value)));
auto head = head_.load(std::memory_order_relaxed); auto head = head_.load(std::memory_order_relaxed);
while (true) { while (true) {
node->next = node->next =
...@@ -147,7 +146,7 @@ AtomicNotificationQueue<Task, Consumer>::AtomicQueue::arm() { ...@@ -147,7 +146,7 @@ AtomicNotificationQueue<Task, Consumer>::AtomicQueue::arm() {
if (!head && if (!head &&
head_.compare_exchange_strong( head_.compare_exchange_strong(
head, head,
reinterpret_cast<typename Queue::Node*>(kQueueArmedTag), reinterpret_cast<Node*>(kQueueArmedTag),
std::memory_order_relaxed, std::memory_order_relaxed,
std::memory_order_relaxed)) { std::memory_order_relaxed)) {
++successfulArmCount_; ++successfulArmCount_;
......
...@@ -44,15 +44,12 @@ class AtomicNotificationQueue : private EventBase::LoopCallback, ...@@ -44,15 +44,12 @@ class AtomicNotificationQueue : private EventBase::LoopCallback,
noexcept(std::declval<Consumer>()(std::declval<Task&&>())), noexcept(std::declval<Consumer>()(std::declval<Task&&>())),
"Consumer::operator()(Task&&) should be noexcept."); "Consumer::operator()(Task&&) should be noexcept.");
class AtomicQueue; class AtomicQueue;
class Queue {
public:
struct Node { struct Node {
Task task; Task task;
std::shared_ptr<RequestContext> rctx{RequestContext::saveContext()}; std::shared_ptr<RequestContext> rctx{RequestContext::saveContext()};
private: private:
friend class AtomicNotificationQueue::AtomicQueue; friend class AtomicNotificationQueue;
friend class Queue;
template <typename T> template <typename T>
explicit Node(T&& t) : task(std::forward<T>(t)) {} explicit Node(T&& t) : task(std::forward<T>(t)) {}
...@@ -60,6 +57,8 @@ class AtomicNotificationQueue : private EventBase::LoopCallback, ...@@ -60,6 +57,8 @@ class AtomicNotificationQueue : private EventBase::LoopCallback,
Node* next{}; Node* next{};
}; };
class Queue {
public:
Queue() {} Queue() {}
Queue(Queue&& other) noexcept; Queue(Queue&& other) noexcept;
Queue& operator=(Queue&& other) noexcept; Queue& operator=(Queue&& other) noexcept;
...@@ -174,8 +173,7 @@ class AtomicNotificationQueue : private EventBase::LoopCallback, ...@@ -174,8 +173,7 @@ class AtomicNotificationQueue : private EventBase::LoopCallback,
} }
private: private:
alignas( alignas(folly::cacheline_align_v) std::atomic<Node*> head_{};
folly::cacheline_align_v) std::atomic<typename Queue::Node*> head_{};
alignas(folly::cacheline_align_v) ssize_t successfulArmCount_{0}; alignas(folly::cacheline_align_v) ssize_t successfulArmCount_{0};
ssize_t consumerDisarmCount_{0}; ssize_t consumerDisarmCount_{0};
static constexpr intptr_t kQueueArmedTag = 1; static constexpr intptr_t kQueueArmedTag = 1;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment