feat: further lay groundwork for worker threads
This commit is contained in:
parent
3490c2dd42
commit
ccb9b77cc8
3 changed files with 100 additions and 47 deletions
|
|
@ -10,12 +10,17 @@
|
|||
|
||||
#define LNM_LOOP_BUF_SIZE 2048
|
||||
|
||||
typedef enum {
|
||||
typedef enum lnm_loop_state {
|
||||
lnm_loop_state_req = 0,
|
||||
lnm_loop_state_res,
|
||||
lnm_loop_state_end,
|
||||
lnm_loop_state_req_blocking,
|
||||
lnm_loop_state_res_blocking,
|
||||
} lnm_loop_state;
|
||||
|
||||
/**
|
||||
* State for a currently active connection
|
||||
*/
|
||||
typedef struct lnm_loop_conn {
|
||||
int fd;
|
||||
lnm_loop_state state;
|
||||
|
|
@ -31,29 +36,8 @@ typedef struct lnm_loop_conn {
|
|||
} w;
|
||||
} lnm_loop_conn;
|
||||
|
||||
typedef struct lnm_loop {
|
||||
int listen_fd;
|
||||
int epoll_fd;
|
||||
atomic_int open;
|
||||
void *gctx;
|
||||
lnm_err (*ctx_init)(void **out, void *gctx);
|
||||
void (*ctx_free)(void *ctx);
|
||||
void (*data_read)(lnm_loop_conn *conn);
|
||||
void (*data_write)(lnm_loop_conn *conn);
|
||||
} lnm_loop;
|
||||
|
||||
lnm_err lnm_loop_init(lnm_loop **out, void *gctx,
|
||||
lnm_err (*ctx_init)(void **out, void *gctx),
|
||||
void (*ctx_free)(void *ctx),
|
||||
void (*data_read)(lnm_loop_conn *conn),
|
||||
void (*data_write)(lnm_loop_conn *conn));
|
||||
|
||||
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
|
||||
* Concurrent fixed-size queue used to distribute work among worker threads
|
||||
*/
|
||||
typedef struct lnm_loop_queue {
|
||||
struct {
|
||||
|
|
@ -83,4 +67,37 @@ void lnm_loop_queue_push(lnm_loop_queue *q, lnm_loop_conn *conn);
|
|||
*/
|
||||
lnm_loop_conn *lnm_loop_queue_pop(lnm_loop_queue *q);
|
||||
|
||||
typedef struct lnm_loop {
|
||||
int listen_fd;
|
||||
int epoll_fd;
|
||||
atomic_int open;
|
||||
void *gctx;
|
||||
lnm_err (*ctx_init)(void **out, void *gctx);
|
||||
void (*ctx_free)(void *ctx);
|
||||
void (*data_read)(lnm_loop_conn *conn);
|
||||
void (*data_write)(lnm_loop_conn *conn);
|
||||
lnm_loop_queue *wq;
|
||||
} lnm_loop;
|
||||
|
||||
lnm_err lnm_loop_init(lnm_loop **out, void *gctx,
|
||||
lnm_err (*ctx_init)(void **out, void *gctx),
|
||||
void (*ctx_free)(void *ctx),
|
||||
void (*data_read)(lnm_loop_conn *conn),
|
||||
void (*data_write)(lnm_loop_conn *conn));
|
||||
|
||||
lnm_err lnm_loop_setup(lnm_loop *l, uint16_t port);
|
||||
|
||||
lnm_err lnm_loop_run(lnm_loop *l, int thread_count);
|
||||
|
||||
/**
|
||||
* Reschedule the given connection, either on the event loop for network IO or
|
||||
* on a worker thread for blocking work. Connections are terminated as needed.
|
||||
*/
|
||||
void lnm_loop_conn_schedule(lnm_loop *l, lnm_loop_conn *conn);
|
||||
|
||||
/**
|
||||
* Main loop executed on the worker threads.
|
||||
*/
|
||||
void lnm_loop_worker_run(void *arg);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue