Sese Framework  x.y.z
A cross-platform framework
载入中...
搜索中...
未找到
LinkedStack.h
浏览该文件的文档.
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
23#pragma once
24
25#include <atomic>
26
27namespace sese::concurrent {
28
29// GCOVR_EXCL_START
30
33template<class T>
36 struct Node {
38 std::atomic<Node *> next{nullptr};
39 };
40
41 std::atomic<Node *> root{nullptr};
42
43public:
45 auto p_node = root.load();
46 while (p_node) {
47 auto p_next = p_node->next.load();
48 delete p_node;
49 p_node = p_next;
50 }
51 }
52
53 void push(const T &value) {
54 auto new_node = new Node;
55 new_node->value = value;
56
57 Node *current_root;
58 while (true) {
59 current_root = root.load();
60 new_node->next.store(current_root);
61 if (root.compare_exchange_weak(current_root, new_node)) {
62 break;
63 }
64 }
65 }
66
67 bool pop(T &value) {
68 Node *old_root;
69 Node *new_root;
70 while (true) {
71 old_root = root.load();
72 if (old_root == nullptr) {
73 return false;
74 }
75 new_root = old_root->next.load();
76 if (root.compare_exchange_weak(old_root, new_root)) {
77 break;
78 }
79 }
80 value = old_root->value;
81 delete old_root;
82 return true;
83 }
84
85 bool empty() {
86 return root.load() == nullptr;
87 }
88};
89
90// GCOVR_EXCL_STOP
91
92} // namespace sese::concurrent