]> git.proxmox.com Git - mirror_qemu.git/blame - util/main-loop.c
bsd-user: Implement several get/set system calls:
[mirror_qemu.git] / util / main-loop.c
CommitLineData
d3b12f5d
PB
1/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
d3b12f5d 24
d38ea87a 25#include "qemu/osdep.h"
da34e65c 26#include "qapi/error.h"
f348b6d1 27#include "qemu/cutils.h"
1de7afc9 28#include "qemu/timer.h"
740b1759 29#include "sysemu/cpu-timers.h"
d759c951 30#include "sysemu/replay.h"
1de7afc9 31#include "qemu/main-loop.h"
737e150e 32#include "block/aio.h"
71ad4713 33#include "block/thread-pool.h"
8297be80 34#include "qemu/error-report.h"
e9ed92bd 35#include "qemu/queue.h"
70ac26b9 36#include "qom/object.h"
e9ed92bd
PB
37
38#ifndef _WIN32
39#include <sys/wait.h>
40#endif
d3b12f5d
PB
41
42#ifndef _WIN32
43
d3b12f5d
PB
44/* If we have signalfd, we mask out the signals we want to handle and then
45 * use signalfd to listen for them. We rely on whatever the current signal
46 * handler is to dispatch the signals when we receive them.
47 */
c905a368
DB
48/*
49 * Disable CFI checks.
d02d06f8 50 * We are going to call a signal handler directly. Such handler may or may not
c905a368
DB
51 * have been defined in our binary, so there's no guarantee that the pointer
52 * used to set the handler is a cfi-valid pointer. Since the handlers are
53 * stored in kernel memory, changing the handler to an attacker-defined
54 * function requires being able to call a sigaction() syscall,
55 * which is not as easy as overwriting a pointer in memory.
56 */
57QEMU_DISABLE_CFI
d3b12f5d
PB
58static void sigfd_handler(void *opaque)
59{
60 int fd = (intptr_t)opaque;
61 struct qemu_signalfd_siginfo info;
62 struct sigaction action;
63 ssize_t len;
64
65 while (1) {
37b0b24e 66 len = RETRY_ON_EINTR(read(fd, &info, sizeof(info)));
d3b12f5d
PB
67
68 if (len == -1 && errno == EAGAIN) {
69 break;
70 }
71
72 if (len != sizeof(info)) {
372a87a1
TH
73 error_report("read from sigfd returned %zd: %s", len,
74 g_strerror(errno));
d3b12f5d
PB
75 return;
76 }
77
78 sigaction(info.ssi_signo, NULL, &action);
79 if ((action.sa_flags & SA_SIGINFO) && action.sa_sigaction) {
d98d4072 80 sigaction_invoke(&action, &info);
d3b12f5d
PB
81 } else if (action.sa_handler) {
82 action.sa_handler(info.ssi_signo);
83 }
84 }
85}
86
78524330 87static int qemu_signal_init(Error **errp)
d3b12f5d
PB
88{
89 int sigfd;
90 sigset_t set;
91
92 /*
93 * SIG_IPI must be blocked in the main thread and must not be caught
94 * by sigwait() in the signal thread. Otherwise, the cpu thread will
95 * not catch it reliably.
96 */
97 sigemptyset(&set);
98 sigaddset(&set, SIG_IPI);
d3b12f5d
PB
99 sigaddset(&set, SIGIO);
100 sigaddset(&set, SIGALRM);
101 sigaddset(&set, SIGBUS);
3e9418e1
JK
102 /* SIGINT cannot be handled via signalfd, so that ^C can be used
103 * to interrupt QEMU when it is being run under gdb. SIGHUP and
104 * SIGTERM are also handled asynchronously, even though it is not
105 * strictly necessary, because they use the same handler as SIGINT.
106 */
d3b12f5d
PB
107 pthread_sigmask(SIG_BLOCK, &set, NULL);
108
4aa7534d 109 sigdelset(&set, SIG_IPI);
d3b12f5d
PB
110 sigfd = qemu_signalfd(&set);
111 if (sigfd == -1) {
78524330 112 error_setg_errno(errp, errno, "failed to create signalfd");
d3b12f5d
PB
113 return -errno;
114 }
115
4d14cb0c 116 g_unix_set_fd_nonblocking(sigfd, true, NULL);
d3b12f5d 117
82e1cc4b 118 qemu_set_fd_handler(sigfd, sigfd_handler, NULL, (void *)(intptr_t)sigfd);
d3b12f5d
PB
119
120 return 0;
121}
122
123#else /* _WIN32 */
124
78524330 125static int qemu_signal_init(Error **errp)
d3b12f5d 126{
d3b12f5d
PB
127 return 0;
128}
4c8d0d27
PB
129#endif
130
131static AioContext *qemu_aio_context;
edec47cf
PB
132static QEMUBH *qemu_notify_bh;
133
134static void notify_event_cb(void *opaque)
135{
136 /* No need to do anything; this bottom half is only used to
137 * kick the kernel out of ppoll/poll/WaitForMultipleObjects.
138 */
139}
d3b12f5d 140
5f3aa1ff
SH
141AioContext *qemu_get_aio_context(void)
142{
143 return qemu_aio_context;
144}
145
d3b12f5d
PB
146void qemu_notify_event(void)
147{
4c8d0d27 148 if (!qemu_aio_context) {
ee77dfb2
MR
149 return;
150 }
edec47cf 151 qemu_bh_schedule(qemu_notify_bh);
d3b12f5d
PB
152}
153
cbff4b34
SH
154static GArray *gpollfds;
155
2f78e491 156int qemu_init_main_loop(Error **errp)
d3b12f5d
PB
157{
158 int ret;
82cbbdc6 159 GSource *src;
d3b12f5d 160
3f53bc61 161 init_clocks(qemu_timer_notify_cb);
172061a0 162
78524330 163 ret = qemu_signal_init(errp);
d3b12f5d
PB
164 if (ret) {
165 return ret;
166 }
167
668f62ec 168 qemu_aio_context = aio_context_new(errp);
2f78e491 169 if (!qemu_aio_context) {
2f78e491
CN
170 return -EMFILE;
171 }
5f50be9b 172 qemu_set_current_aio_context(qemu_aio_context);
28ba61e7 173 qemu_notify_bh = qemu_bh_new(notify_event_cb, NULL);
cbff4b34 174 gpollfds = g_array_new(FALSE, FALSE, sizeof(GPollFD));
82cbbdc6 175 src = aio_get_g_source(qemu_aio_context);
c3ff757d 176 g_source_set_name(src, "aio-context");
82cbbdc6
PB
177 g_source_attach(src, NULL);
178 g_source_unref(src);
f3926945 179 src = iohandler_get_g_source();
c3ff757d 180 g_source_set_name(src, "io-handler");
f3926945
FZ
181 g_source_attach(src, NULL);
182 g_source_unref(src);
d3b12f5d
PB
183 return 0;
184}
185
70ac26b9
NSJ
186static void main_loop_update_params(EventLoopBase *base, Error **errp)
187{
71ad4713
NSJ
188 ERRP_GUARD();
189
70ac26b9
NSJ
190 if (!qemu_aio_context) {
191 error_setg(errp, "qemu aio context not ready");
192 return;
193 }
194
195 aio_context_set_aio_params(qemu_aio_context, base->aio_max_batch, errp);
71ad4713
NSJ
196 if (*errp) {
197 return;
198 }
199
200 aio_context_set_thread_pool_params(qemu_aio_context, base->thread_pool_min,
201 base->thread_pool_max, errp);
70ac26b9
NSJ
202}
203
204MainLoop *mloop;
205
206static void main_loop_init(EventLoopBase *base, Error **errp)
207{
208 MainLoop *m = MAIN_LOOP(base);
209
210 if (mloop) {
211 error_setg(errp, "only one main-loop instance allowed");
212 return;
213 }
214
215 main_loop_update_params(base, errp);
216
217 mloop = m;
218 return;
219}
220
221static bool main_loop_can_be_deleted(EventLoopBase *base)
222{
223 return false;
224}
225
226static void main_loop_class_init(ObjectClass *oc, void *class_data)
227{
228 EventLoopBaseClass *bc = EVENT_LOOP_BASE_CLASS(oc);
229
230 bc->init = main_loop_init;
231 bc->update_params = main_loop_update_params;
232 bc->can_be_deleted = main_loop_can_be_deleted;
233}
234
235static const TypeInfo main_loop_info = {
236 .name = TYPE_MAIN_LOOP,
237 .parent = TYPE_EVENT_LOOP_BASE,
238 .class_init = main_loop_class_init,
239 .instance_size = sizeof(MainLoop),
240};
241
242static void main_loop_register_types(void)
243{
244 type_register_static(&main_loop_info);
245}
246
247type_init(main_loop_register_types)
248
d3b12f5d
PB
249static int max_priority;
250
ea26ce76 251#ifndef _WIN32
48ce11ff
SH
252static int glib_pollfds_idx;
253static int glib_n_poll_fds;
254
7b595f35 255static void glib_pollfds_fill(int64_t *cur_timeout)
d3b12f5d
PB
256{
257 GMainContext *context = g_main_context_default();
4dae83ae 258 int timeout = 0;
7b595f35 259 int64_t timeout_ns;
48ce11ff 260 int n;
d3b12f5d
PB
261
262 g_main_context_prepare(context, &max_priority);
263
48ce11ff
SH
264 glib_pollfds_idx = gpollfds->len;
265 n = glib_n_poll_fds;
266 do {
267 GPollFD *pfds;
268 glib_n_poll_fds = n;
269 g_array_set_size(gpollfds, glib_pollfds_idx + glib_n_poll_fds);
270 pfds = &g_array_index(gpollfds, GPollFD, glib_pollfds_idx);
271 n = g_main_context_query(context, max_priority, &timeout, pfds,
272 glib_n_poll_fds);
273 } while (n != glib_n_poll_fds);
d3b12f5d 274
7b595f35
AB
275 if (timeout < 0) {
276 timeout_ns = -1;
277 } else {
278 timeout_ns = (int64_t)timeout * (int64_t)SCALE_MS;
d3b12f5d 279 }
7b595f35
AB
280
281 *cur_timeout = qemu_soonest_timeout(timeout_ns, *cur_timeout);
d3b12f5d
PB
282}
283
48ce11ff 284static void glib_pollfds_poll(void)
d3b12f5d
PB
285{
286 GMainContext *context = g_main_context_default();
48ce11ff 287 GPollFD *pfds = &g_array_index(gpollfds, GPollFD, glib_pollfds_idx);
d3b12f5d 288
48ce11ff 289 if (g_main_context_check(context, max_priority, pfds, glib_n_poll_fds)) {
d3b12f5d
PB
290 g_main_context_dispatch(context);
291 }
292}
293
893986fe
AL
294#define MAX_MAIN_LOOP_SPIN (1000)
295
7b595f35 296static int os_host_main_loop_wait(int64_t timeout)
15455536 297{
ecbddbb1 298 GMainContext *context = g_main_context_default();
15455536
PB
299 int ret;
300
ecbddbb1
RJ
301 g_main_context_acquire(context);
302
48ce11ff 303 glib_pollfds_fill(&timeout);
15455536 304
d759c951
AB
305 qemu_mutex_unlock_iothread();
306 replay_mutex_unlock();
15455536 307
7b595f35 308 ret = qemu_poll_ns((GPollFD *)gpollfds->data, gpollfds->len, timeout);
cbff4b34 309
d759c951
AB
310 replay_mutex_lock();
311 qemu_mutex_lock_iothread();
15455536 312
48ce11ff 313 glib_pollfds_poll();
ecbddbb1
RJ
314
315 g_main_context_release(context);
316
15455536
PB
317 return ret;
318}
319#else
d3b12f5d
PB
320/***********************************************************/
321/* Polling handling */
322
323typedef struct PollingEntry {
324 PollingFunc *func;
325 void *opaque;
326 struct PollingEntry *next;
327} PollingEntry;
328
329static PollingEntry *first_polling_entry;
330
331int qemu_add_polling_cb(PollingFunc *func, void *opaque)
332{
333 PollingEntry **ppe, *pe;
b21e2380 334 pe = g_new0(PollingEntry, 1);
d3b12f5d
PB
335 pe->func = func;
336 pe->opaque = opaque;
337 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
338 *ppe = pe;
339 return 0;
340}
341
342void qemu_del_polling_cb(PollingFunc *func, void *opaque)
343{
344 PollingEntry **ppe, *pe;
345 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
346 pe = *ppe;
347 if (pe->func == func && pe->opaque == opaque) {
348 *ppe = pe->next;
349 g_free(pe);
350 break;
351 }
352 }
353}
354
355/***********************************************************/
356/* Wait objects support */
357typedef struct WaitObjects {
358 int num;
4f76b3d9
BM
359 int revents[MAXIMUM_WAIT_OBJECTS];
360 HANDLE events[MAXIMUM_WAIT_OBJECTS];
361 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS];
362 void *opaque[MAXIMUM_WAIT_OBJECTS];
d3b12f5d
PB
363} WaitObjects;
364
365static WaitObjects wait_objects = {0};
366
367int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
368{
d393b0a1 369 int i;
d3b12f5d 370 WaitObjects *w = &wait_objects;
d393b0a1 371
d3b12f5d
PB
372 if (w->num >= MAXIMUM_WAIT_OBJECTS) {
373 return -1;
374 }
d393b0a1
BM
375
376 for (i = 0; i < w->num; i++) {
377 /* check if the same handle is added twice */
378 if (w->events[i] == handle) {
379 return -1;
380 }
381 }
382
d3b12f5d
PB
383 w->events[w->num] = handle;
384 w->func[w->num] = func;
385 w->opaque[w->num] = opaque;
06ac7d49 386 w->revents[w->num] = 0;
d3b12f5d
PB
387 w->num++;
388 return 0;
389}
390
391void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
392{
393 int i, found;
394 WaitObjects *w = &wait_objects;
395
396 found = 0;
397 for (i = 0; i < w->num; i++) {
398 if (w->events[i] == handle) {
399 found = 1;
400 }
4f76b3d9 401 if (found && i < (MAXIMUM_WAIT_OBJECTS - 1)) {
d3b12f5d
PB
402 w->events[i] = w->events[i + 1];
403 w->func[i] = w->func[i + 1];
404 w->opaque[i] = w->opaque[i + 1];
06ac7d49 405 w->revents[i] = w->revents[i + 1];
d3b12f5d
PB
406 }
407 }
408 if (found) {
409 w->num--;
410 }
411}
412
cbff4b34
SH
413static int pollfds_fill(GArray *pollfds, fd_set *rfds, fd_set *wfds,
414 fd_set *xfds)
415{
416 int nfds = -1;
417 int i;
418
419 for (i = 0; i < pollfds->len; i++) {
420 GPollFD *pfd = &g_array_index(pollfds, GPollFD, i);
421 int fd = pfd->fd;
422 int events = pfd->events;
8db165b3 423 if (events & G_IO_IN) {
cbff4b34
SH
424 FD_SET(fd, rfds);
425 nfds = MAX(nfds, fd);
426 }
8db165b3 427 if (events & G_IO_OUT) {
cbff4b34
SH
428 FD_SET(fd, wfds);
429 nfds = MAX(nfds, fd);
430 }
431 if (events & G_IO_PRI) {
432 FD_SET(fd, xfds);
433 nfds = MAX(nfds, fd);
434 }
435 }
436 return nfds;
437}
438
439static void pollfds_poll(GArray *pollfds, int nfds, fd_set *rfds,
440 fd_set *wfds, fd_set *xfds)
441{
442 int i;
443
444 for (i = 0; i < pollfds->len; i++) {
445 GPollFD *pfd = &g_array_index(pollfds, GPollFD, i);
446 int fd = pfd->fd;
447 int revents = 0;
448
449 if (FD_ISSET(fd, rfds)) {
8db165b3 450 revents |= G_IO_IN;
cbff4b34
SH
451 }
452 if (FD_ISSET(fd, wfds)) {
8db165b3 453 revents |= G_IO_OUT;
cbff4b34
SH
454 }
455 if (FD_ISSET(fd, xfds)) {
456 revents |= G_IO_PRI;
457 }
458 pfd->revents = revents & pfd->events;
459 }
460}
461
7b595f35 462static int os_host_main_loop_wait(int64_t timeout)
d3b12f5d 463{
ea26ce76 464 GMainContext *context = g_main_context_default();
48ce11ff 465 GPollFD poll_fds[1024 * 2]; /* this is probably overkill */
134a03e0 466 int select_ret = 0;
48ce11ff 467 int g_poll_ret, ret, i, n_poll_fds;
d3b12f5d 468 PollingEntry *pe;
d3385eb4 469 WaitObjects *w = &wait_objects;
42fe1c24 470 gint poll_timeout;
7b595f35 471 int64_t poll_timeout_ns;
15455536 472 static struct timeval tv0;
9cbaacf9
SH
473 fd_set rfds, wfds, xfds;
474 int nfds;
d3b12f5d 475
ecbddbb1
RJ
476 g_main_context_acquire(context);
477
d3b12f5d
PB
478 /* XXX: need to suppress polling by better using win32 events */
479 ret = 0;
480 for (pe = first_polling_entry; pe != NULL; pe = pe->next) {
481 ret |= pe->func(pe->opaque);
482 }
d3385eb4 483 if (ret != 0) {
ecbddbb1 484 g_main_context_release(context);
d3385eb4
PB
485 return ret;
486 }
d3b12f5d 487
3cb8c205
SH
488 FD_ZERO(&rfds);
489 FD_ZERO(&wfds);
490 FD_ZERO(&xfds);
491 nfds = pollfds_fill(gpollfds, &rfds, &wfds, &xfds);
492 if (nfds >= 0) {
493 select_ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv0);
494 if (select_ret != 0) {
495 timeout = 0;
496 }
497 if (select_ret > 0) {
498 pollfds_poll(gpollfds, nfds, &rfds, &wfds, &xfds);
499 }
500 }
501
ea26ce76 502 g_main_context_prepare(context, &max_priority);
42fe1c24 503 n_poll_fds = g_main_context_query(context, max_priority, &poll_timeout,
ea26ce76 504 poll_fds, ARRAY_SIZE(poll_fds));
6512e34b 505 g_assert(n_poll_fds + w->num <= ARRAY_SIZE(poll_fds));
ea26ce76 506
06ac7d49 507 for (i = 0; i < w->num; i++) {
58b9630d 508 poll_fds[n_poll_fds + i].fd = (DWORD_PTR)w->events[i];
ea26ce76 509 poll_fds[n_poll_fds + i].events = G_IO_IN;
06ac7d49
PB
510 }
511
7b595f35
AB
512 if (poll_timeout < 0) {
513 poll_timeout_ns = -1;
514 } else {
515 poll_timeout_ns = (int64_t)poll_timeout * (int64_t)SCALE_MS;
3239ad04
SW
516 }
517
7b595f35
AB
518 poll_timeout_ns = qemu_soonest_timeout(poll_timeout_ns, timeout);
519
d3385eb4 520 qemu_mutex_unlock_iothread();
d759c951
AB
521
522 replay_mutex_unlock();
523
7b595f35
AB
524 g_poll_ret = qemu_poll_ns(poll_fds, n_poll_fds + w->num, poll_timeout_ns);
525
d759c951
AB
526 replay_mutex_lock();
527
d3385eb4 528 qemu_mutex_lock_iothread();
5e3bc735 529 if (g_poll_ret > 0) {
06ac7d49 530 for (i = 0; i < w->num; i++) {
ea26ce76 531 w->revents[i] = poll_fds[n_poll_fds + i].revents;
d3385eb4 532 }
06ac7d49
PB
533 for (i = 0; i < w->num; i++) {
534 if (w->revents[i] && w->func[i]) {
535 w->func[i](w->opaque[i]);
d3b12f5d 536 }
d3b12f5d
PB
537 }
538 }
539
ea26ce76
PB
540 if (g_main_context_check(context, max_priority, poll_fds, n_poll_fds)) {
541 g_main_context_dispatch(context);
542 }
543
ecbddbb1
RJ
544 g_main_context_release(context);
545
5e3bc735 546 return select_ret || g_poll_ret;
d3b12f5d
PB
547}
548#endif
549
1ab67b98
MAL
550static NotifierList main_loop_poll_notifiers =
551 NOTIFIER_LIST_INITIALIZER(main_loop_poll_notifiers);
552
553void main_loop_poll_add_notifier(Notifier *notify)
554{
555 notifier_list_add(&main_loop_poll_notifiers, notify);
556}
557
558void main_loop_poll_remove_notifier(Notifier *notify)
559{
560 notifier_remove(notify);
561}
562
de5f852f 563void main_loop_wait(int nonblocking)
d3b12f5d 564{
1ab67b98
MAL
565 MainLoopPoll mlpoll = {
566 .state = MAIN_LOOP_POLL_FILL,
567 .timeout = UINT32_MAX,
568 .pollfds = gpollfds,
569 };
7c7db755 570 int ret;
7b595f35 571 int64_t timeout_ns;
d3b12f5d
PB
572
573 if (nonblocking) {
1ab67b98 574 mlpoll.timeout = 0;
d3b12f5d
PB
575 }
576
d3b12f5d 577 /* poll any events */
cbff4b34 578 g_array_set_size(gpollfds, 0); /* reset for new iteration */
d3b12f5d 579 /* XXX: separate device handlers from system ones */
1ab67b98 580 notifier_list_notify(&main_loop_poll_notifiers, &mlpoll);
7b595f35 581
1ab67b98 582 if (mlpoll.timeout == UINT32_MAX) {
7b595f35
AB
583 timeout_ns = -1;
584 } else {
1ab67b98 585 timeout_ns = (uint64_t)mlpoll.timeout * (int64_t)(SCALE_MS);
7b595f35
AB
586 }
587
588 timeout_ns = qemu_soonest_timeout(timeout_ns,
589 timerlistgroup_deadline_ns(
590 &main_loop_tlg));
591
592 ret = os_host_main_loop_wait(timeout_ns);
1ab67b98
MAL
593 mlpoll.state = ret < 0 ? MAIN_LOOP_POLL_ERR : MAIN_LOOP_POLL_OK;
594 notifier_list_notify(&main_loop_poll_notifiers, &mlpoll);
d3b12f5d 595
740b1759
CF
596 if (icount_enabled()) {
597 /*
598 * CPU thread can infinitely wait for event after
599 * missing the warp
600 */
8191d368 601 icount_start_warp_timer();
740b1759 602 }
40daca54 603 qemu_clock_run_all_timers();
d3b12f5d 604}
f627aab1
PB
605
606/* Functions to operate on the main QEMU AioContext. */
607
9c86c97f
AB
608QEMUBH *qemu_bh_new_full(QEMUBHFunc *cb, void *opaque, const char *name,
609 MemReentrancyGuard *reentrancy_guard)
f627aab1 610{
9c86c97f
AB
611 return aio_bh_new_full(qemu_aio_context, cb, opaque, name,
612 reentrancy_guard);
f627aab1 613}
e9ed92bd
PB
614
615/*
616 * Functions to operate on the I/O handler AioContext.
617 * This context runs on top of main loop. We can't reuse qemu_aio_context
618 * because iohandlers mustn't be polled by aio_poll(qemu_aio_context).
619 */
620static AioContext *iohandler_ctx;
621
622static void iohandler_init(void)
623{
624 if (!iohandler_ctx) {
625 iohandler_ctx = aio_context_new(&error_abort);
626 }
627}
628
629AioContext *iohandler_get_aio_context(void)
630{
631 iohandler_init();
632 return iohandler_ctx;
633}
634
635GSource *iohandler_get_g_source(void)
636{
637 iohandler_init();
638 return aio_get_g_source(iohandler_ctx);
639}
640
641void qemu_set_fd_handler(int fd,
642 IOHandler *fd_read,
643 IOHandler *fd_write,
644 void *opaque)
645{
646 iohandler_init();
60f782b6
SH
647 aio_set_fd_handler(iohandler_ctx, fd, fd_read, fd_write, NULL, NULL,
648 opaque);
e9ed92bd
PB
649}
650
651void event_notifier_set_handler(EventNotifier *e,
652 EventNotifierHandler *handler)
653{
654 iohandler_init();
60f782b6 655 aio_set_event_notifier(iohandler_ctx, e, handler, NULL, NULL);
e9ed92bd 656}