Use atomic_int for LBQ currentSize

This commit is contained in:
Cameron Gutman committed 2026-11-10 23:02:43 -06:00
1 parent b0deb0a265
commit 7fd0f25967
2 files changed
+10 -6

No files matched your search

+7 -5
View File
@@ -23,7 +23,7 @@ PLINKED_BLOCKING_QUEUE_ENTRY LbqFlushQueueItems(PLINKED_BLOCKING_QUEUE queueHead
if (head != NULL) {
queueHead->head = NULL;
queueHead->tail = NULL;
queueHead->currentSize = 0;
atomic_store_explicit(&queueHead->currentSize, 0, memory_order_release);
}
else {
LC_ASSERT(queueHead->tail == NULL);
@@ -53,6 +53,7 @@ int LbqInitializeLinkedBlockingQueue(PLINKED_BLOCKING_QUEUE queueHead, int sizeB
}
queueHead->sizeBound = sizeBound;
atomic_store_explicit(&queueHead->currentSize, 0, memory_order_release);
return 0;
}
@@ -79,7 +80,8 @@ void LbqSignalQueueUserWake(PLINKED_BLOCKING_QUEUE queueHead) {
}
int LbqGetItemCount(PLINKED_BLOCKING_QUEUE queueHead) {
return queueHead->currentSize;
// Pairs with release stores in LbqOfferQueueItem() and friends
return atomic_load_explicit(&queueHead->currentSize, memory_order_acquire);
}
int LbqOfferQueueItem(PLINKED_BLOCKING_QUEUE queueHead, void* data, PLINKED_BLOCKING_QUEUE_ENTRY entry) {
@@ -116,7 +118,7 @@ int LbqOfferQueueItem(PLINKED_BLOCKING_QUEUE queueHead, void* data, PLINKED_BLOC
queueHead->tail = entry;
}
queueHead->currentSize++;
atomic_fetch_add_explicit(&queueHead->currentSize, 1, memory_order_release);
queueHead->lifetimeSize++;
PltUnlockMutex(&queueHead->mutex);
@@ -180,7 +182,7 @@ int LbqPollQueueElement(PLINKED_BLOCKING_QUEUE queueHead, void** data) {
entry = queueHead->head;
queueHead->head = entry->flink;
queueHead->currentSize--;
atomic_fetch_sub_explicit(&queueHead->currentSize, 1, memory_order_release);
if (queueHead->head == NULL) {
LC_ASSERT(queueHead->currentSize == 0);
queueHead->tail = NULL;
@@ -231,7 +233,7 @@ int LbqWaitForQueueElement(PLINKED_BLOCKING_QUEUE queueHead, void** data) {
entry = queueHead->head;
queueHead->head = entry->flink;
queueHead->currentSize--;
atomic_fetch_sub_explicit(&queueHead->currentSize, 1, memory_order_release);
if (queueHead->head == NULL) {
LC_ASSERT(queueHead->currentSize == 0);
queueHead->tail = NULL;
+3 -1
View File
@@ -3,6 +3,8 @@
#include "Platform.h"
#include "PlatformThreads.h"
#include <stdatomic.h>
#define LBQ_SUCCESS 0
#define LBQ_INTERRUPTED 1
#define LBQ_BOUND_EXCEEDED 2
@@ -21,7 +23,7 @@ typedef struct _LINKED_BLOCKING_QUEUE {
PLINKED_BLOCKING_QUEUE_ENTRY head;
PLINKED_BLOCKING_QUEUE_ENTRY tail;
int sizeBound;
int currentSize;
atomic_int currentSize; // Read lock-free by LbqGetItemCount()
int lifetimeSize;
bool shutdown;
bool draining;