|
|
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, |
… | |
… | |
55 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
72 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
56 | REQ_FSYNC, REQ_FDATASYNC, |
73 | REQ_FSYNC, REQ_FDATASYNC, |
57 | REQ_UNLINK, REQ_RMDIR, REQ_RENAME, |
74 | REQ_UNLINK, REQ_RMDIR, REQ_RENAME, |
58 | REQ_READDIR, |
75 | REQ_READDIR, |
59 | REQ_LINK, REQ_SYMLINK, |
76 | REQ_LINK, REQ_SYMLINK, |
|
|
77 | REQ_GROUP, REQ_NOP, |
60 | REQ_SLEEP, |
78 | REQ_SLEEP, |
61 | REQ_GROUP, |
|
|
62 | }; |
79 | }; |
63 | |
80 | |
64 | #define AIO_REQ_KLASS "IO::AIO::REQ" |
81 | #define AIO_REQ_KLASS "IO::AIO::REQ" |
65 | #define AIO_GRP_KLASS "IO::AIO::GRP" |
82 | #define AIO_GRP_KLASS "IO::AIO::GRP" |
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 | |
|
|
71 | struct aio_cb *grp, *grp_prev, *grp_next, *grp_first; |
|
|
72 | |
|
|
73 | SV *self; /* the perl counterpart of this request, if any */ |
|
|
74 | |
87 | |
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_SLEEP: |
|
|
265 | break; |
|
|
266 | |
|
|
267 | default: |
|
|
268 | PUSHs (sv_2mortal (newSViv (req->result))); |
|
|
269 | break; |
|
|
270 | } |
|
|
271 | |
|
|
272 | |
|
|
273 | PUTBACK; |
|
|
274 | call_sv (req->callback, G_VOID | G_EVAL); |
376 | call_sv (req->callback, G_VOID | G_EVAL); |
275 | SPAGAIN; |
377 | SPAGAIN; |
276 | |
378 | |
277 | FREETMPS; |
379 | FREETMPS; |
278 | LEAVE; |
380 | LEAVE; |
279 | |
|
|
280 | errno = errorno; |
|
|
281 | |
|
|
282 | if (SvTRUE (ERRSV)) |
|
|
283 | { |
381 | } |
284 | req_free (req); |
|
|
285 | croak (0); |
|
|
286 | } |
|
|
287 | } |
|
|
288 | |
382 | |
289 | static void req_free (aio_req req) |
|
|
290 | { |
|
|
291 | if (req->grp) |
383 | if (req->grp) |
292 | { |
384 | { |
293 | aio_req grp = req->grp; |
385 | aio_req grp = req->grp; |
294 | |
386 | |
295 | /* unlink request */ |
387 | /* unlink request */ |
… | |
… | |
300 | grp->grp_first = req->grp_next; |
392 | grp->grp_first = req->grp_next; |
301 | |
393 | |
302 | aio_grp_dec (grp); |
394 | aio_grp_dec (grp); |
303 | } |
395 | } |
304 | |
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 | { |
305 | if (req->self) |
406 | if (req->self) |
306 | { |
407 | { |
307 | sv_unmagic (req->self, PERL_MAGIC_ext); |
408 | sv_unmagic (req->self, PERL_MAGIC_ext); |
308 | SvREFCNT_dec (req->self); |
409 | SvREFCNT_dec (req->self); |
309 | } |
410 | } |
… | |
… | |
320 | Safefree (req); |
421 | Safefree (req); |
321 | } |
422 | } |
322 | |
423 | |
323 | static void req_cancel (aio_req req) |
424 | static void req_cancel (aio_req req) |
324 | { |
425 | { |
325 | req->cancelled = 1; |
426 | req->flags |= FLAG_CANCELLED; |
326 | |
427 | |
327 | if (req->type == REQ_GROUP) |
428 | if (req->type == REQ_GROUP) |
328 | { |
429 | { |
329 | aio_req sub; |
430 | aio_req sub; |
330 | |
431 | |
… | |
… | |
341 | aio_req req; |
442 | aio_req req; |
342 | |
443 | |
343 | for (;;) |
444 | for (;;) |
344 | { |
445 | { |
345 | pthread_mutex_lock (&reslock); |
446 | pthread_mutex_lock (&reslock); |
346 | req = ress; |
447 | req = reqq_shift (&res_queue); |
347 | |
448 | |
348 | if (req) |
449 | if (req) |
349 | { |
450 | { |
350 | ress = req->next; |
|
|
351 | |
|
|
352 | if (!ress) |
451 | if (!res_queue.size) |
353 | { |
452 | { |
354 | /* read any signals sent by the worker threads */ |
453 | /* read any signals sent by the worker threads */ |
355 | char buf [32]; |
454 | char buf [32]; |
356 | while (read (respipe [0], buf, 32) == 32) |
455 | while (read (respipe [0], buf, 32) == 32) |
357 | ; |
456 | ; |
358 | |
|
|
359 | rese = 0; |
|
|
360 | } |
457 | } |
361 | } |
458 | } |
362 | |
459 | |
363 | pthread_mutex_unlock (&reslock); |
460 | pthread_mutex_unlock (&reslock); |
364 | |
461 | |
… | |
… | |
427 | start_thread (); |
524 | start_thread (); |
428 | |
525 | |
429 | ++nreqs; |
526 | ++nreqs; |
430 | |
527 | |
431 | pthread_mutex_lock (&reqlock); |
528 | pthread_mutex_lock (&reqlock); |
432 | |
529 | reqq_push (&req_queue, req); |
433 | req->next = 0; |
|
|
434 | |
|
|
435 | if (reqe) |
|
|
436 | { |
|
|
437 | reqe->next = req; |
|
|
438 | reqe = req; |
|
|
439 | } |
|
|
440 | else |
|
|
441 | reqe = reqs = req; |
|
|
442 | |
|
|
443 | pthread_cond_signal (&reqwait); |
530 | pthread_cond_signal (&reqwait); |
444 | pthread_mutex_unlock (&reqlock); |
531 | pthread_mutex_unlock (&reqlock); |
445 | |
532 | |
446 | if (nreqs > max_outstanding) |
533 | if (nreqs > max_outstanding) |
447 | for (;;) |
534 | for (;;) |
… | |
… | |
456 | } |
543 | } |
457 | |
544 | |
458 | static void end_thread (void) |
545 | static void end_thread (void) |
459 | { |
546 | { |
460 | aio_req req; |
547 | aio_req req; |
|
|
548 | |
461 | Newz (0, req, 1, aio_cb); |
549 | Newz (0, req, 1, aio_cb); |
|
|
550 | |
462 | req->type = REQ_QUIT; |
551 | req->type = REQ_QUIT; |
|
|
552 | req->pri = PRI_MAX + PRI_BIAS; |
463 | |
553 | |
464 | req_send (req); |
554 | req_send (req); |
465 | } |
555 | } |
466 | |
556 | |
467 | static void min_parallel (int nthreads) |
557 | static void min_parallel (int nthreads) |
… | |
… | |
554 | #if !HAVE_READAHEAD |
644 | #if !HAVE_READAHEAD |
555 | # define readahead aio_readahead |
645 | # define readahead aio_readahead |
556 | |
646 | |
557 | 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) |
558 | { |
648 | { |
559 | char readahead_buf[4096]; |
649 | dBUF; |
560 | |
650 | |
561 | while (count > 0) |
651 | while (count > 0) |
562 | { |
652 | { |
563 | size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); |
653 | size_t len = count < AIO_BUFSIZE ? count : AIO_BUFSIZE; |
564 | |
654 | |
565 | pread (fd, readahead_buf, len, offset); |
655 | pread (fd, aio_buf, len, offset); |
566 | offset += len; |
656 | offset += len; |
567 | count -= len; |
657 | count -= len; |
568 | } |
658 | } |
|
|
659 | |
|
|
660 | fBUF; |
569 | |
661 | |
570 | errno = 0; |
662 | errno = 0; |
571 | } |
663 | } |
572 | #endif |
664 | #endif |
573 | |
665 | |
… | |
… | |
659 | #endif |
751 | #endif |
660 | ) |
752 | ) |
661 | ) |
753 | ) |
662 | { |
754 | { |
663 | /* emulate sendfile. this is a major pain in the ass */ |
755 | /* emulate sendfile. this is a major pain in the ass */ |
664 | char buf[4096]; |
756 | dBUF; |
|
|
757 | |
665 | res = 0; |
758 | res = 0; |
666 | |
759 | |
667 | while (count) |
760 | while (count) |
668 | { |
761 | { |
669 | ssize_t cnt; |
762 | ssize_t cnt; |
670 | |
763 | |
671 | cnt = pread (ifd, buf, count > 4096 ? 4096 : count, offset); |
764 | cnt = pread (ifd, aio_buf, count > AIO_BUFSIZE ? AIO_BUFSIZE : count, offset); |
672 | |
765 | |
673 | if (cnt <= 0) |
766 | if (cnt <= 0) |
674 | { |
767 | { |
675 | if (cnt && !res) res = -1; |
768 | if (cnt && !res) res = -1; |
676 | break; |
769 | break; |
677 | } |
770 | } |
678 | |
771 | |
679 | cnt = write (ofd, buf, cnt); |
772 | cnt = write (ofd, aio_buf, cnt); |
680 | |
773 | |
681 | if (cnt <= 0) |
774 | if (cnt <= 0) |
682 | { |
775 | { |
683 | if (cnt && !res) res = -1; |
776 | if (cnt && !res) res = -1; |
684 | break; |
777 | break; |
… | |
… | |
686 | |
779 | |
687 | offset += cnt; |
780 | offset += cnt; |
688 | res += cnt; |
781 | res += cnt; |
689 | count -= cnt; |
782 | count -= cnt; |
690 | } |
783 | } |
|
|
784 | |
|
|
785 | fBUF; |
691 | } |
786 | } |
692 | |
787 | |
693 | return res; |
788 | return res; |
694 | } |
789 | } |
695 | |
790 | |
696 | /* read a full directory */ |
791 | /* read a full directory */ |
697 | static int scandir_ (const char *path, void **namesp) |
792 | static int scandir_ (const char *path, void **namesp) |
698 | { |
793 | { |
699 | DIR *dirp = opendir (path); |
794 | DIR *dirp; |
700 | union |
795 | union |
701 | { |
796 | { |
702 | struct dirent d; |
797 | struct dirent d; |
703 | char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; |
798 | char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; |
704 | } u; |
799 | } *u; |
705 | struct dirent *entp; |
800 | struct dirent *entp; |
706 | char *name, *names; |
801 | char *name, *names; |
707 | int memlen = 4096; |
802 | int memlen = 4096; |
708 | int memofs = 0; |
803 | int memofs = 0; |
709 | int res = 0; |
804 | int res = 0; |
710 | int errorno; |
805 | int errorno; |
711 | |
806 | |
|
|
807 | dirp = opendir (path); |
712 | if (!dirp) |
808 | if (!dirp) |
713 | return -1; |
809 | return -1; |
714 | |
810 | |
|
|
811 | u = malloc (sizeof (*u)); |
715 | names = malloc (memlen); |
812 | names = malloc (memlen); |
716 | |
813 | |
|
|
814 | if (u && names) |
717 | for (;;) |
815 | for (;;) |
718 | { |
816 | { |
|
|
817 | errno = 0; |
719 | errno = 0, readdir_r (dirp, &u.d, &entp); |
818 | readdir_r (dirp, &u->d, &entp); |
720 | |
819 | |
721 | if (!entp) |
820 | if (!entp) |
722 | break; |
821 | break; |
723 | |
822 | |
724 | name = entp->d_name; |
823 | name = entp->d_name; |
725 | |
824 | |
726 | if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) |
825 | if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) |
727 | { |
826 | { |
728 | int len = strlen (name) + 1; |
827 | int len = strlen (name) + 1; |
729 | |
828 | |
730 | res++; |
829 | res++; |
731 | |
830 | |
732 | while (memofs + len > memlen) |
831 | while (memofs + len > memlen) |
733 | { |
832 | { |
734 | memlen *= 2; |
833 | memlen *= 2; |
735 | names = realloc (names, memlen); |
834 | names = realloc (names, memlen); |
736 | if (!names) |
835 | if (!names) |
737 | break; |
836 | break; |
738 | } |
837 | } |
739 | |
838 | |
740 | memcpy (names + memofs, name, len); |
839 | memcpy (names + memofs, name, len); |
741 | memofs += len; |
840 | memofs += len; |
742 | } |
841 | } |
743 | } |
842 | } |
744 | |
843 | |
745 | errorno = errno; |
844 | errorno = errno; |
|
|
845 | free (u); |
746 | closedir (dirp); |
846 | closedir (dirp); |
747 | |
847 | |
748 | if (errorno) |
848 | if (errorno) |
749 | { |
849 | { |
750 | free (names); |
850 | free (names); |
… | |
… | |
767 | { |
867 | { |
768 | pthread_mutex_lock (&reqlock); |
868 | pthread_mutex_lock (&reqlock); |
769 | |
869 | |
770 | for (;;) |
870 | for (;;) |
771 | { |
871 | { |
772 | req = reqs; |
872 | req = reqq_shift (&req_queue); |
773 | |
|
|
774 | if (reqs) |
|
|
775 | { |
|
|
776 | reqs = reqs->next; |
|
|
777 | if (!reqs) reqe = 0; |
|
|
778 | } |
|
|
779 | |
873 | |
780 | if (req) |
874 | if (req) |
781 | break; |
875 | break; |
782 | |
876 | |
783 | pthread_cond_wait (&reqwait, &reqlock); |
877 | pthread_cond_wait (&reqwait, &reqlock); |
784 | } |
878 | } |
785 | |
879 | |
786 | pthread_mutex_unlock (&reqlock); |
880 | pthread_mutex_unlock (&reqlock); |
787 | |
881 | |
788 | errno = 0; /* strictly unnecessary */ |
882 | errno = 0; /* strictly unnecessary */ |
789 | |
|
|
790 | if (!req->cancelled) |
|
|
791 | 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) |
792 | { |
887 | { |
793 | 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; |
794 | 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; |
795 | |
890 | |
796 | 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; |
… | |
… | |
820 | tv.tv_usec = req->fd2; |
915 | tv.tv_usec = req->fd2; |
821 | |
916 | |
822 | req->result = select (0, 0, 0, 0, &tv); |
917 | req->result = select (0, 0, 0, 0, &tv); |
823 | } |
918 | } |
824 | |
919 | |
|
|
920 | case REQ_GROUP: |
|
|
921 | case REQ_NOP: |
825 | case REQ_QUIT: |
922 | case REQ_QUIT: |
826 | break; |
923 | break; |
827 | |
924 | |
828 | default: |
925 | default: |
829 | req->result = ENOSYS; |
926 | req->result = ENOSYS; |
… | |
… | |
832 | |
929 | |
833 | req->errorno = errno; |
930 | req->errorno = errno; |
834 | |
931 | |
835 | pthread_mutex_lock (&reslock); |
932 | pthread_mutex_lock (&reslock); |
836 | |
933 | |
837 | req->next = 0; |
934 | if (!reqq_push (&res_queue, req)) |
838 | |
|
|
839 | if (rese) |
|
|
840 | { |
|
|
841 | rese->next = req; |
|
|
842 | rese = req; |
|
|
843 | } |
|
|
844 | else |
|
|
845 | { |
|
|
846 | rese = ress = req; |
|
|
847 | |
|
|
848 | /* write a dummy byte to the pipe so fh becomes ready */ |
935 | /* write a dummy byte to the pipe so fh becomes ready */ |
849 | write (respipe [1], &respipe, 1); |
936 | write (respipe [1], &respipe, 1); |
850 | } |
|
|
851 | |
937 | |
852 | pthread_mutex_unlock (&reslock); |
938 | pthread_mutex_unlock (&reslock); |
853 | } |
939 | } |
854 | while (type != REQ_QUIT); |
940 | while (type != REQ_QUIT); |
855 | |
941 | |
… | |
… | |
886 | { |
972 | { |
887 | aio_req prv; |
973 | aio_req prv; |
888 | |
974 | |
889 | started = 0; |
975 | started = 0; |
890 | |
976 | |
891 | while (reqs) |
977 | while (prv = reqq_shift (&req_queue)) |
892 | { |
|
|
893 | prv = reqs; |
|
|
894 | reqs = prv->next; |
|
|
895 | req_free (prv); |
978 | req_free (prv); |
896 | } |
|
|
897 | |
979 | |
898 | reqs = reqe = 0; |
980 | while (prv = reqq_shift (&res_queue)) |
899 | |
|
|
900 | while (ress) |
|
|
901 | { |
|
|
902 | prv = ress; |
|
|
903 | ress = prv->next; |
|
|
904 | req_free (prv); |
981 | req_free (prv); |
905 | } |
982 | |
906 | |
|
|
907 | ress = rese = 0; |
|
|
908 | |
|
|
909 | close (respipe [0]); |
983 | close (respipe [0]); |
910 | close (respipe [1]); |
984 | close (respipe [1]); |
911 | create_pipe (); |
985 | create_pipe (); |
912 | |
986 | |
913 | atfork_parent (); |
987 | atfork_parent (); |
914 | } |
988 | } |
915 | |
989 | |
916 | #define dREQ \ |
990 | #define dREQ \ |
917 | aio_req req; \ |
991 | aio_req req; \ |
|
|
992 | int req_pri = next_pri; \ |
|
|
993 | next_pri = DEFAULT_PRI + PRI_BIAS; \ |
918 | \ |
994 | \ |
919 | if (SvOK (callback) && !SvROK (callback)) \ |
995 | if (SvOK (callback) && !SvROK (callback)) \ |
920 | croak ("callback must be undef or of reference type"); \ |
996 | croak ("callback must be undef or of reference type"); \ |
921 | \ |
997 | \ |
922 | Newz (0, req, 1, aio_cb); \ |
998 | Newz (0, req, 1, aio_cb); \ |
923 | if (!req) \ |
999 | if (!req) \ |
924 | croak ("out of memory during aio_req allocation"); \ |
1000 | croak ("out of memory during aio_req allocation"); \ |
925 | \ |
1001 | \ |
926 | req->callback = newSVsv (callback) |
1002 | req->callback = newSVsv (callback); \ |
|
|
1003 | req->pri = req_pri |
927 | |
1004 | |
928 | #define REQ_SEND \ |
1005 | #define REQ_SEND \ |
929 | req_send (req); \ |
1006 | req_send (req); \ |
930 | \ |
1007 | \ |
931 | if (GIMME_V != G_VOID) \ |
1008 | if (GIMME_V != G_VOID) \ |
… | |
… | |
1206 | SV * callback |
1283 | SV * callback |
1207 | PROTOTYPE: ;$ |
1284 | PROTOTYPE: ;$ |
1208 | PPCODE: |
1285 | PPCODE: |
1209 | { |
1286 | { |
1210 | dREQ; |
1287 | dREQ; |
|
|
1288 | |
1211 | req->type = REQ_GROUP; |
1289 | req->type = REQ_GROUP; |
1212 | req_send (req); |
1290 | req_send (req); |
|
|
1291 | |
1213 | XPUSHs (req_sv (req, AIO_GRP_KLASS)); |
1292 | XPUSHs (req_sv (req, AIO_GRP_KLASS)); |
1214 | } |
1293 | } |
|
|
1294 | |
|
|
1295 | void |
|
|
1296 | aio_nop (callback=&PL_sv_undef) |
|
|
1297 | SV * callback |
|
|
1298 | PPCODE: |
|
|
1299 | { |
|
|
1300 | dREQ; |
|
|
1301 | |
|
|
1302 | req->type = REQ_NOP; |
|
|
1303 | |
|
|
1304 | REQ_SEND; |
|
|
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; |
1215 | |
1313 | |
1216 | void |
1314 | void |
1217 | flush () |
1315 | flush () |
1218 | PROTOTYPE: |
1316 | PROTOTYPE: |
1219 | CODE: |
1317 | CODE: |
… | |
… | |
1268 | |
1366 | |
1269 | MODULE = IO::AIO PACKAGE = IO::AIO::REQ |
1367 | MODULE = IO::AIO PACKAGE = IO::AIO::REQ |
1270 | |
1368 | |
1271 | void |
1369 | void |
1272 | cancel (aio_req_ornot req) |
1370 | cancel (aio_req_ornot req) |
1273 | PROTOTYPE: |
|
|
1274 | CODE: |
1371 | CODE: |
1275 | req_cancel (req); |
1372 | req_cancel (req); |
|
|
1373 | |
|
|
1374 | void |
|
|
1375 | cb (aio_req_ornot req, SV *callback=&PL_sv_undef) |
|
|
1376 | CODE: |
|
|
1377 | SvREFCNT_dec (req->callback); |
|
|
1378 | req->callback = newSVsv (callback); |
1276 | |
1379 | |
1277 | MODULE = IO::AIO PACKAGE = IO::AIO::GRP |
1380 | MODULE = IO::AIO PACKAGE = IO::AIO::GRP |
1278 | |
1381 | |
1279 | void |
1382 | void |
1280 | add (aio_req grp, ...) |
1383 | add (aio_req grp, ...) |
… | |
… | |
1322 | SvREFCNT_dec (grp->data); |
1425 | SvREFCNT_dec (grp->data); |
1323 | grp->data = (SV *)av; |
1426 | grp->data = (SV *)av; |
1324 | } |
1427 | } |
1325 | |
1428 | |
1326 | void |
1429 | void |
1327 | lock (aio_req grp) |
|
|
1328 | CODE: |
|
|
1329 | ++grp->length; |
|
|
1330 | |
|
|
1331 | void |
|
|
1332 | unlock (aio_req grp) |
|
|
1333 | CODE: |
|
|
1334 | aio_grp_dec (grp); |
|
|
1335 | |
|
|
1336 | void |
|
|
1337 | feeder_limit (aio_req grp, int limit) |
1430 | limit (aio_req grp, int limit) |
1338 | CODE: |
1431 | CODE: |
1339 | grp->fd2 = limit; |
1432 | grp->fd2 = limit; |
1340 | aio_grp_feed (grp); |
1433 | aio_grp_feed (grp); |
1341 | |
1434 | |
1342 | void |
1435 | void |
1343 | set_feeder (aio_req grp, SV *callback=&PL_sv_undef) |
1436 | feed (aio_req grp, SV *callback=&PL_sv_undef) |
1344 | CODE: |
1437 | CODE: |
1345 | { |
1438 | { |
1346 | SvREFCNT_dec (grp->fh2); |
1439 | SvREFCNT_dec (grp->fh2); |
1347 | grp->fh2 = newSVsv (callback); |
1440 | grp->fh2 = newSVsv (callback); |
1348 | |
1441 | |