You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Since for the original version, programmers may misuse these macros,
to avoid such situation, we have modified macros that are used for
controlling coroutines and macros that programmers use for declaring
coroutine functions and their corresponding contexts.
First of all, cr_proto was introduced as the generator of coroutine
function declarations, and all coroutines should be specified with
this macro.
By introducing cr_proto, we have wrapped the argument that refers to
the coroutine context into a fixed name variable called ctx, which will
be refered by controlling macros such as cr_begin, cr_end, cr_wait and
so on. Invoking these macros is valid only in a coroutine function that
was generated by cr_proto, otherwise, compiler will report errors.
Next, we have modified structure cr that its field local is removed, and
macro cr_context is introduced for programmers to declare the coroutine
contexts. The name assigned to cr_context should be identical with the
one used to generate the coroutine function. After generated a coroutine
function and its corresponding context, programmers could launch that
coroutine via macro cr_run.
Finally, to declare static variables in coroutine, macro cr_local is
introduced, which helps to mark wheather a variable is a part of
coroutine or just for temporary usage.
Abstract using of coroutine macros
With in this version, a set of macros are introduced, which aim to
provide standard style for someone declare or access the coroutine
function and its corresponding context.
One should declare a coroutine function with cr_func_def macro, and
its corresponding context with cr_context macro, the argument name
provides to those macros for the same coroutine should be identical.
After declaration, one could run a coroutine by cr_run macro.
Wrapping a set of cr_run macros in a loop makes several coroutines
execute concurrently.
Since the declaration of a coroutine funciton is wrapped by macro
cr_func_def, there is no need to provide the context as argument of
macros that contorl the coroutine, such as cr_begin, cr_end, cr_wait
etc. That argument was hardcoded as __ct in the body of those macros.
To pass arguments to a coroutine, one can provide a pointer to that
argument as parameter of cr_context_init macro, and to extact them
in the coroutine, two macros, cr_arg and cr_arg_member, were
introduced. Macro cr_arg will cast __ct->arg to the pointer of
corresponding type, while cr_arg_member will return a pointer to the
specific member of given structure type that __ct->arg points to.
Finally, to define a static variable with in the coroutine, one can
define them with cr_local macro, which helps to hightlight wheather
a variable is a part of that coroutine or just for temporary usage.
Rename macros and variables
To make the name of macros and variables indicate purpose of themself
more clearly, some of them were renamed. Following lists the modified
macros and variables:
* Macro cr_func_def is renamed as cr_define, which will avoid confusing
and make programmers focus on dealing body of coroutine.
* Hardcoded __ct is renamed as ctx which stands for context.\
* The name of structure sock_w_loc_t is changed to conn_data_t, since
it aims to hold data used for connection.
Make coroutine function be declared in C-Style
Macro cr_proto is introduced to replace cr_define, which makes
programmers to declare a coroutine function in C-style, that is,
more flexible than cr_define.
Macro cr_run is modified to make respond to cr_proto. The new
version of cr_run accepts variable length argument list.
From now, programmers could specify arguments that are going to be
passed to coroutines, by listing arguments after name while starting
coroutine with macro cr_run. Since the way of passing arguments was
changed, the field arg in structure cr is removed and macros that
aimed to fetch value from arg are also removed.
0 commit comments