1 // SPDX-License-Identifier: LGPL-2.1+
2 // Copyright (C) 2022, Linaro Ltd - Daniel Lezcano <daniel.lezcano@linaro.org>
12 static unsigned short nrhandler;
13 static sig_atomic_t exit_mainloop;
15 struct mainloop_data {
16 mainloop_callback_t cb;
21 static struct mainloop_data **mds;
25 int mainloop(unsigned int timeout)
28 struct epoll_event events[MAX_EVENTS];
29 struct mainloop_data *md;
36 nfds = epoll_wait(epfd, events, MAX_EVENTS, timeout);
38 if (exit_mainloop || !nfds)
47 for (i = 0; i < nfds; i++) {
48 md = events[i].data.ptr;
50 if (md->cb(md->fd, md->data) > 0)
56 int mainloop_add(int fd, mainloop_callback_t cb, void *data)
58 struct epoll_event ev = {
62 struct mainloop_data *md;
64 if (fd >= nrhandler) {
65 mds = realloc(mds, sizeof(*mds) * (fd + 1));
71 md = malloc(sizeof(*md));
82 if (epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev) < 0) {
90 int mainloop_del(int fd)
95 if (epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL) < 0)
103 int mainloop_init(void)
105 epfd = epoll_create(2);
112 void mainloop_exit(void)
117 void mainloop_fini(void)