forked from rasendubi/akernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipe.c
56 lines (45 loc) · 1 KB
/
pipe.c
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
#include <pipe.h>
#include <svc.h>
#include <growbuf.h>
growbuf *pipes[PIPE_LIMIT];
void handle_read(task_struct *ts);
void handle_write(task_struct *ts);
void init_pipes(void) {
for (size_t i = 0; i < PIPE_LIMIT; ++i) {
pipes[i] = growbuf_new();
}
}
void handle_read(task_struct *ts) {
unsigned *task = ts->stack;
ts->state = TASK_READY;
if (task[r0] > PIPE_LIMIT) {
task[r0] = -1;
return;
}
growbuf *pipe = pipes[task[r0]];
if (grow_len(pipe) < task[r2]) {
ts->state = TASK_WAIT_READ;
} else {
char *buf = (char*)task[r1];
for (size_t i = 0; i < task[r2]; ++i) {
buf[i] = grow_pop(pipe);
}
}
}
void handle_write(task_struct *ts) {
unsigned *task = ts->stack;
if (task[r0] > PIPE_LIMIT) {
task[r0] = -1;
return;
}
growbuf *pipe = pipes[task[r0]];
const char *buf = (const char *)task[r1];
for (size_t i = 0; i < task[r2]; ++i) {
grow_push(pipe, buf[i]);
}
for (size_t i = 0; i < task_count; ++i) {
if (tasks[i].state == TASK_WAIT_READ) {
handle_read(&tasks[i]);
}
}
}