From 71413f97e5867e45b47b44f704ac51bf6fbdc2b8 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Sat, 20 Feb 2016 23:56:40 -0800 Subject: [PATCH] More structure for wakeup. --- adsbus/Makefile | 2 +- adsbus/adsbus.c | 2 ++ adsbus/common.c | 4 ++-- adsbus/wakeup.c | 36 ++++++++++++++++++++++++++++++++++++ adsbus/wakeup.h | 1 + 5 files changed, 42 insertions(+), 3 deletions(-) diff --git a/adsbus/Makefile b/adsbus/Makefile index 9b9e872..cb4fc94 100644 --- a/adsbus/Makefile +++ b/adsbus/Makefile @@ -1,5 +1,5 @@ CC ?= clang -CFLAGS ?= -Wall -Werror -O4 -g --std=gnu11 --pedantic-errors -fPIE -pie -fstack-protector-strong +CFLAGS ?= -Wall -Werror -O4 -g --std=gnu11 --pedantic-errors -fPIE -pie -fstack-protector-strong -pthread LDFLAGS ?= $(CFLAGS) -Wl,-z,relro -Wl,-z,now LIBS ?= -luuid -ljansson diff --git a/adsbus/adsbus.c b/adsbus/adsbus.c index ed981b5..581aa97 100644 --- a/adsbus/adsbus.c +++ b/adsbus/adsbus.c @@ -109,6 +109,8 @@ int main(int argc, char *argv[]) { peer_loop(); + wakeup_cleanup(); send_cleanup(); + return EXIT_SUCCESS; } diff --git a/adsbus/common.c b/adsbus/common.c index 164c366..a3f893c 100644 --- a/adsbus/common.c +++ b/adsbus/common.c @@ -30,11 +30,11 @@ void peer_epoll_add(struct peer *peer, uint32_t events) { .ptr = peer, }, }; - assert(epoll_ctl(epoll_fd, EPOLL_CTL_ADD, peer->fd, &ev) == 0); + assert(!epoll_ctl(epoll_fd, EPOLL_CTL_ADD, peer->fd, &ev)); } void peer_epoll_del(struct peer *peer) { - assert(epoll_ctl(epoll_fd, EPOLL_CTL_DEL, peer->fd, NULL) == 0); + assert(!epoll_ctl(epoll_fd, EPOLL_CTL_DEL, peer->fd, NULL)); } void peer_loop() { diff --git a/adsbus/wakeup.c b/adsbus/wakeup.c index 35f517d..8cf21e7 100644 --- a/adsbus/wakeup.c +++ b/adsbus/wakeup.c @@ -1,13 +1,49 @@ +#include #include #include +#include +#include #include "common.h" #include "wakeup.h" +static pthread_t wakeup_thread; +static int wakeup_write_fd; + +static void *wakeup_main(void *arg) { + int read_fd = (intptr_t) arg; + + int epoll_fd = epoll_create1(0); + assert(epoll_fd >= 0); + + struct epoll_event ev = { + .events = EPOLLIN, + }; + assert(!epoll_ctl(epoll_fd, EPOLL_CTL_ADD, read_fd, &ev)); + +#define MAX_EVENTS 10 + struct epoll_event events[MAX_EVENTS]; + int nfds = epoll_wait(epoll_fd, events, MAX_EVENTS, -1); + if (nfds < 0) { + perror("epoll_wait"); + } + + close(read_fd); + close(epoll_fd); + return NULL; +} + void wakeup_init() { int pipefd[2]; assert(!pipe(pipefd)); + assert(!pthread_create(&wakeup_thread, NULL, wakeup_main, (void *) (intptr_t) pipefd[0])); + wakeup_write_fd = pipefd[1]; +} + +void wakeup_cleanup() { + close(wakeup_write_fd); + assert(!pthread_join(wakeup_thread, NULL)); } void wakeup_add(struct peer *peer, int delay_ms) { diff --git a/adsbus/wakeup.h b/adsbus/wakeup.h index 036bca7..793d0f4 100644 --- a/adsbus/wakeup.h +++ b/adsbus/wakeup.h @@ -3,4 +3,5 @@ struct peer; void wakeup_init(); +void wakeup_cleanup(); void wakeup_add(struct peer *, int);