Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit71c9e22

Browse files
committed
use spin-lock instead of mutex
1 parentd79a27c commit71c9e22

File tree

1 file changed

+32
-11
lines changed

1 file changed

+32
-11
lines changed

‎include/acto/intrusive.h

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,28 @@ class sequence {
4242
T* head_;
4343
};
4444

45-
template<typename T,typename Mutex = std::mutex>
45+
classspin_lock {
46+
public:
47+
voidlock()noexcept {
48+
while (flag_.test_and_set(std::memory_order_acquire)) {
49+
#if defined(__cpp_lib_atomic_wait) && __cpp_lib_atomic_wait >= 201907L
50+
flag_.wait(true, std::memory_order_relaxed);
51+
#endif
52+
}
53+
}
54+
55+
voidunlock()noexcept {
56+
flag_.clear(std::memory_order_release);
57+
#if defined(__cpp_lib_atomic_wait) && __cpp_lib_atomic_wait >= 201907L
58+
flag_.notify_one();
59+
#endif
60+
}
61+
62+
private:
63+
std::atomic_flag flag_ = ATOMIC_FLAG_INIT;
64+
};
65+
66+
template<typename T,typename Mutex = spin_lock>
4667
classqueue {
4768
public:
4869
sequence<T>extract() {
@@ -113,7 +134,7 @@ class mpsc_stack {
113134

114135
sequence<T>extract()noexcept {
115136
do {
116-
T* top = head_.load();
137+
T* top = head_.load(std::memory_order_relaxed);
117138

118139
if (top ==nullptr) {
119140
return sequence<T>{nullptr};
@@ -126,7 +147,7 @@ class mpsc_stack {
126147

127148
voidpush(T*const node)noexcept {
128149
do {
129-
T* top = head_.load();
150+
T* top = head_.load(std::memory_order_relaxed);
130151
node->next = top;
131152
if (head_.compare_exchange_weak(top, node)) {
132153
return;
@@ -142,17 +163,17 @@ class mpsc_stack {
142163

143164
T*pop()noexcept {
144165
do {
145-
T* top = head_.load();
146-
166+
T* top = head_.load(std::memory_order_consume);
167+
// Check the stack is empty.
147168
if (top ==nullptr) {
148169
returnnullptr;
149-
}else {
150-
T* next = top->next;
170+
}
151171

152-
if (head_.compare_exchange_weak(top, next)) {
153-
top->next =nullptr;
154-
return top;
155-
}
172+
T* next = top->next;
173+
// Pop the item.
174+
if (head_.compare_exchange_weak(top, next)) {
175+
top->next =nullptr;
176+
return top;
156177
}
157178
}while (true);
158179
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp