mirror of
https://github.com/moonlight-stream/moonlight-common-c.git
synced 2026-12-03 19:51:33 +00:00
Use atomic_int for LBQ currentSize
This commit is contained in:
1 parent
b0deb0a265
commit
7fd0f25967
2 files changed
+10
-6
No files matched your search
@@ -23,7 +23,7 @@ PLINKED_BLOCKING_QUEUE_ENTRY LbqFlushQueueItems(PLINKED_BLOCKING_QUEUE queueHead
|
|||||||
if (head != NULL) {
|
if (head != NULL) {
|
||||||
queueHead->head = NULL;
|
queueHead->head = NULL;
|
||||||
queueHead->tail = NULL;
|
queueHead->tail = NULL;
|
||||||
queueHead->currentSize = 0;
|
atomic_store_explicit(&queueHead->currentSize, 0, memory_order_release);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
LC_ASSERT(queueHead->tail == NULL);
|
LC_ASSERT(queueHead->tail == NULL);
|
||||||
@@ -53,6 +53,7 @@ int LbqInitializeLinkedBlockingQueue(PLINKED_BLOCKING_QUEUE queueHead, int sizeB
|
|||||||
}
|
}
|
||||||
|
|
||||||
queueHead->sizeBound = sizeBound;
|
queueHead->sizeBound = sizeBound;
|
||||||
|
atomic_store_explicit(&queueHead->currentSize, 0, memory_order_release);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -79,7 +80,8 @@ void LbqSignalQueueUserWake(PLINKED_BLOCKING_QUEUE queueHead) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int LbqGetItemCount(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) {
|
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->tail = entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
queueHead->currentSize++;
|
atomic_fetch_add_explicit(&queueHead->currentSize, 1, memory_order_release);
|
||||||
queueHead->lifetimeSize++;
|
queueHead->lifetimeSize++;
|
||||||
|
|
||||||
PltUnlockMutex(&queueHead->mutex);
|
PltUnlockMutex(&queueHead->mutex);
|
||||||
@@ -180,7 +182,7 @@ int LbqPollQueueElement(PLINKED_BLOCKING_QUEUE queueHead, void** data) {
|
|||||||
|
|
||||||
entry = queueHead->head;
|
entry = queueHead->head;
|
||||||
queueHead->head = entry->flink;
|
queueHead->head = entry->flink;
|
||||||
queueHead->currentSize--;
|
atomic_fetch_sub_explicit(&queueHead->currentSize, 1, memory_order_release);
|
||||||
if (queueHead->head == NULL) {
|
if (queueHead->head == NULL) {
|
||||||
LC_ASSERT(queueHead->currentSize == 0);
|
LC_ASSERT(queueHead->currentSize == 0);
|
||||||
queueHead->tail = NULL;
|
queueHead->tail = NULL;
|
||||||
@@ -231,7 +233,7 @@ int LbqWaitForQueueElement(PLINKED_BLOCKING_QUEUE queueHead, void** data) {
|
|||||||
|
|
||||||
entry = queueHead->head;
|
entry = queueHead->head;
|
||||||
queueHead->head = entry->flink;
|
queueHead->head = entry->flink;
|
||||||
queueHead->currentSize--;
|
atomic_fetch_sub_explicit(&queueHead->currentSize, 1, memory_order_release);
|
||||||
if (queueHead->head == NULL) {
|
if (queueHead->head == NULL) {
|
||||||
LC_ASSERT(queueHead->currentSize == 0);
|
LC_ASSERT(queueHead->currentSize == 0);
|
||||||
queueHead->tail = NULL;
|
queueHead->tail = NULL;
|
||||||
|
|||||||
@@ -3,6 +3,8 @@
|
|||||||
#include "Platform.h"
|
#include "Platform.h"
|
||||||
#include "PlatformThreads.h"
|
#include "PlatformThreads.h"
|
||||||
|
|
||||||
|
#include <stdatomic.h>
|
||||||
|
|
||||||
#define LBQ_SUCCESS 0
|
#define LBQ_SUCCESS 0
|
||||||
#define LBQ_INTERRUPTED 1
|
#define LBQ_INTERRUPTED 1
|
||||||
#define LBQ_BOUND_EXCEEDED 2
|
#define LBQ_BOUND_EXCEEDED 2
|
||||||
@@ -21,7 +23,7 @@ typedef struct _LINKED_BLOCKING_QUEUE {
|
|||||||
PLINKED_BLOCKING_QUEUE_ENTRY head;
|
PLINKED_BLOCKING_QUEUE_ENTRY head;
|
||||||
PLINKED_BLOCKING_QUEUE_ENTRY tail;
|
PLINKED_BLOCKING_QUEUE_ENTRY tail;
|
||||||
int sizeBound;
|
int sizeBound;
|
||||||
int currentSize;
|
atomic_int currentSize; // Read lock-free by LbqGetItemCount()
|
||||||
int lifetimeSize;
|
int lifetimeSize;
|
||||||
bool shutdown;
|
bool shutdown;
|
||||||
bool draining;
|
bool draining;
|
||||||
|
|||||||
Reference in new issue
Block a user