blob: 4d0b26147c11e7ab655021f92695093f75814778 [file] [log] [blame]
Vishnu Nair60d902e2022-07-20 02:55:37 +00001/*
2 * Copyright 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
Patrick Williams6de9f802024-07-15 16:43:02 -050018
Vishnu Nair60d902e2022-07-20 02:55:37 +000019#include <atomic>
20#include <optional>
21
Patrick Williams6de9f802024-07-15 16:43:02 -050022// Single consumer multi producer queue. We can understand the two operations independently to see
Vishnu Nair60d902e2022-07-20 02:55:37 +000023// why they are without race condition.
24//
25// push is responsible for maintaining a linked list stored in mPush, and called from multiple
26// threads without lock. We can see that if two threads never observe the same value from
27// mPush.load, it just functions as a normal linked list. In the case where two threads observe the
28// same value, one of them has to execute the compare_exchange first. The one that doesn't execute
29// the compare exchange first, will receive false from compare_exchange. previousHead is updated (by
30// compare_exchange) to the most recent value of mPush, and we try again. It's relatively clear to
31// see that the process can repeat with an arbitrary number of threads.
32//
33// Pop is much simpler. If mPop is empty (as it begins) it atomically exchanges
34// the entire push list with null. This is safe, since the only other reader (push)
35// of mPush will retry if it changes in between it's read and atomic compare. We
36// then store the list and pop one element.
37//
38// If we already had something in the pop list we just pop directly.
Patrick Williams6de9f802024-07-15 16:43:02 -050039template <typename T>
Vishnu Nair60d902e2022-07-20 02:55:37 +000040class LocklessQueue {
41public:
Vishnu Nair60d902e2022-07-20 02:55:37 +000042 bool isEmpty() { return (mPush.load() == nullptr) && (mPop.load() == nullptr); }
43
44 void push(T value) {
Patrick Williams6de9f802024-07-15 16:43:02 -050045 Entry* entry = new Entry(std::move(value));
Vishnu Nair60d902e2022-07-20 02:55:37 +000046 Entry* previousHead = mPush.load(/*std::memory_order_relaxed*/);
47 do {
48 entry->mNext = previousHead;
49 } while (!mPush.compare_exchange_weak(previousHead, entry)); /*std::memory_order_release*/
50 }
Patrick Williams6de9f802024-07-15 16:43:02 -050051
Vishnu Nair60d902e2022-07-20 02:55:37 +000052 std::optional<T> pop() {
53 Entry* popped = mPop.load(/*std::memory_order_acquire*/);
54 if (popped) {
55 // Single consumer so this is fine
56 mPop.store(popped->mNext /* , std::memory_order_release */);
Patrick Williams6de9f802024-07-15 16:43:02 -050057 auto value = std::move(popped->mValue);
Vishnu Nair60d902e2022-07-20 02:55:37 +000058 delete popped;
Patrick Williams6de9f802024-07-15 16:43:02 -050059 return value;
Vishnu Nair60d902e2022-07-20 02:55:37 +000060 } else {
61 Entry* grabbedList = mPush.exchange(nullptr /* , std::memory_order_acquire */);
62 if (!grabbedList) return std::nullopt;
63 // Reverse the list
64 while (grabbedList->mNext) {
65 Entry* next = grabbedList->mNext;
66 grabbedList->mNext = popped;
67 popped = grabbedList;
68 grabbedList = next;
69 }
70 mPop.store(popped /* , std::memory_order_release */);
Patrick Williams6de9f802024-07-15 16:43:02 -050071 auto value = std::move(grabbedList->mValue);
Vishnu Nair60d902e2022-07-20 02:55:37 +000072 delete grabbedList;
Patrick Williams6de9f802024-07-15 16:43:02 -050073 return value;
Vishnu Nair60d902e2022-07-20 02:55:37 +000074 }
75 }
Patrick Williams6de9f802024-07-15 16:43:02 -050076
77private:
78 class Entry {
79 public:
80 T mValue;
81 std::atomic<Entry*> mNext;
82 Entry(T value) : mValue(value) {}
83 };
84 std::atomic<Entry*> mPush = nullptr;
85 std::atomic<Entry*> mPop = nullptr;
Vishnu Nair60d902e2022-07-20 02:55:37 +000086};