Skip to content

Commit dfe5f13

Browse files
lihuibaColdwings
andauthored
Manual PR 0.7 main (#511)
* new CI workflow for linux.x86 (#494) new CI workflow for linux.x86 * CI use images that pre-installed all deps (#505) * Manual pr 0.6 0.7 (#509) * FIX: lockfree MPMC queue should not fail to pop/push when queue is not empty/full (#504) * FIX: lockfree MPMC queue should not fail to pop/push when queue is not empty/full Signed-off-by: Coldwings <coldwings@me.com> * Old CI image not able to access repo, change to preset image Signed-off-by: Coldwings <coldwings@me.com> --------- Signed-off-by: Coldwings <coldwings@me.com> Co-authored-by: Coldwings <coldwings@me.com>
1 parent d857c26 commit dfe5f13

File tree

3 files changed

+14
-26
lines changed

3 files changed

+14
-26
lines changed

.github/workflows/ci.linux.x86-64.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ jobs:
4646
runs-on: ubuntu-latest
4747
container:
4848
image: ghcr.io/coldwings/photon-ut-base:latest
49-
options: --cpus 4
49+
options: --cpus 4 --privileged
5050
steps:
5151
- uses: szenius/set-timezone@v1.2
5252
with:
@@ -89,7 +89,7 @@ jobs:
8989
runs-on: ubuntu-latest
9090
container:
9191
image: ghcr.io/coldwings/photon-ut-base:latest
92-
options: --cpus 4
92+
options: --cpus 4 --privileged
9393
steps:
9494
- uses: szenius/set-timezone@v1.2
9595
with:
@@ -132,7 +132,7 @@ jobs:
132132
runs-on: ubuntu-latest
133133
container:
134134
image: ghcr.io/coldwings/photon-ut-base:latest
135-
options: --cpus 4
135+
options: --cpus 4 --privileged
136136
steps:
137137
- uses: szenius/set-timezone@v1.2
138138
with:
@@ -175,7 +175,7 @@ jobs:
175175
runs-on: ubuntu-latest
176176
container:
177177
image: ghcr.io/coldwings/photon-ut-base:latest
178-
options: --cpus 4
178+
options: --cpus 4 --privileged
179179
steps:
180180
- uses: szenius/set-timezone@v1.2
181181
with:
@@ -218,7 +218,7 @@ jobs:
218218
runs-on: ubuntu-latest
219219
container:
220220
image: ghcr.io/coldwings/photon-ut-base:latest
221-
options: --cpus 4
221+
options: --cpus 4 --privileged
222222
steps:
223223
- uses: szenius/set-timezone@v1.2
224224
with:

common/lockfree_queue.h

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ class LockfreeMPMCRingQueue : public LockfreeRingQueueBase<T, N> {
181181
using Base::empty;
182182
using Base::full;
183183

184-
bool push_weak(const T& x) {
184+
bool push(const T& x) {
185185
auto t = tail.load(std::memory_order_acquire);
186186
for (;;) {
187187
auto& slot = slots[idx(t)];
@@ -194,15 +194,16 @@ class LockfreeMPMCRingQueue : public LockfreeRingQueueBase<T, N> {
194194
}
195195
} else {
196196
auto const prevTail = t;
197+
auto h = head.load(std::memory_order_acquire);
197198
t = tail.load(std::memory_order_acquire);
198-
if (t == prevTail) {
199+
if (t == prevTail && Base::check_full(h, t)) {
199200
return false;
200201
}
201202
}
202203
}
203204
}
204205

205-
bool pop_weak(T& x) {
206+
bool pop(T& x) {
206207
auto h = head.load(std::memory_order_acquire);
207208
for (;;) {
208209
auto& slot = slots[idx(h)];
@@ -215,28 +216,15 @@ class LockfreeMPMCRingQueue : public LockfreeRingQueueBase<T, N> {
215216
}
216217
} else {
217218
auto const prevHead = h;
219+
auto t = tail.load(std::memory_order_acquire);
218220
h = head.load(std::memory_order_acquire);
219-
if (h == prevHead) {
221+
if (h == prevHead && Base::check_empty(h, t)) {
220222
return false;
221223
}
222224
}
223225
}
224226
}
225227

226-
bool push(const T& x) {
227-
do {
228-
if (push_weak(x)) return true;
229-
} while (!full());
230-
return false;
231-
}
232-
233-
bool pop(T& x) {
234-
do {
235-
if (pop_weak(x)) return true;
236-
} while (!empty());
237-
return false;
238-
}
239-
240228
template <typename Pause = ThreadPause>
241229
void send(const T& x) {
242230
static_assert(std::is_base_of<PauseBase, Pause>::value,
@@ -538,8 +526,8 @@ namespace common {
538526
* and load balancing.
539527
* Watch out that `recv` should run in photon environment (because it has to)
540528
* use photon semaphore to be notified that new item has sended. `send` could
541-
* running in photon or std::thread environment (needs to set template `Pause` as
542-
* `ThreadPause`).
529+
* running in photon or std::thread environment (needs to set template `Pause`
530+
* as `ThreadPause`).
543531
*
544532
* @tparam QueueType shoulde be one of LockfreeMPMCRingQueue,
545533
* LockfreeBatchMPMCRingQueue, or LockfreeSPSCRingQueue, with their own template

examples/sync-primitive/sync-primitive.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ int main() {
109109
while (true) {
110110
func_1us();
111111
Message* m;
112-
bool ok = ring.pop_weak(m);
112+
bool ok = ring.pop(m);
113113
if (!ok)
114114
continue;
115115
m->start = std::chrono::steady_clock::now();

0 commit comments

Comments
 (0)