2 * event notifier support
4 * Copyright Red Hat, Inc. 2010
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/event_notifier.h"
16 #include "sysemu/char.h"
17 #include "qemu/main-loop.h"
20 #include <sys/eventfd.h>
23 void event_notifier_init_fd(EventNotifier *e, int fd)
29 int event_notifier_init(EventNotifier *e, int active)
35 ret = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
41 e->rfd = e->wfd = ret;
43 if (errno != ENOSYS) {
46 if (qemu_pipe(fds) < 0) {
49 ret = fcntl_setfl(fds[0], O_NONBLOCK);
54 ret = fcntl_setfl(fds[1], O_NONBLOCK);
63 event_notifier_set(e);
73 void event_notifier_cleanup(EventNotifier *e)
75 if (e->rfd != e->wfd) {
81 int event_notifier_get_fd(const EventNotifier *e)
86 int event_notifier_set_handler(EventNotifier *e,
87 EventNotifierHandler *handler)
89 qemu_set_fd_handler(e->rfd, (IOHandler *)handler, NULL, e);
93 int event_notifier_set(EventNotifier *e)
95 static const uint64_t value = 1;
99 ret = write(e->wfd, &value, sizeof(value));
100 } while (ret < 0 && errno == EINTR);
102 /* EAGAIN is fine, a read must be pending. */
103 if (ret < 0 && errno != EAGAIN) {
109 int event_notifier_test_and_clear(EventNotifier *e)
115 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
118 len = read(e->rfd, buffer, sizeof(buffer));
120 } while ((len == -1 && errno == EINTR) || len == sizeof(buffer));