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. See
10 * the COPYING file in the top-level directory.
14 #include "event_notifier.h"
16 #include <sys/eventfd.h>
19 int event_notifier_init(EventNotifier
*e
, int active
)
22 int fd
= eventfd(!!active
, EFD_NONBLOCK
| EFD_CLOEXEC
);
32 void event_notifier_cleanup(EventNotifier
*e
)
37 int event_notifier_get_fd(EventNotifier
*e
)
42 int event_notifier_test_and_clear(EventNotifier
*e
)
45 int r
= read(e
->fd
, &value
, sizeof(value
));
46 return r
== sizeof(value
);
49 int event_notifier_test(EventNotifier
*e
)
52 int r
= read(e
->fd
, &value
, sizeof(value
));
53 if (r
== sizeof(value
)) {
54 /* restore previous value. */
55 int s
= write(e
->fd
, &value
, sizeof(value
));
56 /* never blocks because we use EFD_SEMAPHORE.
57 * If we didn't we'd get EAGAIN on overflow
58 * and we'd have to write code to ignore it. */
59 assert(s
== sizeof(value
));
61 return r
== sizeof(value
);