|
|
1 | #if __linux |
|
|
2 | # define _GNU_SOURCE |
|
|
3 | #endif |
|
|
4 | |
1 | #define _REENTRANT 1 |
5 | #define _REENTRANT 1 |
|
|
6 | |
2 | #include <errno.h> |
7 | #include <errno.h> |
3 | |
8 | |
4 | #include "EXTERN.h" |
9 | #include "EXTERN.h" |
5 | #include "perl.h" |
10 | #include "perl.h" |
6 | #include "XSUB.h" |
11 | #include "XSUB.h" |
… | |
… | |
41 | # define NAME_MAX 4096 |
46 | # define NAME_MAX 4096 |
42 | #endif |
47 | #endif |
43 | |
48 | |
44 | #if __ia64 |
49 | #if __ia64 |
45 | # define STACKSIZE 65536 |
50 | # define STACKSIZE 65536 |
|
|
51 | #elif __i386 || __x86_64 /* 16k is unreasonably high :( */ |
|
|
52 | # define STACKSIZE PTHREAD_STACK_MIN |
46 | #else |
53 | #else |
47 | # define STACKSIZE 8192 |
54 | # define STACKSIZE 16384 |
48 | #endif |
55 | #endif |
|
|
56 | |
|
|
57 | /* buffer size for various temporary buffers */ |
|
|
58 | #define AIO_BUFSIZE 65536 |
|
|
59 | |
|
|
60 | #define dBUF \ |
|
|
61 | char *aio_buf = malloc (AIO_BUFSIZE); \ |
|
|
62 | if (!aio_buf) \ |
|
|
63 | return -1; |
|
|
64 | |
|
|
65 | #define fBUF free (aio_buf) |
49 | |
66 | |
50 | enum { |
67 | enum { |
51 | REQ_QUIT, |
68 | REQ_QUIT, |
52 | REQ_OPEN, REQ_CLOSE, |
69 | REQ_OPEN, REQ_CLOSE, |
53 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
70 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
… | |
… | |
66 | |
83 | |
67 | typedef struct aio_cb |
84 | typedef struct aio_cb |
68 | { |
85 | { |
69 | struct aio_cb *volatile next; |
86 | struct aio_cb *volatile next; |
70 | |
87 | |
71 | struct aio_cb *grp, *grp_prev, *grp_next, *grp_first; |
|
|
72 | |
|
|
73 | SV *self; /* the perl counterpart of this request, if any */ |
|
|
74 | |
|
|
75 | SV *data, *callback; |
88 | SV *data, *callback; |
76 | SV *fh, *fh2; |
89 | SV *fh, *fh2; |
77 | void *dataptr, *data2ptr; |
90 | void *dataptr, *data2ptr; |
78 | Stat_t *statdata; |
91 | Stat_t *statdata; |
79 | off_t offset; |
92 | off_t offset; |
80 | size_t length; |
93 | size_t length; |
81 | ssize_t result; |
94 | ssize_t result; |
82 | |
95 | |
|
|
96 | STRLEN dataoffset; |
83 | int type; |
97 | int type; |
84 | int fd, fd2; |
98 | int fd, fd2; |
85 | int errorno; |
99 | int errorno; |
86 | STRLEN dataoffset; |
|
|
87 | mode_t mode; /* open */ |
100 | mode_t mode; /* open */ |
|
|
101 | |
88 | unsigned char cancelled; |
102 | unsigned char flags; |
|
|
103 | unsigned char pri; |
|
|
104 | |
|
|
105 | SV *self; /* the perl counterpart of this request, if any */ |
|
|
106 | struct aio_cb *grp, *grp_prev, *grp_next, *grp_first; |
89 | } aio_cb; |
107 | } aio_cb; |
|
|
108 | |
|
|
109 | enum { |
|
|
110 | FLAG_CANCELLED = 0x01, |
|
|
111 | }; |
90 | |
112 | |
91 | typedef aio_cb *aio_req; |
113 | typedef aio_cb *aio_req; |
92 | typedef aio_cb *aio_req_ornot; |
114 | typedef aio_cb *aio_req_ornot; |
|
|
115 | |
|
|
116 | enum { |
|
|
117 | PRI_MIN = -4, |
|
|
118 | PRI_MAX = 4, |
|
|
119 | |
|
|
120 | DEFAULT_PRI = 0, |
|
|
121 | PRI_BIAS = -PRI_MIN, |
|
|
122 | NUM_PRI = PRI_MAX + PRI_BIAS + 1, |
|
|
123 | }; |
|
|
124 | |
|
|
125 | static int next_pri = DEFAULT_PRI + PRI_BIAS; |
93 | |
126 | |
94 | static int started, wanted; |
127 | static int started, wanted; |
95 | static volatile int nreqs; |
128 | static volatile int nreqs; |
96 | static int max_outstanding = 1<<30; |
129 | static int max_outstanding = 1<<30; |
97 | static int respipe [2]; |
130 | static int respipe [2]; |
98 | |
131 | |
|
|
132 | #if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) |
|
|
133 | # define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP |
|
|
134 | #else |
|
|
135 | # define AIO_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER |
|
|
136 | #endif |
|
|
137 | |
99 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
138 | static pthread_mutex_t reslock = AIO_MUTEX_INIT; |
100 | static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; |
139 | static pthread_mutex_t reqlock = AIO_MUTEX_INIT; |
101 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
140 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
102 | |
141 | |
103 | static volatile aio_req reqs, reqe; /* queue start, queue end */ |
142 | /* |
104 | static volatile aio_req ress, rese; /* queue start, queue end */ |
143 | * a somewhat faster data structure might be nice, but |
|
|
144 | * with 8 priorities this actually needs <20 insns |
|
|
145 | * per shift, the most expensive operation. |
|
|
146 | */ |
|
|
147 | typedef struct { |
|
|
148 | aio_req qs[NUM_PRI], qe[NUM_PRI]; /* qstart, qend */ |
|
|
149 | int size; |
|
|
150 | } reqq; |
|
|
151 | |
|
|
152 | static reqq req_queue; |
|
|
153 | static reqq res_queue; |
|
|
154 | |
|
|
155 | int reqq_push (reqq *q, aio_req req) |
|
|
156 | { |
|
|
157 | int pri = req->pri; |
|
|
158 | req->next = 0; |
|
|
159 | |
|
|
160 | if (q->qe[pri]) |
|
|
161 | { |
|
|
162 | q->qe[pri]->next = req; |
|
|
163 | q->qe[pri] = req; |
|
|
164 | } |
|
|
165 | else |
|
|
166 | q->qe[pri] = q->qs[pri] = req; |
|
|
167 | |
|
|
168 | return q->size++; |
|
|
169 | } |
|
|
170 | |
|
|
171 | aio_req reqq_shift (reqq *q) |
|
|
172 | { |
|
|
173 | int pri; |
|
|
174 | |
|
|
175 | if (!q->size) |
|
|
176 | return 0; |
|
|
177 | |
|
|
178 | --q->size; |
|
|
179 | |
|
|
180 | for (pri = NUM_PRI; pri--; ) |
|
|
181 | { |
|
|
182 | aio_req req = q->qs[pri]; |
|
|
183 | |
|
|
184 | if (req) |
|
|
185 | { |
|
|
186 | if (!(q->qs[pri] = req->next)) |
|
|
187 | q->qe[pri] = 0; |
|
|
188 | |
|
|
189 | return req; |
|
|
190 | } |
|
|
191 | } |
|
|
192 | |
|
|
193 | abort (); |
|
|
194 | } |
105 | |
195 | |
106 | static void req_invoke (aio_req req); |
196 | static void req_invoke (aio_req req); |
107 | static void req_free (aio_req req); |
197 | static void req_free (aio_req req); |
108 | |
198 | |
109 | /* must be called at most once */ |
199 | /* must be called at most once */ |
… | |
… | |
130 | return mg ? (aio_req)mg->mg_ptr : 0; |
220 | return mg ? (aio_req)mg->mg_ptr : 0; |
131 | } |
221 | } |
132 | |
222 | |
133 | static void aio_grp_feed (aio_req grp) |
223 | static void aio_grp_feed (aio_req grp) |
134 | { |
224 | { |
135 | while (grp->length < grp->fd2) |
225 | while (grp->length < grp->fd2 && !(grp->flags & FLAG_CANCELLED)) |
136 | { |
226 | { |
137 | int old_len = grp->length; |
227 | int old_len = grp->length; |
138 | |
228 | |
139 | if (grp->fh2 && SvOK (grp->fh2)) |
229 | if (grp->fh2 && SvOK (grp->fh2)) |
140 | { |
230 | { |
… | |
… | |
143 | ENTER; |
233 | ENTER; |
144 | SAVETMPS; |
234 | SAVETMPS; |
145 | PUSHMARK (SP); |
235 | PUSHMARK (SP); |
146 | XPUSHs (req_sv (grp, AIO_GRP_KLASS)); |
236 | XPUSHs (req_sv (grp, AIO_GRP_KLASS)); |
147 | PUTBACK; |
237 | PUTBACK; |
148 | call_sv (grp->fh2, G_VOID | G_EVAL); |
238 | call_sv (grp->fh2, G_VOID | G_EVAL | G_KEEPERR); |
149 | SPAGAIN; |
239 | SPAGAIN; |
150 | FREETMPS; |
240 | FREETMPS; |
151 | LEAVE; |
241 | LEAVE; |
152 | } |
242 | } |
153 | |
243 | |
… | |
… | |
176 | } |
266 | } |
177 | } |
267 | } |
178 | |
268 | |
179 | static void poll_wait () |
269 | static void poll_wait () |
180 | { |
270 | { |
181 | if (nreqs && !ress) |
|
|
182 | { |
|
|
183 | fd_set rfd; |
271 | fd_set rfd; |
|
|
272 | |
|
|
273 | while (nreqs) |
|
|
274 | { |
|
|
275 | int size; |
|
|
276 | #if !(__i386 || __x86_64) /* safe without sempahore on this archs */ |
|
|
277 | pthread_mutex_lock (&reslock); |
|
|
278 | #endif |
|
|
279 | size = res_queue.size; |
|
|
280 | #if !(__i386 || __x86_64) /* safe without sempahore on this archs */ |
|
|
281 | pthread_mutex_unlock (&reslock); |
|
|
282 | #endif |
|
|
283 | |
|
|
284 | if (size) |
|
|
285 | return; |
|
|
286 | |
184 | FD_ZERO(&rfd); |
287 | FD_ZERO(&rfd); |
185 | FD_SET(respipe [0], &rfd); |
288 | FD_SET(respipe [0], &rfd); |
186 | |
289 | |
187 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
290 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
188 | } |
291 | } |
189 | } |
292 | } |
190 | |
293 | |
191 | static void req_invoke (aio_req req) |
294 | static void req_invoke (aio_req req) |
192 | { |
295 | { |
193 | dSP; |
296 | dSP; |
194 | int errorno = errno; |
|
|
195 | |
297 | |
196 | if (req->cancelled || !SvOK (req->callback)) |
298 | if (!(req->flags & FLAG_CANCELLED) && SvOK (req->callback)) |
197 | return; |
299 | { |
198 | |
|
|
199 | errno = req->errorno; |
300 | errno = req->errorno; |
200 | |
301 | |
201 | ENTER; |
302 | ENTER; |
202 | SAVETMPS; |
303 | SAVETMPS; |
203 | PUSHMARK (SP); |
304 | PUSHMARK (SP); |
204 | EXTEND (SP, 1); |
305 | EXTEND (SP, 1); |
205 | |
306 | |
206 | switch (req->type) |
307 | switch (req->type) |
207 | { |
|
|
208 | case REQ_READDIR: |
|
|
209 | { |
308 | { |
210 | SV *rv = &PL_sv_undef; |
309 | case REQ_READDIR: |
211 | |
|
|
212 | if (req->result >= 0) |
|
|
213 | { |
310 | { |
214 | char *buf = req->data2ptr; |
311 | SV *rv = &PL_sv_undef; |
215 | AV *av = newAV (); |
|
|
216 | |
312 | |
217 | while (req->result) |
313 | if (req->result >= 0) |
218 | { |
314 | { |
|
|
315 | char *buf = req->data2ptr; |
|
|
316 | AV *av = newAV (); |
|
|
317 | |
|
|
318 | while (req->result) |
|
|
319 | { |
219 | SV *sv = newSVpv (buf, 0); |
320 | SV *sv = newSVpv (buf, 0); |
220 | |
321 | |
221 | av_push (av, sv); |
322 | av_push (av, sv); |
222 | buf += SvCUR (sv) + 1; |
323 | buf += SvCUR (sv) + 1; |
223 | req->result--; |
324 | req->result--; |
|
|
325 | } |
|
|
326 | |
|
|
327 | rv = sv_2mortal (newRV_noinc ((SV *)av)); |
224 | } |
328 | } |
225 | |
329 | |
226 | rv = sv_2mortal (newRV_noinc ((SV *)av)); |
330 | PUSHs (rv); |
227 | } |
331 | } |
|
|
332 | break; |
228 | |
333 | |
229 | PUSHs (rv); |
334 | case REQ_OPEN: |
|
|
335 | { |
|
|
336 | /* convert fd to fh */ |
|
|
337 | SV *fh; |
|
|
338 | |
|
|
339 | PUSHs (sv_2mortal (newSViv (req->result))); |
|
|
340 | PUTBACK; |
|
|
341 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
|
|
342 | SPAGAIN; |
|
|
343 | |
|
|
344 | fh = SvREFCNT_inc (POPs); |
|
|
345 | |
|
|
346 | PUSHMARK (SP); |
|
|
347 | XPUSHs (sv_2mortal (fh)); |
|
|
348 | } |
|
|
349 | break; |
|
|
350 | |
|
|
351 | case REQ_GROUP: |
|
|
352 | req->fd = 2; /* mark group as finished */ |
|
|
353 | |
|
|
354 | if (req->data) |
|
|
355 | { |
|
|
356 | int i; |
|
|
357 | AV *av = (AV *)req->data; |
|
|
358 | |
|
|
359 | EXTEND (SP, AvFILL (av) + 1); |
|
|
360 | for (i = 0; i <= AvFILL (av); ++i) |
|
|
361 | PUSHs (*av_fetch (av, i, 0)); |
|
|
362 | } |
|
|
363 | break; |
|
|
364 | |
|
|
365 | case REQ_NOP: |
|
|
366 | case REQ_SLEEP: |
|
|
367 | break; |
|
|
368 | |
|
|
369 | default: |
|
|
370 | PUSHs (sv_2mortal (newSViv (req->result))); |
|
|
371 | break; |
230 | } |
372 | } |
231 | break; |
|
|
232 | |
373 | |
233 | case REQ_OPEN: |
|
|
234 | { |
|
|
235 | /* convert fd to fh */ |
|
|
236 | SV *fh; |
|
|
237 | |
374 | |
238 | PUSHs (sv_2mortal (newSViv (req->result))); |
|
|
239 | PUTBACK; |
375 | PUTBACK; |
240 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
|
|
241 | SPAGAIN; |
|
|
242 | |
|
|
243 | fh = SvREFCNT_inc (POPs); |
|
|
244 | |
|
|
245 | PUSHMARK (SP); |
|
|
246 | XPUSHs (sv_2mortal (fh)); |
|
|
247 | } |
|
|
248 | break; |
|
|
249 | |
|
|
250 | case REQ_GROUP: |
|
|
251 | req->fd = 2; /* mark group as finished */ |
|
|
252 | |
|
|
253 | if (req->data) |
|
|
254 | { |
|
|
255 | int i; |
|
|
256 | AV *av = (AV *)req->data; |
|
|
257 | |
|
|
258 | EXTEND (SP, AvFILL (av) + 1); |
|
|
259 | for (i = 0; i <= AvFILL (av); ++i) |
|
|
260 | PUSHs (*av_fetch (av, i, 0)); |
|
|
261 | } |
|
|
262 | break; |
|
|
263 | |
|
|
264 | case REQ_NOP: |
|
|
265 | case REQ_SLEEP: |
|
|
266 | break; |
|
|
267 | |
|
|
268 | default: |
|
|
269 | PUSHs (sv_2mortal (newSViv (req->result))); |
|
|
270 | break; |
|
|
271 | } |
|
|
272 | |
|
|
273 | |
|
|
274 | PUTBACK; |
|
|
275 | call_sv (req->callback, G_VOID | G_EVAL); |
376 | call_sv (req->callback, G_VOID | G_EVAL); |
276 | SPAGAIN; |
377 | SPAGAIN; |
277 | |
378 | |
278 | FREETMPS; |
379 | FREETMPS; |
279 | LEAVE; |
380 | LEAVE; |
280 | |
|
|
281 | errno = errorno; |
|
|
282 | |
|
|
283 | if (SvTRUE (ERRSV)) |
|
|
284 | { |
381 | } |
285 | req_free (req); |
|
|
286 | croak (0); |
|
|
287 | } |
|
|
288 | } |
|
|
289 | |
382 | |
290 | static void req_free (aio_req req) |
|
|
291 | { |
|
|
292 | if (req->grp) |
383 | if (req->grp) |
293 | { |
384 | { |
294 | aio_req grp = req->grp; |
385 | aio_req grp = req->grp; |
295 | |
386 | |
296 | /* unlink request */ |
387 | /* unlink request */ |
… | |
… | |
301 | grp->grp_first = req->grp_next; |
392 | grp->grp_first = req->grp_next; |
302 | |
393 | |
303 | aio_grp_dec (grp); |
394 | aio_grp_dec (grp); |
304 | } |
395 | } |
305 | |
396 | |
|
|
397 | if (SvTRUE (ERRSV)) |
|
|
398 | { |
|
|
399 | req_free (req); |
|
|
400 | croak (0); |
|
|
401 | } |
|
|
402 | } |
|
|
403 | |
|
|
404 | static void req_free (aio_req req) |
|
|
405 | { |
306 | if (req->self) |
406 | if (req->self) |
307 | { |
407 | { |
308 | sv_unmagic (req->self, PERL_MAGIC_ext); |
408 | sv_unmagic (req->self, PERL_MAGIC_ext); |
309 | SvREFCNT_dec (req->self); |
409 | SvREFCNT_dec (req->self); |
310 | } |
410 | } |
… | |
… | |
321 | Safefree (req); |
421 | Safefree (req); |
322 | } |
422 | } |
323 | |
423 | |
324 | static void req_cancel (aio_req req) |
424 | static void req_cancel (aio_req req) |
325 | { |
425 | { |
326 | req->cancelled = 1; |
426 | req->flags |= FLAG_CANCELLED; |
327 | |
427 | |
328 | if (req->type == REQ_GROUP) |
428 | if (req->type == REQ_GROUP) |
329 | { |
429 | { |
330 | aio_req sub; |
430 | aio_req sub; |
331 | |
431 | |
… | |
… | |
342 | aio_req req; |
442 | aio_req req; |
343 | |
443 | |
344 | for (;;) |
444 | for (;;) |
345 | { |
445 | { |
346 | pthread_mutex_lock (&reslock); |
446 | pthread_mutex_lock (&reslock); |
347 | req = ress; |
447 | req = reqq_shift (&res_queue); |
348 | |
448 | |
349 | if (req) |
449 | if (req) |
350 | { |
450 | { |
351 | ress = req->next; |
|
|
352 | |
|
|
353 | if (!ress) |
451 | if (!res_queue.size) |
354 | { |
452 | { |
355 | /* read any signals sent by the worker threads */ |
453 | /* read any signals sent by the worker threads */ |
356 | char buf [32]; |
454 | char buf [32]; |
357 | while (read (respipe [0], buf, 32) == 32) |
455 | while (read (respipe [0], buf, 32) == 32) |
358 | ; |
456 | ; |
359 | |
|
|
360 | rese = 0; |
|
|
361 | } |
457 | } |
362 | } |
458 | } |
363 | |
459 | |
364 | pthread_mutex_unlock (&reslock); |
460 | pthread_mutex_unlock (&reslock); |
365 | |
461 | |
… | |
… | |
428 | start_thread (); |
524 | start_thread (); |
429 | |
525 | |
430 | ++nreqs; |
526 | ++nreqs; |
431 | |
527 | |
432 | pthread_mutex_lock (&reqlock); |
528 | pthread_mutex_lock (&reqlock); |
433 | |
529 | reqq_push (&req_queue, req); |
434 | req->next = 0; |
|
|
435 | |
|
|
436 | if (reqe) |
|
|
437 | { |
|
|
438 | reqe->next = req; |
|
|
439 | reqe = req; |
|
|
440 | } |
|
|
441 | else |
|
|
442 | reqe = reqs = req; |
|
|
443 | |
|
|
444 | pthread_cond_signal (&reqwait); |
530 | pthread_cond_signal (&reqwait); |
445 | pthread_mutex_unlock (&reqlock); |
531 | pthread_mutex_unlock (&reqlock); |
446 | |
532 | |
447 | if (nreqs > max_outstanding) |
533 | if (nreqs > max_outstanding) |
448 | for (;;) |
534 | for (;;) |
… | |
… | |
457 | } |
543 | } |
458 | |
544 | |
459 | static void end_thread (void) |
545 | static void end_thread (void) |
460 | { |
546 | { |
461 | aio_req req; |
547 | aio_req req; |
|
|
548 | |
462 | Newz (0, req, 1, aio_cb); |
549 | Newz (0, req, 1, aio_cb); |
|
|
550 | |
463 | req->type = REQ_QUIT; |
551 | req->type = REQ_QUIT; |
|
|
552 | req->pri = PRI_MAX + PRI_BIAS; |
464 | |
553 | |
465 | req_send (req); |
554 | req_send (req); |
466 | } |
555 | } |
467 | |
556 | |
468 | static void min_parallel (int nthreads) |
557 | static void min_parallel (int nthreads) |
… | |
… | |
555 | #if !HAVE_READAHEAD |
644 | #if !HAVE_READAHEAD |
556 | # define readahead aio_readahead |
645 | # define readahead aio_readahead |
557 | |
646 | |
558 | static ssize_t readahead (int fd, off_t offset, size_t count) |
647 | static ssize_t readahead (int fd, off_t offset, size_t count) |
559 | { |
648 | { |
560 | char readahead_buf[4096]; |
649 | dBUF; |
561 | |
650 | |
562 | while (count > 0) |
651 | while (count > 0) |
563 | { |
652 | { |
564 | size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); |
653 | size_t len = count < AIO_BUFSIZE ? count : AIO_BUFSIZE; |
565 | |
654 | |
566 | pread (fd, readahead_buf, len, offset); |
655 | pread (fd, aio_buf, len, offset); |
567 | offset += len; |
656 | offset += len; |
568 | count -= len; |
657 | count -= len; |
569 | } |
658 | } |
|
|
659 | |
|
|
660 | fBUF; |
570 | |
661 | |
571 | errno = 0; |
662 | errno = 0; |
572 | } |
663 | } |
573 | #endif |
664 | #endif |
574 | |
665 | |
… | |
… | |
660 | #endif |
751 | #endif |
661 | ) |
752 | ) |
662 | ) |
753 | ) |
663 | { |
754 | { |
664 | /* emulate sendfile. this is a major pain in the ass */ |
755 | /* emulate sendfile. this is a major pain in the ass */ |
665 | char buf[4096]; |
756 | dBUF; |
|
|
757 | |
666 | res = 0; |
758 | res = 0; |
667 | |
759 | |
668 | while (count) |
760 | while (count) |
669 | { |
761 | { |
670 | ssize_t cnt; |
762 | ssize_t cnt; |
671 | |
763 | |
672 | cnt = pread (ifd, buf, count > 4096 ? 4096 : count, offset); |
764 | cnt = pread (ifd, aio_buf, count > AIO_BUFSIZE ? AIO_BUFSIZE : count, offset); |
673 | |
765 | |
674 | if (cnt <= 0) |
766 | if (cnt <= 0) |
675 | { |
767 | { |
676 | if (cnt && !res) res = -1; |
768 | if (cnt && !res) res = -1; |
677 | break; |
769 | break; |
678 | } |
770 | } |
679 | |
771 | |
680 | cnt = write (ofd, buf, cnt); |
772 | cnt = write (ofd, aio_buf, cnt); |
681 | |
773 | |
682 | if (cnt <= 0) |
774 | if (cnt <= 0) |
683 | { |
775 | { |
684 | if (cnt && !res) res = -1; |
776 | if (cnt && !res) res = -1; |
685 | break; |
777 | break; |
… | |
… | |
687 | |
779 | |
688 | offset += cnt; |
780 | offset += cnt; |
689 | res += cnt; |
781 | res += cnt; |
690 | count -= cnt; |
782 | count -= cnt; |
691 | } |
783 | } |
|
|
784 | |
|
|
785 | fBUF; |
692 | } |
786 | } |
693 | |
787 | |
694 | return res; |
788 | return res; |
695 | } |
789 | } |
696 | |
790 | |
697 | /* read a full directory */ |
791 | /* read a full directory */ |
698 | static int scandir_ (const char *path, void **namesp) |
792 | static int scandir_ (const char *path, void **namesp) |
699 | { |
793 | { |
700 | DIR *dirp = opendir (path); |
794 | DIR *dirp; |
701 | union |
795 | union |
702 | { |
796 | { |
703 | struct dirent d; |
797 | struct dirent d; |
704 | char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; |
798 | char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; |
705 | } u; |
799 | } *u; |
706 | struct dirent *entp; |
800 | struct dirent *entp; |
707 | char *name, *names; |
801 | char *name, *names; |
708 | int memlen = 4096; |
802 | int memlen = 4096; |
709 | int memofs = 0; |
803 | int memofs = 0; |
710 | int res = 0; |
804 | int res = 0; |
711 | int errorno; |
805 | int errorno; |
712 | |
806 | |
|
|
807 | dirp = opendir (path); |
713 | if (!dirp) |
808 | if (!dirp) |
714 | return -1; |
809 | return -1; |
715 | |
810 | |
|
|
811 | u = malloc (sizeof (*u)); |
716 | names = malloc (memlen); |
812 | names = malloc (memlen); |
717 | |
813 | |
|
|
814 | if (u && names) |
718 | for (;;) |
815 | for (;;) |
719 | { |
816 | { |
|
|
817 | errno = 0; |
720 | errno = 0, readdir_r (dirp, &u.d, &entp); |
818 | readdir_r (dirp, &u->d, &entp); |
721 | |
819 | |
722 | if (!entp) |
820 | if (!entp) |
723 | break; |
821 | break; |
724 | |
822 | |
725 | name = entp->d_name; |
823 | name = entp->d_name; |
726 | |
824 | |
727 | if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) |
825 | if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) |
728 | { |
826 | { |
729 | int len = strlen (name) + 1; |
827 | int len = strlen (name) + 1; |
730 | |
828 | |
731 | res++; |
829 | res++; |
732 | |
830 | |
733 | while (memofs + len > memlen) |
831 | while (memofs + len > memlen) |
734 | { |
832 | { |
735 | memlen *= 2; |
833 | memlen *= 2; |
736 | names = realloc (names, memlen); |
834 | names = realloc (names, memlen); |
737 | if (!names) |
835 | if (!names) |
738 | break; |
836 | break; |
739 | } |
837 | } |
740 | |
838 | |
741 | memcpy (names + memofs, name, len); |
839 | memcpy (names + memofs, name, len); |
742 | memofs += len; |
840 | memofs += len; |
743 | } |
841 | } |
744 | } |
842 | } |
745 | |
843 | |
746 | errorno = errno; |
844 | errorno = errno; |
|
|
845 | free (u); |
747 | closedir (dirp); |
846 | closedir (dirp); |
748 | |
847 | |
749 | if (errorno) |
848 | if (errorno) |
750 | { |
849 | { |
751 | free (names); |
850 | free (names); |
… | |
… | |
768 | { |
867 | { |
769 | pthread_mutex_lock (&reqlock); |
868 | pthread_mutex_lock (&reqlock); |
770 | |
869 | |
771 | for (;;) |
870 | for (;;) |
772 | { |
871 | { |
773 | req = reqs; |
872 | req = reqq_shift (&req_queue); |
774 | |
|
|
775 | if (reqs) |
|
|
776 | { |
|
|
777 | reqs = reqs->next; |
|
|
778 | if (!reqs) reqe = 0; |
|
|
779 | } |
|
|
780 | |
873 | |
781 | if (req) |
874 | if (req) |
782 | break; |
875 | break; |
783 | |
876 | |
784 | pthread_cond_wait (&reqwait, &reqlock); |
877 | pthread_cond_wait (&reqwait, &reqlock); |
785 | } |
878 | } |
786 | |
879 | |
787 | pthread_mutex_unlock (&reqlock); |
880 | pthread_mutex_unlock (&reqlock); |
788 | |
881 | |
789 | errno = 0; /* strictly unnecessary */ |
882 | errno = 0; /* strictly unnecessary */ |
790 | |
|
|
791 | if (!req->cancelled) |
|
|
792 | switch (type = req->type) /* remember type for QUIT check */ |
883 | type = req->type; /* remember type for QUIT check */ |
|
|
884 | |
|
|
885 | if (!(req->flags & FLAG_CANCELLED)) |
|
|
886 | switch (type) |
793 | { |
887 | { |
794 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
888 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
795 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
889 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
796 | |
890 | |
797 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
891 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
… | |
… | |
835 | |
929 | |
836 | req->errorno = errno; |
930 | req->errorno = errno; |
837 | |
931 | |
838 | pthread_mutex_lock (&reslock); |
932 | pthread_mutex_lock (&reslock); |
839 | |
933 | |
840 | req->next = 0; |
934 | if (!reqq_push (&res_queue, req)) |
841 | |
|
|
842 | if (rese) |
|
|
843 | { |
|
|
844 | rese->next = req; |
|
|
845 | rese = req; |
|
|
846 | } |
|
|
847 | else |
|
|
848 | { |
|
|
849 | rese = ress = req; |
|
|
850 | |
|
|
851 | /* write a dummy byte to the pipe so fh becomes ready */ |
935 | /* write a dummy byte to the pipe so fh becomes ready */ |
852 | write (respipe [1], &respipe, 1); |
936 | write (respipe [1], &respipe, 1); |
853 | } |
|
|
854 | |
937 | |
855 | pthread_mutex_unlock (&reslock); |
938 | pthread_mutex_unlock (&reslock); |
856 | } |
939 | } |
857 | while (type != REQ_QUIT); |
940 | while (type != REQ_QUIT); |
858 | |
941 | |
… | |
… | |
889 | { |
972 | { |
890 | aio_req prv; |
973 | aio_req prv; |
891 | |
974 | |
892 | started = 0; |
975 | started = 0; |
893 | |
976 | |
894 | while (reqs) |
977 | while (prv = reqq_shift (&req_queue)) |
895 | { |
|
|
896 | prv = reqs; |
|
|
897 | reqs = prv->next; |
|
|
898 | req_free (prv); |
978 | req_free (prv); |
899 | } |
|
|
900 | |
979 | |
901 | reqs = reqe = 0; |
980 | while (prv = reqq_shift (&res_queue)) |
902 | |
|
|
903 | while (ress) |
|
|
904 | { |
|
|
905 | prv = ress; |
|
|
906 | ress = prv->next; |
|
|
907 | req_free (prv); |
981 | req_free (prv); |
908 | } |
982 | |
909 | |
|
|
910 | ress = rese = 0; |
|
|
911 | |
|
|
912 | close (respipe [0]); |
983 | close (respipe [0]); |
913 | close (respipe [1]); |
984 | close (respipe [1]); |
914 | create_pipe (); |
985 | create_pipe (); |
915 | |
986 | |
916 | atfork_parent (); |
987 | atfork_parent (); |
917 | } |
988 | } |
918 | |
989 | |
919 | #define dREQ \ |
990 | #define dREQ \ |
920 | aio_req req; \ |
991 | aio_req req; \ |
|
|
992 | int req_pri = next_pri; \ |
|
|
993 | next_pri = DEFAULT_PRI + PRI_BIAS; \ |
921 | \ |
994 | \ |
922 | if (SvOK (callback) && !SvROK (callback)) \ |
995 | if (SvOK (callback) && !SvROK (callback)) \ |
923 | croak ("callback must be undef or of reference type"); \ |
996 | croak ("callback must be undef or of reference type"); \ |
924 | \ |
997 | \ |
925 | Newz (0, req, 1, aio_cb); \ |
998 | Newz (0, req, 1, aio_cb); \ |
926 | if (!req) \ |
999 | if (!req) \ |
927 | croak ("out of memory during aio_req allocation"); \ |
1000 | croak ("out of memory during aio_req allocation"); \ |
928 | \ |
1001 | \ |
929 | req->callback = newSVsv (callback) |
1002 | req->callback = newSVsv (callback); \ |
|
|
1003 | req->pri = req_pri |
930 | |
1004 | |
931 | #define REQ_SEND \ |
1005 | #define REQ_SEND \ |
932 | req_send (req); \ |
1006 | req_send (req); \ |
933 | \ |
1007 | \ |
934 | if (GIMME_V != G_VOID) \ |
1008 | if (GIMME_V != G_VOID) \ |
… | |
… | |
1209 | SV * callback |
1283 | SV * callback |
1210 | PROTOTYPE: ;$ |
1284 | PROTOTYPE: ;$ |
1211 | PPCODE: |
1285 | PPCODE: |
1212 | { |
1286 | { |
1213 | dREQ; |
1287 | dREQ; |
|
|
1288 | |
1214 | req->type = REQ_GROUP; |
1289 | req->type = REQ_GROUP; |
1215 | req_send (req); |
1290 | req_send (req); |
|
|
1291 | |
1216 | XPUSHs (req_sv (req, AIO_GRP_KLASS)); |
1292 | XPUSHs (req_sv (req, AIO_GRP_KLASS)); |
1217 | } |
1293 | } |
1218 | |
1294 | |
1219 | void |
1295 | void |
1220 | aio_nop (callback=&PL_sv_undef) |
1296 | aio_nop (callback=&PL_sv_undef) |
… | |
… | |
1225 | |
1301 | |
1226 | req->type = REQ_NOP; |
1302 | req->type = REQ_NOP; |
1227 | |
1303 | |
1228 | REQ_SEND; |
1304 | REQ_SEND; |
1229 | } |
1305 | } |
|
|
1306 | |
|
|
1307 | void |
|
|
1308 | aioreq_pri (int pri = DEFAULT_PRI) |
|
|
1309 | CODE: |
|
|
1310 | if (pri < PRI_MIN) pri = PRI_MIN; |
|
|
1311 | if (pri > PRI_MAX) pri = PRI_MAX; |
|
|
1312 | next_pri = pri + PRI_BIAS; |
|
|
1313 | |
|
|
1314 | void |
|
|
1315 | aioreq_nice (int nice = 0) |
|
|
1316 | CODE: |
|
|
1317 | nice = next_pri - nice; |
|
|
1318 | if (nice < PRI_MIN) nice = PRI_MIN; |
|
|
1319 | if (nice > PRI_MAX) nice = PRI_MAX; |
|
|
1320 | next_pri = nice + PRI_BIAS; |
1230 | |
1321 | |
1231 | void |
1322 | void |
1232 | flush () |
1323 | flush () |
1233 | PROTOTYPE: |
1324 | PROTOTYPE: |
1234 | CODE: |
1325 | CODE: |
… | |
… | |
1283 | |
1374 | |
1284 | MODULE = IO::AIO PACKAGE = IO::AIO::REQ |
1375 | MODULE = IO::AIO PACKAGE = IO::AIO::REQ |
1285 | |
1376 | |
1286 | void |
1377 | void |
1287 | cancel (aio_req_ornot req) |
1378 | cancel (aio_req_ornot req) |
1288 | PROTOTYPE: |
|
|
1289 | CODE: |
1379 | CODE: |
1290 | req_cancel (req); |
1380 | req_cancel (req); |
|
|
1381 | |
|
|
1382 | void |
|
|
1383 | cb (aio_req_ornot req, SV *callback=&PL_sv_undef) |
|
|
1384 | CODE: |
|
|
1385 | SvREFCNT_dec (req->callback); |
|
|
1386 | req->callback = newSVsv (callback); |
1291 | |
1387 | |
1292 | MODULE = IO::AIO PACKAGE = IO::AIO::GRP |
1388 | MODULE = IO::AIO PACKAGE = IO::AIO::GRP |
1293 | |
1389 | |
1294 | void |
1390 | void |
1295 | add (aio_req grp, ...) |
1391 | add (aio_req grp, ...) |
… | |
… | |
1337 | SvREFCNT_dec (grp->data); |
1433 | SvREFCNT_dec (grp->data); |
1338 | grp->data = (SV *)av; |
1434 | grp->data = (SV *)av; |
1339 | } |
1435 | } |
1340 | |
1436 | |
1341 | void |
1437 | void |
1342 | lock (aio_req grp) |
|
|
1343 | CODE: |
|
|
1344 | ++grp->length; |
|
|
1345 | |
|
|
1346 | void |
|
|
1347 | unlock (aio_req grp) |
|
|
1348 | CODE: |
|
|
1349 | aio_grp_dec (grp); |
|
|
1350 | |
|
|
1351 | void |
|
|
1352 | feeder_limit (aio_req grp, int limit) |
1438 | limit (aio_req grp, int limit) |
1353 | CODE: |
1439 | CODE: |
1354 | grp->fd2 = limit; |
1440 | grp->fd2 = limit; |
1355 | aio_grp_feed (grp); |
1441 | aio_grp_feed (grp); |
1356 | |
1442 | |
1357 | void |
1443 | void |
1358 | set_feeder (aio_req grp, SV *callback=&PL_sv_undef) |
1444 | feed (aio_req grp, SV *callback=&PL_sv_undef) |
1359 | CODE: |
1445 | CODE: |
1360 | { |
1446 | { |
1361 | SvREFCNT_dec (grp->fh2); |
1447 | SvREFCNT_dec (grp->fh2); |
1362 | grp->fh2 = newSVsv (callback); |
1448 | grp->fh2 = newSVsv (callback); |
1363 | |
1449 | |