hw/pxa2xx_timer: Explicitly mark fallthroughs
[qemu/pbrook.git] / tests / test-coroutine.c
blob4c6cc81fb98da864f9c101735cd0fb2709ba20bf
1 /*
2 * Coroutine tests
4 * Copyright IBM, Corp. 2011
6 * Authors:
7 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.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.
14 #include <glib.h>
15 #include "block/coroutine.h"
18 * Check that qemu_in_coroutine() works
21 static void coroutine_fn verify_in_coroutine(void *opaque)
23 g_assert(qemu_in_coroutine());
26 static void test_in_coroutine(void)
28 Coroutine *coroutine;
30 g_assert(!qemu_in_coroutine());
32 coroutine = qemu_coroutine_create(verify_in_coroutine);
33 qemu_coroutine_enter(coroutine, NULL);
37 * Check that qemu_coroutine_self() works
40 static void coroutine_fn verify_self(void *opaque)
42 g_assert(qemu_coroutine_self() == opaque);
45 static void test_self(void)
47 Coroutine *coroutine;
49 coroutine = qemu_coroutine_create(verify_self);
50 qemu_coroutine_enter(coroutine, coroutine);
54 * Check that coroutines may nest multiple levels
57 typedef struct {
58 unsigned int n_enter; /* num coroutines entered */
59 unsigned int n_return; /* num coroutines returned */
60 unsigned int max; /* maximum level of nesting */
61 } NestData;
63 static void coroutine_fn nest(void *opaque)
65 NestData *nd = opaque;
67 nd->n_enter++;
69 if (nd->n_enter < nd->max) {
70 Coroutine *child;
72 child = qemu_coroutine_create(nest);
73 qemu_coroutine_enter(child, nd);
76 nd->n_return++;
79 static void test_nesting(void)
81 Coroutine *root;
82 NestData nd = {
83 .n_enter = 0,
84 .n_return = 0,
85 .max = 128,
88 root = qemu_coroutine_create(nest);
89 qemu_coroutine_enter(root, &nd);
91 /* Must enter and return from max nesting level */
92 g_assert_cmpint(nd.n_enter, ==, nd.max);
93 g_assert_cmpint(nd.n_return, ==, nd.max);
97 * Check that yield/enter transfer control correctly
100 static void coroutine_fn yield_5_times(void *opaque)
102 bool *done = opaque;
103 int i;
105 for (i = 0; i < 5; i++) {
106 qemu_coroutine_yield();
108 *done = true;
111 static void test_yield(void)
113 Coroutine *coroutine;
114 bool done = false;
115 int i = -1; /* one extra time to return from coroutine */
117 coroutine = qemu_coroutine_create(yield_5_times);
118 while (!done) {
119 qemu_coroutine_enter(coroutine, &done);
120 i++;
122 g_assert_cmpint(i, ==, 5); /* coroutine must yield 5 times */
126 * Check that creation, enter, and return work
129 static void coroutine_fn set_and_exit(void *opaque)
131 bool *done = opaque;
133 *done = true;
136 static void test_lifecycle(void)
138 Coroutine *coroutine;
139 bool done = false;
141 /* Create, enter, and return from coroutine */
142 coroutine = qemu_coroutine_create(set_and_exit);
143 qemu_coroutine_enter(coroutine, &done);
144 g_assert(done); /* expect done to be true (first time) */
146 /* Repeat to check that no state affects this test */
147 done = false;
148 coroutine = qemu_coroutine_create(set_and_exit);
149 qemu_coroutine_enter(coroutine, &done);
150 g_assert(done); /* expect done to be true (second time) */
154 * Lifecycle benchmark
157 static void coroutine_fn empty_coroutine(void *opaque)
159 /* Do nothing */
162 static void perf_lifecycle(void)
164 Coroutine *coroutine;
165 unsigned int i, max;
166 double duration;
168 max = 1000000;
170 g_test_timer_start();
171 for (i = 0; i < max; i++) {
172 coroutine = qemu_coroutine_create(empty_coroutine);
173 qemu_coroutine_enter(coroutine, NULL);
175 duration = g_test_timer_elapsed();
177 g_test_message("Lifecycle %u iterations: %f s\n", max, duration);
180 static void perf_nesting(void)
182 unsigned int i, maxcycles, maxnesting;
183 double duration;
185 maxcycles = 100000000;
186 maxnesting = 20000;
187 Coroutine *root;
188 NestData nd = {
189 .n_enter = 0,
190 .n_return = 0,
191 .max = maxnesting,
194 g_test_timer_start();
195 for (i = 0; i < maxcycles; i++) {
196 root = qemu_coroutine_create(nest);
197 qemu_coroutine_enter(root, &nd);
199 duration = g_test_timer_elapsed();
201 g_test_message("Nesting %u iterations of %u depth each: %f s\n",
202 maxcycles, maxnesting, duration);
206 int main(int argc, char **argv)
208 g_test_init(&argc, &argv, NULL);
209 g_test_add_func("/basic/lifecycle", test_lifecycle);
210 g_test_add_func("/basic/yield", test_yield);
211 g_test_add_func("/basic/nesting", test_nesting);
212 g_test_add_func("/basic/self", test_self);
213 g_test_add_func("/basic/in_coroutine", test_in_coroutine);
214 if (g_test_perf()) {
215 g_test_add_func("/perf/lifecycle", perf_lifecycle);
216 g_test_add_func("/perf/nesting", perf_nesting);
218 return g_test_run();