summaryrefslogtreecommitdiff
path: root/shared/queue.c
blob: 86e743f36ea5e5a7ebb92948d5fb2f3068bae1d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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;
}