mirror of
https://github.com/moonlight-stream/moonlight-common-c.git
synced 2025-08-18 09:25:49 +00:00
Use a double-linked list for the linked blocking queue so insertions are done in O(1) time
This commit is contained in:
parent
761f324465
commit
8dcf4372f4
@ -33,7 +33,7 @@ void destroyAudioStream(void) {
|
|||||||
entry = LbqDestroyLinkedBlockingQueue(&packetQueue);
|
entry = LbqDestroyLinkedBlockingQueue(&packetQueue);
|
||||||
|
|
||||||
while (entry != NULL) {
|
while (entry != NULL) {
|
||||||
nextEntry = entry->next;
|
nextEntry = entry->flink;
|
||||||
free(entry->data);
|
free(entry->data);
|
||||||
free(entry);
|
free(entry);
|
||||||
entry = nextEntry;
|
entry = nextEntry;
|
||||||
|
@ -35,7 +35,7 @@ void destroyInputStream(void) {
|
|||||||
entry = LbqDestroyLinkedBlockingQueue(&packetQueue);
|
entry = LbqDestroyLinkedBlockingQueue(&packetQueue);
|
||||||
|
|
||||||
while (entry != NULL) {
|
while (entry != NULL) {
|
||||||
nextEntry = entry->next;
|
nextEntry = entry->flink;
|
||||||
free(entry->data);
|
free(entry->data);
|
||||||
free(entry);
|
free(entry);
|
||||||
entry = nextEntry;
|
entry = nextEntry;
|
||||||
|
@ -21,6 +21,7 @@ int LbqInitializeLinkedBlockingQueue(PLINKED_BLOCKING_QUEUE queueHead, int sizeB
|
|||||||
}
|
}
|
||||||
|
|
||||||
queueHead->head = NULL;
|
queueHead->head = NULL;
|
||||||
|
queueHead->tail = NULL;
|
||||||
queueHead->sizeBound = sizeBound;
|
queueHead->sizeBound = sizeBound;
|
||||||
queueHead->currentSize = 0;
|
queueHead->currentSize = 0;
|
||||||
|
|
||||||
@ -28,14 +29,14 @@ int LbqInitializeLinkedBlockingQueue(PLINKED_BLOCKING_QUEUE queueHead, int sizeB
|
|||||||
}
|
}
|
||||||
|
|
||||||
int LbqOfferQueueItem(PLINKED_BLOCKING_QUEUE queueHead, void* data) {
|
int LbqOfferQueueItem(PLINKED_BLOCKING_QUEUE queueHead, void* data) {
|
||||||
PLINKED_BLOCKING_QUEUE_ENTRY entry, lastEntry;
|
PLINKED_BLOCKING_QUEUE_ENTRY entry;
|
||||||
|
|
||||||
entry = (PLINKED_BLOCKING_QUEUE_ENTRY) malloc(sizeof(*entry));
|
entry = (PLINKED_BLOCKING_QUEUE_ENTRY) malloc(sizeof(*entry));
|
||||||
if (entry == NULL) {
|
if (entry == NULL) {
|
||||||
return LBQ_NO_MEMORY;
|
return LBQ_NO_MEMORY;
|
||||||
}
|
}
|
||||||
|
|
||||||
entry->next = NULL;
|
entry->flink = NULL;
|
||||||
entry->data = data;
|
entry->data = data;
|
||||||
|
|
||||||
PltLockMutex(&queueHead->mutex);
|
PltLockMutex(&queueHead->mutex);
|
||||||
@ -48,15 +49,17 @@ int LbqOfferQueueItem(PLINKED_BLOCKING_QUEUE queueHead, void* data) {
|
|||||||
|
|
||||||
if (queueHead->head == NULL) {
|
if (queueHead->head == NULL) {
|
||||||
LC_ASSERT(queueHead->currentSize == 0);
|
LC_ASSERT(queueHead->currentSize == 0);
|
||||||
|
LC_ASSERT(queueHead->tail == NULL);
|
||||||
queueHead->head = entry;
|
queueHead->head = entry;
|
||||||
|
queueHead->tail = entry;
|
||||||
|
entry->blink = NULL;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
LC_ASSERT(queueHead->currentSize >= 1);
|
LC_ASSERT(queueHead->currentSize >= 1);
|
||||||
lastEntry = queueHead->head;
|
LC_ASSERT(queueHead->head != NULL);
|
||||||
while (lastEntry->next != NULL) {
|
queueHead->tail->flink = entry;
|
||||||
lastEntry = lastEntry->next;
|
entry->blink = queueHead->tail;
|
||||||
}
|
queueHead->tail = entry;
|
||||||
lastEntry->next = entry;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
queueHead->currentSize++;
|
queueHead->currentSize++;
|
||||||
@ -86,18 +89,22 @@ int LbqWaitForQueueElement(PLINKED_BLOCKING_QUEUE queueHead, void** data) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
entry = queueHead->head;
|
entry = queueHead->head;
|
||||||
queueHead->head = entry->next;
|
queueHead->head = entry->flink;
|
||||||
queueHead->currentSize--;
|
queueHead->currentSize--;
|
||||||
|
if (queueHead->head == NULL) {
|
||||||
|
LC_ASSERT(queueHead->currentSize == 0);
|
||||||
|
queueHead->tail = NULL;
|
||||||
|
PltClearEvent(&queueHead->containsDataEvent);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
LC_ASSERT(queueHead->currentSize != 0);
|
||||||
|
queueHead->head->blink = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
*data = entry->data;
|
*data = entry->data;
|
||||||
|
|
||||||
free(entry);
|
free(entry);
|
||||||
|
|
||||||
if (queueHead->head == NULL) {
|
|
||||||
LC_ASSERT(queueHead->currentSize == 0);
|
|
||||||
PltClearEvent(&queueHead->containsDataEvent);
|
|
||||||
}
|
|
||||||
|
|
||||||
PltUnlockMutex(&queueHead->mutex);
|
PltUnlockMutex(&queueHead->mutex);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
@ -9,7 +9,8 @@
|
|||||||
#define LBQ_NO_MEMORY 3
|
#define LBQ_NO_MEMORY 3
|
||||||
|
|
||||||
typedef struct _LINKED_BLOCKING_QUEUE_ENTRY {
|
typedef struct _LINKED_BLOCKING_QUEUE_ENTRY {
|
||||||
struct _LINKED_BLOCKING_QUEUE_ENTRY *next;
|
struct _LINKED_BLOCKING_QUEUE_ENTRY *flink;
|
||||||
|
struct _LINKED_BLOCKING_QUEUE_ENTRY *blink;
|
||||||
void* data;
|
void* data;
|
||||||
} LINKED_BLOCKING_QUEUE_ENTRY, *PLINKED_BLOCKING_QUEUE_ENTRY;
|
} LINKED_BLOCKING_QUEUE_ENTRY, *PLINKED_BLOCKING_QUEUE_ENTRY;
|
||||||
|
|
||||||
@ -19,6 +20,7 @@ typedef struct _LINKED_BLOCKING_QUEUE {
|
|||||||
int sizeBound;
|
int sizeBound;
|
||||||
int currentSize;
|
int currentSize;
|
||||||
PLINKED_BLOCKING_QUEUE_ENTRY head;
|
PLINKED_BLOCKING_QUEUE_ENTRY head;
|
||||||
|
PLINKED_BLOCKING_QUEUE_ENTRY tail;
|
||||||
} LINKED_BLOCKING_QUEUE, *PLINKED_BLOCKING_QUEUE;
|
} LINKED_BLOCKING_QUEUE, *PLINKED_BLOCKING_QUEUE;
|
||||||
|
|
||||||
int LbqInitializeLinkedBlockingQueue(PLINKED_BLOCKING_QUEUE queueHead, int sizeBound);
|
int LbqInitializeLinkedBlockingQueue(PLINKED_BLOCKING_QUEUE queueHead, int sizeBound);
|
||||||
|
@ -45,7 +45,7 @@ void destroyVideoDepacketizer(void) {
|
|||||||
|
|
||||||
entry = LbqDestroyLinkedBlockingQueue(&decodeUnitQueue);
|
entry = LbqDestroyLinkedBlockingQueue(&decodeUnitQueue);
|
||||||
while (entry != NULL) {
|
while (entry != NULL) {
|
||||||
nextEntry = entry->next;
|
nextEntry = entry->flink;
|
||||||
free(entry->data);
|
free(entry->data);
|
||||||
free(entry);
|
free(entry);
|
||||||
entry = nextEntry;
|
entry = nextEntry;
|
||||||
|
@ -45,7 +45,7 @@ void destroyVideoStream(void) {
|
|||||||
entry = LbqDestroyLinkedBlockingQueue(&packetQueue);
|
entry = LbqDestroyLinkedBlockingQueue(&packetQueue);
|
||||||
|
|
||||||
while (entry != NULL) {
|
while (entry != NULL) {
|
||||||
nextEntry = entry->next;
|
nextEntry = entry->flink;
|
||||||
free(entry->data);
|
free(entry->data);
|
||||||
free(entry);
|
free(entry);
|
||||||
entry = nextEntry;
|
entry = nextEntry;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user