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,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) {