2019-08-04 05:36:13 +00:00
|
|
|
/*
|
|
|
|
* Mr. 4th Dimention - Allen Webster
|
|
|
|
*
|
|
|
|
* 19.07.2017
|
|
|
|
*
|
|
|
|
* Coroutine implementation from thread+mutex+cv
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
// TOP
|
|
|
|
|
|
|
|
internal void
|
|
|
|
coroutine__pass_control(Coroutine *me, Coroutine *other,
|
|
|
|
Coroutine_State my_new_state, Coroutine_Pass_Control control){
|
|
|
|
Assert(me->state == CoroutineState_Active);
|
|
|
|
Assert(me->sys == other->sys);
|
|
|
|
|
|
|
|
me->state = my_new_state;
|
|
|
|
other->state = CoroutineState_Active;
|
|
|
|
me->sys->active = other;
|
2019-10-03 17:57:44 +00:00
|
|
|
system_condition_variable_signal(other->cv);
|
2019-08-04 05:36:13 +00:00
|
|
|
if (control == CoroutinePassControl_BlockMe){
|
|
|
|
for (;me->state != CoroutineState_Active;){
|
2019-10-03 17:57:44 +00:00
|
|
|
system_condition_variable_wait(me->cv, me->sys->lock);
|
2019-08-04 05:36:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
internal void
|
|
|
|
coroutine_main(void *ptr){
|
|
|
|
Coroutine *me = (Coroutine*)ptr;
|
|
|
|
|
|
|
|
// NOTE(allen): Init handshake
|
|
|
|
Assert(me->state == CoroutineState_Dead);
|
2019-10-03 17:57:44 +00:00
|
|
|
system_mutex_acquire(me->sys->lock);
|
2019-08-04 05:36:13 +00:00
|
|
|
me->sys->did_init = true;
|
2019-10-03 17:57:44 +00:00
|
|
|
system_condition_variable_signal(me->sys->init_cv);
|
2019-08-04 05:36:13 +00:00
|
|
|
|
|
|
|
for (;;){
|
|
|
|
// NOTE(allen): Wait until someone wakes us up, then go into our procedure.
|
|
|
|
for (;me->state != CoroutineState_Active;){
|
2019-10-03 17:57:44 +00:00
|
|
|
system_condition_variable_wait(me->cv, me->sys->lock);
|
2019-08-04 05:36:13 +00:00
|
|
|
}
|
|
|
|
Assert(me->type != CoroutineType_Root);
|
|
|
|
Assert(me->yield_ctx != 0);
|
2019-10-01 22:03:21 +00:00
|
|
|
Assert(me->func != 0);
|
2019-08-04 05:36:13 +00:00
|
|
|
|
2019-10-01 22:03:21 +00:00
|
|
|
me->func(me);
|
2019-08-04 05:36:13 +00:00
|
|
|
|
|
|
|
// NOTE(allen): Wake up the caller and set this coroutine back to being dead.
|
|
|
|
Coroutine *other = me->yield_ctx;
|
|
|
|
Assert(other != 0);
|
|
|
|
Assert(other->state == CoroutineState_Waiting);
|
|
|
|
|
|
|
|
coroutine__pass_control(me, other, CoroutineState_Dead, CoroutinePassControl_ExitMe);
|
2019-10-01 22:03:21 +00:00
|
|
|
me->func = 0;
|
2019-08-04 05:36:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
internal void
|
|
|
|
coroutine_sub_init(Coroutine *co, Coroutine_Group *sys){
|
|
|
|
block_zero_struct(co);
|
|
|
|
co->sys = sys;
|
|
|
|
co->state = CoroutineState_Dead;
|
|
|
|
co->type = CoroutineType_Sub;
|
2019-10-03 17:57:44 +00:00
|
|
|
co->cv = system_condition_variable_make();
|
2019-08-04 05:36:13 +00:00
|
|
|
sys->did_init = false;
|
2019-10-03 17:57:44 +00:00
|
|
|
co->thread = system_thread_launch(coroutine_main, co);
|
2019-08-04 05:36:13 +00:00
|
|
|
for (;!sys->did_init;){
|
2019-10-03 17:57:44 +00:00
|
|
|
system_condition_variable_wait(sys->init_cv, sys->lock);
|
2019-08-04 05:36:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
internal void
|
2019-10-03 17:57:44 +00:00
|
|
|
coroutine_system_init(Coroutine_Group *sys){
|
|
|
|
sys->arena = make_arena_system();
|
2019-08-04 05:36:13 +00:00
|
|
|
|
|
|
|
Coroutine *root = &sys->root;
|
|
|
|
|
2019-10-03 17:57:44 +00:00
|
|
|
sys->lock = system_mutex_make();
|
|
|
|
sys->init_cv = system_condition_variable_make();
|
2019-08-04 05:36:13 +00:00
|
|
|
sys->active = root;
|
|
|
|
|
|
|
|
block_zero_struct(root);
|
|
|
|
root->sys = sys;
|
|
|
|
root->state = CoroutineState_Active;
|
|
|
|
root->type = CoroutineType_Root;
|
2019-10-03 17:57:44 +00:00
|
|
|
root->cv = system_condition_variable_make();
|
2019-08-04 05:36:13 +00:00
|
|
|
|
|
|
|
sys->unused = 0;
|
|
|
|
|
2019-10-03 17:57:44 +00:00
|
|
|
system_mutex_acquire(sys->lock);
|
2019-08-04 05:36:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
internal Coroutine*
|
|
|
|
coroutine_system_alloc(Coroutine_Group *sys){
|
|
|
|
Coroutine *result = sys->unused;
|
|
|
|
if (result != 0){
|
|
|
|
sll_stack_pop(sys->unused);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
result = push_array(&sys->arena, Coroutine, 1);
|
|
|
|
coroutine_sub_init(result, sys);
|
|
|
|
}
|
|
|
|
result->next = 0;
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
internal void
|
|
|
|
coroutine_system_free(Coroutine_Group *sys, Coroutine *coroutine){
|
|
|
|
sll_stack_push(sys->unused, coroutine);
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////
|
|
|
|
|
|
|
|
internal Coroutine*
|
|
|
|
coroutine_create(Coroutine_Group *coroutines, Coroutine_Function *func){
|
|
|
|
Coroutine *result = coroutine_system_alloc(coroutines);
|
|
|
|
Assert(result->state == CoroutineState_Dead);
|
2019-10-01 22:03:21 +00:00
|
|
|
result->func = func;
|
2019-08-04 05:36:13 +00:00
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
internal Coroutine*
|
2019-08-05 21:10:50 +00:00
|
|
|
coroutine_run(Coroutine_Group *sys, Coroutine *other, void *in, void *out){
|
2019-08-04 05:36:13 +00:00
|
|
|
other->in = in;
|
|
|
|
other->out = out;
|
|
|
|
|
|
|
|
Coroutine *me = other->sys->active;
|
|
|
|
Assert(me != 0);
|
|
|
|
Assert(me->sys == other->sys);
|
|
|
|
Assert(other->state == CoroutineState_Dead || other->state == CoroutineState_Inactive);
|
|
|
|
other->yield_ctx = me;
|
|
|
|
coroutine__pass_control(me, other, CoroutineState_Waiting, CoroutinePassControl_BlockMe);
|
|
|
|
Assert(me == other->sys->active);
|
|
|
|
|
|
|
|
Coroutine *result = other;
|
|
|
|
if (other->state == CoroutineState_Dead){
|
|
|
|
coroutine_system_free(sys, other);
|
|
|
|
result = 0;
|
|
|
|
}
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
internal void
|
|
|
|
coroutine_yield(Coroutine *me){
|
|
|
|
Coroutine *other = me->yield_ctx;
|
|
|
|
Assert(other != 0);
|
|
|
|
Assert(me->sys == other->sys);
|
|
|
|
Assert(other->state == CoroutineState_Waiting);
|
|
|
|
coroutine__pass_control(me, other, CoroutineState_Inactive, CoroutinePassControl_BlockMe);
|
|
|
|
}
|
|
|
|
|
|
|
|
// BOTTOM
|
|
|
|
|