-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsyscall.c
More file actions
286 lines (268 loc) · 6.47 KB
/
syscall.c
File metadata and controls
286 lines (268 loc) · 6.47 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <errno.h>
#include <unistd.h>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <errno.h>
#include "rbtree.h"
#include "co.h"
struct sleep_info {
void *co;
struct timeval t;
struct rb_node rb;
};
static struct rb_root sleep_info_root = RB_ROOT;
static int key_sleep = -1;
//cokill,确保能销毁协程的sleep_info信息
static void sleep_info_cleanup(void *d)
{
struct sleep_info *si = d;
rb_erase(&si->rb, &sleep_info_root);
free(si);
}
static void __init init_syscall()
{
key_sleep = co_key_create(sleep_info_cleanup);
}
static inline int sleep_info_cmp(struct sleep_info *s1, struct sleep_info *s2)
{
int r = intcmp(s1->t.tv_sec, s2->t.tv_sec);
return r != 0 ? r : intcmp(s1->t.tv_usec, s2->t.tv_usec);
}
//对usleep的封装
int cousleep(useconds_t us)
{
if(unlikely(coid() == 0)) {
return usleep(us);
}
int ret = 0;
struct sleep_info *si = malloc(sizeof(*si));
struct sleep_info cur;
time_t tv_sec = us / 1000000;
long tv_usec = us % 1000000;
if(!si) {
errno = ENOMEM;
return -1;
}
si->co = coself();
gettimeofday(&si->t, NULL);
si->t.tv_sec += tv_sec;
si->t.tv_usec += tv_usec;
if(si->t.tv_usec > 1000000) {
si->t.tv_sec++;
si->t.tv_usec -= 1000000;
}
rb_init_node(&si->rb);
rb_insert(&sleep_info_root, si, rb, sleep_info_cmp);
co_setspecific(key_sleep, si);
cowait();
co_setspecific(key_sleep, NULL);
rb_erase(&si->rb, &sleep_info_root);
gettimeofday(&cur.t, NULL);
if(sleep_info_cmp(si, &cur) > 0) {
errno = EINTR;
ret = -1;
}
free(si);
return ret;
}
//对sleep的封装
unsigned int cosleep(unsigned int seconds)
{
if(unlikely(coid() == 0)) {
return sleep(seconds);
}
struct sleep_info *si = malloc(sizeof(*si));
struct sleep_info cur;
if(!si) {
errno = ENOMEM;
return seconds;
}
si->co = coself();
gettimeofday(&si->t, NULL);
si->t.tv_sec += seconds;
rb_init_node(&si->rb);
rb_insert(&sleep_info_root, si, rb, sleep_info_cmp);
co_setspecific(key_sleep, si);
cowait();
co_setspecific(key_sleep, NULL);
rb_erase(&si->rb, &sleep_info_root);
gettimeofday(&cur.t, NULL);
if(sleep_info_cmp(si, &cur) > 0) {
long d = si->t.tv_usec - cur.t.tv_usec;
int t = 0;
if(d < -500000) t = -1;
else if(d > 500000) t = 1;
return si->t.tv_sec - cur.t.tv_sec - t;
}
free(si);
return 0;
}
//对nanosleep的封装
int conanosleep(const struct timespec *req, struct timespec *rem)
{
if(unlikely(coid() == 0)) {
return nanosleep(req, rem);
}
int ret = 0;
struct sleep_info *si = malloc(sizeof(*si));
struct sleep_info cur;
time_t tv_sec = req->tv_sec;
long tv_usec = req->tv_nsec / 1000;
if(!si) {
*rem = *req;
errno = ENOMEM;
return -1;
}
si->co = coself();
gettimeofday(&si->t, NULL);
si->t.tv_sec += tv_sec;
si->t.tv_usec += tv_usec;
if(si->t.tv_usec > 1000000) {
si->t.tv_sec++;
si->t.tv_usec -= 1000000;
}
rb_init_node(&si->rb);
rb_insert(&sleep_info_root, si, rb, sleep_info_cmp);
co_setspecific(key_sleep, si);
cowait();
co_setspecific(key_sleep, NULL);
rb_erase(&si->rb, &sleep_info_root);
gettimeofday(&cur.t, NULL);
if(sleep_info_cmp(si, &cur) > 0) {
errno = EINTR;
ret = -1;
if(rem) {
long us = si->t.tv_usec - cur.t.tv_usec;
rem->tv_sec = si->t.tv_sec - cur.t.tv_sec;
if(us < 0) {
rem->tv_sec--;
us += 1000000;
}
rem->tv_nsec = us * 1000 + req->tv_nsec % 1000;
}
}
free(si);
return ret;
}
//返回值是微妙
static long co_timeout()
{
struct rb_node *rb_node;
rb_node = rb_first(&sleep_info_root);
if(rb_node) {
struct timeval tv;
struct sleep_info *si;
long us;
si = rb_entry(rb_node, struct sleep_info, rb);
gettimeofday(&tv, NULL);
us = (si->t.tv_sec - tv.tv_sec) * 1000000 + (si->t.tv_usec - tv.tv_usec);
return us < 0 ? 0 : us;
}
return -1;
}
//唤醒sleep到期的协程
static void co_wakeup()
{
if(rb_first(&sleep_info_root)) {
struct sleep_info *si;
struct sleep_info cur;
gettimeofday(&cur.t, NULL);
rb_for_each_entry(si, &sleep_info_root, rb) {
if(sleep_info_cmp(si, &cur) <= 0) {
__cowakeup(si->co);
}
else
break;
}
}
}
//对read的封装
int coread(int fd, void *buf, size_t count)
{
int ret, len = 0;
reread:
ret = read(fd, buf+len, count-len);
if(ret < 0) {
if(errno == EINTR)
goto reread;
if(errno == EAGAIN) {
modify_event(fd, EPOLLIN);
cowait();
goto reread;
}
}
return ret;
}
//对read的封装,读满count个字节
int coread1(int fd, void *buf, size_t count)
{
int ret, len = 0;
reread:
ret = read(fd, buf+len, count-len);
if(ret < 0) {
if(errno == EINTR)
goto reread;
if(errno == EAGAIN) {
modify_event(fd, EPOLLIN);
cowait();
goto reread;
}
return ret;
}
len += ret;
if(ret == 0 || len == count)
return len;
goto reread;
}
//对write的封装
int cowrite(int fd, const void *buf, size_t count)
{
int ret, len = 0;
rewrite:
ret = write(fd, buf+len, count-len);
if(ret < 0) {
if(errno == EINTR)
goto rewrite;
if(errno == EAGAIN) {
modify_event(fd, EPOLLOUT);
cowait();
goto rewrite;
}
return ret;
}
len += ret;
if(len == count)
return len;
goto rewrite;
}
//对accept的封装
int coaccept(int sockfd, struct sockaddr *addr, socklen_t *addrlen)
{
int ret;
reaccept:
ret = accept(sockfd, addr, addrlen);
if(ret < 0) {
if(errno == EINTR)
goto reaccept;
if(errno == EAGAIN) {
modify_event(sockfd, EPOLLIN);
cowait();
goto reaccept;
}
}
return ret;
}
extern int event_loop(int timeout);
extern int __cotree_is_empty();
int coloop()
{
int loop;
while(schedule());
loop = event_loop(co_timeout());
co_wakeup();
return loop || __cotree_is_empty();
}