target-ppc: Fix SRR0 when taking unaligned exceptions
[qemu/ar7.git] / tests / test-aio.c
blob217e33772ec11327f99e22e8db69ddd4eaf1f1b4
1 /*
2 * AioContext tests
4 * Copyright Red Hat, Inc. 2012
6 * Authors:
7 * Paolo Bonzini <pbonzini@redhat.com>
9 * This work is licensed under the terms of the GNU LGPL, version 2 or later.
10 * See the COPYING.LIB file in the top-level directory.
13 #include <glib.h>
14 #include "block/aio.h"
15 #include "qemu/timer.h"
16 #include "qemu/sockets.h"
17 #include "qemu/error-report.h"
19 static AioContext *ctx;
21 typedef struct {
22 EventNotifier e;
23 int n;
24 int active;
25 bool auto_set;
26 } EventNotifierTestData;
28 /* Wait until event notifier becomes inactive */
29 static void wait_until_inactive(EventNotifierTestData *data)
31 while (data->active > 0) {
32 aio_poll(ctx, true);
36 /* Simple callbacks for testing. */
38 typedef struct {
39 QEMUBH *bh;
40 int n;
41 int max;
42 } BHTestData;
44 typedef struct {
45 QEMUTimer timer;
46 QEMUClockType clock_type;
47 int n;
48 int max;
49 int64_t ns;
50 AioContext *ctx;
51 } TimerTestData;
53 static void bh_test_cb(void *opaque)
55 BHTestData *data = opaque;
56 if (++data->n < data->max) {
57 qemu_bh_schedule(data->bh);
61 static void timer_test_cb(void *opaque)
63 TimerTestData *data = opaque;
64 if (++data->n < data->max) {
65 timer_mod(&data->timer,
66 qemu_clock_get_ns(data->clock_type) + data->ns);
70 static void dummy_io_handler_read(EventNotifier *e)
74 static void bh_delete_cb(void *opaque)
76 BHTestData *data = opaque;
77 if (++data->n < data->max) {
78 qemu_bh_schedule(data->bh);
79 } else {
80 qemu_bh_delete(data->bh);
81 data->bh = NULL;
85 static void event_ready_cb(EventNotifier *e)
87 EventNotifierTestData *data = container_of(e, EventNotifierTestData, e);
88 g_assert(event_notifier_test_and_clear(e));
89 data->n++;
90 if (data->active > 0) {
91 data->active--;
93 if (data->auto_set && data->active) {
94 event_notifier_set(e);
98 /* Tests using aio_*. */
100 typedef struct {
101 QemuMutex start_lock;
102 bool thread_acquired;
103 } AcquireTestData;
105 static void *test_acquire_thread(void *opaque)
107 AcquireTestData *data = opaque;
109 /* Wait for other thread to let us start */
110 qemu_mutex_lock(&data->start_lock);
111 qemu_mutex_unlock(&data->start_lock);
113 aio_context_acquire(ctx);
114 aio_context_release(ctx);
116 data->thread_acquired = true; /* success, we got here */
118 return NULL;
121 static void dummy_notifier_read(EventNotifier *unused)
123 g_assert(false); /* should never be invoked */
126 static void test_acquire(void)
128 QemuThread thread;
129 EventNotifier notifier;
130 AcquireTestData data;
132 /* Dummy event notifier ensures aio_poll() will block */
133 event_notifier_init(&notifier, false);
134 aio_set_event_notifier(ctx, &notifier, dummy_notifier_read);
135 g_assert(!aio_poll(ctx, false)); /* consume aio_notify() */
137 qemu_mutex_init(&data.start_lock);
138 qemu_mutex_lock(&data.start_lock);
139 data.thread_acquired = false;
141 qemu_thread_create(&thread, "test_acquire_thread",
142 test_acquire_thread,
143 &data, QEMU_THREAD_JOINABLE);
145 /* Block in aio_poll(), let other thread kick us and acquire context */
146 aio_context_acquire(ctx);
147 qemu_mutex_unlock(&data.start_lock); /* let the thread run */
148 g_assert(!aio_poll(ctx, true));
149 aio_context_release(ctx);
151 qemu_thread_join(&thread);
152 aio_set_event_notifier(ctx, &notifier, NULL);
153 event_notifier_cleanup(&notifier);
155 g_assert(data.thread_acquired);
158 static void test_bh_schedule(void)
160 BHTestData data = { .n = 0 };
161 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
163 qemu_bh_schedule(data.bh);
164 g_assert_cmpint(data.n, ==, 0);
166 g_assert(aio_poll(ctx, true));
167 g_assert_cmpint(data.n, ==, 1);
169 g_assert(!aio_poll(ctx, false));
170 g_assert_cmpint(data.n, ==, 1);
171 qemu_bh_delete(data.bh);
174 static void test_bh_schedule10(void)
176 BHTestData data = { .n = 0, .max = 10 };
177 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
179 qemu_bh_schedule(data.bh);
180 g_assert_cmpint(data.n, ==, 0);
182 g_assert(aio_poll(ctx, false));
183 g_assert_cmpint(data.n, ==, 1);
185 g_assert(aio_poll(ctx, true));
186 g_assert_cmpint(data.n, ==, 2);
188 while (data.n < 10) {
189 aio_poll(ctx, true);
191 g_assert_cmpint(data.n, ==, 10);
193 g_assert(!aio_poll(ctx, false));
194 g_assert_cmpint(data.n, ==, 10);
195 qemu_bh_delete(data.bh);
198 static void test_bh_cancel(void)
200 BHTestData data = { .n = 0 };
201 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
203 qemu_bh_schedule(data.bh);
204 g_assert_cmpint(data.n, ==, 0);
206 qemu_bh_cancel(data.bh);
207 g_assert_cmpint(data.n, ==, 0);
209 g_assert(!aio_poll(ctx, false));
210 g_assert_cmpint(data.n, ==, 0);
211 qemu_bh_delete(data.bh);
214 static void test_bh_delete(void)
216 BHTestData data = { .n = 0 };
217 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
219 qemu_bh_schedule(data.bh);
220 g_assert_cmpint(data.n, ==, 0);
222 qemu_bh_delete(data.bh);
223 g_assert_cmpint(data.n, ==, 0);
225 g_assert(!aio_poll(ctx, false));
226 g_assert_cmpint(data.n, ==, 0);
229 static void test_bh_delete_from_cb(void)
231 BHTestData data1 = { .n = 0, .max = 1 };
233 data1.bh = aio_bh_new(ctx, bh_delete_cb, &data1);
235 qemu_bh_schedule(data1.bh);
236 g_assert_cmpint(data1.n, ==, 0);
238 while (data1.n < data1.max) {
239 aio_poll(ctx, true);
241 g_assert_cmpint(data1.n, ==, data1.max);
242 g_assert(data1.bh == NULL);
244 g_assert(!aio_poll(ctx, false));
247 static void test_bh_delete_from_cb_many(void)
249 BHTestData data1 = { .n = 0, .max = 1 };
250 BHTestData data2 = { .n = 0, .max = 3 };
251 BHTestData data3 = { .n = 0, .max = 2 };
252 BHTestData data4 = { .n = 0, .max = 4 };
254 data1.bh = aio_bh_new(ctx, bh_delete_cb, &data1);
255 data2.bh = aio_bh_new(ctx, bh_delete_cb, &data2);
256 data3.bh = aio_bh_new(ctx, bh_delete_cb, &data3);
257 data4.bh = aio_bh_new(ctx, bh_delete_cb, &data4);
259 qemu_bh_schedule(data1.bh);
260 qemu_bh_schedule(data2.bh);
261 qemu_bh_schedule(data3.bh);
262 qemu_bh_schedule(data4.bh);
263 g_assert_cmpint(data1.n, ==, 0);
264 g_assert_cmpint(data2.n, ==, 0);
265 g_assert_cmpint(data3.n, ==, 0);
266 g_assert_cmpint(data4.n, ==, 0);
268 g_assert(aio_poll(ctx, false));
269 g_assert_cmpint(data1.n, ==, 1);
270 g_assert_cmpint(data2.n, ==, 1);
271 g_assert_cmpint(data3.n, ==, 1);
272 g_assert_cmpint(data4.n, ==, 1);
273 g_assert(data1.bh == NULL);
275 while (data1.n < data1.max ||
276 data2.n < data2.max ||
277 data3.n < data3.max ||
278 data4.n < data4.max) {
279 aio_poll(ctx, true);
281 g_assert_cmpint(data1.n, ==, data1.max);
282 g_assert_cmpint(data2.n, ==, data2.max);
283 g_assert_cmpint(data3.n, ==, data3.max);
284 g_assert_cmpint(data4.n, ==, data4.max);
285 g_assert(data1.bh == NULL);
286 g_assert(data2.bh == NULL);
287 g_assert(data3.bh == NULL);
288 g_assert(data4.bh == NULL);
291 static void test_bh_flush(void)
293 BHTestData data = { .n = 0 };
294 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
296 qemu_bh_schedule(data.bh);
297 g_assert_cmpint(data.n, ==, 0);
299 g_assert(aio_poll(ctx, true));
300 g_assert_cmpint(data.n, ==, 1);
302 g_assert(!aio_poll(ctx, false));
303 g_assert_cmpint(data.n, ==, 1);
304 qemu_bh_delete(data.bh);
307 static void test_set_event_notifier(void)
309 EventNotifierTestData data = { .n = 0, .active = 0 };
310 event_notifier_init(&data.e, false);
311 aio_set_event_notifier(ctx, &data.e, event_ready_cb);
312 g_assert(!aio_poll(ctx, false));
313 g_assert_cmpint(data.n, ==, 0);
315 aio_set_event_notifier(ctx, &data.e, NULL);
316 g_assert(!aio_poll(ctx, false));
317 g_assert_cmpint(data.n, ==, 0);
318 event_notifier_cleanup(&data.e);
321 static void test_wait_event_notifier(void)
323 EventNotifierTestData data = { .n = 0, .active = 1 };
324 event_notifier_init(&data.e, false);
325 aio_set_event_notifier(ctx, &data.e, event_ready_cb);
326 while (aio_poll(ctx, false));
327 g_assert_cmpint(data.n, ==, 0);
328 g_assert_cmpint(data.active, ==, 1);
330 event_notifier_set(&data.e);
331 g_assert(aio_poll(ctx, false));
332 g_assert_cmpint(data.n, ==, 1);
333 g_assert_cmpint(data.active, ==, 0);
335 g_assert(!aio_poll(ctx, false));
336 g_assert_cmpint(data.n, ==, 1);
337 g_assert_cmpint(data.active, ==, 0);
339 aio_set_event_notifier(ctx, &data.e, NULL);
340 g_assert(!aio_poll(ctx, false));
341 g_assert_cmpint(data.n, ==, 1);
343 event_notifier_cleanup(&data.e);
346 static void test_flush_event_notifier(void)
348 EventNotifierTestData data = { .n = 0, .active = 10, .auto_set = true };
349 event_notifier_init(&data.e, false);
350 aio_set_event_notifier(ctx, &data.e, event_ready_cb);
351 while (aio_poll(ctx, false));
352 g_assert_cmpint(data.n, ==, 0);
353 g_assert_cmpint(data.active, ==, 10);
355 event_notifier_set(&data.e);
356 g_assert(aio_poll(ctx, false));
357 g_assert_cmpint(data.n, ==, 1);
358 g_assert_cmpint(data.active, ==, 9);
359 g_assert(aio_poll(ctx, false));
361 wait_until_inactive(&data);
362 g_assert_cmpint(data.n, ==, 10);
363 g_assert_cmpint(data.active, ==, 0);
364 g_assert(!aio_poll(ctx, false));
366 aio_set_event_notifier(ctx, &data.e, NULL);
367 g_assert(!aio_poll(ctx, false));
368 event_notifier_cleanup(&data.e);
371 static void test_wait_event_notifier_noflush(void)
373 EventNotifierTestData data = { .n = 0 };
374 EventNotifierTestData dummy = { .n = 0, .active = 1 };
376 event_notifier_init(&data.e, false);
377 aio_set_event_notifier(ctx, &data.e, event_ready_cb);
379 g_assert(!aio_poll(ctx, false));
380 g_assert_cmpint(data.n, ==, 0);
382 /* Until there is an active descriptor, aio_poll may or may not call
383 * event_ready_cb. Still, it must not block. */
384 event_notifier_set(&data.e);
385 g_assert(aio_poll(ctx, true));
386 data.n = 0;
388 /* An active event notifier forces aio_poll to look at EventNotifiers. */
389 event_notifier_init(&dummy.e, false);
390 aio_set_event_notifier(ctx, &dummy.e, event_ready_cb);
392 event_notifier_set(&data.e);
393 g_assert(aio_poll(ctx, false));
394 g_assert_cmpint(data.n, ==, 1);
395 g_assert(!aio_poll(ctx, false));
396 g_assert_cmpint(data.n, ==, 1);
398 event_notifier_set(&data.e);
399 g_assert(aio_poll(ctx, false));
400 g_assert_cmpint(data.n, ==, 2);
401 g_assert(!aio_poll(ctx, false));
402 g_assert_cmpint(data.n, ==, 2);
404 event_notifier_set(&dummy.e);
405 wait_until_inactive(&dummy);
406 g_assert_cmpint(data.n, ==, 2);
407 g_assert_cmpint(dummy.n, ==, 1);
408 g_assert_cmpint(dummy.active, ==, 0);
410 aio_set_event_notifier(ctx, &dummy.e, NULL);
411 event_notifier_cleanup(&dummy.e);
413 aio_set_event_notifier(ctx, &data.e, NULL);
414 g_assert(!aio_poll(ctx, false));
415 g_assert_cmpint(data.n, ==, 2);
417 event_notifier_cleanup(&data.e);
420 static void test_timer_schedule(void)
422 TimerTestData data = { .n = 0, .ctx = ctx, .ns = SCALE_MS * 750LL,
423 .max = 2,
424 .clock_type = QEMU_CLOCK_VIRTUAL };
425 EventNotifier e;
427 /* aio_poll will not block to wait for timers to complete unless it has
428 * an fd to wait on. Fixing this breaks other tests. So create a dummy one.
430 event_notifier_init(&e, false);
431 aio_set_event_notifier(ctx, &e, dummy_io_handler_read);
432 aio_poll(ctx, false);
434 aio_timer_init(ctx, &data.timer, data.clock_type,
435 SCALE_NS, timer_test_cb, &data);
436 timer_mod(&data.timer,
437 qemu_clock_get_ns(data.clock_type) +
438 data.ns);
440 g_assert_cmpint(data.n, ==, 0);
442 /* timer_mod may well cause an event notifer to have gone off,
443 * so clear that
445 do {} while (aio_poll(ctx, false));
447 g_assert(!aio_poll(ctx, false));
448 g_assert_cmpint(data.n, ==, 0);
450 g_usleep(1 * G_USEC_PER_SEC);
451 g_assert_cmpint(data.n, ==, 0);
453 g_assert(aio_poll(ctx, false));
454 g_assert_cmpint(data.n, ==, 1);
456 /* timer_mod called by our callback */
457 do {} while (aio_poll(ctx, false));
459 g_assert(!aio_poll(ctx, false));
460 g_assert_cmpint(data.n, ==, 1);
462 g_assert(aio_poll(ctx, true));
463 g_assert_cmpint(data.n, ==, 2);
465 /* As max is now 2, an event notifier should not have gone off */
467 g_assert(!aio_poll(ctx, false));
468 g_assert_cmpint(data.n, ==, 2);
470 aio_set_event_notifier(ctx, &e, NULL);
471 event_notifier_cleanup(&e);
473 timer_del(&data.timer);
476 /* Now the same tests, using the context as a GSource. They are
477 * very similar to the ones above, with g_main_context_iteration
478 * replacing aio_poll. However:
479 * - sometimes both the AioContext and the glib main loop wake
480 * themselves up. Hence, some "g_assert(!aio_poll(ctx, false));"
481 * are replaced by "while (g_main_context_iteration(NULL, false));".
482 * - there is no exact replacement for a blocking wait.
483 * "while (g_main_context_iteration(NULL, true)" seems to work,
484 * but it is not documented _why_ it works. For these tests a
485 * non-blocking loop like "while (g_main_context_iteration(NULL, false)"
486 * works well, and that's what I am using.
489 static void test_source_flush(void)
491 g_assert(!g_main_context_iteration(NULL, false));
492 aio_notify(ctx);
493 while (g_main_context_iteration(NULL, false));
494 g_assert(!g_main_context_iteration(NULL, false));
497 static void test_source_bh_schedule(void)
499 BHTestData data = { .n = 0 };
500 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
502 qemu_bh_schedule(data.bh);
503 g_assert_cmpint(data.n, ==, 0);
505 g_assert(g_main_context_iteration(NULL, true));
506 g_assert_cmpint(data.n, ==, 1);
508 g_assert(!g_main_context_iteration(NULL, false));
509 g_assert_cmpint(data.n, ==, 1);
510 qemu_bh_delete(data.bh);
513 static void test_source_bh_schedule10(void)
515 BHTestData data = { .n = 0, .max = 10 };
516 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
518 qemu_bh_schedule(data.bh);
519 g_assert_cmpint(data.n, ==, 0);
521 g_assert(g_main_context_iteration(NULL, false));
522 g_assert_cmpint(data.n, ==, 1);
524 g_assert(g_main_context_iteration(NULL, true));
525 g_assert_cmpint(data.n, ==, 2);
527 while (g_main_context_iteration(NULL, false));
528 g_assert_cmpint(data.n, ==, 10);
530 g_assert(!g_main_context_iteration(NULL, false));
531 g_assert_cmpint(data.n, ==, 10);
532 qemu_bh_delete(data.bh);
535 static void test_source_bh_cancel(void)
537 BHTestData data = { .n = 0 };
538 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
540 qemu_bh_schedule(data.bh);
541 g_assert_cmpint(data.n, ==, 0);
543 qemu_bh_cancel(data.bh);
544 g_assert_cmpint(data.n, ==, 0);
546 while (g_main_context_iteration(NULL, false));
547 g_assert_cmpint(data.n, ==, 0);
548 qemu_bh_delete(data.bh);
551 static void test_source_bh_delete(void)
553 BHTestData data = { .n = 0 };
554 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
556 qemu_bh_schedule(data.bh);
557 g_assert_cmpint(data.n, ==, 0);
559 qemu_bh_delete(data.bh);
560 g_assert_cmpint(data.n, ==, 0);
562 while (g_main_context_iteration(NULL, false));
563 g_assert_cmpint(data.n, ==, 0);
566 static void test_source_bh_delete_from_cb(void)
568 BHTestData data1 = { .n = 0, .max = 1 };
570 data1.bh = aio_bh_new(ctx, bh_delete_cb, &data1);
572 qemu_bh_schedule(data1.bh);
573 g_assert_cmpint(data1.n, ==, 0);
575 g_main_context_iteration(NULL, true);
576 g_assert_cmpint(data1.n, ==, data1.max);
577 g_assert(data1.bh == NULL);
579 g_assert(!g_main_context_iteration(NULL, false));
582 static void test_source_bh_delete_from_cb_many(void)
584 BHTestData data1 = { .n = 0, .max = 1 };
585 BHTestData data2 = { .n = 0, .max = 3 };
586 BHTestData data3 = { .n = 0, .max = 2 };
587 BHTestData data4 = { .n = 0, .max = 4 };
589 data1.bh = aio_bh_new(ctx, bh_delete_cb, &data1);
590 data2.bh = aio_bh_new(ctx, bh_delete_cb, &data2);
591 data3.bh = aio_bh_new(ctx, bh_delete_cb, &data3);
592 data4.bh = aio_bh_new(ctx, bh_delete_cb, &data4);
594 qemu_bh_schedule(data1.bh);
595 qemu_bh_schedule(data2.bh);
596 qemu_bh_schedule(data3.bh);
597 qemu_bh_schedule(data4.bh);
598 g_assert_cmpint(data1.n, ==, 0);
599 g_assert_cmpint(data2.n, ==, 0);
600 g_assert_cmpint(data3.n, ==, 0);
601 g_assert_cmpint(data4.n, ==, 0);
603 g_assert(g_main_context_iteration(NULL, false));
604 g_assert_cmpint(data1.n, ==, 1);
605 g_assert_cmpint(data2.n, ==, 1);
606 g_assert_cmpint(data3.n, ==, 1);
607 g_assert_cmpint(data4.n, ==, 1);
608 g_assert(data1.bh == NULL);
610 while (g_main_context_iteration(NULL, false));
611 g_assert_cmpint(data1.n, ==, data1.max);
612 g_assert_cmpint(data2.n, ==, data2.max);
613 g_assert_cmpint(data3.n, ==, data3.max);
614 g_assert_cmpint(data4.n, ==, data4.max);
615 g_assert(data1.bh == NULL);
616 g_assert(data2.bh == NULL);
617 g_assert(data3.bh == NULL);
618 g_assert(data4.bh == NULL);
621 static void test_source_bh_flush(void)
623 BHTestData data = { .n = 0 };
624 data.bh = aio_bh_new(ctx, bh_test_cb, &data);
626 qemu_bh_schedule(data.bh);
627 g_assert_cmpint(data.n, ==, 0);
629 g_assert(g_main_context_iteration(NULL, true));
630 g_assert_cmpint(data.n, ==, 1);
632 g_assert(!g_main_context_iteration(NULL, false));
633 g_assert_cmpint(data.n, ==, 1);
634 qemu_bh_delete(data.bh);
637 static void test_source_set_event_notifier(void)
639 EventNotifierTestData data = { .n = 0, .active = 0 };
640 event_notifier_init(&data.e, false);
641 aio_set_event_notifier(ctx, &data.e, event_ready_cb);
642 while (g_main_context_iteration(NULL, false));
643 g_assert_cmpint(data.n, ==, 0);
645 aio_set_event_notifier(ctx, &data.e, NULL);
646 while (g_main_context_iteration(NULL, false));
647 g_assert_cmpint(data.n, ==, 0);
648 event_notifier_cleanup(&data.e);
651 static void test_source_wait_event_notifier(void)
653 EventNotifierTestData data = { .n = 0, .active = 1 };
654 event_notifier_init(&data.e, false);
655 aio_set_event_notifier(ctx, &data.e, event_ready_cb);
656 while (g_main_context_iteration(NULL, false));
657 g_assert_cmpint(data.n, ==, 0);
658 g_assert_cmpint(data.active, ==, 1);
660 event_notifier_set(&data.e);
661 g_assert(g_main_context_iteration(NULL, false));
662 g_assert_cmpint(data.n, ==, 1);
663 g_assert_cmpint(data.active, ==, 0);
665 while (g_main_context_iteration(NULL, false));
666 g_assert_cmpint(data.n, ==, 1);
667 g_assert_cmpint(data.active, ==, 0);
669 aio_set_event_notifier(ctx, &data.e, NULL);
670 while (g_main_context_iteration(NULL, false));
671 g_assert_cmpint(data.n, ==, 1);
673 event_notifier_cleanup(&data.e);
676 static void test_source_flush_event_notifier(void)
678 EventNotifierTestData data = { .n = 0, .active = 10, .auto_set = true };
679 event_notifier_init(&data.e, false);
680 aio_set_event_notifier(ctx, &data.e, event_ready_cb);
681 while (g_main_context_iteration(NULL, false));
682 g_assert_cmpint(data.n, ==, 0);
683 g_assert_cmpint(data.active, ==, 10);
685 event_notifier_set(&data.e);
686 g_assert(g_main_context_iteration(NULL, false));
687 g_assert_cmpint(data.n, ==, 1);
688 g_assert_cmpint(data.active, ==, 9);
689 g_assert(g_main_context_iteration(NULL, false));
691 while (g_main_context_iteration(NULL, false));
692 g_assert_cmpint(data.n, ==, 10);
693 g_assert_cmpint(data.active, ==, 0);
694 g_assert(!g_main_context_iteration(NULL, false));
696 aio_set_event_notifier(ctx, &data.e, NULL);
697 while (g_main_context_iteration(NULL, false));
698 event_notifier_cleanup(&data.e);
701 static void test_source_wait_event_notifier_noflush(void)
703 EventNotifierTestData data = { .n = 0 };
704 EventNotifierTestData dummy = { .n = 0, .active = 1 };
706 event_notifier_init(&data.e, false);
707 aio_set_event_notifier(ctx, &data.e, event_ready_cb);
709 while (g_main_context_iteration(NULL, false));
710 g_assert_cmpint(data.n, ==, 0);
712 /* Until there is an active descriptor, glib may or may not call
713 * event_ready_cb. Still, it must not block. */
714 event_notifier_set(&data.e);
715 g_main_context_iteration(NULL, true);
716 data.n = 0;
718 /* An active event notifier forces aio_poll to look at EventNotifiers. */
719 event_notifier_init(&dummy.e, false);
720 aio_set_event_notifier(ctx, &dummy.e, event_ready_cb);
722 event_notifier_set(&data.e);
723 g_assert(g_main_context_iteration(NULL, false));
724 g_assert_cmpint(data.n, ==, 1);
725 g_assert(!g_main_context_iteration(NULL, false));
726 g_assert_cmpint(data.n, ==, 1);
728 event_notifier_set(&data.e);
729 g_assert(g_main_context_iteration(NULL, false));
730 g_assert_cmpint(data.n, ==, 2);
731 g_assert(!g_main_context_iteration(NULL, false));
732 g_assert_cmpint(data.n, ==, 2);
734 event_notifier_set(&dummy.e);
735 while (g_main_context_iteration(NULL, false));
736 g_assert_cmpint(data.n, ==, 2);
737 g_assert_cmpint(dummy.n, ==, 1);
738 g_assert_cmpint(dummy.active, ==, 0);
740 aio_set_event_notifier(ctx, &dummy.e, NULL);
741 event_notifier_cleanup(&dummy.e);
743 aio_set_event_notifier(ctx, &data.e, NULL);
744 while (g_main_context_iteration(NULL, false));
745 g_assert_cmpint(data.n, ==, 2);
747 event_notifier_cleanup(&data.e);
750 static void test_source_timer_schedule(void)
752 TimerTestData data = { .n = 0, .ctx = ctx, .ns = SCALE_MS * 750LL,
753 .max = 2,
754 .clock_type = QEMU_CLOCK_VIRTUAL };
755 EventNotifier e;
756 int64_t expiry;
758 /* aio_poll will not block to wait for timers to complete unless it has
759 * an fd to wait on. Fixing this breaks other tests. So create a dummy one.
761 event_notifier_init(&e, false);
762 aio_set_event_notifier(ctx, &e, dummy_io_handler_read);
763 do {} while (g_main_context_iteration(NULL, false));
765 aio_timer_init(ctx, &data.timer, data.clock_type,
766 SCALE_NS, timer_test_cb, &data);
767 expiry = qemu_clock_get_ns(data.clock_type) +
768 data.ns;
769 timer_mod(&data.timer, expiry);
771 g_assert_cmpint(data.n, ==, 0);
773 g_usleep(1 * G_USEC_PER_SEC);
774 g_assert_cmpint(data.n, ==, 0);
776 g_assert(g_main_context_iteration(NULL, true));
777 g_assert_cmpint(data.n, ==, 1);
778 expiry += data.ns;
780 while (data.n < 2) {
781 g_main_context_iteration(NULL, true);
784 g_assert_cmpint(data.n, ==, 2);
785 g_assert(qemu_clock_get_ns(data.clock_type) > expiry);
787 aio_set_event_notifier(ctx, &e, NULL);
788 event_notifier_cleanup(&e);
790 timer_del(&data.timer);
794 /* End of tests. */
796 int main(int argc, char **argv)
798 Error *local_error = NULL;
799 GSource *src;
801 init_clocks();
803 ctx = aio_context_new(&local_error);
804 if (!ctx) {
805 error_report("Failed to create AIO Context: '%s'",
806 error_get_pretty(local_error));
807 error_free(local_error);
808 exit(1);
810 src = aio_get_g_source(ctx);
811 g_source_attach(src, NULL);
812 g_source_unref(src);
814 while (g_main_context_iteration(NULL, false));
816 g_test_init(&argc, &argv, NULL);
817 g_test_add_func("/aio/acquire", test_acquire);
818 g_test_add_func("/aio/bh/schedule", test_bh_schedule);
819 g_test_add_func("/aio/bh/schedule10", test_bh_schedule10);
820 g_test_add_func("/aio/bh/cancel", test_bh_cancel);
821 g_test_add_func("/aio/bh/delete", test_bh_delete);
822 g_test_add_func("/aio/bh/callback-delete/one", test_bh_delete_from_cb);
823 g_test_add_func("/aio/bh/callback-delete/many", test_bh_delete_from_cb_many);
824 g_test_add_func("/aio/bh/flush", test_bh_flush);
825 g_test_add_func("/aio/event/add-remove", test_set_event_notifier);
826 g_test_add_func("/aio/event/wait", test_wait_event_notifier);
827 g_test_add_func("/aio/event/wait/no-flush-cb", test_wait_event_notifier_noflush);
828 g_test_add_func("/aio/event/flush", test_flush_event_notifier);
829 g_test_add_func("/aio/timer/schedule", test_timer_schedule);
831 g_test_add_func("/aio-gsource/flush", test_source_flush);
832 g_test_add_func("/aio-gsource/bh/schedule", test_source_bh_schedule);
833 g_test_add_func("/aio-gsource/bh/schedule10", test_source_bh_schedule10);
834 g_test_add_func("/aio-gsource/bh/cancel", test_source_bh_cancel);
835 g_test_add_func("/aio-gsource/bh/delete", test_source_bh_delete);
836 g_test_add_func("/aio-gsource/bh/callback-delete/one", test_source_bh_delete_from_cb);
837 g_test_add_func("/aio-gsource/bh/callback-delete/many", test_source_bh_delete_from_cb_many);
838 g_test_add_func("/aio-gsource/bh/flush", test_source_bh_flush);
839 g_test_add_func("/aio-gsource/event/add-remove", test_source_set_event_notifier);
840 g_test_add_func("/aio-gsource/event/wait", test_source_wait_event_notifier);
841 g_test_add_func("/aio-gsource/event/wait/no-flush-cb", test_source_wait_event_notifier_noflush);
842 g_test_add_func("/aio-gsource/event/flush", test_source_flush_event_notifier);
843 g_test_add_func("/aio-gsource/timer/schedule", test_source_timer_schedule);
844 return g_test_run();