feat: implement concurrent worker queue

This commit is contained in:
Jef Roosens 2024-02-01 11:15:34 +01:00
parent 854e8c3809
commit 3490c2dd42
Signed by: Jef Roosens
GPG key ID: B75D4F293C7052DB
2 changed files with 93 additions and 0 deletions

View file

@ -1,6 +1,7 @@
#ifndef LNM_LOOP
#define LNM_LOOP
#include <pthread.h>
#include <stdatomic.h>
#include <stdint.h>
#include <stdlib.h>
@ -51,4 +52,35 @@ lnm_err lnm_loop_setup(lnm_loop *l, uint16_t port);
lnm_err lnm_loop_run(lnm_loop *l, int thread_count);
/**
* Concurrent fixed-size queue used by the worked threads to distribute tasks
*/
typedef struct lnm_loop_queue {
struct {
lnm_loop_conn **arr;
size_t len;
} buf;
size_t head;
size_t tail;
pthread_mutex_t mutex;
pthread_cond_t cond;
} lnm_loop_queue;
/**
* Initialize a new queue with the specified fixed capacity
*/
lnm_err lnm_loop_queue_init(lnm_loop_queue **out, size_t cap);
/**
* Queue the given connection. If the queue is currently full, this action
* blocks until there is space available.
*/
void lnm_loop_queue_push(lnm_loop_queue *q, lnm_loop_conn *conn);
/**
* Pop a connection from the queue. This action blocks until a connection is
* available.
*/
lnm_loop_conn *lnm_loop_queue_pop(lnm_loop_queue *q);
#endif