44 #include <sys/signalfd.h>
45 #include <sys/timerfd.h>
46 #include <sys/epoll.h>
50 #define MAX_EPOLL_EVENTS 10
72 #define MAX_MAINLOOP_ENTRIES 128
105 epoll_fd = epoll_create1(EPOLL_CLOEXEC);
108 mainloop_list[i] = NULL;
150 struct signalfd_siginfo si;
153 if (events & (EPOLLERR | EPOLLHUP)) {
158 result = read(fd, &si,
sizeof(si));
159 if (result !=
sizeof(si))
179 if (sigprocmask(SIG_BLOCK, &signal_data->
mask, NULL) < 0)
182 signal_data->
fd = signalfd(-1, &signal_data->
mask,
183 SFD_NONBLOCK | SFD_CLOEXEC);
184 if (signal_data->
fd < 0)
189 close(signal_data->
fd);
197 struct epoll_event events[MAX_EPOLL_EVENTS];
200 nfds = epoll_wait(
epoll_fd, events, MAX_EPOLL_EVENTS, -1);
204 for (n = 0; n < nfds; n++) {
214 close(signal_data->
fd);
223 mainloop_list[i] = NULL;
226 epoll_ctl(
epoll_fd, EPOLL_CTL_DEL, data->
fd, NULL);
256 struct epoll_event ev;
262 data = malloc(
sizeof(*data));
266 memset(data, 0,
sizeof(*data));
273 memset(&ev, 0,
sizeof(ev));
277 err = epoll_ctl(
epoll_fd, EPOLL_CTL_ADD, data->
fd, &ev);
283 mainloop_list[fd] = data;
299 struct epoll_event ev;
305 data = mainloop_list[fd];
309 memset(&ev, 0,
sizeof(ev));
313 err = epoll_ctl(
epoll_fd, EPOLL_CTL_MOD, data->
fd, &ev);
330 data = mainloop_list[
fd];
334 mainloop_list[
fd] = NULL;
336 err = epoll_ctl(
epoll_fd, EPOLL_CTL_DEL, data->
fd, NULL);
363 if (events & (EPOLLERR | EPOLLHUP))
366 result = read(data->
fd, &expired,
sizeof(expired));
367 if (result !=
sizeof(expired))
376 struct itimerspec itimer;
377 unsigned int sec = msec / 1000;
379 memset(&itimer, 0,
sizeof(itimer));
380 itimer.it_interval.tv_sec = 0;
381 itimer.it_interval.tv_nsec = 0;
382 itimer.it_value.tv_sec = sec;
383 itimer.it_value.tv_nsec = (msec - (sec * 1000)) * 1000;
385 return timerfd_settime(fd, 0, &itimer, NULL);
396 data = malloc(
sizeof(*data));
400 memset(data, 0,
sizeof(*data));
405 data->
fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);
460 struct signal_data *data;
462 if (!mask || !callback)
465 data = malloc(
sizeof(*data));
469 memset(data, 0,
sizeof(*data));
475 memcpy(&data->
mask, mask,
sizeof(sigset_t));
mainloop_signal_func callback
#define MAX_MAINLOOP_ENTRIES
int mainloop_add_timeout(unsigned int msec, mainloop_timeout_func callback, void *user_data, mainloop_destroy_func destroy)
int mainloop_remove_timeout(int id)
void mainloop_exit_failure(void)
void(* mainloop_timeout_func)(int id, void *user_data)
static int timeout_set(int fd, unsigned int msec)
int mainloop_set_signal(sigset_t *mask, mainloop_signal_func callback, void *user_data, mainloop_destroy_func destroy)
int fd
socket or file descriptor, incl. standard i/o
int mainloop_add_fd(int fd, uint32_t events, mainloop_event_func callback, void *user_data, mainloop_destroy_func destroy)
static void timeout_callback(int fd, uint32_t events, void *user_data)
int mainloop_remove_fd(int fd)
mainloop_destroy_func destroy
data management call back function(void *user_data);
void(* mainloop_event_func)(int fd, uint32_t events, void *user_data)
mainloop_timeout_func callback
int mainloop_modify_fd(int fd, uint32_t events)
void(* mainloop_signal_func)(int signum, void *user_data)
uint32_t events
epoll event
static void signal_callback(int fd, uint32_t events, void *user_data)
int mainloop_modify_timeout(int id, unsigned int msec)
static struct signal_data * signal_data
mainloop file descriptor event data structure
mainloop_destroy_func destroy
static int epoll_terminate
static struct mainloop_data * mainloop_list[MAX_MAINLOOP_ENTRIES]
array of file descriptor event stub
mainloop_destroy_func destroy
mainloop_event_func callback
call back function(int fd, uint32_t events, void *user_data);
static void timeout_destroy(void *user_data)
void mainloop_exit_success(void)
void(* mainloop_destroy_func)(void *user_data)
void * user_data
pointer to a user specific data structure