diff options
Diffstat (limited to 'shared/queue.c')
| -rw-r--r-- | shared/queue.c | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/shared/queue.c b/shared/queue.c new file mode 100644 index 0000000..86e743f --- /dev/null +++ b/shared/queue.c @@ -0,0 +1,29 @@ +#include "queue.h" +#include <string.h> + +void QueueInit(PacketQueue *q) { + q->head = 0; + q->tail = 0; + pthread_mutex_init(&q->lock, NULL); +} + +void QueuePush(PacketQueue *q, void *data, int size) { + pthread_mutex_lock(&q->lock); + memcpy(q->data[q->tail], data, size); + q->size[q->tail] = size; + q->tail = (q->tail + 1) % QUEUE_SIZE; + pthread_mutex_unlock(&q->lock); +} + +bool QueuePop(PacketQueue *q, void *data, int *size) { + pthread_mutex_lock(&q->lock); + if (q->head == q->tail) { + pthread_mutex_unlock(&q->lock); + return false; + } + memcpy(data, q->data[q->head], q->size[q->head]); + *size = q->size[q->head]; + q->head = (q->head + 1) % QUEUE_SIZE; + pthread_mutex_unlock(&q->lock); + return true; +} |