Skip to content

Commit

Permalink
Fix yet another minor race condition in the non-threadqueue queue dat…
Browse files Browse the repository at this point in the history
…a structure.
  • Loading branch information
insertinterestingnamehere committed Oct 21, 2024
1 parent 2945351 commit 138c8f5
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
2 changes: 1 addition & 1 deletion include/qt_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

typedef struct qthread_queue_node_s {
struct qthread_queue_node_s *_Atomic next;
qthread_t *thread;
qthread_t *_Atomic thread;
} qthread_queue_node_t;

typedef struct qthread_queue_NEMESIS_s {
Expand Down
9 changes: 5 additions & 4 deletions src/queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ void INTERNAL qthread_queue_internal_nosync_enqueue(qthread_queue_nosync_t *q,
assert(q);
assert(t);

node->thread = t;
atomic_store_explicit(&node->thread, t, memory_order_relaxed);
atomic_store_explicit(&node->next, NULL, memory_order_relaxed);
if (atomic_load_explicit(&q->tail, memory_order_relaxed) == NULL) {
atomic_store_explicit(&q->head, node, memory_order_relaxed);
Expand All @@ -229,7 +229,7 @@ qthread_queue_internal_nosync_dequeue(qthread_queue_nosync_t *q) {
&q->head,
atomic_load_explicit(&node->next, memory_order_relaxed),
memory_order_relaxed);
t = node->thread;
t = atomic_load_explicit(&node->thread, memory_order_relaxed);
FREE_TQNODE(node);
}
return t;
Expand All @@ -241,7 +241,7 @@ void INTERNAL qthread_queue_internal_NEMESIS_enqueue(qthread_queue_NEMESIS_t *q,

node = ALLOC_TQNODE();
assert(node != NULL);
node->thread = t;
atomic_store_explicit(&node->thread, t, memory_order_relaxed);
atomic_store_explicit(&node->next, NULL, memory_order_relaxed);

prev = qt_internal_atomic_swap_ptr((void **)&(q->tail), node);
Expand Down Expand Up @@ -279,7 +279,8 @@ qthread_queue_internal_NEMESIS_dequeue(qthread_queue_NEMESIS_t *q) {
atomic_store_explicit(&dequeued->next, NULL, memory_order_relaxed);
}
}
qthread_t *retval = dequeued->thread;
qthread_t *retval =
atomic_load_explicit(&dequeued->thread, memory_order_relaxed);
FREE_TQNODE(dequeued);
return retval;
} else {
Expand Down

0 comments on commit 138c8f5

Please sign in to comment.