-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoop.h
102 lines (84 loc) · 1.54 KB
/
coop.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
#include <setjmp.h>
#include "sys/types.h"
#include <fcntl.h>
#include "pthread.h"
#define STACK_SIZE 2 * 1024
#define INIT 0
#define EXIT 1
#define SCHED 2
enum status {
CREATED,
RUNNABLE,
RUNNING,
WAITING_IO,
};
struct coroutine {
int id;
enum status status;
jmp_buf context;
void (*func)(void*);
void* args;
void* stack_top;
void* stack_bottom;
void* io_response;
};
void coop(void (*func)(void*), void* args);
ssize_t coop_read(int fd, void *buf, size_t count);
ssize_t coop_write(int fd, void *buf, size_t count);
int coop_open(const char* path, int oflag, mode_t mode);
int coop_close(int fd);
void coop_print(const char* str);
void yield();
struct node {
void* data;
struct node* next;
};
struct list {
struct node* head;
struct node* tail;
};
struct blocking_queue {
struct list l;
pthread_mutex_t mu;
pthread_cond_t empty;
};
struct scheduler {
jmp_buf context;
struct coroutine* current;
struct list coop_list;
pthread_t worker_thread;
struct blocking_queue io_queue;
};
enum io_type {
IO_READ,
IO_WRITE,
IO_OPEN,
IO_CLOSE,
};
struct io_rw_request {
int fd;
void *buf;
size_t count;
};
struct io_rw_response {
ssize_t n;
};
struct io_open_request {
const char* path;
int oflag;
mode_t mode;
};
struct io_open_response {
int fd;
};
struct io_close_request {
int fd;
};
struct io_close_response {
int r;
};
struct io_request {
struct coroutine* coop;
enum io_type type;
void *args;
};