cwist
Modern C web framework with libttak runtime
Loading...
Searching...
No Matches
kqueue.c File Reference

BSD kqueue backend for the generic CWIST job queue interface. More...

#include <cwist/sys/io/cwist_io.h>
#include <cwist/core/mem/alloc.h>
#include <cwist/core/mem/gc.h>
#include <sys/types.h>
#include <sys/event.h>
#include <sys/time.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <stdatomic.h>
#include <stdint.h>
#include <time.h>
Include dependency graph for kqueue.c:

Go to the source code of this file.

Macros

#define _POSIX_C_SOURCE   200809L
 
#define KQ_WAKE_IDENT   0
 

Functions

cwist_io_queue * cwist_io_queue_create (size_t capacity)
 Allocate a kqueue-backed IO queue wrapper.
 
bool cwist_io_queue_submit (cwist_io_queue *q, cwist_job_func func, void *arg)
 Enqueue one callback by triggering an EVFILT_USER event.
 
void cwist_io_queue_run (cwist_io_queue *q)
 Process queued jobs until shutdown is requested and no work remains.
 
void cwist_io_queue_stop (cwist_io_queue *q)
 Request that runners stop without freeing the queue.
 
void cwist_io_queue_destroy (cwist_io_queue *q)
 Free jobs nobody ran, close the kqueue descriptor, free the queue.
 

Detailed Description

BSD kqueue backend for the generic CWIST job queue interface.

Definition in file kqueue.c.

Macro Definition Documentation

◆ _POSIX_C_SOURCE

#define _POSIX_C_SOURCE   200809L

Definition at line 1 of file kqueue.c.

◆ KQ_WAKE_IDENT

#define KQ_WAKE_IDENT   0

Definition at line 22 of file kqueue.c.

Function Documentation

◆ cwist_io_queue_create()

cwist_io_queue * cwist_io_queue_create ( size_t  capacity)

Allocate a kqueue-backed IO queue wrapper.

Initialize the job queue.

Parameters
capacityCapacity hint accepted for interface parity and currently unused.
Returns
Queue handle, or NULL when kqueue setup fails.

Definition at line 36 of file kqueue.c.

References cap, cwist_alloc(), cwist_free(), cwist_full_gc_enabled(), cwist_gc_scope_disown(), and KQ_WAKE_IDENT.

Referenced by cwist_scheduler_create().

◆ cwist_io_queue_destroy()

void cwist_io_queue_destroy ( cwist_io_queue *  q)

Free jobs nobody ran, close the kqueue descriptor, free the queue.

Destroy the queue and free resources.

All runners are gone by contract (stop, then join, then destroy).

Parameters
qQueue handle to destroy.

Definition at line 185 of file kqueue.c.

References cap, cwist_free(), and KQ_WAKE_IDENT.

Referenced by cwist_scheduler_create(), and cwist_scheduler_destroy().

◆ cwist_io_queue_run()

void cwist_io_queue_run ( cwist_io_queue *  q)

Process queued jobs until shutdown is requested and no work remains.

Run the event loop. Blocks indefinitely, processing jobs as they complete/trigger.

Parameters
qKqueue-backed queue handle.

Definition at line 119 of file kqueue.c.

References cap, cwist_free(), cwist_full_gc_enabled(), cwist_gc_scope_flush(), and KQ_WAKE_IDENT.

◆ cwist_io_queue_stop()

void cwist_io_queue_stop ( cwist_io_queue *  q)

Request that runners stop without freeing the queue.

Request that all runners stop processing without freeing the queue.

Runners finish the jobs already submitted, then return. The descriptor stays open until cwist_io_queue_destroy(), so a runner blocked in kevent() is woken by the wake event rather than by a close() that BSD kernels need not notice.

Parameters
qQueue handle to stop.

Definition at line 168 of file kqueue.c.

References cap, and KQ_WAKE_IDENT.

Referenced by cwist_scheduler_destroy().

◆ cwist_io_queue_submit()

bool cwist_io_queue_submit ( cwist_io_queue *  q,
cwist_job_func  func,
void *  arg 
)

Enqueue one callback by triggering an EVFILT_USER event.

Submit a job to be executed asynchronously.

Parameters
qKqueue-backed queue handle.
funcCallback to execute when the event is consumed.
argUser payload forwarded to func.
Returns
true on success, false when the queue is stopping, OOM, or event registration fails.

Definition at line 80 of file kqueue.c.

References cap, cwist_alloc(), cwist_free(), cwist_full_gc_enabled(), and cwist_gc_scope_disown().

Referenced by cwist_scheduler_schedule(), and cwist_scheduler_submit().