1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <sys/epoll.h>
16#include "hw/dataplane/event-poll.h"
17
18
19void event_poll_add(EventPoll *poll, EventHandler *handler,
20 EventNotifier *notifier, EventCallback *callback)
21{
22 struct epoll_event event = {
23 .events = EPOLLIN,
24 .data.ptr = handler,
25 };
26 handler->notifier = notifier;
27 handler->callback = callback;
28 if (epoll_ctl(poll->epoll_fd, EPOLL_CTL_ADD,
29 event_notifier_get_fd(notifier), &event) != 0) {
30 fprintf(stderr, "failed to add event handler to epoll: %m\n");
31 exit(1);
32 }
33}
34
35
36static void handle_stop(EventHandler *handler)
37{
38
39}
40
41void event_poll_init(EventPoll *poll)
42{
43
44 poll->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
45 if (poll->epoll_fd < 0) {
46 fprintf(stderr, "epoll_create1 failed: %m\n");
47 exit(1);
48 }
49
50
51 if (event_notifier_init(&poll->stop_notifier, 0) < 0) {
52 fprintf(stderr, "failed to init stop notifier\n");
53 exit(1);
54 }
55 event_poll_add(poll, &poll->stop_handler,
56 &poll->stop_notifier, handle_stop);
57}
58
59void event_poll_cleanup(EventPoll *poll)
60{
61 event_notifier_cleanup(&poll->stop_notifier);
62 close(poll->epoll_fd);
63 poll->epoll_fd = -1;
64}
65
66
67void event_poll(EventPoll *poll)
68{
69 EventHandler *handler;
70 struct epoll_event event;
71 int nevents;
72
73
74
75 do {
76 nevents = epoll_wait(poll->epoll_fd, &event, 1, -1);
77 } while (nevents < 0 && errno == EINTR);
78 if (unlikely(nevents != 1)) {
79 fprintf(stderr, "epoll_wait failed: %m\n");
80 exit(1);
81 }
82
83
84 handler = event.data.ptr;
85
86
87 event_notifier_test_and_clear(handler->notifier);
88
89
90 handler->callback(handler);
91}
92
93
94
95
96
97void event_poll_notify(EventPoll *poll)
98{
99 event_notifier_set(&poll->stop_notifier);
100}
101