-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtime.h
130 lines (115 loc) · 2.07 KB
/
runtime.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#ifndef RUNTIME_H
#define RUNTIME_H
#ifndef bool
#define bool int
#define true 1
#define false 0
#endif
#define nil ((void *)0)
#define len(p) (sizeof(p) / sizeof(p[0]))
#define G_IDLE 0
#define G_RUNNABLE 1
#define G_RUNNING 2
#define G_WAITING 3
#define G_DEAD 4
#define EV_READ 1
#define EV_WRITE 2
typedef struct G G;
typedef struct Event Event;
typedef struct Gobuf Gobuf;
typedef struct Timers Timers;
typedef struct Sched Sched;
typedef struct Chan Chan;
typedef struct WaitQ WaitQ;
typedef struct Case Case;
struct Case {
G *g;
Chan *c;
int op;
};
struct WaitQ {
G *head;
G *tail;
};
struct Chan {
void *ptr;
int len;
int cap;
int elsize;
int closed;
int sendx;
int recvx;
WaitQ sendq;
WaitQ recvq;
};
struct Gobuf {
void *sp;
void *pc;
void *gr[8];
};
struct Event {
int fd;
int mode;
long when;
long period;
void (*f)(void *, long);
void *arg;
G *g;
};
struct Timers {
Event **heap;
int len;
int cap;
};
struct G {
void *stackguard;
G *next;
Gobuf ctx;
Event ev;
int state;
int mp;
int id;
void *p;
};
struct Sched {
G *g; /* current g running on this scheduler */
G *gfree;
G *ghead;
G *gtail;
Timers t;
int genid;
/* debug */
int glen;
};
void debug(const char *, ...);
void throw(const char *, ...);
void setcloexec(int);
void setnonblock(int);
G *getg(void);
void ready(G *);
void yield0(int);
void yield1(void);
void yield(void);
void schedule(void);
void spawn(void (*)(void *), void *, int);
int await(int, int, long, long);
void asleep(long);
void schedinit(void);
void netpollinit(void);
int netpollopen(Event *);
void netpollready(G **, Event *, int);
G *netpoll(long);
long timens(void);
void timersinit(Timers *);
void timersopen(Timers *, Event *);
long timerscheck(Timers *);
void hookinit(void);
Chan *makechan(int, int);
bool chansend(Chan *, void *, bool);
bool chanrecv(Chan *, void *, bool);
void chanclose(Chan *);
int dial0(int, const char *, unsigned short);
int listen0(int, const char *, unsigned short);
int dialunix0(int, const char *);
int listenunix0(int, const char *);
#endif