mirror of
https://github.com/moonlight-stream/moonlight-common-c.git
synced 2026-04-18 22:30:01 +00:00
Initial work for depacketizer
This commit is contained in:
84
limelight-common/LinkedBlockingQueue.cpp
Normal file
84
limelight-common/LinkedBlockingQueue.cpp
Normal file
@@ -0,0 +1,84 @@
|
||||
#include "LinkedBlockingQueue.h"
|
||||
|
||||
int initializeLinkedBlockingQueue(PLINKED_BLOCKING_QUEUE queueHead, int sizeBound) {
|
||||
int err;
|
||||
|
||||
err = PltCreateEvent(&queueHead->containsDataEvent);
|
||||
if (err != 0) {
|
||||
return err;
|
||||
}
|
||||
|
||||
err = PltCreateMutex(&queueHead->mutex);
|
||||
if (err != 0) {
|
||||
return err;
|
||||
}
|
||||
|
||||
queueHead->head = NULL;
|
||||
queueHead->sizeBound = sizeBound;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int offerQueueItem(PLINKED_BLOCKING_QUEUE queueHead, void* data) {
|
||||
PLINKED_BLOCKING_QUEUE_ENTRY entry, lastEntry;
|
||||
|
||||
entry = (PLINKED_BLOCKING_QUEUE_ENTRY) malloc(sizeof(*entry));
|
||||
if (entry == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
entry->next = NULL;
|
||||
entry->data = data;
|
||||
|
||||
PltLockMutex(queueHead->mutex);
|
||||
|
||||
if (queueHead->head == NULL) {
|
||||
queueHead->head = entry;
|
||||
}
|
||||
else {
|
||||
lastEntry = queueHead->head;
|
||||
while (lastEntry->next != NULL) {
|
||||
lastEntry = lastEntry->next;
|
||||
}
|
||||
lastEntry->next = entry;
|
||||
}
|
||||
|
||||
PltSetEvent(queueHead->containsDataEvent);
|
||||
|
||||
PltUnlockMutex(queueHead->mutex);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void* waitForQueueElement(PLINKED_BLOCKING_QUEUE queueHead) {
|
||||
PLINKED_BLOCKING_QUEUE_ENTRY entry;
|
||||
void* data;
|
||||
|
||||
for (;;) {
|
||||
PltWaitForEvent(queueHead->containsDataEvent);
|
||||
|
||||
PltLockMutex(queueHead->mutex);
|
||||
|
||||
if (queueHead->head == NULL) {
|
||||
PltUnlockMutex(queueHead->mutex);
|
||||
continue;
|
||||
}
|
||||
|
||||
entry = queueHead->head;
|
||||
queueHead->head = entry->next;
|
||||
|
||||
data = entry->data;
|
||||
|
||||
free(entry);
|
||||
|
||||
if (queueHead->head == NULL) {
|
||||
PltClearEvent(queueHead->containsDataEvent);
|
||||
}
|
||||
|
||||
PltUnlockMutex(queueHead->mutex);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
Reference in New Issue
Block a user