virtio-net: stop vhost backend on vmstop
[qemu.git] / hw / event_notifier.c
blob13f3656460035fb2f252724a0721f9653c66ce5e
1 /*
2 * event notifier support
4 * Copyright Red Hat, Inc. 2010
6 * Authors:
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.
13 #include "hw.h"
14 #include "event_notifier.h"
15 #ifdef CONFIG_EVENTFD
16 #include <sys/eventfd.h>
17 #endif
19 int event_notifier_init(EventNotifier *e, int active)
21 #ifdef CONFIG_EVENTFD
22 int fd = eventfd(!!active, EFD_NONBLOCK | EFD_CLOEXEC);
23 if (fd < 0)
24 return -errno;
25 e->fd = fd;
26 return 0;
27 #else
28 return -ENOSYS;
29 #endif
32 void event_notifier_cleanup(EventNotifier *e)
34 close(e->fd);
37 int event_notifier_get_fd(EventNotifier *e)
39 return e->fd;
42 int event_notifier_test_and_clear(EventNotifier *e)
44 uint64_t value;
45 int r = read(e->fd, &value, sizeof(value));
46 return r == sizeof(value);
49 int event_notifier_test(EventNotifier *e)
51 uint64_t value;
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);