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 "event_notifier.h"
15 #include <sys/eventfd.h>
18 int event_notifier_init(EventNotifier *e, int active)
21 int fd = eventfd(!!active, EFD_NONBLOCK | EFD_CLOEXEC);
31 void event_notifier_cleanup(EventNotifier *e)
36 int event_notifier_get_fd(EventNotifier *e)
41 int event_notifier_test_and_clear(EventNotifier *e)
44 int r = read(e->fd, &value, sizeof(value));
45 return r == sizeof(value);
48 int event_notifier_test(EventNotifier *e)
51 int r = read(e->fd, &value, sizeof(value));
52 if (r == sizeof(value)) {
53 /* restore previous value. */
54 int s = write(e->fd, &value, sizeof(value));
55 /* never blocks because we use EFD_SEMAPHORE.
56 * If we didn't we'd get EAGAIN on overflow
57 * and we'd have to write code to ignore it. */
58 assert(s == sizeof(value));
60 return r == sizeof(value);