]> git.proxmox.com Git - mirror_qemu.git/blame - util/event_notifier-posix.c
hw/arm/aspeed: Set default CPU count using aspeed_soc_num_cpus()
[mirror_qemu.git] / util / event_notifier-posix.c
CommitLineData
2292b339
MT
1/*
2 * event notifier support
3 *
4 * Copyright Red Hat, Inc. 2010
5 *
6 * Authors:
7 * Michael S. Tsirkin <mst@redhat.com>
8 *
6b620ca3
PB
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.
2292b339
MT
11 */
12
aafd7584 13#include "qemu/osdep.h"
f348b6d1 14#include "qemu/cutils.h"
1de7afc9 15#include "qemu/event_notifier.h"
1de7afc9 16#include "qemu/main-loop.h"
e80c262b 17
2292b339
MT
18#ifdef CONFIG_EVENTFD
19#include <sys/eventfd.h>
20#endif
21
330b5836
MA
22#ifdef CONFIG_EVENTFD
23/*
24 * Initialize @e with existing file descriptor @fd.
25 * @fd must be a genuine eventfd object, emulation with pipe won't do.
26 */
e80c262b
PB
27void event_notifier_init_fd(EventNotifier *e, int fd)
28{
d0cc2fbf
PB
29 e->rfd = fd;
30 e->wfd = fd;
e34e47eb 31 e->initialized = true;
e80c262b 32}
330b5836 33#endif
e80c262b 34
2292b339
MT
35int event_notifier_init(EventNotifier *e, int active)
36{
d0cc2fbf
PB
37 int fds[2];
38 int ret;
39
2292b339 40#ifdef CONFIG_EVENTFD
d0cc2fbf 41 ret = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
2292b339 42#else
d0cc2fbf
PB
43 ret = -1;
44 errno = ENOSYS;
2292b339 45#endif
d0cc2fbf
PB
46 if (ret >= 0) {
47 e->rfd = e->wfd = ret;
48 } else {
49 if (errno != ENOSYS) {
50 return -errno;
51 }
a7241974 52 if (!g_unix_open_pipe(fds, FD_CLOEXEC, NULL)) {
d0cc2fbf
PB
53 return -errno;
54 }
4d14cb0c 55 if (!g_unix_set_fd_nonblocking(fds[0], true, NULL)) {
d0cc2fbf
PB
56 ret = -errno;
57 goto fail;
58 }
4d14cb0c 59 if (!g_unix_set_fd_nonblocking(fds[1], true, NULL)) {
d0cc2fbf
PB
60 ret = -errno;
61 goto fail;
62 }
63 e->rfd = fds[0];
64 e->wfd = fds[1];
65 }
82e27568 66 e->initialized = true;
d0cc2fbf
PB
67 if (active) {
68 event_notifier_set(e);
69 }
70 return 0;
71
72fail:
73 close(fds[0]);
74 close(fds[1]);
75 return ret;
2292b339
MT
76}
77
78void event_notifier_cleanup(EventNotifier *e)
79{
e34e47eb
ML
80 if (!e->initialized) {
81 return;
82 }
83
d0cc2fbf
PB
84 if (e->rfd != e->wfd) {
85 close(e->rfd);
86 }
e34e47eb 87
105e1023 88 e->rfd = -1;
d0cc2fbf 89 close(e->wfd);
aa262928 90 e->wfd = -1;
e34e47eb 91 e->initialized = false;
2292b339
MT
92}
93
12f0b68c 94int event_notifier_get_fd(const EventNotifier *e)
2292b339 95{
d0cc2fbf 96 return e->rfd;
2292b339
MT
97}
98
3bcf0fb3
SL
99int event_notifier_get_wfd(const EventNotifier *e)
100{
101 return e->wfd;
102}
103
2ec10b95
PB
104int event_notifier_set(EventNotifier *e)
105{
d0cc2fbf
PB
106 static const uint64_t value = 1;
107 ssize_t ret;
108
e34e47eb
ML
109 if (!e->initialized) {
110 return -1;
111 }
112
d0cc2fbf
PB
113 do {
114 ret = write(e->wfd, &value, sizeof(value));
115 } while (ret < 0 && errno == EINTR);
116
117 /* EAGAIN is fine, a read must be pending. */
118 if (ret < 0 && errno != EAGAIN) {
119 return -errno;
120 }
121 return 0;
2ec10b95
PB
122}
123
2292b339
MT
124int event_notifier_test_and_clear(EventNotifier *e)
125{
d0cc2fbf
PB
126 int value;
127 ssize_t len;
128 char buffer[512];
129
e34e47eb
ML
130 if (!e->initialized) {
131 return 0;
132 }
133
d0cc2fbf
PB
134 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
135 value = 0;
136 do {
137 len = read(e->rfd, buffer, sizeof(buffer));
138 value |= (len > 0);
139 } while ((len == -1 && errno == EINTR) || len == sizeof(buffer));
140
141 return value;
2292b339 142}