Sese Framework  2.3.0
A cross-platform framework
Loading...
Searching...
No Matches
LinkedQueue.h
Go to the documentation of this file.
1// Copyright 2024 libsese
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
24#pragma once
25
26#include <atomic>
27
28namespace sese::concurrent {
29
30// GCOVR_EXCL_START
31
34template<class T>
37 struct Node {
38 std::atomic<T> value{};
39 std::atomic<Node *> next{nullptr};
40 };
41
42 std::atomic<Node *> head{nullptr};
43 std::atomic<Node *> tail{nullptr};
44
45public:
47 auto node = new Node();
48 head.store(node);
49 tail.store(node);
50 }
51
53 auto p_node = head.load();
54 while (p_node) {
55 auto p_next = p_node->next.load();
56 if (p_next == p_node) {
57 p_next = nullptr;
58 }
59 delete p_node;
60 p_node = p_next;
61 }
62 }
63
64 void push(const T &value) {
65 Node *tail;
66 auto node = new Node();
67 node->value.store(value);
68 node->next.store(nullptr);
69 while (true) {
70 tail = this->tail.load();
71 auto next = tail->next.load();
72 if (tail == this->tail) {
73 if (next == nullptr) {
74 if (tail->next.compare_exchange_weak(next, node)) {
75 break;
76 }
77 } else {
78 this->tail.compare_exchange_weak(tail, next);
79 }
80 }
81 }
82 this->tail.compare_exchange_weak(tail, node);
83 }
84
85 bool pop(T &value) {
86 Node *head;
87 while (true) {
88 head = this->head.load();
89 auto tail = this->tail.load();
90 auto next = head->next.load();
91 if (head == this->head) {
92 if (head == tail) {
93 if (next == nullptr) {
94 return false;
95 }
96 this->tail.compare_exchange_weak(tail, next);
97 } else {
98 value = next->value;
99 if (this->head.compare_exchange_weak(head, next)) {
100 break;
101 }
102 }
103 }
104 }
105 delete head;
106 return true;
107 }
108
109 bool empty() {
110 return head == tail;
111 }
112};
113
114// GCOVR_EXCL_STOP
115
116} // namespace sese::concurrent