mirror of
https://github.com/swaywm/sway.git
synced 2024-11-21 15:31:28 +00:00
Remove timerfd from loop implementation
timerfd doesn't work on the BSDs, so this replaces it with a timespec for the expiry and uses a poll timeout to check the timers when needed.
This commit is contained in:
parent
f98f351a52
commit
6921fdc6d6
143
common/loop.c
143
common/loop.c
|
@ -1,18 +1,24 @@
|
||||||
|
#include <limits.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <poll.h>
|
#include <poll.h>
|
||||||
#include <sys/timerfd.h>
|
#include <time.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include "list.h"
|
#include "list.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "loop.h"
|
#include "loop.h"
|
||||||
|
|
||||||
struct loop_event {
|
struct loop_fd_event {
|
||||||
void (*callback)(int fd, short mask, void *data);
|
void (*callback)(int fd, short mask, void *data);
|
||||||
void *data;
|
void *data;
|
||||||
bool is_timer;
|
};
|
||||||
|
|
||||||
|
struct loop_timer {
|
||||||
|
void (*callback)(void *data);
|
||||||
|
void *data;
|
||||||
|
struct timespec expiry;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct loop {
|
struct loop {
|
||||||
|
@ -20,7 +26,8 @@ struct loop {
|
||||||
int fd_length;
|
int fd_length;
|
||||||
int fd_capacity;
|
int fd_capacity;
|
||||||
|
|
||||||
list_t *events; // struct loop_event
|
list_t *fd_events; // struct loop_fd_event
|
||||||
|
list_t *timers; // struct loop_timer
|
||||||
};
|
};
|
||||||
|
|
||||||
struct loop *loop_create(void) {
|
struct loop *loop_create(void) {
|
||||||
|
@ -31,86 +38,136 @@ struct loop *loop_create(void) {
|
||||||
}
|
}
|
||||||
loop->fd_capacity = 10;
|
loop->fd_capacity = 10;
|
||||||
loop->fds = malloc(sizeof(struct pollfd) * loop->fd_capacity);
|
loop->fds = malloc(sizeof(struct pollfd) * loop->fd_capacity);
|
||||||
loop->events = create_list();
|
loop->fd_events = create_list();
|
||||||
|
loop->timers = create_list();
|
||||||
return loop;
|
return loop;
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop_destroy(struct loop *loop) {
|
void loop_destroy(struct loop *loop) {
|
||||||
list_foreach(loop->events, free);
|
list_foreach(loop->fd_events, free);
|
||||||
list_free(loop->events);
|
list_foreach(loop->timers, free);
|
||||||
|
list_free(loop->fd_events);
|
||||||
|
list_free(loop->timers);
|
||||||
free(loop);
|
free(loop);
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop_poll(struct loop *loop) {
|
void loop_poll(struct loop *loop) {
|
||||||
poll(loop->fds, loop->fd_length, -1);
|
// Calculate next timer in ms
|
||||||
|
int ms = INT_MAX;
|
||||||
|
if (loop->timers->length) {
|
||||||
|
struct timespec now;
|
||||||
|
clock_gettime(CLOCK_MONOTONIC, &now);
|
||||||
|
for (int i = 0; i < loop->timers->length; ++i) {
|
||||||
|
struct loop_timer *timer = loop->timers->items[i];
|
||||||
|
int timer_ms = (timer->expiry.tv_sec - now.tv_sec) * 1000;
|
||||||
|
timer_ms += (timer->expiry.tv_nsec - now.tv_nsec) / 1000000;
|
||||||
|
if (timer_ms < ms) {
|
||||||
|
ms = timer_ms;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
poll(loop->fds, loop->fd_length, ms);
|
||||||
|
|
||||||
|
// Dispatch fds
|
||||||
for (int i = 0; i < loop->fd_length; ++i) {
|
for (int i = 0; i < loop->fd_length; ++i) {
|
||||||
struct pollfd pfd = loop->fds[i];
|
struct pollfd pfd = loop->fds[i];
|
||||||
struct loop_event *event = loop->events->items[i];
|
struct loop_fd_event *event = loop->fd_events->items[i];
|
||||||
|
|
||||||
// Always send these events
|
// Always send these events
|
||||||
unsigned events = pfd.events | POLLHUP | POLLERR;
|
unsigned events = pfd.events | POLLHUP | POLLERR;
|
||||||
|
|
||||||
if (pfd.revents & events) {
|
if (pfd.revents & events) {
|
||||||
event->callback(pfd.fd, pfd.revents, event->data);
|
event->callback(pfd.fd, pfd.revents, event->data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (event->is_timer) {
|
// Dispatch timers
|
||||||
loop_remove_event(loop, event);
|
if (loop->timers->length) {
|
||||||
|
struct timespec now;
|
||||||
|
clock_gettime(CLOCK_MONOTONIC, &now);
|
||||||
|
for (int i = 0; i < loop->timers->length; ++i) {
|
||||||
|
struct loop_timer *timer = loop->timers->items[i];
|
||||||
|
bool expired = timer->expiry.tv_sec < now.tv_sec ||
|
||||||
|
(timer->expiry.tv_sec == now.tv_sec &&
|
||||||
|
timer->expiry.tv_nsec < now.tv_nsec);
|
||||||
|
if (expired) {
|
||||||
|
timer->callback(timer->data);
|
||||||
|
loop_remove_timer(loop, timer);
|
||||||
--i;
|
--i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct loop_event *loop_add_fd(struct loop *loop, int fd, short mask,
|
void loop_add_fd(struct loop *loop, int fd, short mask,
|
||||||
void (*callback)(int fd, short mask, void *data), void *data) {
|
void (*callback)(int fd, short mask, void *data), void *data) {
|
||||||
|
struct loop_fd_event *event = calloc(1, sizeof(struct loop_fd_event));
|
||||||
|
if (!event) {
|
||||||
|
wlr_log(WLR_ERROR, "Unable to allocate memory for event");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
event->callback = callback;
|
||||||
|
event->data = data;
|
||||||
|
list_add(loop->fd_events, event);
|
||||||
|
|
||||||
struct pollfd pfd = {fd, mask, 0};
|
struct pollfd pfd = {fd, mask, 0};
|
||||||
|
|
||||||
if (loop->fd_length == loop->fd_capacity) {
|
if (loop->fd_length == loop->fd_capacity) {
|
||||||
loop->fd_capacity += 10;
|
loop->fd_capacity += 10;
|
||||||
loop->fds = realloc(loop->fds, sizeof(struct pollfd) * loop->fd_capacity);
|
loop->fds = realloc(loop->fds,
|
||||||
|
sizeof(struct pollfd) * loop->fd_capacity);
|
||||||
}
|
}
|
||||||
|
|
||||||
loop->fds[loop->fd_length++] = pfd;
|
loop->fds[loop->fd_length++] = pfd;
|
||||||
|
|
||||||
struct loop_event *event = calloc(1, sizeof(struct loop_event));
|
|
||||||
event->callback = callback;
|
|
||||||
event->data = data;
|
|
||||||
|
|
||||||
list_add(loop->events, event);
|
|
||||||
|
|
||||||
return event;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct loop_event *loop_add_timer(struct loop *loop, int ms,
|
struct loop_timer *loop_add_timer(struct loop *loop, int ms,
|
||||||
void (*callback)(int fd, short mask, void *data), void *data) {
|
void (*callback)(void *data), void *data) {
|
||||||
int fd = timerfd_create(CLOCK_MONOTONIC, 0);
|
struct loop_timer *timer = calloc(1, sizeof(struct loop_timer));
|
||||||
struct itimerspec its;
|
if (!timer) {
|
||||||
its.it_interval.tv_sec = 0;
|
wlr_log(WLR_ERROR, "Unable to allocate memory for timer");
|
||||||
its.it_interval.tv_nsec = 0;
|
return NULL;
|
||||||
its.it_value.tv_sec = ms / 1000;
|
}
|
||||||
its.it_value.tv_nsec = (ms % 1000) * 1000000;
|
timer->callback = callback;
|
||||||
timerfd_settime(fd, 0, &its, NULL);
|
timer->data = data;
|
||||||
|
|
||||||
struct loop_event *event = loop_add_fd(loop, fd, POLLIN, callback, data);
|
clock_gettime(CLOCK_MONOTONIC, &timer->expiry);
|
||||||
event->is_timer = true;
|
timer->expiry.tv_sec += ms / 1000;
|
||||||
|
|
||||||
return event;
|
long int nsec = (ms % 1000) * 1000000;
|
||||||
|
if (timer->expiry.tv_nsec + nsec >= 1000000000) {
|
||||||
|
timer->expiry.tv_sec++;
|
||||||
|
nsec -= 1000000000;
|
||||||
|
}
|
||||||
|
timer->expiry.tv_nsec += nsec;
|
||||||
|
|
||||||
|
list_add(loop->timers, timer);
|
||||||
|
|
||||||
|
return timer;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool loop_remove_event(struct loop *loop, struct loop_event *event) {
|
bool loop_remove_fd(struct loop *loop, int fd) {
|
||||||
for (int i = 0; i < loop->events->length; ++i) {
|
for (int i = 0; i < loop->fd_length; ++i) {
|
||||||
if (loop->events->items[i] == event) {
|
if (loop->fds[i].fd == fd) {
|
||||||
list_del(loop->events, i);
|
free(loop->fd_events->items[i]);
|
||||||
|
list_del(loop->fd_events, i);
|
||||||
if (event->is_timer) {
|
|
||||||
close(loop->fds[i].fd);
|
|
||||||
}
|
|
||||||
|
|
||||||
loop->fd_length--;
|
loop->fd_length--;
|
||||||
memmove(&loop->fds[i], &loop->fds[i + 1], sizeof(void*) * (loop->fd_length - i));
|
memmove(&loop->fds[i], &loop->fds[i + 1],
|
||||||
|
sizeof(void*) * (loop->fd_length - i));
|
||||||
|
|
||||||
free(event);
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool loop_remove_timer(struct loop *loop, struct loop_timer *timer) {
|
||||||
|
for (int i = 0; i < loop->timers->length; ++i) {
|
||||||
|
if (loop->timers->items[i] == timer) {
|
||||||
|
list_del(loop->timers, i);
|
||||||
|
free(timer);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,12 +5,12 @@
|
||||||
/**
|
/**
|
||||||
* This is an event loop system designed for sway clients, not sway itself.
|
* This is an event loop system designed for sway clients, not sway itself.
|
||||||
*
|
*
|
||||||
* It uses pollfds to block on multiple file descriptors at once, and provides
|
* The loop consists of file descriptors and timers. Typically the Wayland
|
||||||
* an easy way to set timers. Typically the Wayland display's fd will be one of
|
* display's file descriptor will be one of the fds in the loop.
|
||||||
* the fds in the loop.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
struct loop;
|
struct loop;
|
||||||
|
struct loop_timer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create an event loop.
|
* Create an event loop.
|
||||||
|
@ -28,20 +28,27 @@ void loop_destroy(struct loop *loop);
|
||||||
void loop_poll(struct loop *loop);
|
void loop_poll(struct loop *loop);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add an fd to the loop.
|
* Add a file descriptor to the loop.
|
||||||
*/
|
*/
|
||||||
struct loop_event *loop_add_fd(struct loop *loop, int fd, short mask,
|
void loop_add_fd(struct loop *loop, int fd, short mask,
|
||||||
void (*func)(int fd, short mask, void *data), void *data);
|
void (*func)(int fd, short mask, void *data), void *data);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a timer to the loop.
|
* Add a timer to the loop.
|
||||||
|
*
|
||||||
|
* When the timer expires, the timer will be removed from the loop and freed.
|
||||||
*/
|
*/
|
||||||
struct loop_event *loop_add_timer(struct loop *loop, int ms,
|
struct loop_timer *loop_add_timer(struct loop *loop, int ms,
|
||||||
void (*callback)(int fd, short mask, void *data), void *data);
|
void (*callback)(void *data), void *data);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove an event from the loop.
|
* Remove a file descriptor from the loop.
|
||||||
*/
|
*/
|
||||||
bool loop_remove_event(struct loop *loop, struct loop_event *event);
|
bool loop_remove_fd(struct loop *loop, int fd);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove a timer from the loop.
|
||||||
|
*/
|
||||||
|
bool loop_remove_timer(struct loop *loop, struct loop_timer *timer);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -69,7 +69,6 @@ struct swaybar {
|
||||||
struct status_line *status;
|
struct status_line *status;
|
||||||
|
|
||||||
struct loop *eventloop;
|
struct loop *eventloop;
|
||||||
struct loop_event *status_event;
|
|
||||||
|
|
||||||
int ipc_event_socketfd;
|
int ipc_event_socketfd;
|
||||||
int ipc_socketfd;
|
int ipc_socketfd;
|
||||||
|
|
|
@ -55,9 +55,9 @@ struct swaylock_password {
|
||||||
|
|
||||||
struct swaylock_state {
|
struct swaylock_state {
|
||||||
struct loop *eventloop;
|
struct loop *eventloop;
|
||||||
struct loop_event *clear_indicator_timer; // clears the indicator
|
struct loop_timer *clear_indicator_timer; // clears the indicator
|
||||||
struct loop_event *clear_password_timer; // clears the password buffer
|
struct loop_timer *clear_password_timer; // clears the password buffer
|
||||||
struct loop_event *verify_password_timer;
|
struct loop_timer *verify_password_timer;
|
||||||
struct wl_display *display;
|
struct wl_display *display;
|
||||||
struct wl_compositor *compositor;
|
struct wl_compositor *compositor;
|
||||||
struct zwlr_layer_shell_v1 *layer_shell;
|
struct zwlr_layer_shell_v1 *layer_shell;
|
||||||
|
|
|
@ -647,7 +647,7 @@ static void status_in(int fd, short mask, void *data) {
|
||||||
if (mask & (POLLHUP | POLLERR)) {
|
if (mask & (POLLHUP | POLLERR)) {
|
||||||
status_error(bar->status, "[error reading from status command]");
|
status_error(bar->status, "[error reading from status command]");
|
||||||
set_bar_dirty(bar);
|
set_bar_dirty(bar);
|
||||||
loop_remove_event(bar->eventloop, bar->status_event);
|
loop_remove_fd(bar->eventloop, fd);
|
||||||
} else if (status_handle_readable(bar->status)) {
|
} else if (status_handle_readable(bar->status)) {
|
||||||
set_bar_dirty(bar);
|
set_bar_dirty(bar);
|
||||||
}
|
}
|
||||||
|
@ -658,8 +658,8 @@ void bar_run(struct swaybar *bar) {
|
||||||
display_in, bar);
|
display_in, bar);
|
||||||
loop_add_fd(bar->eventloop, bar->ipc_event_socketfd, POLLIN, ipc_in, bar);
|
loop_add_fd(bar->eventloop, bar->ipc_event_socketfd, POLLIN, ipc_in, bar);
|
||||||
if (bar->status) {
|
if (bar->status) {
|
||||||
bar->status_event = loop_add_fd(
|
loop_add_fd(bar->eventloop, bar->status->read_fd, POLLIN,
|
||||||
bar->eventloop, bar->status->read_fd, POLLIN, status_in, bar);
|
status_in, bar);
|
||||||
}
|
}
|
||||||
while (1) {
|
while (1) {
|
||||||
wl_display_flush(bar->display);
|
wl_display_flush(bar->display);
|
||||||
|
|
|
@ -16,7 +16,7 @@
|
||||||
|
|
||||||
static void status_line_close_fds(struct status_line *status) {
|
static void status_line_close_fds(struct status_line *status) {
|
||||||
if (status->read_fd != -1) {
|
if (status->read_fd != -1) {
|
||||||
loop_remove_event(status->bar->eventloop, status->bar->status_event);
|
loop_remove_fd(status->bar->eventloop, status->read_fd);
|
||||||
close(status->read_fd);
|
close(status->read_fd);
|
||||||
status->read_fd = -1;
|
status->read_fd = -1;
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,7 +40,7 @@ static void append_ch(struct swaylock_password *pw, uint32_t codepoint) {
|
||||||
pw->len += utf8_size;
|
pw->len += utf8_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void clear_indicator(int fd, short mask, void *data) {
|
static void clear_indicator(void *data) {
|
||||||
struct swaylock_state *state = data;
|
struct swaylock_state *state = data;
|
||||||
state->clear_indicator_timer = NULL;
|
state->clear_indicator_timer = NULL;
|
||||||
state->auth_state = AUTH_STATE_IDLE;
|
state->auth_state = AUTH_STATE_IDLE;
|
||||||
|
@ -49,13 +49,13 @@ static void clear_indicator(int fd, short mask, void *data) {
|
||||||
|
|
||||||
static void schedule_indicator_clear(struct swaylock_state *state) {
|
static void schedule_indicator_clear(struct swaylock_state *state) {
|
||||||
if (state->clear_indicator_timer) {
|
if (state->clear_indicator_timer) {
|
||||||
loop_remove_event(state->eventloop, state->clear_indicator_timer);
|
loop_remove_timer(state->eventloop, state->clear_indicator_timer);
|
||||||
}
|
}
|
||||||
state->clear_indicator_timer = loop_add_timer(
|
state->clear_indicator_timer = loop_add_timer(
|
||||||
state->eventloop, 3000, clear_indicator, state);
|
state->eventloop, 3000, clear_indicator, state);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void clear_password(int fd, short mask, void *data) {
|
static void clear_password(void *data) {
|
||||||
struct swaylock_state *state = data;
|
struct swaylock_state *state = data;
|
||||||
state->clear_password_timer = NULL;
|
state->clear_password_timer = NULL;
|
||||||
state->auth_state = AUTH_STATE_CLEAR;
|
state->auth_state = AUTH_STATE_CLEAR;
|
||||||
|
@ -66,13 +66,13 @@ static void clear_password(int fd, short mask, void *data) {
|
||||||
|
|
||||||
static void schedule_password_clear(struct swaylock_state *state) {
|
static void schedule_password_clear(struct swaylock_state *state) {
|
||||||
if (state->clear_password_timer) {
|
if (state->clear_password_timer) {
|
||||||
loop_remove_event(state->eventloop, state->clear_password_timer);
|
loop_remove_timer(state->eventloop, state->clear_password_timer);
|
||||||
}
|
}
|
||||||
state->clear_password_timer = loop_add_timer(
|
state->clear_password_timer = loop_add_timer(
|
||||||
state->eventloop, 10000, clear_password, state);
|
state->eventloop, 10000, clear_password, state);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void handle_preverify_timeout(int fd, short mask, void *data) {
|
static void handle_preverify_timeout(void *data) {
|
||||||
struct swaylock_state *state = data;
|
struct swaylock_state *state = data;
|
||||||
state->verify_password_timer = NULL;
|
state->verify_password_timer = NULL;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue