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

Job queue fallback used when no platform-specific async backend is selected. More...

#include <cwist/sys/io/cwist_io.h>
#include <cwist/core/mem/alloc.h>
#include <pthread.h>
#include <stdatomic.h>
#include <stdbool.h>
Include dependency graph for io_select.c:

Go to the source code of this file.

Macros

#define _POSIX_C_SOURCE   200809L
 

Typedefs

typedef struct job_node job_node_t
 

Functions

cwist_io_queue * cwist_io_queue_create (size_t capacity)
 Allocate a queue instance and install its sentinel node.
 
bool cwist_io_queue_submit (cwist_io_queue *q, cwist_job_func func, void *arg)
 Submit a callback for asynchronous execution by the queue runner.
 
void cwist_io_queue_run (cwist_io_queue *q)
 Drain 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)
 Stop the queue, wake sleepers, and free every remaining node.
 

Detailed Description

Job queue fallback used when no platform-specific async backend is selected.

A Michael & Scott two-lock queue: producers append under tail_lock and consumers pop under head_lock, around a dummy sentinel node. The lock-free variant needs safe memory reclamation (see io_queue.c), which this portable fallback does without: a node is freed only by a consumer holding head_lock, after it stopped being the sentinel, and no producer reads a node after linking past it, so nothing can still hold a freed node.

Definition in file io_select.c.

Macro Definition Documentation

◆ _POSIX_C_SOURCE

#define _POSIX_C_SOURCE   200809L

Definition at line 1 of file io_select.c.

Typedef Documentation

◆ job_node_t

Function Documentation

◆ cwist_io_queue_create()

cwist_io_queue * cwist_io_queue_create ( size_t  capacity)

Allocate a queue instance and install its sentinel node.

Initialize the job queue.

Parameters
capacityRequested capacity hint. Currently unused by the select fallback.
Returns
Queue handle, or NULL when allocation or synchronization setup fails.

Definition at line 133 of file io_select.c.

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

◆ cwist_io_queue_destroy()

void cwist_io_queue_destroy ( cwist_io_queue *  q)

Stop the queue, wake sleepers, and free every remaining node.

Destroy the queue and free resources.

Parameters
qQueue instance to destroy.

Definition at line 224 of file io_select.c.

References cap, and cwist_free().

◆ cwist_io_queue_run()

void cwist_io_queue_run ( cwist_io_queue *  q)

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

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

Parameters
qQueue to run on the current thread.

Definition at line 190 of file io_select.c.

References cap.

◆ 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.

Parameters
qQueue instance to stop.

Definition at line 212 of file io_select.c.

References cap.

◆ cwist_io_queue_submit()

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

Submit a callback for asynchronous execution by the queue runner.

Submit a job to be executed asynchronously.

Parameters
qQueue that should own the job.
funcCallback to execute.
argUser payload forwarded to func.
Returns
true on success, or false when the queue is shutting down or OOM.

Definition at line 174 of file io_select.c.

References cap.