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-common.h"
15 #include "qemu/cutils.h"
16 #include "qemu/event_notifier.h"
17 #include "qemu/main-loop.h"
20 #include <sys/eventfd.h>
25 * Initialize @e with existing file descriptor @fd.
26 * @fd must be a genuine eventfd object, emulation with pipe won't do.
28 void event_notifier_init_fd(EventNotifier
*e
, int fd
)
32 e
->initialized
= true;
36 int event_notifier_init(EventNotifier
*e
, int active
)
42 ret
= eventfd(0, EFD_NONBLOCK
| EFD_CLOEXEC
);
48 e
->rfd
= e
->wfd
= ret
;
50 if (errno
!= ENOSYS
) {
53 if (qemu_pipe(fds
) < 0) {
56 ret
= fcntl_setfl(fds
[0], O_NONBLOCK
);
61 ret
= fcntl_setfl(fds
[1], O_NONBLOCK
);
69 e
->initialized
= true;
71 event_notifier_set(e
);
81 void event_notifier_cleanup(EventNotifier
*e
)
83 if (!e
->initialized
) {
87 if (e
->rfd
!= e
->wfd
) {
94 e
->initialized
= false;
97 int event_notifier_get_fd(const EventNotifier
*e
)
102 int event_notifier_set(EventNotifier
*e
)
104 static const uint64_t value
= 1;
107 if (!e
->initialized
) {
112 ret
= write(e
->wfd
, &value
, sizeof(value
));
113 } while (ret
< 0 && errno
== EINTR
);
115 /* EAGAIN is fine, a read must be pending. */
116 if (ret
< 0 && errno
!= EAGAIN
) {
122 int event_notifier_test_and_clear(EventNotifier
*e
)
128 if (!e
->initialized
) {
132 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
135 len
= read(e
->rfd
, buffer
, sizeof(buffer
));
137 } while ((len
== -1 && errno
== EINTR
) || len
== sizeof(buffer
));