From 4056c09e13c1aeead6dd4085fc7e263a17a0b195 Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Sat, 13 Oct 2018 16:04:37 +1000 Subject: Move swaybar's event loop to common directory and refactor * The loop functions are now prefixed with `loop_`. * It is now easy to add timers to the loop. * Timers are implemented using pollfd and timerfd, rather than manually checking them when any other event happens to arrive. --- common/loop.c | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++ common/meson.build | 1 + 2 files changed, 106 insertions(+) create mode 100644 common/loop.c (limited to 'common') diff --git a/common/loop.c b/common/loop.c new file mode 100644 index 00000000..bfbfd5a6 --- /dev/null +++ b/common/loop.c @@ -0,0 +1,105 @@ +#include +#include +#include +#include +#include +#include +#include +#include "list.h" +#include "log.h" +#include "loop.h" + +struct loop_event { + void (*callback)(int fd, short mask, void *data); + void *data; +}; + +struct loop { + struct pollfd *fds; + int fd_length; + int fd_capacity; + + list_t *events; // struct loop_event +}; + +struct loop *loop_create(void) { + struct loop *loop = calloc(1, sizeof(struct loop)); + if (!loop) { + wlr_log(WLR_ERROR, "Unable to allocate memory for loop"); + return NULL; + } + loop->fd_capacity = 10; + loop->fds = malloc(sizeof(struct pollfd) * loop->fd_capacity); + loop->events = create_list(); + return loop; +} + +void loop_destroy(struct loop *loop) { + list_foreach(loop->events, free); + list_free(loop->events); + free(loop); +} + +void loop_poll(struct loop *loop) { + poll(loop->fds, loop->fd_length, -1); + + for (int i = 0; i < loop->fd_length; ++i) { + struct pollfd pfd = loop->fds[i]; + struct loop_event *event = loop->events->items[i]; + + // Always send these events + unsigned events = pfd.events | POLLHUP | POLLERR; + + if (pfd.revents & events) { + event->callback(pfd.fd, pfd.revents, event->data); + } + } +} + +struct loop_event *loop_add_fd(struct loop *loop, int fd, short mask, + void (*callback)(int fd, short mask, void *data), void *data) { + struct pollfd pfd = {fd, mask, 0}; + + if (loop->fd_length == loop->fd_capacity) { + loop->fd_capacity += 10; + loop->fds = realloc(loop->fds, sizeof(struct pollfd) * loop->fd_capacity); + } + + 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, + void (*callback)(int fd, short mask, void *data), void *data) { + int fd = timerfd_create(CLOCK_MONOTONIC, 0); + struct itimerspec its; + its.it_interval.tv_sec = 0; + its.it_interval.tv_nsec = 0; + its.it_value.tv_sec = ms / 1000000000; + its.it_value.tv_nsec = (ms * 1000000) % 1000000000; + timerfd_settime(fd, 0, &its, NULL); + + return loop_add_fd(loop, fd, POLLIN, callback, data); +} + +bool loop_remove_event(struct loop *loop, struct loop_event *event) { + for (int i = 0; i < loop->events->length; ++i) { + if (loop->events->items[i] == event) { + list_del(loop->events, i); + + loop->fd_length--; + memmove(&loop->fds[i], &loop->fds[i + 1], sizeof(void*) * (loop->fd_length - i)); + + free(event); + return true; + } + } + return false; +} diff --git a/common/meson.build b/common/meson.build index 44a29508..224a9c3f 100644 --- a/common/meson.build +++ b/common/meson.build @@ -5,6 +5,7 @@ lib_sway_common = static_library( 'cairo.c', 'ipc-client.c', 'log.c', + 'loop.c', 'list.c', 'pango.c', 'readline.c', -- cgit v1.2.3 From c242712262c5cb751fc0f89050a7f7a24433b105 Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Sat, 13 Oct 2018 16:52:13 +1000 Subject: swaylock: Remove indicator after 3 seconds --- common/loop.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'common') diff --git a/common/loop.c b/common/loop.c index bfbfd5a6..da3c2142 100644 --- a/common/loop.c +++ b/common/loop.c @@ -12,6 +12,7 @@ struct loop_event { void (*callback)(int fd, short mask, void *data); void *data; + bool is_timer; }; struct loop { @@ -52,6 +53,11 @@ void loop_poll(struct loop *loop) { if (pfd.revents & events) { event->callback(pfd.fd, pfd.revents, event->data); + + if (event->is_timer) { + loop_remove_event(loop, event); + --i; + } } } } @@ -82,11 +88,14 @@ struct loop_event *loop_add_timer(struct loop *loop, int ms, struct itimerspec its; its.it_interval.tv_sec = 0; its.it_interval.tv_nsec = 0; - its.it_value.tv_sec = ms / 1000000000; - its.it_value.tv_nsec = (ms * 1000000) % 1000000000; + its.it_value.tv_sec = ms / 1000; + its.it_value.tv_nsec = (ms % 1000) * 1000000; timerfd_settime(fd, 0, &its, NULL); - return loop_add_fd(loop, fd, POLLIN, callback, data); + struct loop_event *event = loop_add_fd(loop, fd, POLLIN, callback, data); + event->is_timer = true; + + return event; } bool loop_remove_event(struct loop *loop, struct loop_event *event) { @@ -94,6 +103,10 @@ bool loop_remove_event(struct loop *loop, struct loop_event *event) { if (loop->events->items[i] == event) { list_del(loop->events, i); + if (event->is_timer) { + close(loop->fds[i].fd); + } + loop->fd_length--; memmove(&loop->fds[i], &loop->fds[i + 1], sizeof(void*) * (loop->fd_length - i)); -- cgit v1.2.3 From 6921fdc6d6134fd7aaf38ffc1686623eca9bbd18 Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Sun, 14 Oct 2018 12:28:38 +1000 Subject: 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. --- common/loop.c | 143 ++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 100 insertions(+), 43 deletions(-) (limited to 'common') diff --git a/common/loop.c b/common/loop.c index da3c2142..c358e212 100644 --- a/common/loop.c +++ b/common/loop.c @@ -1,18 +1,24 @@ +#include #include #include #include #include #include -#include +#include #include #include "list.h" #include "log.h" #include "loop.h" -struct loop_event { +struct loop_fd_event { void (*callback)(int fd, short mask, void *data); void *data; - bool is_timer; +}; + +struct loop_timer { + void (*callback)(void *data); + void *data; + struct timespec expiry; }; struct loop { @@ -20,7 +26,8 @@ struct loop { int fd_length; 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) { @@ -31,86 +38,136 @@ struct loop *loop_create(void) { } loop->fd_capacity = 10; loop->fds = malloc(sizeof(struct pollfd) * loop->fd_capacity); - loop->events = create_list(); + loop->fd_events = create_list(); + loop->timers = create_list(); return loop; } void loop_destroy(struct loop *loop) { - list_foreach(loop->events, free); - list_free(loop->events); + list_foreach(loop->fd_events, free); + list_foreach(loop->timers, free); + list_free(loop->fd_events); + list_free(loop->timers); free(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) { 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 unsigned events = pfd.events | POLLHUP | POLLERR; if (pfd.revents & events) { event->callback(pfd.fd, pfd.revents, event->data); + } + } - if (event->is_timer) { - loop_remove_event(loop, event); + // Dispatch timers + 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; } } } } -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) { + 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}; if (loop->fd_length == loop->fd_capacity) { 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; +} - struct loop_event *event = calloc(1, sizeof(struct loop_event)); - event->callback = callback; - event->data = data; +struct loop_timer *loop_add_timer(struct loop *loop, int ms, + void (*callback)(void *data), void *data) { + struct loop_timer *timer = calloc(1, sizeof(struct loop_timer)); + if (!timer) { + wlr_log(WLR_ERROR, "Unable to allocate memory for timer"); + return NULL; + } + timer->callback = callback; + timer->data = data; - list_add(loop->events, event); + clock_gettime(CLOCK_MONOTONIC, &timer->expiry); + 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; -struct loop_event *loop_add_timer(struct loop *loop, int ms, - void (*callback)(int fd, short mask, void *data), void *data) { - int fd = timerfd_create(CLOCK_MONOTONIC, 0); - struct itimerspec its; - its.it_interval.tv_sec = 0; - its.it_interval.tv_nsec = 0; - its.it_value.tv_sec = ms / 1000; - its.it_value.tv_nsec = (ms % 1000) * 1000000; - timerfd_settime(fd, 0, &its, NULL); - - struct loop_event *event = loop_add_fd(loop, fd, POLLIN, callback, data); - event->is_timer = true; - - return event; -} + list_add(loop->timers, timer); -bool loop_remove_event(struct loop *loop, struct loop_event *event) { - for (int i = 0; i < loop->events->length; ++i) { - if (loop->events->items[i] == event) { - list_del(loop->events, i); + return timer; +} - if (event->is_timer) { - close(loop->fds[i].fd); - } +bool loop_remove_fd(struct loop *loop, int fd) { + for (int i = 0; i < loop->fd_length; ++i) { + if (loop->fds[i].fd == fd) { + free(loop->fd_events->items[i]); + list_del(loop->fd_events, i); 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)); + + return true; + } + } + return false; +} - free(event); +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; } } -- cgit v1.2.3 From 893f61d03a18b8e5dcb3893b2e3d164690a1f433 Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Sun, 14 Oct 2018 23:59:51 +1000 Subject: Event loop: Free fds and fix race condition --- common/loop.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'common') diff --git a/common/loop.c b/common/loop.c index c358e212..54b034d9 100644 --- a/common/loop.c +++ b/common/loop.c @@ -48,6 +48,7 @@ void loop_destroy(struct loop *loop) { list_foreach(loop->timers, free); list_free(loop->fd_events); list_free(loop->timers); + free(loop->fds); free(loop); } @@ -66,6 +67,9 @@ void loop_poll(struct loop *loop) { } } } + if (ms < 0) { + ms = 0; + } poll(loop->fds, loop->fd_length, ms); -- cgit v1.2.3 From c6f153d8f9ad6c961c8dd8e620dc2e8fcb20e7bc Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Mon, 15 Oct 2018 00:23:53 +1000 Subject: Event loop: Fix memmove and remove extraneous declaration --- common/loop.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'common') diff --git a/common/loop.c b/common/loop.c index 54b034d9..1b174967 100644 --- a/common/loop.c +++ b/common/loop.c @@ -159,7 +159,7 @@ bool loop_remove_fd(struct loop *loop, int fd) { loop->fd_length--; memmove(&loop->fds[i], &loop->fds[i + 1], - sizeof(void*) * (loop->fd_length - i)); + sizeof(struct pollfd) * (loop->fd_length - i)); return true; } -- cgit v1.2.3