-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpipe.c
More file actions
60 lines (49 loc) · 1.09 KB
/
pipe.c
File metadata and controls
60 lines (49 loc) · 1.09 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
#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]);
}
task_struct *cur_task = first_task;
if (cur_task) {
do {
if (cur_task->state == TASK_WAIT_READ) {
handle_read(cur_task);
}
cur_task = cur_task->next;
} while (cur_task != first_task);
}
}