4coder/4ed_coroutine.cpp

163 lines
4.5 KiB
C++
Raw Normal View History

/*
* 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);
System_Functions *system = me->system;
me->state = my_new_state;
other->state = CoroutineState_Active;
me->sys->active = other;
system->condition_variable_signal(other->cv);
if (control == CoroutinePassControl_BlockMe){
for (;me->state != CoroutineState_Active;){
system->condition_variable_wait(me->cv, me->sys->lock);
}
}
}
internal void
coroutine_main(void *ptr){
Coroutine *me = (Coroutine*)ptr;
System_Functions *system = me->system;
// NOTE(allen): Init handshake
Assert(me->state == CoroutineState_Dead);
system->mutex_acquire(me->sys->lock);
me->sys->did_init = true;
system->condition_variable_signal(me->sys->init_cv);
for (;;){
// NOTE(allen): Wait until someone wakes us up, then go into our procedure.
for (;me->state != CoroutineState_Active;){
system->condition_variable_wait(me->cv, me->sys->lock);
}
Assert(me->type != CoroutineType_Root);
Assert(me->yield_ctx != 0);
Assert(me->function != 0);
me->function(me);
// 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);
me->function = 0;
}
}
internal void
coroutine_sub_init(Coroutine *co, Coroutine_Group *sys){
System_Functions *system = sys->system;
block_zero_struct(co);
co->system = system;
co->sys = sys;
co->state = CoroutineState_Dead;
co->type = CoroutineType_Sub;
co->cv = system->condition_variable_make();
sys->did_init = false;
co->thread = system->thread_launch(coroutine_main, co);
for (;!sys->did_init;){
system->condition_variable_wait(sys->init_cv, sys->lock);
}
}
internal void
coroutine_system_init(System_Functions *system, Coroutine_Group *sys){
sys->arena = make_arena_system(system);
sys->system = system;
Coroutine *root = &sys->root;
sys->lock = system->mutex_make();
sys->init_cv = system->condition_variable_make();
sys->active = root;
block_zero_struct(root);
root->system = system;
root->sys = sys;
root->state = CoroutineState_Active;
root->type = CoroutineType_Root;
root->cv = system->condition_variable_make();
sys->unused = 0;
system->mutex_acquire(sys->lock);
}
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);
result->function = func;
return(result);
}
internal Coroutine*
coroutine_run(Coroutine_Group *sys, Coroutine *other, void *in, void *out_arena){
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