cwist
Modern C web framework with libttak runtime
Loading...
Searching...
No Matches
writer_fast.c File Reference
#include <cwist/sys/wasi.h>
#include <cwist/core/mem/alloc.h>
#include <cwist/net/http/writer_fast.h>
#include <sys/socket.h>
#include <errno.h>
#include <stdatomic.h>
#include <unistd.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
Include dependency graph for writer_fast.c:

Go to the source code of this file.

Macros

#define _GNU_SOURCE
 

Functions

cwist_write_status_t cwist_http_send_speculative (int fd, const void *buf, size_t len, size_t *sent_out)
 Attempt a single non-blocking speculative write of a buffer to a socket.
 
cwist_write_status_t cwist_http_sendmsg_speculative (int fd, struct iovec *iov, int iovcnt, int flags, size_t *total_sent)
 Attempt a single non-blocking speculative sendmsg over an iovec array.
 
uint32_t cwist_sched_p2c_select_worker (const _Atomic uint32_t *worker_loads, uint32_t num_workers)
 Select the less-loaded of two randomly sampled workers (power-of-two choices).
 
uint32_t cwist_fast_monotonic_sec (void)
 Return a cheap monotonic timestamp in whole seconds.
 

Macro Definition Documentation

◆ _GNU_SOURCE

#define _GNU_SOURCE

Definition at line 2 of file writer_fast.c.

Function Documentation

◆ cwist_fast_monotonic_sec()

uint32_t cwist_fast_monotonic_sec ( void  )

Return a cheap monotonic timestamp in whole seconds.

Fast monotonic clock in seconds (vDSO coarse where available).

Uses CLOCK_MONOTONIC_COARSE when available for speed, falling back to CLOCK_MONOTONIC. Intended for coarse-grained elapsed-time checks, not sub-second precision.

Returns
Monotonic time in seconds, truncated to uint32_t.

Definition at line 200 of file writer_fast.c.

References cap.

Referenced by cwist_http_async_rearm(), cwist_http_coalesce_append(), cwist_http_pool_submit_async(), cwist_http_receive_request_nb(), cwist_http_send_response_coalesced(), and cwist_websocket_async_attach_ex().

◆ cwist_http_send_speculative()

cwist_write_status_t cwist_http_send_speculative ( int  fd,
const void *  buf,
size_t  len,
size_t *  sent_out 
)

Attempt a single non-blocking speculative write of a buffer to a socket.

Speculative non-blocking direct sender.

Performs one best-effort send() with MSG_DONTWAIT (plus MSG_NOSIGNAL when available). Never blocks: if the socket buffer is full, the write is left pending for the caller's blocking drain path.

Parameters
fdConnected socket descriptor.
bufData to send; must not be NULL unless len is 0.
lenNumber of bytes to send.
sent_outOptional output; receives the number of bytes accepted by the socket (0 on EAGAIN or when nothing was sent).
Return values
CWIST_WRITE_DONEThe whole buffer was sent.
CWIST_WRITE_PENDINGPartial progress or the socket would block.
CWIST_WRITE_ERRA hard send error occurred.

Definition at line 31 of file writer_fast.c.

References cap, CWIST_WRITE_DONE, CWIST_WRITE_ERR, CWIST_WRITE_PENDING, and MSG_DONTWAIT.

◆ cwist_http_sendmsg_speculative()

cwist_write_status_t cwist_http_sendmsg_speculative ( int  fd,
struct iovec *  iov,
int  iovcnt,
int  flags,
size_t *  total_sent 
)

Attempt a single non-blocking speculative sendmsg over an iovec array.

Speculative scatter/gather sendmsg without blocking poll().

Like cwist_http_send_speculative(), but sends a scatter/gather buffer. Under CWIST_WASI_SOCKETS (no sendmsg(2) in wasi:sockets 0.2) the iovecs are coalesced into one heap allocation (owned and freed here) and sent with a single send(); the blocking drain in cwist_http_sendmsg_all() re-drives whatever remains.

Parameters
fdConnected socket descriptor.
iovArray of iovec entries; must not be NULL unless iovcnt <= 0.
iovcntNumber of entries in iov.
flagsExtra send flags ORed into MSG_DONTWAIT/MSG_NOSIGNAL.
total_sentOptional output; receives total bytes accepted by the socket (0 on EAGAIN or when nothing was sent).
Return values
CWIST_WRITE_DONEThe entire payload was sent.
CWIST_WRITE_PENDINGPartial progress or the socket would block.
CWIST_WRITE_ERRAllocation or hard send error occurred.

Definition at line 83 of file writer_fast.c.

References cap, cwist_alloc(), cwist_free(), CWIST_WRITE_DONE, CWIST_WRITE_ERR, CWIST_WRITE_PENDING, and MSG_DONTWAIT.

Referenced by cwist_http_async_send_response(), cwist_http_coalesce_flush(), cwist_http_send_response_async(), and cwist_http_send_response_coalesced().

◆ cwist_sched_p2c_select_worker()

uint32_t cwist_sched_p2c_select_worker ( const _Atomic uint32_t *  worker_loads,
uint32_t  num_workers 
)

Select the less-loaded of two randomly sampled workers (power-of-two choices).

Select worker using Power of Two Random Choices (P2C).

Draws two distinct worker indices using a thread-local xorshift32 PRNG, reads their current loads with a relaxed atomic load, and returns the index of the worker with the smaller load. Thread-safe: the PRNG seed is _Thread_local and the load array is only read.

Parameters
worker_loadsArray of per-worker load counters (atomic uint32_t).
num_workersNumber of workers in the array.
Returns
Index of the selected worker; 0 if worker_loads is NULL or num_workers <= 1.

Definition at line 161 of file writer_fast.c.

References cap.

Referenced by cwist_http_pool_submit_async().