More structure for wakeup.
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -109,6 +109,8 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
peer_loop();
|
||||
|
||||
wakeup_cleanup();
|
||||
send_cleanup();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -3,4 +3,5 @@
|
||||
struct peer;
|
||||
|
||||
void wakeup_init();
|
||||
void wakeup_cleanup();
|
||||
void wakeup_add(struct peer *, int);
|
||||
|
||||
Reference in New Issue
Block a user