More structure for wakeup.

This commit is contained in:
Ian Gulliver
2016-02-20 23:56:40 -08:00
parent 0e8dd4007c
commit 71413f97e5
5 changed files with 42 additions and 3 deletions

View File

@@ -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

View File

@@ -109,6 +109,8 @@ int main(int argc, char *argv[]) {
peer_loop();
wakeup_cleanup();
send_cleanup();
return EXIT_SUCCESS;
}

View File

@@ -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() {

View File

@@ -1,13 +1,49 @@
#include <stdio.h>
#include <assert.h>
#include <unistd.h>
#include <sys/epoll.h>
#include <pthread.h>
#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) {

View File

@@ -3,4 +3,5 @@
struct peer;
void wakeup_init();
void wakeup_cleanup();
void wakeup_add(struct peer *, int);