… | |
… | |
9 | |
9 | |
10 | #include <pthread.h> |
10 | #include <pthread.h> |
11 | |
11 | |
12 | #include <stddef.h> |
12 | #include <stddef.h> |
13 | #include <errno.h> |
13 | #include <errno.h> |
|
|
14 | #include <sys/time.h> |
|
|
15 | #include <sys/select.h> |
14 | #include <sys/types.h> |
16 | #include <sys/types.h> |
15 | #include <sys/stat.h> |
17 | #include <sys/stat.h> |
16 | #include <limits.h> |
18 | #include <limits.h> |
17 | #include <unistd.h> |
19 | #include <unistd.h> |
18 | #include <fcntl.h> |
20 | #include <fcntl.h> |
… | |
… | |
53 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
55 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
54 | REQ_FSYNC, REQ_FDATASYNC, |
56 | REQ_FSYNC, REQ_FDATASYNC, |
55 | REQ_UNLINK, REQ_RMDIR, REQ_RENAME, |
57 | REQ_UNLINK, REQ_RMDIR, REQ_RENAME, |
56 | REQ_READDIR, |
58 | REQ_READDIR, |
57 | REQ_LINK, REQ_SYMLINK, |
59 | REQ_LINK, REQ_SYMLINK, |
|
|
60 | REQ_GROUP, REQ_NOP, |
|
|
61 | REQ_SLEEP, |
58 | }; |
62 | }; |
59 | |
63 | |
60 | #define AIO_CB_KLASS "IO::AIO::CB" |
64 | #define AIO_REQ_KLASS "IO::AIO::REQ" |
|
|
65 | #define AIO_GRP_KLASS "IO::AIO::GRP" |
61 | |
66 | |
62 | typedef struct aio_cb |
67 | typedef struct aio_cb |
63 | { |
68 | { |
64 | struct aio_cb *grp_prev, *grp_next; |
|
|
65 | struct aio_grp *grp; |
|
|
66 | |
|
|
67 | struct aio_cb *volatile next; |
69 | struct aio_cb *volatile next; |
|
|
70 | |
|
|
71 | struct aio_cb *grp, *grp_prev, *grp_next, *grp_first; |
68 | |
72 | |
69 | SV *self; /* the perl counterpart of this request, if any */ |
73 | SV *self; /* the perl counterpart of this request, if any */ |
70 | |
74 | |
71 | SV *data, *callback; |
75 | SV *data, *callback; |
72 | SV *fh, *fh2; |
76 | SV *fh, *fh2; |
… | |
… | |
79 | int type; |
83 | int type; |
80 | int fd, fd2; |
84 | int fd, fd2; |
81 | int errorno; |
85 | int errorno; |
82 | STRLEN dataoffset; |
86 | STRLEN dataoffset; |
83 | mode_t mode; /* open */ |
87 | mode_t mode; /* open */ |
|
|
88 | unsigned char pri; |
84 | unsigned char cancelled; |
89 | unsigned char flags; |
85 | } aio_cb; |
90 | } aio_cb; |
|
|
91 | |
|
|
92 | enum { |
|
|
93 | FLAG_CANCELLED = 0x01, |
|
|
94 | }; |
86 | |
95 | |
87 | typedef aio_cb *aio_req; |
96 | typedef aio_cb *aio_req; |
88 | typedef aio_cb *aio_req_ornot; |
97 | typedef aio_cb *aio_req_ornot; |
89 | |
98 | |
90 | static int started, wanted; |
99 | static int started, wanted; |
… | |
… | |
97 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
106 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
98 | |
107 | |
99 | static volatile aio_req reqs, reqe; /* queue start, queue end */ |
108 | static volatile aio_req reqs, reqe; /* queue start, queue end */ |
100 | static volatile aio_req ress, rese; /* queue start, queue end */ |
109 | static volatile aio_req ress, rese; /* queue start, queue end */ |
101 | |
110 | |
102 | typedef struct aio_grp |
111 | static void req_invoke (aio_req req); |
103 | { |
112 | static void req_free (aio_req req); |
104 | struct aio_cb *first, *last; |
|
|
105 | SV *callback; |
|
|
106 | int busycount; |
|
|
107 | } aio_grp; |
|
|
108 | |
|
|
109 | static void aio_grp_begin (aio_grp *grp) |
|
|
110 | { |
|
|
111 | ++grp->busycount; |
|
|
112 | } |
|
|
113 | |
|
|
114 | static void aio_grp_end (aio_grp *grp) |
|
|
115 | { |
|
|
116 | --grp->busycount; |
|
|
117 | |
|
|
118 | if (grp->busycount) |
|
|
119 | return; |
|
|
120 | |
|
|
121 | SvREFCNT_dec (grp->callback); |
|
|
122 | grp->callback = 0; |
|
|
123 | } |
|
|
124 | |
|
|
125 | static aio_grp *aio_grp_new () |
|
|
126 | { |
|
|
127 | aio_grp *grp; |
|
|
128 | |
|
|
129 | Newz (0, grp, 1, aio_grp); |
|
|
130 | aio_grp_begin (grp); |
|
|
131 | |
|
|
132 | return grp; |
|
|
133 | } |
|
|
134 | |
113 | |
135 | /* must be called at most once */ |
114 | /* must be called at most once */ |
136 | static SV *req_sv (aio_req req) |
115 | static SV *req_sv (aio_req req, const char *klass) |
137 | { |
116 | { |
|
|
117 | if (!req->self) |
|
|
118 | { |
138 | req->self = (SV *)newHV (); |
119 | req->self = (SV *)newHV (); |
139 | sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0); |
120 | sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0); |
|
|
121 | } |
140 | |
122 | |
141 | return sv_bless (newRV_noinc (req->self), gv_stashpv (AIO_CB_KLASS, 1)); |
123 | return sv_2mortal (sv_bless (newRV_inc (req->self), gv_stashpv (klass, 1))); |
142 | } |
124 | } |
143 | |
125 | |
144 | static aio_req SvAIO_REQ (SV *sv) |
126 | static aio_req SvAIO_REQ (SV *sv) |
145 | { |
127 | { |
|
|
128 | MAGIC *mg; |
|
|
129 | |
146 | if (!sv_derived_from (sv, AIO_CB_KLASS) || !SvROK (sv)) |
130 | if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv)) |
147 | croak ("object of class " AIO_CB_KLASS " expected"); |
131 | croak ("object of class " AIO_REQ_KLASS " expected"); |
148 | |
132 | |
149 | MAGIC *mg = mg_find (SvRV (sv), PERL_MAGIC_ext); |
133 | mg = mg_find (SvRV (sv), PERL_MAGIC_ext); |
150 | |
134 | |
151 | return mg ? (aio_req)mg->mg_ptr : 0; |
135 | return mg ? (aio_req)mg->mg_ptr : 0; |
152 | } |
136 | } |
153 | |
137 | |
154 | static void req_free (aio_req req) |
138 | static void aio_grp_feed (aio_req grp) |
155 | { |
139 | { |
156 | if (req->self) |
140 | while (grp->length < grp->fd2 && !(grp->flags & FLAG_CANCELLED)) |
157 | { |
|
|
158 | sv_unmagic (req->self, PERL_MAGIC_ext); |
|
|
159 | SvREFCNT_dec (req->self); |
|
|
160 | } |
141 | { |
|
|
142 | int old_len = grp->length; |
161 | |
143 | |
162 | if (req->data) |
144 | if (grp->fh2 && SvOK (grp->fh2)) |
163 | SvREFCNT_dec (req->data); |
145 | { |
|
|
146 | dSP; |
164 | |
147 | |
165 | if (req->fh) |
148 | ENTER; |
166 | SvREFCNT_dec (req->fh); |
149 | SAVETMPS; |
|
|
150 | PUSHMARK (SP); |
|
|
151 | XPUSHs (req_sv (grp, AIO_GRP_KLASS)); |
|
|
152 | PUTBACK; |
|
|
153 | call_sv (grp->fh2, G_VOID | G_EVAL); |
|
|
154 | SPAGAIN; |
|
|
155 | FREETMPS; |
|
|
156 | LEAVE; |
|
|
157 | } |
167 | |
158 | |
168 | if (req->fh2) |
159 | /* stop if no progress has been made */ |
|
|
160 | if (old_len == grp->length) |
|
|
161 | { |
169 | SvREFCNT_dec (req->fh2); |
162 | SvREFCNT_dec (grp->fh2); |
170 | |
163 | grp->fh2 = 0; |
171 | if (req->statdata) |
164 | break; |
172 | Safefree (req->statdata); |
165 | } |
173 | |
166 | } |
174 | if (req->callback) |
|
|
175 | SvREFCNT_dec (req->callback); |
|
|
176 | |
|
|
177 | if (req->type == REQ_READDIR && req->result >= 0) |
|
|
178 | free (req->data2ptr); |
|
|
179 | |
|
|
180 | Safefree (req); |
|
|
181 | } |
167 | } |
182 | |
168 | |
183 | static void |
169 | static void aio_grp_dec (aio_req grp) |
184 | poll_wait () |
170 | { |
|
|
171 | --grp->length; |
|
|
172 | |
|
|
173 | /* call feeder, if applicable */ |
|
|
174 | aio_grp_feed (grp); |
|
|
175 | |
|
|
176 | /* finish, if done */ |
|
|
177 | if (!grp->length && grp->fd) |
|
|
178 | { |
|
|
179 | req_invoke (grp); |
|
|
180 | req_free (grp); |
|
|
181 | } |
|
|
182 | } |
|
|
183 | |
|
|
184 | static void poll_wait () |
185 | { |
185 | { |
186 | if (nreqs && !ress) |
186 | if (nreqs && !ress) |
187 | { |
187 | { |
188 | fd_set rfd; |
188 | fd_set rfd; |
189 | FD_ZERO(&rfd); |
189 | FD_ZERO(&rfd); |
… | |
… | |
191 | |
191 | |
192 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
192 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
193 | } |
193 | } |
194 | } |
194 | } |
195 | |
195 | |
196 | static int |
196 | static void req_invoke (aio_req req) |
197 | poll_cb () |
197 | { |
|
|
198 | dSP; |
|
|
199 | int errorno = errno; |
|
|
200 | |
|
|
201 | if (req->flags & FLAG_CANCELLED || !SvOK (req->callback)) |
|
|
202 | return; |
|
|
203 | |
|
|
204 | errno = req->errorno; |
|
|
205 | |
|
|
206 | ENTER; |
|
|
207 | SAVETMPS; |
|
|
208 | PUSHMARK (SP); |
|
|
209 | EXTEND (SP, 1); |
|
|
210 | |
|
|
211 | switch (req->type) |
|
|
212 | { |
|
|
213 | case REQ_READDIR: |
|
|
214 | { |
|
|
215 | SV *rv = &PL_sv_undef; |
|
|
216 | |
|
|
217 | if (req->result >= 0) |
|
|
218 | { |
|
|
219 | char *buf = req->data2ptr; |
|
|
220 | AV *av = newAV (); |
|
|
221 | |
|
|
222 | while (req->result) |
|
|
223 | { |
|
|
224 | SV *sv = newSVpv (buf, 0); |
|
|
225 | |
|
|
226 | av_push (av, sv); |
|
|
227 | buf += SvCUR (sv) + 1; |
|
|
228 | req->result--; |
|
|
229 | } |
|
|
230 | |
|
|
231 | rv = sv_2mortal (newRV_noinc ((SV *)av)); |
|
|
232 | } |
|
|
233 | |
|
|
234 | PUSHs (rv); |
|
|
235 | } |
|
|
236 | break; |
|
|
237 | |
|
|
238 | case REQ_OPEN: |
|
|
239 | { |
|
|
240 | /* convert fd to fh */ |
|
|
241 | SV *fh; |
|
|
242 | |
|
|
243 | PUSHs (sv_2mortal (newSViv (req->result))); |
|
|
244 | PUTBACK; |
|
|
245 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
|
|
246 | SPAGAIN; |
|
|
247 | |
|
|
248 | fh = SvREFCNT_inc (POPs); |
|
|
249 | |
|
|
250 | PUSHMARK (SP); |
|
|
251 | XPUSHs (sv_2mortal (fh)); |
|
|
252 | } |
|
|
253 | break; |
|
|
254 | |
|
|
255 | case REQ_GROUP: |
|
|
256 | req->fd = 2; /* mark group as finished */ |
|
|
257 | |
|
|
258 | if (req->data) |
|
|
259 | { |
|
|
260 | int i; |
|
|
261 | AV *av = (AV *)req->data; |
|
|
262 | |
|
|
263 | EXTEND (SP, AvFILL (av) + 1); |
|
|
264 | for (i = 0; i <= AvFILL (av); ++i) |
|
|
265 | PUSHs (*av_fetch (av, i, 0)); |
|
|
266 | } |
|
|
267 | break; |
|
|
268 | |
|
|
269 | case REQ_NOP: |
|
|
270 | case REQ_SLEEP: |
|
|
271 | break; |
|
|
272 | |
|
|
273 | default: |
|
|
274 | PUSHs (sv_2mortal (newSViv (req->result))); |
|
|
275 | break; |
|
|
276 | } |
|
|
277 | |
|
|
278 | |
|
|
279 | PUTBACK; |
|
|
280 | call_sv (req->callback, G_VOID | G_EVAL); |
|
|
281 | SPAGAIN; |
|
|
282 | |
|
|
283 | FREETMPS; |
|
|
284 | LEAVE; |
|
|
285 | |
|
|
286 | errno = errorno; |
|
|
287 | |
|
|
288 | if (SvTRUE (ERRSV)) |
|
|
289 | { |
|
|
290 | req_free (req); |
|
|
291 | croak (0); |
|
|
292 | } |
|
|
293 | } |
|
|
294 | |
|
|
295 | static void req_free (aio_req req) |
|
|
296 | { |
|
|
297 | if (req->grp) |
|
|
298 | { |
|
|
299 | aio_req grp = req->grp; |
|
|
300 | |
|
|
301 | /* unlink request */ |
|
|
302 | if (req->grp_next) req->grp_next->grp_prev = req->grp_prev; |
|
|
303 | if (req->grp_prev) req->grp_prev->grp_next = req->grp_next; |
|
|
304 | |
|
|
305 | if (grp->grp_first == req) |
|
|
306 | grp->grp_first = req->grp_next; |
|
|
307 | |
|
|
308 | aio_grp_dec (grp); |
|
|
309 | } |
|
|
310 | |
|
|
311 | if (req->self) |
|
|
312 | { |
|
|
313 | sv_unmagic (req->self, PERL_MAGIC_ext); |
|
|
314 | SvREFCNT_dec (req->self); |
|
|
315 | } |
|
|
316 | |
|
|
317 | SvREFCNT_dec (req->data); |
|
|
318 | SvREFCNT_dec (req->fh); |
|
|
319 | SvREFCNT_dec (req->fh2); |
|
|
320 | SvREFCNT_dec (req->callback); |
|
|
321 | Safefree (req->statdata); |
|
|
322 | |
|
|
323 | if (req->type == REQ_READDIR && req->result >= 0) |
|
|
324 | free (req->data2ptr); |
|
|
325 | |
|
|
326 | Safefree (req); |
|
|
327 | } |
|
|
328 | |
|
|
329 | static void req_cancel (aio_req req) |
|
|
330 | { |
|
|
331 | req->flags |= FLAG_CANCELLED; |
|
|
332 | |
|
|
333 | if (req->type == REQ_GROUP) |
|
|
334 | { |
|
|
335 | aio_req sub; |
|
|
336 | |
|
|
337 | for (sub = req->grp_first; sub; sub = sub->grp_next) |
|
|
338 | req_cancel (sub); |
|
|
339 | } |
|
|
340 | } |
|
|
341 | |
|
|
342 | static int poll_cb () |
198 | { |
343 | { |
199 | dSP; |
344 | dSP; |
200 | int count = 0; |
345 | int count = 0; |
201 | int do_croak = 0; |
346 | int do_croak = 0; |
202 | aio_req req; |
347 | aio_req req; |
… | |
… | |
224 | pthread_mutex_unlock (&reslock); |
369 | pthread_mutex_unlock (&reslock); |
225 | |
370 | |
226 | if (!req) |
371 | if (!req) |
227 | break; |
372 | break; |
228 | |
373 | |
229 | nreqs--; |
374 | --nreqs; |
230 | |
375 | |
231 | if (req->type == REQ_QUIT) |
376 | if (req->type == REQ_QUIT) |
232 | started--; |
377 | started--; |
|
|
378 | else if (req->type == REQ_GROUP && req->length) |
|
|
379 | { |
|
|
380 | req->fd = 1; /* mark request as delayed */ |
|
|
381 | continue; |
|
|
382 | } |
233 | else |
383 | else |
234 | { |
384 | { |
235 | int errorno = errno; |
|
|
236 | errno = req->errorno; |
|
|
237 | |
|
|
238 | if (req->type == REQ_READ) |
385 | if (req->type == REQ_READ) |
239 | SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); |
386 | SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); |
240 | |
387 | |
241 | if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE)) |
388 | if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE)) |
242 | SvREADONLY_off (req->data); |
389 | SvREADONLY_off (req->data); |
… | |
… | |
246 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
393 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
247 | PL_laststatval = req->result; |
394 | PL_laststatval = req->result; |
248 | PL_statcache = *(req->statdata); |
395 | PL_statcache = *(req->statdata); |
249 | } |
396 | } |
250 | |
397 | |
251 | ENTER; |
398 | req_invoke (req); |
252 | PUSHMARK (SP); |
|
|
253 | |
399 | |
254 | if (req->type == REQ_READDIR) |
|
|
255 | { |
|
|
256 | SV *rv = &PL_sv_undef; |
|
|
257 | |
|
|
258 | if (req->result >= 0) |
|
|
259 | { |
|
|
260 | char *buf = req->data2ptr; |
|
|
261 | AV *av = newAV (); |
|
|
262 | |
|
|
263 | while (req->result) |
|
|
264 | { |
|
|
265 | SV *sv = newSVpv (buf, 0); |
|
|
266 | |
|
|
267 | av_push (av, sv); |
|
|
268 | buf += SvCUR (sv) + 1; |
|
|
269 | req->result--; |
|
|
270 | } |
|
|
271 | |
|
|
272 | rv = sv_2mortal (newRV_noinc ((SV *)av)); |
|
|
273 | } |
|
|
274 | |
|
|
275 | XPUSHs (rv); |
|
|
276 | } |
|
|
277 | else |
|
|
278 | { |
|
|
279 | XPUSHs (sv_2mortal (newSViv (req->result))); |
|
|
280 | |
|
|
281 | if (req->type == REQ_OPEN) |
|
|
282 | { |
|
|
283 | /* convert fd to fh */ |
|
|
284 | SV *fh; |
|
|
285 | |
|
|
286 | PUTBACK; |
|
|
287 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
|
|
288 | SPAGAIN; |
|
|
289 | |
|
|
290 | fh = SvREFCNT_inc (POPs); |
|
|
291 | |
|
|
292 | PUSHMARK (SP); |
|
|
293 | XPUSHs (sv_2mortal (fh)); |
|
|
294 | } |
|
|
295 | } |
|
|
296 | |
|
|
297 | if (SvOK (req->callback) && !req->cancelled) |
|
|
298 | { |
|
|
299 | PUTBACK; |
|
|
300 | call_sv (req->callback, G_VOID | G_EVAL); |
|
|
301 | SPAGAIN; |
|
|
302 | |
|
|
303 | if (SvTRUE (ERRSV)) |
|
|
304 | { |
|
|
305 | req_free (req); |
|
|
306 | croak (0); |
|
|
307 | } |
|
|
308 | } |
|
|
309 | |
|
|
310 | LEAVE; |
|
|
311 | |
|
|
312 | errno = errorno; |
|
|
313 | count++; |
400 | count++; |
314 | } |
401 | } |
315 | |
402 | |
316 | req_free (req); |
403 | req_free (req); |
317 | } |
404 | } |
… | |
… | |
319 | return count; |
406 | return count; |
320 | } |
407 | } |
321 | |
408 | |
322 | static void *aio_proc(void *arg); |
409 | static void *aio_proc(void *arg); |
323 | |
410 | |
324 | static void |
|
|
325 | start_thread (void) |
411 | static void start_thread (void) |
326 | { |
412 | { |
327 | sigset_t fullsigset, oldsigset; |
413 | sigset_t fullsigset, oldsigset; |
328 | pthread_t tid; |
414 | pthread_t tid; |
329 | pthread_attr_t attr; |
415 | pthread_attr_t attr; |
330 | |
416 | |
… | |
… | |
339 | started++; |
425 | started++; |
340 | |
426 | |
341 | sigprocmask (SIG_SETMASK, &oldsigset, 0); |
427 | sigprocmask (SIG_SETMASK, &oldsigset, 0); |
342 | } |
428 | } |
343 | |
429 | |
344 | static void |
|
|
345 | req_send (aio_req req) |
430 | static void req_send (aio_req req) |
346 | { |
431 | { |
347 | while (started < wanted && nreqs >= started) |
432 | while (started < wanted && nreqs >= started) |
348 | start_thread (); |
433 | start_thread (); |
349 | |
434 | |
350 | nreqs++; |
435 | ++nreqs; |
351 | |
436 | |
352 | pthread_mutex_lock (&reqlock); |
437 | pthread_mutex_lock (&reqlock); |
353 | |
438 | |
354 | req->next = 0; |
439 | req->next = 0; |
355 | |
440 | |
… | |
… | |
374 | |
459 | |
375 | poll_wait (); |
460 | poll_wait (); |
376 | } |
461 | } |
377 | } |
462 | } |
378 | |
463 | |
379 | static void |
464 | static void end_thread (void) |
380 | end_thread (void) |
|
|
381 | { |
465 | { |
382 | aio_req req; |
466 | aio_req req; |
383 | Newz (0, req, 1, aio_cb); |
467 | Newz (0, req, 1, aio_cb); |
384 | req->type = REQ_QUIT; |
468 | req->type = REQ_QUIT; |
385 | |
469 | |
… | |
… | |
436 | * normal read/write by using a mutex. slows down execution a lot, |
520 | * normal read/write by using a mutex. slows down execution a lot, |
437 | * but that's your problem, not mine. |
521 | * but that's your problem, not mine. |
438 | */ |
522 | */ |
439 | static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER; |
523 | static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER; |
440 | |
524 | |
441 | static ssize_t |
525 | static ssize_t pread (int fd, void *buf, size_t count, off_t offset) |
442 | pread (int fd, void *buf, size_t count, off_t offset) |
|
|
443 | { |
526 | { |
444 | ssize_t res; |
527 | ssize_t res; |
445 | off_t ooffset; |
528 | off_t ooffset; |
446 | |
529 | |
447 | pthread_mutex_lock (&preadwritelock); |
530 | pthread_mutex_lock (&preadwritelock); |
… | |
… | |
452 | pthread_mutex_unlock (&preadwritelock); |
535 | pthread_mutex_unlock (&preadwritelock); |
453 | |
536 | |
454 | return res; |
537 | return res; |
455 | } |
538 | } |
456 | |
539 | |
457 | static ssize_t |
|
|
458 | pwrite (int fd, void *buf, size_t count, off_t offset) |
540 | static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset) |
459 | { |
541 | { |
460 | ssize_t res; |
542 | ssize_t res; |
461 | off_t ooffset; |
543 | off_t ooffset; |
462 | |
544 | |
463 | pthread_mutex_lock (&preadwritelock); |
545 | pthread_mutex_lock (&preadwritelock); |
… | |
… | |
476 | #endif |
558 | #endif |
477 | |
559 | |
478 | #if !HAVE_READAHEAD |
560 | #if !HAVE_READAHEAD |
479 | # define readahead aio_readahead |
561 | # define readahead aio_readahead |
480 | |
562 | |
481 | static ssize_t |
|
|
482 | readahead (int fd, off_t offset, size_t count) |
563 | static ssize_t readahead (int fd, off_t offset, size_t count) |
483 | { |
564 | { |
484 | char readahead_buf[4096]; |
565 | char readahead_buf[4096]; |
485 | |
566 | |
486 | while (count > 0) |
567 | while (count > 0) |
487 | { |
568 | { |
… | |
… | |
499 | #if !HAVE_READDIR_R |
580 | #if !HAVE_READDIR_R |
500 | # define readdir_r aio_readdir_r |
581 | # define readdir_r aio_readdir_r |
501 | |
582 | |
502 | static pthread_mutex_t readdirlock = PTHREAD_MUTEX_INITIALIZER; |
583 | static pthread_mutex_t readdirlock = PTHREAD_MUTEX_INITIALIZER; |
503 | |
584 | |
504 | static int |
|
|
505 | readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) |
585 | static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) |
506 | { |
586 | { |
507 | struct dirent *e; |
587 | struct dirent *e; |
508 | int errorno; |
588 | int errorno; |
509 | |
589 | |
510 | pthread_mutex_lock (&readdirlock); |
590 | pthread_mutex_lock (&readdirlock); |
… | |
… | |
526 | return e ? 0 : -1; |
606 | return e ? 0 : -1; |
527 | } |
607 | } |
528 | #endif |
608 | #endif |
529 | |
609 | |
530 | /* sendfile always needs emulation */ |
610 | /* sendfile always needs emulation */ |
531 | static ssize_t |
|
|
532 | sendfile_ (int ofd, int ifd, off_t offset, size_t count) |
611 | static ssize_t sendfile_ (int ofd, int ifd, off_t offset, size_t count) |
533 | { |
612 | { |
534 | ssize_t res; |
613 | ssize_t res; |
535 | |
614 | |
536 | if (!count) |
615 | if (!count) |
537 | return 0; |
616 | return 0; |
… | |
… | |
619 | |
698 | |
620 | return res; |
699 | return res; |
621 | } |
700 | } |
622 | |
701 | |
623 | /* read a full directory */ |
702 | /* read a full directory */ |
624 | static int |
|
|
625 | scandir_ (const char *path, void **namesp) |
703 | static int scandir_ (const char *path, void **namesp) |
626 | { |
704 | { |
627 | DIR *dirp = opendir (path); |
705 | DIR *dirp = opendir (path); |
628 | union |
706 | union |
629 | { |
707 | { |
630 | struct dirent d; |
708 | struct dirent d; |
… | |
… | |
684 | return res; |
762 | return res; |
685 | } |
763 | } |
686 | |
764 | |
687 | /*****************************************************************************/ |
765 | /*****************************************************************************/ |
688 | |
766 | |
689 | static void * |
|
|
690 | aio_proc (void *thr_arg) |
767 | static void *aio_proc (void *thr_arg) |
691 | { |
768 | { |
692 | aio_req req; |
769 | aio_req req; |
693 | int type; |
770 | int type; |
694 | |
771 | |
695 | do |
772 | do |
… | |
… | |
714 | |
791 | |
715 | pthread_mutex_unlock (&reqlock); |
792 | pthread_mutex_unlock (&reqlock); |
716 | |
793 | |
717 | errno = 0; /* strictly unnecessary */ |
794 | errno = 0; /* strictly unnecessary */ |
718 | |
795 | |
719 | if (!req->cancelled) |
796 | if (!(req->flags & FLAG_CANCELLED)) |
720 | switch (req->type) |
797 | switch (type = req->type) /* remember type for QUIT check */ |
721 | { |
798 | { |
722 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
799 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
723 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
800 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
724 | |
801 | |
725 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
802 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
… | |
… | |
739 | |
816 | |
740 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
817 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
741 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
818 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
742 | case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break; |
819 | case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break; |
743 | |
820 | |
|
|
821 | case REQ_SLEEP: |
|
|
822 | { |
|
|
823 | struct timeval tv; |
|
|
824 | |
|
|
825 | tv.tv_sec = req->fd; |
|
|
826 | tv.tv_usec = req->fd2; |
|
|
827 | |
|
|
828 | req->result = select (0, 0, 0, 0, &tv); |
|
|
829 | } |
|
|
830 | |
|
|
831 | case REQ_GROUP: |
|
|
832 | case REQ_NOP: |
744 | case REQ_QUIT: |
833 | case REQ_QUIT: |
745 | break; |
834 | break; |
746 | |
835 | |
747 | default: |
836 | default: |
748 | req->result = ENOSYS; |
837 | req->result = ENOSYS; |
… | |
… | |
846 | |
935 | |
847 | #define REQ_SEND \ |
936 | #define REQ_SEND \ |
848 | req_send (req); \ |
937 | req_send (req); \ |
849 | \ |
938 | \ |
850 | if (GIMME_V != G_VOID) \ |
939 | if (GIMME_V != G_VOID) \ |
851 | XPUSHs (req_sv (req)); |
940 | XPUSHs (req_sv (req, AIO_REQ_KLASS)); |
852 | |
941 | |
853 | MODULE = IO::AIO PACKAGE = IO::AIO |
942 | MODULE = IO::AIO PACKAGE = IO::AIO |
854 | |
943 | |
855 | PROTOTYPES: ENABLE |
944 | PROTOTYPES: ENABLE |
856 | |
945 | |
… | |
… | |
1103 | req->dataptr = SvPVbyte_nolen (req->data); |
1192 | req->dataptr = SvPVbyte_nolen (req->data); |
1104 | |
1193 | |
1105 | REQ_SEND; |
1194 | REQ_SEND; |
1106 | } |
1195 | } |
1107 | |
1196 | |
1108 | #if 0 |
|
|
1109 | |
|
|
1110 | # undocumented, because it does not cancel active requests |
|
|
1111 | void |
1197 | void |
1112 | cancel_most_requests () |
1198 | aio_sleep (delay,callback=&PL_sv_undef) |
|
|
1199 | double delay |
|
|
1200 | SV * callback |
|
|
1201 | PPCODE: |
|
|
1202 | { |
|
|
1203 | dREQ; |
|
|
1204 | |
|
|
1205 | req->type = REQ_SLEEP; |
|
|
1206 | req->fd = delay < 0. ? 0 : delay; |
|
|
1207 | req->fd2 = delay < 0. ? 0 : 1000. * (delay - req->fd); |
|
|
1208 | |
|
|
1209 | REQ_SEND; |
|
|
1210 | } |
|
|
1211 | |
|
|
1212 | void |
|
|
1213 | aio_group (callback=&PL_sv_undef) |
|
|
1214 | SV * callback |
1113 | PROTOTYPE: |
1215 | PROTOTYPE: ;$ |
1114 | CODE: |
1216 | PPCODE: |
1115 | { |
1217 | { |
1116 | aio_req *req; |
1218 | dREQ; |
1117 | |
1219 | req->type = REQ_GROUP; |
1118 | pthread_mutex_lock (&reqlock); |
1220 | req_send (req); |
1119 | for (req = reqs; req; req = req->next) |
1221 | XPUSHs (req_sv (req, AIO_GRP_KLASS)); |
1120 | req->flags |= 1; |
|
|
1121 | pthread_mutex_unlock (&reqlock); |
|
|
1122 | |
|
|
1123 | pthread_mutex_lock (&reslock); |
|
|
1124 | for (req = ress; req; req = req->next) |
|
|
1125 | req->flags |= 1; |
|
|
1126 | pthread_mutex_unlock (&reslock); |
|
|
1127 | } |
1222 | } |
1128 | |
1223 | |
1129 | #endif |
1224 | void |
|
|
1225 | aio_nop (callback=&PL_sv_undef) |
|
|
1226 | SV * callback |
|
|
1227 | PPCODE: |
|
|
1228 | { |
|
|
1229 | dREQ; |
|
|
1230 | |
|
|
1231 | req->type = REQ_NOP; |
|
|
1232 | |
|
|
1233 | REQ_SEND; |
|
|
1234 | } |
1130 | |
1235 | |
1131 | void |
1236 | void |
1132 | flush () |
1237 | flush () |
1133 | PROTOTYPE: |
1238 | PROTOTYPE: |
1134 | CODE: |
1239 | CODE: |
… | |
… | |
1177 | CODE: |
1282 | CODE: |
1178 | RETVAL = nreqs; |
1283 | RETVAL = nreqs; |
1179 | OUTPUT: |
1284 | OUTPUT: |
1180 | RETVAL |
1285 | RETVAL |
1181 | |
1286 | |
|
|
1287 | PROTOTYPES: DISABLE |
|
|
1288 | |
1182 | MODULE = IO::AIO PACKAGE = IO::AIO::CB |
1289 | MODULE = IO::AIO PACKAGE = IO::AIO::REQ |
1183 | |
1290 | |
1184 | void |
1291 | void |
1185 | cancel (aio_req_ornot req) |
1292 | cancel (aio_req_ornot req) |
1186 | PROTOTYPE: |
1293 | PROTOTYPE: |
1187 | CODE: |
1294 | CODE: |
1188 | req->cancelled = 1; |
1295 | req_cancel (req); |
1189 | |
1296 | |
|
|
1297 | void |
|
|
1298 | cb (aio_req_ornot req, SV *callback=&PL_sv_undef) |
|
|
1299 | CODE: |
|
|
1300 | SvREFCNT_dec (req->callback); |
|
|
1301 | req->callback = newSVsv (callback); |
|
|
1302 | |
|
|
1303 | MODULE = IO::AIO PACKAGE = IO::AIO::GRP |
|
|
1304 | |
|
|
1305 | void |
|
|
1306 | add (aio_req grp, ...) |
|
|
1307 | PPCODE: |
|
|
1308 | { |
|
|
1309 | int i; |
|
|
1310 | aio_req req; |
|
|
1311 | |
|
|
1312 | if (grp->fd == 2) |
|
|
1313 | croak ("cannot add requests to IO::AIO::GRP after the group finished"); |
|
|
1314 | |
|
|
1315 | for (i = 1; i < items; ++i ) |
|
|
1316 | { |
|
|
1317 | if (GIMME_V != G_VOID) |
|
|
1318 | XPUSHs (sv_2mortal (newSVsv (ST (i)))); |
|
|
1319 | |
|
|
1320 | req = SvAIO_REQ (ST (i)); |
|
|
1321 | |
|
|
1322 | if (req) |
|
|
1323 | { |
|
|
1324 | ++grp->length; |
|
|
1325 | req->grp = grp; |
|
|
1326 | |
|
|
1327 | req->grp_prev = 0; |
|
|
1328 | req->grp_next = grp->grp_first; |
|
|
1329 | |
|
|
1330 | if (grp->grp_first) |
|
|
1331 | grp->grp_first->grp_prev = req; |
|
|
1332 | |
|
|
1333 | grp->grp_first = req; |
|
|
1334 | } |
|
|
1335 | } |
|
|
1336 | } |
|
|
1337 | |
|
|
1338 | void |
|
|
1339 | result (aio_req grp, ...) |
|
|
1340 | CODE: |
|
|
1341 | { |
|
|
1342 | int i; |
|
|
1343 | AV *av = newAV (); |
|
|
1344 | |
|
|
1345 | for (i = 1; i < items; ++i ) |
|
|
1346 | av_push (av, newSVsv (ST (i))); |
|
|
1347 | |
|
|
1348 | SvREFCNT_dec (grp->data); |
|
|
1349 | grp->data = (SV *)av; |
|
|
1350 | } |
|
|
1351 | |
|
|
1352 | void |
|
|
1353 | feed_limit (aio_req grp, int limit) |
|
|
1354 | CODE: |
|
|
1355 | grp->fd2 = limit; |
|
|
1356 | aio_grp_feed (grp); |
|
|
1357 | |
|
|
1358 | void |
|
|
1359 | feed (aio_req grp, SV *callback=&PL_sv_undef) |
|
|
1360 | CODE: |
|
|
1361 | { |
|
|
1362 | SvREFCNT_dec (grp->fh2); |
|
|
1363 | grp->fh2 = newSVsv (callback); |
|
|
1364 | |
|
|
1365 | if (grp->fd2 <= 0) |
|
|
1366 | grp->fd2 = 2; |
|
|
1367 | |
|
|
1368 | aio_grp_feed (grp); |
|
|
1369 | } |
|
|
1370 | |