-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcoroutine.h
80 lines (64 loc) · 1.2 KB
/
coroutine.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "hook.h"
#include "aio.h"
#include "array.h"
#include "heap.h"
#include "timer.h"
#include "list.h"
#include "ctx.h"
#include "macros.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef enum
{
CO_SUSPEND,
CO_FINISH
}co_status_t;
struct co_t;
typedef int cid_t;
typedef void * (*co_entry_t)(void *args);
typedef struct co_t
{
ctx_t ctx;
ctx_t main;
co_entry_t entry;
co_status_t status;
list_t * wait_list;
int id;
char * stack;
struct co_t * last;
}co_t;
typedef struct
{
int inited;
co_t * running;
list_t * free_list;
array_t * co_pool;
heap_t * timer_mgr;
aio_t * aio;
} env_t;
static int CO_ID_INVALID = -1;
typedef struct
{
co_t * co;
env_t * env;
}awaitable_t;
void co_init();
co_t * co_create(void *entry, void * args);
inline void * co_resume(co_t * id);
inline void co_yield(void * ret);
awaitable_t co_start(void * entrry, void * args);
void *co_await(awaitable_t awaitable);
co_t * co_running();
void co_finish();
int co_count();
void co_loop();
inline env_t * thread_env();
void * co_main(void * entry, void * args);
#ifdef __cplusplus
}
#endif