Documentation for coroutine annotations
v4 cover Documentation for coroutine annotations
These patches were the first two from my GSoC series and were reasonably straight-forward and well accepted. Gabriel and I are hoping the patches from GSoC can be merged before I start my job in December, so I'm starting by sending the simple parts of the overall patchset, when they are merged then I will redo the later parts in several smaller and more manageable patchsets.
---
Changes since v3:
- Added missing sign-off.
- Added patches by Gabriel Kerneis to add blocking_fn annotation and to protect coroutine_fn and blocking_fn with #ifndef.
Charlie Shepherd (2):
Add an explanation of when a function should be marked coroutine_fn Rename qemu_coroutine_self to qemu_coroutine_self_int and add an
annotated wrapper
Gabriel Kerneis (2):
Introduce blocking_fn annotation
Protect coroutine_fn and blocking_fn with #ifndef
coroutine-gthread.c | 2 +- coroutine-sigaltstack.c | 2 +- coroutine-ucontext.c | 2 +- coroutine-win32.c | 2 +- include/block/coroutine.h | 35 +++++++++++++++++++++++++++++++++++ include/block/coroutine_int.h | 1 + qemu-coroutine.c | 15 ++++++++++++++- 7 files changed, 54 insertions(+), 5 deletions(-)
Signature · 1 line
1.8.4.rc3
PATCH v4 1/4 Add an explanation of when a function should be marked coroutine_fn
Coroutine functions that can yield directly or indirectly should be annotated with a coroutine_fn annotation. Add an explanation to that effect in include/block/coroutine.h.
--- include/block/coroutine.h | 8 ++++++++ 1 file changed, 8 insertions(+)
include/block/coroutine.h +8 -0
@@ -38,6 +38,14 @@* static checker support for catching such errors. This annotation might make * it possible and in the meantime it serves as documentation. * +* A function must be marked with coroutine_fn if it can yield execution, either +* directly or indirectly. +* +* Some functions dynamically determine whether to yield or not based on +* whether they are executing in a coroutine context or not. These functions +* do not need to be annotated coroutine_fn. Note that this practice is +* deprecated and is being phased out, new code should not do this. +* * For example: * * static void coroutine_fn foo(void) {
Signature · 1 line
1.8.4.rc3
PATCH v4 2/4 Rename qemu_coroutine_self to qemu_coroutine_self_int and add an annotated wrapper
In reply to Charlie ShepherdWhile it only really makes sense to call qemu_coroutine_self() in a coroutine context, some coroutine internals need to call it from functions not annotated as coroutine_fn, so add an annotated wrapper and rename the implementation versions to qemu_coroutine_self_int.
--- coroutine-gthread.c | 2 +- coroutine-sigaltstack.c | 2 +- coroutine-ucontext.c | 2 +- coroutine-win32.c | 2 +- include/block/coroutine_int.h | 1 + qemu-coroutine.c | 15 ++++++++++++++- 6 files changed, 19 insertions(+), 5 deletions(-)
coroutine-gthread.c +1 -1
@@ -194,7 +194,7 @@ CoroutineAction qemu_coroutine_switch(Coroutine *from_,return from->action; } -Coroutine *qemu_coroutine_self (void ) +Coroutine *qemu_coroutine_self_int (void ) {CoroutineGThread *co =get_coroutine_key ();if (!co) {
coroutine-sigaltstack.c +1 -1
@@ -277,7 +277,7 @@ CoroutineAction qemu_coroutine_switch(Coroutine *from_, Coroutine *to_,return ret; } -Coroutine *qemu_coroutine_self (void ) +Coroutine *qemu_coroutine_self_int (void ) {CoroutineThreadState *s =coroutine_get_thread_state ();
coroutine-ucontext.c +1 -1
@@ -210,7 +210,7 @@ CoroutineAction qemu_coroutine_switch(Coroutine *from_, Coroutine *to_,return ret; } -Coroutine *qemu_coroutine_self (void ) +Coroutine *qemu_coroutine_self_int (void ) {CoroutineThreadState *s =coroutine_get_thread_state ();
coroutine-win32.c +1 -1
@@ -77,7 +77,7 @@ void qemu_coroutine_delete(Coroutine *co_)g_free (co); } -Coroutine *qemu_coroutine_self (void ) +Coroutine *qemu_coroutine_self_int (void ) {if (!current) { current = &leader.base;
include/block/coroutine_int.h +1 -0
@@ -48,6 +48,7 @@ Coroutine *qemu_coroutine_new(void);void qemu_coroutine_delete (Coroutine *co);CoroutineAction qemu_coroutine_switch (Coroutine *from,Coroutine *to,CoroutineAction action); +Coroutine *qemu_coroutine_self_int (void ); void coroutine_fn qemu_co_queue_run_restart(Coroutine *co);#endif
qemu-coroutine.c +14 -1
@@ -108,7 +108,7 @@ static void coroutine_swap(Coroutine *from, Coroutine *to)void qemu_coroutine_enter (Coroutine *co,void *opaque) { -Coroutine *self =qemu_coroutine_self (); +Coroutine *self =qemu_coroutine_self_int ();trace_qemu_coroutine_enter (self, co, opaque);
@@ -137,3 +137,16 @@ void coroutine_fn qemu_coroutine_yield(void) self->caller =NULL ;coroutine_swap (self, to); } + +Coroutine *coroutine_fn qemu_coroutine_self(void) +{ +/* Call the internal version of this function, which is +* non-coroutine_fn and can therefore be called from from +* non-coroutine contexts. Internally we know it's always possible +* to pull a Coroutine* out of thin air (or thread-local storage). +* External callers shouldn't assume they can always get a +* Coroutine* since we may not be in coroutine context, hence the +* external version of this function. +*/ +return qemu_coroutine_self_int (); +}
Signature · 1 line
1.8.4.rc3
PATCH v4 3/4 Introduce blocking_fn annotation
In reply to Charlie ShepherdFrom: Gabriel Kerneis <[email protected]>
A blocking function is a function that must not be called in coroutine context, for example because it might block for a long amount of time. This annotation should be used to mark normal functions that have a coroutine_fn counterpart, to make sure that the former is not used instead of the later in coroutine context.
--- include/block/coroutine.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)
include/block/coroutine.h +23 -0
@@ -54,6 +54,29 @@*/ #define coroutine_fn +/** +* Mark a function that executes in blocking context +* +* Functions that execute in blocking context cannot be called directly from +* coroutine functions. In the future it would be nice to enable compiler or +* static checker support for catching such errors. This annotation might make +* it possible and in the meantime it serves as documentation. +* +* Annotating a function as "blocking" is stronger than having a mere +* (unannotated) normal function. It means that it might block the main +* loop for a significant amount of time, and therefore must not be +* called in coroutine context. In general, its hints that an +* alternative coroutine function performing the same taks is available +* for use in coroutine context. +* +* For example: +* +* static void blocking_fn foo(void) { +* .... +* } +*/ +#define blocking_fn +typedef struct Coroutine Coroutine ;/**
Signature · 1 line
1.8.4.rc3
PATCH v4 4/4 Protect coroutine_fn and blocking_fn with #ifndef
In reply to Charlie ShepherdFrom: Gabriel Kerneis <[email protected]>
This patch allows defining coroutine and blocking annotations with ./configure --extra-cflags instead of modifying coroutine.h.
--- include/block/coroutine.h | 4 ++++ 1 file changed, 4 insertions(+)
include/block/coroutine.h +4 -0
@@ -52,7 +52,9 @@* .... * } */ +#ifndef coroutine_fn#define coroutine_fn +#endif /** * Mark a function that executes in blocking context
@@ -75,7 +77,9 @@* .... * } */ +#ifndef blocking_fn#define blocking_fn +#endif typedef struct Coroutine Coroutine ;
Signature · 1 line
1.8.4.rc3