1 // SPDX-License-Identifier: LGPL-2.1+
2 // Copyright (C) 2022, Linaro Ltd - Daniel Lezcano <daniel.lezcano@linaro.org>
12 static sig_atomic_t exit_mainloop
;
14 struct mainloop_data
{
15 mainloop_callback_t cb
;
22 int mainloop(unsigned int timeout
)
25 struct epoll_event events
[MAX_EVENTS
];
26 struct mainloop_data
*md
;
33 nfds
= epoll_wait(epfd
, events
, MAX_EVENTS
, timeout
);
35 if (exit_mainloop
|| !nfds
)
44 for (i
= 0; i
< nfds
; i
++) {
45 md
= events
[i
].data
.ptr
;
47 if (md
->cb(md
->fd
, md
->data
) > 0)
53 int mainloop_add(int fd
, mainloop_callback_t cb
, void *data
)
55 struct epoll_event ev
= {
59 struct mainloop_data
*md
;
61 md
= malloc(sizeof(*md
));
71 if (epoll_ctl(epfd
, EPOLL_CTL_ADD
, fd
, &ev
) < 0) {
79 int mainloop_del(int fd
)
81 if (epoll_ctl(epfd
, EPOLL_CTL_DEL
, fd
, NULL
) < 0)
87 int mainloop_init(void)
89 epfd
= epoll_create(2);
96 void mainloop_exit(void)
101 void mainloop_fini(void)