-
Notifications
You must be signed in to change notification settings - Fork 9
/
coroutine.h
executable file
·54 lines (47 loc) · 1.13 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
#pragma once
#include <setjmp.h>
#define MAXCOROUTINES 10000 //the allowable maximum of threads, you can change it.
#define STACK_MIN (4096*32) // 128 Kbytes
typedef enum STATUS Status;
enum STATUS {NONE, READY, SUSPEND, RUNNING, DEAD};
typedef struct __co Coroutine;
struct __co
{
jmp_buf regs;
unsigned long stacktop;
unsigned long stacksize;
void *stack;
void *recv;
};
typedef struct __file Cofile;
struct __file
{
int cid;
Coroutine *co;
Cofile *next;
Status op;
};
typedef struct __stack Stack_t;
struct __stack
{
char stk[STACK_MIN];
} __attribute__((aligned(16)));
typedef struct __schedular Sched_t;
struct __schedular
{
Stack_t stack;
Cofile files[MAXCOROUTINES];
Cofile *freefiles;
Coroutine cmain;
int running;
int relay;
unsigned long stacktop;
unsigned long stacksize;
};
Sched_t* coroutine_open(void);
void coroutine_close(Sched_t **);
int coroutine_new(Sched_t *, void *(*start_rtn)(Sched_t *, void *), void *);
int coroutine_resume(Sched_t *, int, void *);
int coroutine_status(Sched_t *, int);
int coroutine_running(Sched_t *);
int coroutine_yield(Sched_t *, void *);