|
cwist
Modern C web framework with libttak runtime
|
#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>
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. | |
| #define _GNU_SOURCE |
Definition at line 2 of file writer_fast.c.
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.
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_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.
| fd | Connected socket descriptor. |
| buf | Data to send; must not be NULL unless len is 0. |
| len | Number of bytes to send. |
| sent_out | Optional output; receives the number of bytes accepted by the socket (0 on EAGAIN or when nothing was sent). |
| CWIST_WRITE_DONE | The whole buffer was sent. |
| CWIST_WRITE_PENDING | Partial progress or the socket would block. |
| CWIST_WRITE_ERR | A 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_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.
| fd | Connected socket descriptor. |
| iov | Array of iovec entries; must not be NULL unless iovcnt <= 0. |
| iovcnt | Number of entries in iov. |
| flags | Extra send flags ORed into MSG_DONTWAIT/MSG_NOSIGNAL. |
| total_sent | Optional output; receives total bytes accepted by the socket (0 on EAGAIN or when nothing was sent). |
| CWIST_WRITE_DONE | The entire payload was sent. |
| CWIST_WRITE_PENDING | Partial progress or the socket would block. |
| CWIST_WRITE_ERR | Allocation 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().
| 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.
| worker_loads | Array of per-worker load counters (atomic uint32_t). |
| num_workers | Number of workers in the array. |
Definition at line 161 of file writer_fast.c.
References cap.
Referenced by cwist_http_pool_submit_async().