blob: 0b829813d38bf6b5ece73e53a41961c12588b12f [file] [log] [blame]
Michael S. Tsirkin2292b332010-03-17 13:07:58 +02001/*
2 * event notifier support
3 *
4 * Copyright Red Hat, Inc. 2010
5 *
6 * Authors:
7 * Michael S. Tsirkin <mst@redhat.com>
8 *
Paolo Bonzini6b620ca2012-01-13 17:44:23 +01009 * 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.
Michael S. Tsirkin2292b332010-03-17 13:07:58 +020011 */
12
Michael S. Tsirkin2292b332010-03-17 13:07:58 +020013#include "event_notifier.h"
14#ifdef CONFIG_EVENTFD
15#include <sys/eventfd.h>
16#endif
17
18int event_notifier_init(EventNotifier *e, int active)
19{
20#ifdef CONFIG_EVENTFD
21 int fd = eventfd(!!active, EFD_NONBLOCK | EFD_CLOEXEC);
22 if (fd < 0)
23 return -errno;
24 e->fd = fd;
25 return 0;
26#else
27 return -ENOSYS;
28#endif
29}
30
31void event_notifier_cleanup(EventNotifier *e)
32{
33 close(e->fd);
34}
35
36int event_notifier_get_fd(EventNotifier *e)
37{
38 return e->fd;
39}
40
41int event_notifier_test_and_clear(EventNotifier *e)
42{
43 uint64_t value;
44 int r = read(e->fd, &value, sizeof(value));
45 return r == sizeof(value);
46}
47
48int event_notifier_test(EventNotifier *e)
49{
50 uint64_t value;
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));
59 }
60 return r == sizeof(value);
61}