loop.c: one poll(2) per run, monotonic one-shot timers (sorted, FIFO on equal deadlines), non-blocking wakeup pipe and safe fd dispatch when a callback removes another fd. test_loop covers timer order/cancel, timeout, wakeup, pipe, delete-in-callback, timer-added-timer, monotonicity and invalid arguments. make mcde-host exit=0; test_json, test_frame and test_loop all ok.
363 lines
7.0 KiB
C
363 lines
7.0 KiB
C
/*
|
|
* loop.c -- the core's poll(2) event loop
|
|
*
|
|
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
|
|
*
|
|
* One poll(2) per dc_loop_run(), a monotonic one-shot timer list and a
|
|
* non-blocking wakeup pipe. The loop never calls into Xt; the UI watches
|
|
* dc_loop_wakeup_fd() and calls dc_loop_run() from its own callback.
|
|
*/
|
|
#include "dc_internal.h"
|
|
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <stdint.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <time.h>
|
|
#include <unistd.h>
|
|
|
|
struct DcFdEntry {
|
|
int id;
|
|
int fd;
|
|
short events;
|
|
DcLoopFdFn fn;
|
|
void *arg;
|
|
};
|
|
|
|
struct DcTimerEntry {
|
|
int id;
|
|
long long when;
|
|
DcLoopTimerFn fn;
|
|
void *arg;
|
|
};
|
|
|
|
struct DcReady {
|
|
int id;
|
|
short revents;
|
|
};
|
|
|
|
struct DcLoop {
|
|
int wake_r;
|
|
int wake_w;
|
|
int next_id;
|
|
struct DcFdEntry *fds;
|
|
size_t nfds, capfds;
|
|
struct DcTimerEntry *timers;
|
|
size_t ntimers, captimers;
|
|
struct pollfd *pfds;
|
|
size_t pcap;
|
|
struct DcReady *ready;
|
|
size_t rcap;
|
|
};
|
|
|
|
static int
|
|
set_nonblock_cloexec(int fd)
|
|
{
|
|
int fl = fcntl(fd, F_GETFL, 0);
|
|
|
|
if (fl < 0 || fcntl(fd, F_SETFL, fl | O_NONBLOCK) < 0)
|
|
return -1;
|
|
if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0)
|
|
return -1;
|
|
return 0;
|
|
}
|
|
|
|
long long
|
|
dc_loop_now_ms(void)
|
|
{
|
|
struct timespec ts;
|
|
|
|
if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0)
|
|
return 0;
|
|
return (long long)ts.tv_sec * 1000 + (long long)ts.tv_nsec / 1000000;
|
|
}
|
|
|
|
DcLoop *
|
|
dc_loop_new(void)
|
|
{
|
|
DcLoop *loop;
|
|
int fds[2];
|
|
|
|
if (pipe(fds) != 0)
|
|
return NULL;
|
|
if (set_nonblock_cloexec(fds[0]) != 0 ||
|
|
set_nonblock_cloexec(fds[1]) != 0) {
|
|
close(fds[0]);
|
|
close(fds[1]);
|
|
return NULL;
|
|
}
|
|
loop = calloc(1, sizeof(*loop));
|
|
if (loop == NULL) {
|
|
close(fds[0]);
|
|
close(fds[1]);
|
|
return NULL;
|
|
}
|
|
loop->wake_r = fds[0];
|
|
loop->wake_w = fds[1];
|
|
loop->next_id = 1;
|
|
return loop;
|
|
}
|
|
|
|
void
|
|
dc_loop_free(DcLoop *loop)
|
|
{
|
|
if (loop == NULL)
|
|
return;
|
|
close(loop->wake_r);
|
|
close(loop->wake_w);
|
|
free(loop->fds);
|
|
free(loop->timers);
|
|
free(loop->pfds);
|
|
free(loop->ready);
|
|
free(loop);
|
|
}
|
|
|
|
int
|
|
dc_loop_wakeup_fd(const DcLoop *loop)
|
|
{
|
|
return loop ? loop->wake_r : -1;
|
|
}
|
|
|
|
int
|
|
dc_loop_wake(DcLoop *loop)
|
|
{
|
|
if (loop == NULL)
|
|
return -1;
|
|
for (;;) {
|
|
ssize_t n = write(loop->wake_w, "x", 1);
|
|
if (n == 1)
|
|
return 0;
|
|
if (n < 0 && errno == EINTR)
|
|
continue;
|
|
if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK))
|
|
return 0;
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
static int
|
|
ensure_array(void **ptr, size_t *cap, size_t need, size_t elem)
|
|
{
|
|
size_t ncap;
|
|
void *np;
|
|
|
|
if (need <= *cap)
|
|
return 0;
|
|
if (elem != 0 && need > SIZE_MAX / elem)
|
|
return -1;
|
|
ncap = *cap ? *cap : 8;
|
|
while (ncap < need) {
|
|
if (ncap > SIZE_MAX / 2)
|
|
return -1;
|
|
ncap *= 2;
|
|
}
|
|
np = realloc(*ptr, ncap * elem);
|
|
if (np == NULL)
|
|
return -1;
|
|
*ptr = np;
|
|
*cap = ncap;
|
|
return 0;
|
|
}
|
|
|
|
int
|
|
dc_loop_add_fd(DcLoop *loop, int fd, short events, DcLoopFdFn fn, void *arg)
|
|
{
|
|
struct DcFdEntry *entry;
|
|
int id;
|
|
|
|
if (loop == NULL || fd < 0 || events == 0 || fn == NULL)
|
|
return -1;
|
|
if (ensure_array((void **)&loop->fds, &loop->capfds,
|
|
loop->nfds + 1, sizeof(*loop->fds)) != 0)
|
|
return -1;
|
|
id = loop->next_id++;
|
|
entry = &loop->fds[loop->nfds++];
|
|
entry->id = id;
|
|
entry->fd = fd;
|
|
entry->events = events;
|
|
entry->fn = fn;
|
|
entry->arg = arg;
|
|
return id;
|
|
}
|
|
|
|
static struct DcFdEntry *
|
|
find_fd(DcLoop *loop, int id)
|
|
{
|
|
size_t i;
|
|
|
|
for (i = 0; i < loop->nfds; i++)
|
|
if (loop->fds[i].id == id)
|
|
return &loop->fds[i];
|
|
return NULL;
|
|
}
|
|
|
|
int
|
|
dc_loop_del_fd(DcLoop *loop, int id)
|
|
{
|
|
size_t i;
|
|
|
|
if (loop == NULL)
|
|
return -1;
|
|
for (i = 0; i < loop->nfds; i++) {
|
|
if (loop->fds[i].id == id) {
|
|
memmove(&loop->fds[i], &loop->fds[i + 1],
|
|
(loop->nfds - i - 1) * sizeof(*loop->fds));
|
|
loop->nfds--;
|
|
return 0;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int
|
|
dc_loop_add_timer(DcLoop *loop, long long delay_ms, DcLoopTimerFn fn,
|
|
void *arg)
|
|
{
|
|
struct DcTimerEntry *entry;
|
|
long long when;
|
|
size_t i;
|
|
int id;
|
|
|
|
if (loop == NULL || delay_ms < 0 || fn == NULL)
|
|
return -1;
|
|
if (ensure_array((void **)&loop->timers, &loop->captimers,
|
|
loop->ntimers + 1, sizeof(*loop->timers)) != 0)
|
|
return -1;
|
|
when = dc_loop_now_ms() + delay_ms;
|
|
/* equal deadlines keep their insertion order */
|
|
i = loop->ntimers;
|
|
while (i > 0 && loop->timers[i - 1].when > when)
|
|
i--;
|
|
memmove(&loop->timers[i + 1], &loop->timers[i],
|
|
(loop->ntimers - i) * sizeof(*loop->timers));
|
|
id = loop->next_id++;
|
|
entry = &loop->timers[i];
|
|
entry->id = id;
|
|
entry->when = when;
|
|
entry->fn = fn;
|
|
entry->arg = arg;
|
|
loop->ntimers++;
|
|
return id;
|
|
}
|
|
|
|
int
|
|
dc_loop_del_timer(DcLoop *loop, int id)
|
|
{
|
|
size_t i;
|
|
|
|
if (loop == NULL)
|
|
return -1;
|
|
for (i = 0; i < loop->ntimers; i++) {
|
|
if (loop->timers[i].id == id) {
|
|
memmove(&loop->timers[i], &loop->timers[i + 1],
|
|
(loop->ntimers - i - 1) * sizeof(*loop->timers));
|
|
loop->ntimers--;
|
|
return 0;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
static int
|
|
dispatch_timers(DcLoop *loop, long long now)
|
|
{
|
|
int n = 0;
|
|
|
|
while (loop->ntimers > 0 && loop->timers[0].when <= now) {
|
|
struct DcTimerEntry timer = loop->timers[0];
|
|
|
|
memmove(&loop->timers[0], &loop->timers[1],
|
|
(loop->ntimers - 1) * sizeof(*loop->timers));
|
|
loop->ntimers--;
|
|
timer.fn(timer.arg);
|
|
n++;
|
|
}
|
|
return n;
|
|
}
|
|
|
|
int
|
|
dc_loop_run(DcLoop *loop, int timeout_ms)
|
|
{
|
|
long long start, wait_until, now;
|
|
int fired = 0;
|
|
int poll_ms, rc;
|
|
size_t i, nready = 0;
|
|
|
|
if (loop == NULL)
|
|
return -1;
|
|
start = dc_loop_now_ms();
|
|
fired += dispatch_timers(loop, start);
|
|
|
|
wait_until = timeout_ms < 0 ? -1 : start + timeout_ms;
|
|
if (loop->ntimers > 0) {
|
|
long long t = loop->timers[0].when;
|
|
|
|
if (wait_until < 0 || t < wait_until)
|
|
wait_until = t;
|
|
}
|
|
if (wait_until < 0) {
|
|
poll_ms = -1;
|
|
} else {
|
|
now = dc_loop_now_ms();
|
|
if (wait_until <= now) {
|
|
poll_ms = 0;
|
|
} else {
|
|
long long d = wait_until - now;
|
|
|
|
poll_ms = d > 3600000 ? 3600000 : (int)d;
|
|
}
|
|
}
|
|
|
|
if (ensure_array((void **)&loop->pfds, &loop->pcap,
|
|
loop->nfds + 1, sizeof(*loop->pfds)) != 0 ||
|
|
ensure_array((void **)&loop->ready, &loop->rcap,
|
|
loop->nfds, sizeof(*loop->ready)) != 0)
|
|
return -1;
|
|
|
|
loop->pfds[0].fd = loop->wake_r;
|
|
loop->pfds[0].events = POLLIN;
|
|
loop->pfds[0].revents = 0;
|
|
for (i = 0; i < loop->nfds; i++) {
|
|
loop->pfds[i + 1].fd = loop->fds[i].fd;
|
|
loop->pfds[i + 1].events = loop->fds[i].events;
|
|
loop->pfds[i + 1].revents = 0;
|
|
}
|
|
rc = poll(loop->pfds, (nfds_t)(loop->nfds + 1), poll_ms);
|
|
if (rc < 0) {
|
|
if (errno == EINTR)
|
|
return fired;
|
|
return -1;
|
|
}
|
|
if (loop->pfds[0].revents & POLLIN) {
|
|
char buf[64];
|
|
|
|
while (read(loop->wake_r, buf, sizeof(buf)) > 0)
|
|
;
|
|
fired++;
|
|
}
|
|
|
|
/*
|
|
* Snapshot the ready fds before any callback runs: a callback may
|
|
* remove another fd or add a new one, and the array then moves.
|
|
*/
|
|
for (i = 0; i < loop->nfds && nready < loop->nfds; i++) {
|
|
if (loop->pfds[i + 1].revents != 0) {
|
|
loop->ready[nready].id = loop->fds[i].id;
|
|
loop->ready[nready].revents = loop->pfds[i + 1].revents;
|
|
nready++;
|
|
}
|
|
}
|
|
for (i = 0; i < nready; i++) {
|
|
struct DcFdEntry *entry = find_fd(loop, loop->ready[i].id);
|
|
|
|
if (entry != NULL) {
|
|
entry->fn(entry->fd, loop->ready[i].revents, entry->arg);
|
|
fired++;
|
|
}
|
|
}
|
|
|
|
fired += dispatch_timers(loop, dc_loop_now_ms());
|
|
return fired;
|
|
}
|