Revision 4c8d0d27
b/main-loop.c | ||
---|---|---|
32 | 32 |
|
33 | 33 |
#include "compatfd.h" |
34 | 34 |
|
35 |
static int io_thread_fd = -1; |
|
36 |
|
|
37 |
void qemu_notify_event(void) |
|
38 |
{ |
|
39 |
/* Write 8 bytes to be compatible with eventfd. */ |
|
40 |
static const uint64_t val = 1; |
|
41 |
ssize_t ret; |
|
42 |
|
|
43 |
if (io_thread_fd == -1) { |
|
44 |
return; |
|
45 |
} |
|
46 |
do { |
|
47 |
ret = write(io_thread_fd, &val, sizeof(val)); |
|
48 |
} while (ret < 0 && errno == EINTR); |
|
49 |
|
|
50 |
/* EAGAIN is fine, a read must be pending. */ |
|
51 |
if (ret < 0 && errno != EAGAIN) { |
|
52 |
fprintf(stderr, "qemu_notify_event: write() failed: %s\n", |
|
53 |
strerror(errno)); |
|
54 |
exit(1); |
|
55 |
} |
|
56 |
} |
|
57 |
|
|
58 |
static void qemu_event_read(void *opaque) |
|
59 |
{ |
|
60 |
int fd = (intptr_t)opaque; |
|
61 |
ssize_t len; |
|
62 |
char buffer[512]; |
|
63 |
|
|
64 |
/* Drain the notify pipe. For eventfd, only 8 bytes will be read. */ |
|
65 |
do { |
|
66 |
len = read(fd, buffer, sizeof(buffer)); |
|
67 |
} while ((len == -1 && errno == EINTR) || len == sizeof(buffer)); |
|
68 |
} |
|
69 |
|
|
70 |
static int qemu_event_init(void) |
|
71 |
{ |
|
72 |
int err; |
|
73 |
int fds[2]; |
|
74 |
|
|
75 |
err = qemu_eventfd(fds); |
|
76 |
if (err == -1) { |
|
77 |
return -errno; |
|
78 |
} |
|
79 |
err = fcntl_setfl(fds[0], O_NONBLOCK); |
|
80 |
if (err < 0) { |
|
81 |
goto fail; |
|
82 |
} |
|
83 |
err = fcntl_setfl(fds[1], O_NONBLOCK); |
|
84 |
if (err < 0) { |
|
85 |
goto fail; |
|
86 |
} |
|
87 |
qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL, |
|
88 |
(void *)(intptr_t)fds[0]); |
|
89 |
|
|
90 |
io_thread_fd = fds[1]; |
|
91 |
return 0; |
|
92 |
|
|
93 |
fail: |
|
94 |
close(fds[0]); |
|
95 |
close(fds[1]); |
|
96 |
return err; |
|
97 |
} |
|
98 |
|
|
99 | 35 |
/* If we have signalfd, we mask out the signals we want to handle and then |
100 | 36 |
* use signalfd to listen for them. We rely on whatever the current signal |
101 | 37 |
* handler is to dispatch the signals when we receive them. |
... | ... | |
165 | 101 |
|
166 | 102 |
#else /* _WIN32 */ |
167 | 103 |
|
168 |
static HANDLE qemu_event_handle = NULL; |
|
169 |
|
|
170 |
static void dummy_event_handler(void *opaque) |
|
171 |
{ |
|
172 |
} |
|
173 |
|
|
174 |
static int qemu_event_init(void) |
|
104 |
static int qemu_signal_init(void) |
|
175 | 105 |
{ |
176 |
qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL); |
|
177 |
if (!qemu_event_handle) { |
|
178 |
fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError()); |
|
179 |
return -1; |
|
180 |
} |
|
181 |
qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL); |
|
182 | 106 |
return 0; |
183 | 107 |
} |
108 |
#endif |
|
109 |
|
|
110 |
static AioContext *qemu_aio_context; |
|
184 | 111 |
|
185 | 112 |
void qemu_notify_event(void) |
186 | 113 |
{ |
187 |
if (!qemu_event_handle) {
|
|
114 |
if (!qemu_aio_context) {
|
|
188 | 115 |
return; |
189 | 116 |
} |
190 |
if (!SetEvent(qemu_event_handle)) { |
|
191 |
fprintf(stderr, "qemu_notify_event: SetEvent failed: %ld\n", |
|
192 |
GetLastError()); |
|
193 |
exit(1); |
|
194 |
} |
|
117 |
aio_notify(qemu_aio_context); |
|
195 | 118 |
} |
196 | 119 |
|
197 |
static int qemu_signal_init(void) |
|
198 |
{ |
|
199 |
return 0; |
|
200 |
} |
|
201 |
#endif |
|
202 |
|
|
203 |
static AioContext *qemu_aio_context; |
|
204 |
|
|
205 | 120 |
int qemu_init_main_loop(void) |
206 | 121 |
{ |
207 | 122 |
int ret; |
... | ... | |
216 | 131 |
return ret; |
217 | 132 |
} |
218 | 133 |
|
219 |
/* Note eventfd must be drained before signalfd handlers run */ |
|
220 |
ret = qemu_event_init(); |
|
221 |
if (ret) { |
|
222 |
return ret; |
|
223 |
} |
|
224 |
|
|
225 | 134 |
qemu_aio_context = aio_context_new(); |
226 | 135 |
src = aio_get_g_source(qemu_aio_context); |
227 | 136 |
g_source_attach(src, NULL); |
... | ... | |
411 | 320 |
|
412 | 321 |
void qemu_fd_register(int fd) |
413 | 322 |
{ |
414 |
WSAEventSelect(fd, qemu_event_handle, FD_READ | FD_ACCEPT | FD_CLOSE | |
|
323 |
WSAEventSelect(fd, event_notifier_get_handle(&qemu_aio_context->notifier), |
|
324 |
FD_READ | FD_ACCEPT | FD_CLOSE | |
|
415 | 325 |
FD_CONNECT | FD_WRITE | FD_OOB); |
416 | 326 |
} |
417 | 327 |
|
Also available in: Unified diff