block: Add coroutine support to synchronous I/O functions
[qemu/stefanha.git] / qemu-coroutine.h
blob1fad3bf648ea9c0fdf1a21a5c362a2bb2862a3e4
1 /*
2 * QEMU coroutine implementation
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 #ifndef QEMU_COROUTINE_H
15 #define QEMU_COROUTINE_H
17 #include <stdbool.h>
19 /**
20 * Mark a function that executes in coroutine context
22 * Functions that execute in coroutine context cannot be called directly from
23 * normal functions. In the future it would be nice to enable compiler or
24 * static checker support for catching such errors. This annotation might make
25 * it possible and in the meantime it serves as documentation.
27 * For example:
29 * static void coroutine_fn foo(void) {
30 * ....
31 * }
33 #define coroutine_fn
35 typedef struct Coroutine Coroutine;
37 /**
38 * Coroutine entry point
40 * When the coroutine is entered for the first time, opaque is passed in as an
41 * argument.
43 * When this function returns, the coroutine is destroyed automatically and the
44 * return value is passed back to the caller who last entered the coroutine.
46 typedef void * coroutine_fn CoroutineEntry(void *opaque);
48 /**
49 * Create a new coroutine
51 * Use qemu_coroutine_enter() to actually transfer control to the coroutine.
53 Coroutine *qemu_coroutine_create(CoroutineEntry *entry);
55 /**
56 * Transfer control to a coroutine
58 * The opaque argument is made available to the coroutine either as the entry
59 * function argument if this is the first time a new coroutine is entered, or
60 * as the return value from qemu_coroutine_yield().
62 * The return value from this function is either an opaque value yielded by the
63 * coroutine or the coroutine entry function return value when the coroutine
64 * terminates.
66 void *qemu_coroutine_enter(Coroutine *coroutine, void *opaque);
68 /**
69 * Transfer control back to a coroutine's caller
71 * The opaque argument is returned from the calling qemu_coroutine_enter().
73 * The return value is the argument passed back in from the next
74 * qemu_coroutine_enter().
76 void * coroutine_fn qemu_coroutine_yield(void *opaque);
78 /**
79 * Get the currently executing coroutine
81 Coroutine * coroutine_fn qemu_coroutine_self(void);
83 /**
84 * Return whether or not currently inside a coroutine
86 bool qemu_in_coroutine(void);
88 #endif /* QEMU_COROUTINE_H */