2016-02-20 23:56:40 -08:00
|
|
|
#include <stdio.h>
|
2016-02-18 09:33:32 -08:00
|
|
|
#include <assert.h>
|
|
|
|
|
#include <unistd.h>
|
2016-02-21 13:57:35 -08:00
|
|
|
#include <errno.h>
|
2016-02-20 23:56:40 -08:00
|
|
|
#include <sys/epoll.h>
|
|
|
|
|
#include <pthread.h>
|
2016-02-18 09:33:32 -08:00
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
|
|
|
|
|
#include "wakeup.h"
|
|
|
|
|
|
2016-02-20 23:56:40 -08:00
|
|
|
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));
|
|
|
|
|
|
2016-02-21 13:57:35 -08:00
|
|
|
while (1) {
|
2016-02-20 23:56:40 -08:00
|
|
|
#define MAX_EVENTS 10
|
2016-02-21 13:57:35 -08:00
|
|
|
struct epoll_event events[MAX_EVENTS];
|
|
|
|
|
int nfds = epoll_wait(epoll_fd, events, MAX_EVENTS, -1);
|
|
|
|
|
if (nfds == -1 && errno == EINTR) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
assert(nfds >= 0);
|
|
|
|
|
break; // XXX
|
2016-02-20 23:56:40 -08:00
|
|
|
}
|
|
|
|
|
|
2016-02-21 13:57:35 -08:00
|
|
|
assert(!close(read_fd));
|
|
|
|
|
assert(!close(epoll_fd));
|
2016-02-20 23:56:40 -08:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-18 09:33:32 -08:00
|
|
|
void wakeup_init() {
|
|
|
|
|
int pipefd[2];
|
|
|
|
|
assert(!pipe(pipefd));
|
2016-02-20 23:56:40 -08:00
|
|
|
assert(!pthread_create(&wakeup_thread, NULL, wakeup_main, (void *) (intptr_t) pipefd[0]));
|
|
|
|
|
wakeup_write_fd = pipefd[1];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void wakeup_cleanup() {
|
2016-02-21 13:57:35 -08:00
|
|
|
assert(!close(wakeup_write_fd));
|
2016-02-20 23:56:40 -08:00
|
|
|
assert(!pthread_join(wakeup_thread, NULL));
|
2016-02-18 09:33:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void wakeup_add(struct peer *peer, int delay_ms) {
|
|
|
|
|
}
|