The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.
Please select the desired protocol below to get the URL.
This URL has Read-Only access.
main_repo / deps / liboi / oi_async.h @ 40c0f755
History | View | Annotate | Download (4.79 KB)
1 |
#include <ev.h> |
---|---|
2 |
#include <pthread.h> |
3 |
#include <netdb.h> |
4 |
#include <oi.h> |
5 |
|
6 |
#ifndef oi_async_h
|
7 |
#define oi_async_h
|
8 |
#ifdef __cplusplus
|
9 |
extern "C" { |
10 |
#endif
|
11 |
|
12 |
typedef struct oi_async oi_async; |
13 |
typedef struct oi_task oi_task; |
14 |
|
15 |
struct oi_async {
|
16 |
/* private */
|
17 |
ev_async watcher; |
18 |
struct ev_loop *loop;
|
19 |
|
20 |
oi_queue finished_tasks; |
21 |
oi_queue new_tasks; |
22 |
|
23 |
/* public */
|
24 |
void *data;
|
25 |
}; |
26 |
|
27 |
typedef void (*oi_task_int_cb)(oi_task *, int result); |
28 |
typedef void (*oi_task_uint_cb)(oi_task *, unsigned int result); |
29 |
typedef void (*oi_task_ssize_cb)(oi_task *, ssize_t result); |
30 |
|
31 |
struct oi_task {
|
32 |
/* private */
|
33 |
oi_async *async; |
34 |
oi_queue queue; |
35 |
int type;
|
36 |
union {
|
37 |
|
38 |
struct {
|
39 |
const char *pathname; |
40 |
int flags;
|
41 |
mode_t mode; |
42 |
oi_task_int_cb cb; |
43 |
int result;
|
44 |
} open; |
45 |
|
46 |
struct {
|
47 |
int fd;
|
48 |
void *buf;
|
49 |
size_t count; |
50 |
oi_task_ssize_cb cb; |
51 |
ssize_t result; |
52 |
} read; |
53 |
|
54 |
struct {
|
55 |
int fd;
|
56 |
const void *buf; |
57 |
size_t count; |
58 |
oi_task_ssize_cb cb; |
59 |
ssize_t result; |
60 |
} write; |
61 |
|
62 |
struct {
|
63 |
int fd;
|
64 |
oi_task_int_cb cb; |
65 |
int result;
|
66 |
} close; |
67 |
|
68 |
struct {
|
69 |
unsigned int seconds; |
70 |
oi_task_uint_cb cb; |
71 |
unsigned int result; |
72 |
} sleep; |
73 |
|
74 |
struct {
|
75 |
int out_fd;
|
76 |
int in_fd;
|
77 |
off_t offset; |
78 |
size_t count; |
79 |
oi_task_ssize_cb cb; |
80 |
ssize_t result; |
81 |
} eio__sendfile; |
82 |
|
83 |
struct {
|
84 |
const char *nodename; /* restrict ? */ |
85 |
const char *servname; /* restrict ? */ |
86 |
struct addrinfo *hints;
|
87 |
struct addrinfo **res; /* restrict ? */ |
88 |
oi_task_int_cb cb; |
89 |
int result;
|
90 |
} getaddrinfo; |
91 |
|
92 |
struct {
|
93 |
const char *path; |
94 |
struct stat *buf;
|
95 |
oi_task_int_cb cb; |
96 |
int result;
|
97 |
} lstat; |
98 |
|
99 |
} params; |
100 |
|
101 |
/* read-only */
|
102 |
volatile unsigned active:1; |
103 |
int errorno;
|
104 |
|
105 |
/* public */
|
106 |
void *data;
|
107 |
}; |
108 |
|
109 |
void oi_async_init (oi_async *);
|
110 |
void oi_async_attach (struct ev_loop *loop, oi_async *); |
111 |
void oi_async_detach (oi_async *);
|
112 |
void oi_async_submit (oi_async *, oi_task *);
|
113 |
|
114 |
/* To submit a task for async processing
|
115 |
* (0) allocate memory for your task
|
116 |
* (1) initialize the task with one of the functions below
|
117 |
* (2) optionally set the task->data pointer
|
118 |
* (3) oi_async_submit() the task
|
119 |
*/
|
120 |
|
121 |
enum { OI_TASK_OPEN
|
122 |
, OI_TASK_READ |
123 |
, OI_TASK_WRITE |
124 |
, OI_TASK_CLOSE |
125 |
, OI_TASK_SLEEP |
126 |
, OI_TASK_SENDFILE |
127 |
, OI_TASK_GETADDRINFO |
128 |
, OI_TASK_LSTAT |
129 |
}; |
130 |
|
131 |
#define oi_task_init_common(task, _type) do {\ |
132 |
(task)->active = 0;\
|
133 |
(task)->async = NULL;\
|
134 |
(task)->type = _type;\ |
135 |
} while(0) |
136 |
|
137 |
static inline void |
138 |
oi_task_init_open(oi_task *t, oi_task_int_cb cb, const char *pathname, int flags, mode_t mode) |
139 |
{ |
140 |
oi_task_init_common(t, OI_TASK_OPEN); |
141 |
t->params.open.cb = cb; |
142 |
t->params.open.pathname = pathname; |
143 |
t->params.open.flags = flags; |
144 |
t->params.open.mode = mode; |
145 |
} |
146 |
|
147 |
static inline void |
148 |
oi_task_init_read(oi_task *t, oi_task_ssize_cb cb, int fd, void *buf, size_t count) |
149 |
{ |
150 |
oi_task_init_common(t, OI_TASK_READ); |
151 |
t->params.read.cb = cb; |
152 |
t->params.read.fd = fd; |
153 |
t->params.read.buf = buf; |
154 |
t->params.read.count = count; |
155 |
} |
156 |
|
157 |
static inline void |
158 |
oi_task_init_write(oi_task *t, oi_task_ssize_cb cb, int fd, const void *buf, size_t count) |
159 |
{ |
160 |
oi_task_init_common(t, OI_TASK_WRITE); |
161 |
t->params.write.cb = cb; |
162 |
t->params.write.fd = fd; |
163 |
t->params.write.buf = buf; |
164 |
t->params.write.count = count; |
165 |
} |
166 |
|
167 |
static inline void |
168 |
oi_task_init_close(oi_task *t, oi_task_int_cb cb, int fd)
|
169 |
{ |
170 |
oi_task_init_common(t, OI_TASK_CLOSE); |
171 |
t->params.close.cb = cb; |
172 |
t->params.close.fd = fd; |
173 |
} |
174 |
|
175 |
static inline void |
176 |
oi_task_init_sleep(oi_task *t, oi_task_uint_cb cb, unsigned int seconds) |
177 |
{ |
178 |
oi_task_init_common(t, OI_TASK_SLEEP); |
179 |
t->params.sleep.cb = cb; |
180 |
t->params.sleep.seconds = seconds; |
181 |
} |
182 |
|
183 |
static inline void |
184 |
oi_task_init_sendfile(oi_task *t, oi_task_ssize_cb cb, int out_fd, int in_fd, off_t offset, size_t count) |
185 |
{ |
186 |
oi_task_init_common(t, OI_TASK_SENDFILE); |
187 |
t->params.eio__sendfile.cb = cb; |
188 |
t->params.eio__sendfile.out_fd = out_fd; |
189 |
t->params.eio__sendfile.in_fd = in_fd; |
190 |
t->params.eio__sendfile.offset = offset; |
191 |
t->params.eio__sendfile.count = count; |
192 |
} |
193 |
|
194 |
static inline void |
195 |
oi_task_init_getaddrinfo(oi_task *t, oi_task_int_cb cb, const char *node, |
196 |
const char *service, struct addrinfo *hints, struct addrinfo **res) |
197 |
{ |
198 |
oi_task_init_common(t, OI_TASK_GETADDRINFO); |
199 |
t->params.getaddrinfo.cb = cb; |
200 |
t->params.getaddrinfo.nodename = node; |
201 |
t->params.getaddrinfo.servname = service; |
202 |
t->params.getaddrinfo.hints = hints; |
203 |
t->params.getaddrinfo.res = res; |
204 |
} |
205 |
|
206 |
static inline void |
207 |
oi_task_init_lstat(oi_task *t, oi_task_int_cb cb, const char *path, struct stat *buf) |
208 |
{ |
209 |
oi_task_init_common(t, OI_TASK_LSTAT); |
210 |
t->params.lstat.cb = cb; |
211 |
t->params.lstat.path = path; |
212 |
t->params.lstat.buf = buf; |
213 |
} |
214 |
|
215 |
#ifdef __cplusplus
|
216 |
} |
217 |
#endif
|
218 |
#endif /* oi_async_h */ |