2019-08-04 05:36:13 +00:00
|
|
|
/*
|
|
|
|
* Mr. 4th Dimention - Allen Webster
|
|
|
|
*
|
|
|
|
* 03.08.2019
|
|
|
|
*
|
|
|
|
* Coroutine implementation from thread+mutex+cv
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
// TOP
|
|
|
|
|
|
|
|
#if !defined(FRED_COROUTINE_H)
|
|
|
|
#define FRED_COROUTINE_H
|
|
|
|
|
|
|
|
typedef void Coroutine_Function(struct Coroutine *head);
|
|
|
|
|
|
|
|
typedef u32 Coroutine_State;
|
|
|
|
enum{
|
|
|
|
CoroutineState_Dead,
|
|
|
|
CoroutineState_Active,
|
|
|
|
CoroutineState_Inactive,
|
|
|
|
CoroutineState_Waiting,
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef u32 Coroutine_Type;
|
|
|
|
enum{
|
|
|
|
CoroutineType_Uninitialized,
|
|
|
|
CoroutineType_Root,
|
|
|
|
CoroutineType_Sub,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Coroutine{
|
|
|
|
Coroutine *next;
|
2019-10-11 01:40:10 +00:00
|
|
|
Thread_Context *tctx;
|
2019-08-04 05:36:13 +00:00
|
|
|
void *in;
|
|
|
|
void *out;
|
|
|
|
System_Thread thread;
|
|
|
|
System_Condition_Variable cv;
|
|
|
|
struct Coroutine_Group *sys;
|
2019-10-01 22:03:21 +00:00
|
|
|
Coroutine_Function *func;
|
2019-08-04 05:36:13 +00:00
|
|
|
Coroutine *yield_ctx;
|
|
|
|
Coroutine_State state;
|
|
|
|
Coroutine_Type type;
|
2019-10-25 23:33:50 +00:00
|
|
|
void *user_data;
|
2019-08-04 05:36:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Coroutine_Group{
|
|
|
|
Arena arena;
|
|
|
|
System_Mutex lock;
|
|
|
|
System_Condition_Variable init_cv;
|
|
|
|
b32 did_init;
|
|
|
|
Coroutine *active;
|
|
|
|
Coroutine *unused;
|
|
|
|
Coroutine root;
|
|
|
|
};
|
|
|
|
|
|
|
|
////////////////////////////////
|
|
|
|
|
|
|
|
typedef i32 Coroutine_Pass_Control;
|
|
|
|
enum{
|
|
|
|
CoroutinePassControl_ExitMe,
|
|
|
|
CoroutinePassControl_BlockMe,
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// BOTTOM
|