2 * event notifier support
4 * Copyright Red Hat, Inc. 2010
7 * Michael S. Tsirkin <mst@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
13 #include "qemu/osdep.h"
14 #include "qemu/cutils.h"
15 #include "qemu/event_notifier.h"
16 #include "qemu/main-loop.h"
19 #include <sys/eventfd.h>
24 * Initialize @e with existing file descriptor @fd.
25 * @fd must be a genuine eventfd object, emulation with pipe won't do.
27 void event_notifier_init_fd(EventNotifier
*e
, int fd
)
31 e
->initialized
= true;
35 int event_notifier_init(EventNotifier
*e
, int active
)
41 ret
= eventfd(0, EFD_NONBLOCK
| EFD_CLOEXEC
);
47 e
->rfd
= e
->wfd
= ret
;
49 if (errno
!= ENOSYS
) {
52 if (qemu_pipe(fds
) < 0) {
55 ret
= fcntl_setfl(fds
[0], O_NONBLOCK
);
60 ret
= fcntl_setfl(fds
[1], O_NONBLOCK
);
68 e
->initialized
= true;
70 event_notifier_set(e
);
80 void event_notifier_cleanup(EventNotifier
*e
)
82 if (!e
->initialized
) {
86 if (e
->rfd
!= e
->wfd
) {
93 e
->initialized
= false;
96 int event_notifier_get_fd(const EventNotifier
*e
)
101 int event_notifier_get_wfd(const EventNotifier
*e
)
106 int event_notifier_set(EventNotifier
*e
)
108 static const uint64_t value
= 1;
111 if (!e
->initialized
) {
116 ret
= write(e
->wfd
, &value
, sizeof(value
));
117 } while (ret
< 0 && errno
== EINTR
);
119 /* EAGAIN is fine, a read must be pending. */
120 if (ret
< 0 && errno
!= EAGAIN
) {
126 int event_notifier_test_and_clear(EventNotifier
*e
)
132 if (!e
->initialized
) {
136 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
139 len
= read(e
->rfd
, buffer
, sizeof(buffer
));
141 } while ((len
== -1 && errno
== EINTR
) || len
== sizeof(buffer
));