move engine to engine/
This commit is contained in:
31
engine/src/queue.c
Normal file
31
engine/src/queue.c
Normal file
@@ -0,0 +1,31 @@
|
||||
#include "queue.h"
|
||||
#include <stdatomic.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
void queue_init(spsc_queue_t *q) {
|
||||
/* nothing to allocate, just ensure head/tail start at 0 */
|
||||
q->head = 0;
|
||||
q->tail = 0;
|
||||
}
|
||||
|
||||
bool queue_push(spsc_queue_t *q, command_t cmd) {
|
||||
int h = atomic_load_explicit(&q->head, memory_order_relaxed);
|
||||
int t = atomic_load_explicit(&q->tail, memory_order_acquire);
|
||||
int next = (h + 1) % QUEUE_CAPACITY;
|
||||
if (next == t)
|
||||
return false; /* queue full */
|
||||
q->buffer[h] = cmd;
|
||||
atomic_store_explicit(&q->head, next, memory_order_release);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool queue_pop(spsc_queue_t *q, command_t *cmd) {
|
||||
int t = atomic_load_explicit(&q->tail, memory_order_relaxed);
|
||||
int h = atomic_load_explicit(&q->head, memory_order_acquire);
|
||||
if (t == h)
|
||||
return false; /* queue empty */
|
||||
*cmd = q->buffer[t];
|
||||
atomic_store_explicit(&q->tail, (t + 1) % QUEUE_CAPACITY,
|
||||
memory_order_release);
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user