… | |
… | |
5 | #include "perl.h" |
5 | #include "perl.h" |
6 | #include "XSUB.h" |
6 | #include "XSUB.h" |
7 | |
7 | |
8 | #include "autoconf/config.h" |
8 | #include "autoconf/config.h" |
9 | |
9 | |
|
|
10 | #include <pthread.h> |
|
|
11 | |
|
|
12 | #include <stddef.h> |
10 | #include <sys/types.h> |
13 | #include <sys/types.h> |
11 | #include <sys/stat.h> |
14 | #include <sys/stat.h> |
12 | |
15 | #include <limits.h> |
13 | #include <unistd.h> |
16 | #include <unistd.h> |
14 | #include <fcntl.h> |
17 | #include <fcntl.h> |
15 | #include <signal.h> |
18 | #include <signal.h> |
16 | #include <sched.h> |
19 | #include <sched.h> |
17 | |
20 | |
18 | #include <pthread.h> |
21 | #if HAVE_SENDFILE |
|
|
22 | # if __linux |
|
|
23 | # include <sys/sendfile.h> |
|
|
24 | # elif __freebsd |
|
|
25 | # include <sys/socket.h> |
|
|
26 | # include <sys/uio.h> |
|
|
27 | # elif __hpux |
|
|
28 | # include <sys/socket.h> |
|
|
29 | # elif __solaris /* not yet */ |
|
|
30 | # include <sys/sendfile.h> |
|
|
31 | # else |
|
|
32 | # error sendfile support requested but not available |
|
|
33 | # endif |
|
|
34 | #endif |
19 | |
35 | |
20 | typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
36 | /* used for struct dirent, AIX doesn't provide it */ |
21 | typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
37 | #ifndef NAME_MAX |
22 | typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
38 | # define NAME_MAX 4096 |
|
|
39 | #endif |
23 | |
40 | |
24 | #if __ia64 |
41 | #if __ia64 |
25 | # define STACKSIZE 65536 |
42 | # define STACKSIZE 65536 |
26 | #else |
43 | #else |
27 | # define STACKSIZE 4096 |
44 | # define STACKSIZE 8192 |
28 | #endif |
45 | #endif |
29 | |
46 | |
30 | enum { |
47 | enum { |
31 | REQ_QUIT, |
48 | REQ_QUIT, |
32 | REQ_OPEN, REQ_CLOSE, |
49 | REQ_OPEN, REQ_CLOSE, |
33 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
50 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
|
|
51 | REQ_SENDFILE, |
34 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
52 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
35 | REQ_FSYNC, REQ_FDATASYNC, |
53 | REQ_FSYNC, REQ_FDATASYNC, |
36 | REQ_UNLINK, REQ_RMDIR, |
54 | REQ_UNLINK, REQ_RMDIR, |
|
|
55 | REQ_READDIR, |
37 | REQ_SYMLINK, |
56 | REQ_SYMLINK, |
38 | }; |
57 | }; |
39 | |
58 | |
40 | typedef struct aio_cb { |
59 | typedef struct aio_cb { |
41 | struct aio_cb *volatile next; |
60 | struct aio_cb *volatile next; |
42 | |
61 | |
43 | int type; |
62 | int type; |
44 | |
63 | |
|
|
64 | /* should receive a cleanup, with unions */ |
45 | int fd; |
65 | int fd, fd2; |
46 | off_t offset; |
66 | off_t offset; |
47 | size_t length; |
67 | size_t length; |
48 | ssize_t result; |
68 | ssize_t result; |
49 | mode_t mode; /* open */ |
69 | mode_t mode; /* open */ |
50 | int errorno; |
70 | int errorno; |
51 | SV *data, *callback, *fh; |
71 | SV *data, *callback; |
|
|
72 | SV *fh, *fh2; |
52 | void *dataptr, *data2ptr; |
73 | void *dataptr, *data2ptr; |
53 | STRLEN dataoffset; |
74 | STRLEN dataoffset; |
54 | |
75 | |
55 | Stat_t *statdata; |
76 | Stat_t *statdata; |
56 | } aio_cb; |
77 | } aio_cb; |
57 | |
78 | |
58 | typedef aio_cb *aio_req; |
79 | typedef aio_cb *aio_req; |
59 | |
80 | |
60 | static int started; |
81 | static int started, wanted; |
61 | static volatile int nreqs; |
82 | static volatile int nreqs; |
62 | static int max_outstanding = 1<<30; |
83 | static int max_outstanding = 1<<30; |
63 | static int respipe [2]; |
84 | static int respipe [2]; |
64 | |
85 | |
65 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
86 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
… | |
… | |
75 | SvREFCNT_dec (req->data); |
96 | SvREFCNT_dec (req->data); |
76 | |
97 | |
77 | if (req->fh) |
98 | if (req->fh) |
78 | SvREFCNT_dec (req->fh); |
99 | SvREFCNT_dec (req->fh); |
79 | |
100 | |
|
|
101 | if (req->fh2) |
|
|
102 | SvREFCNT_dec (req->fh2); |
|
|
103 | |
80 | if (req->statdata) |
104 | if (req->statdata) |
81 | Safefree (req->statdata); |
105 | Safefree (req->statdata); |
82 | |
106 | |
83 | if (req->callback) |
107 | if (req->callback) |
84 | SvREFCNT_dec (req->callback); |
108 | SvREFCNT_dec (req->callback); |
|
|
109 | |
|
|
110 | if (req->type == REQ_READDIR && req->result >= 0) |
|
|
111 | free (req->data2ptr); |
85 | |
112 | |
86 | Safefree (req); |
113 | Safefree (req); |
87 | } |
114 | } |
88 | |
115 | |
89 | static void |
116 | static void |
… | |
… | |
116 | { |
143 | { |
117 | ress = req->next; |
144 | ress = req->next; |
118 | |
145 | |
119 | if (!ress) |
146 | if (!ress) |
120 | { |
147 | { |
121 | rese = 0; |
|
|
122 | |
|
|
123 | /* read any signals sent by the worker threads */ |
148 | /* read any signals sent by the worker threads */ |
124 | char buf [32]; |
149 | char buf [32]; |
125 | while (read (respipe [0], buf, 32) == 32) |
150 | while (read (respipe [0], buf, 32) == 32) |
126 | ; |
151 | ; |
|
|
152 | |
|
|
153 | rese = 0; |
127 | } |
154 | } |
128 | } |
155 | } |
129 | |
156 | |
130 | pthread_mutex_unlock (&reslock); |
157 | pthread_mutex_unlock (&reslock); |
131 | |
158 | |
… | |
… | |
154 | PL_statcache = *(req->statdata); |
181 | PL_statcache = *(req->statdata); |
155 | } |
182 | } |
156 | |
183 | |
157 | ENTER; |
184 | ENTER; |
158 | PUSHMARK (SP); |
185 | PUSHMARK (SP); |
159 | XPUSHs (sv_2mortal (newSViv (req->result))); |
|
|
160 | |
186 | |
161 | if (req->type == REQ_OPEN) |
187 | if (req->type == REQ_READDIR) |
162 | { |
188 | { |
|
|
189 | SV *rv = &PL_sv_undef; |
|
|
190 | |
|
|
191 | if (req->result >= 0) |
|
|
192 | { |
|
|
193 | char *buf = req->data2ptr; |
|
|
194 | AV *av = newAV (); |
|
|
195 | |
|
|
196 | while (req->result) |
|
|
197 | { |
|
|
198 | SV *sv = newSVpv (buf, 0); |
|
|
199 | |
|
|
200 | av_push (av, sv); |
|
|
201 | buf += SvCUR (sv) + 1; |
|
|
202 | req->result--; |
|
|
203 | } |
|
|
204 | |
|
|
205 | rv = sv_2mortal (newRV_noinc ((SV *)av)); |
|
|
206 | } |
|
|
207 | |
|
|
208 | XPUSHs (rv); |
|
|
209 | } |
|
|
210 | else |
|
|
211 | { |
|
|
212 | XPUSHs (sv_2mortal (newSViv (req->result))); |
|
|
213 | |
|
|
214 | if (req->type == REQ_OPEN) |
|
|
215 | { |
163 | /* convert fd to fh */ |
216 | /* convert fd to fh */ |
164 | SV *fh; |
217 | SV *fh; |
165 | |
218 | |
166 | PUTBACK; |
219 | PUTBACK; |
167 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
220 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
168 | SPAGAIN; |
221 | SPAGAIN; |
169 | |
222 | |
170 | fh = SvREFCNT_inc (POPs); |
223 | fh = SvREFCNT_inc (POPs); |
171 | |
224 | |
172 | PUSHMARK (SP); |
225 | PUSHMARK (SP); |
173 | XPUSHs (sv_2mortal (fh)); |
226 | XPUSHs (sv_2mortal (fh)); |
|
|
227 | } |
174 | } |
228 | } |
175 | |
229 | |
176 | if (SvOK (req->callback)) |
230 | if (SvOK (req->callback)) |
177 | { |
231 | { |
178 | PUTBACK; |
232 | PUTBACK; |
… | |
… | |
221 | } |
275 | } |
222 | |
276 | |
223 | static void |
277 | static void |
224 | send_req (aio_req req) |
278 | send_req (aio_req req) |
225 | { |
279 | { |
|
|
280 | while (started < wanted && nreqs >= started) |
|
|
281 | start_thread (); |
|
|
282 | |
226 | nreqs++; |
283 | nreqs++; |
227 | |
284 | |
228 | pthread_mutex_lock (&reqlock); |
285 | pthread_mutex_lock (&reqlock); |
229 | |
286 | |
230 | req->next = 0; |
287 | req->next = 0; |
… | |
… | |
262 | send_req (req); |
319 | send_req (req); |
263 | } |
320 | } |
264 | |
321 | |
265 | static void min_parallel (int nthreads) |
322 | static void min_parallel (int nthreads) |
266 | { |
323 | { |
267 | while (nthreads > started) |
324 | if (wanted < nthreads) |
268 | start_thread (); |
325 | wanted = nthreads; |
269 | } |
326 | } |
270 | |
327 | |
271 | static void max_parallel (int nthreads) |
328 | static void max_parallel (int nthreads) |
272 | { |
329 | { |
273 | int cur = started; |
330 | int cur = started; |
274 | |
331 | |
|
|
332 | if (wanted > nthreads) |
|
|
333 | wanted = nthreads; |
|
|
334 | |
275 | while (cur > nthreads) |
335 | while (cur > wanted) |
276 | { |
336 | { |
277 | end_thread (); |
337 | end_thread (); |
278 | cur--; |
338 | cur--; |
279 | } |
339 | } |
280 | |
340 | |
281 | while (started > nthreads) |
341 | while (started > wanted) |
282 | { |
342 | { |
283 | poll_wait (); |
343 | poll_wait (); |
284 | poll_cb (); |
344 | poll_cb (); |
285 | } |
345 | } |
286 | } |
346 | } |
… | |
… | |
293 | if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)) |
353 | if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)) |
294 | croak ("cannot set result pipe to nonblocking mode"); |
354 | croak ("cannot set result pipe to nonblocking mode"); |
295 | |
355 | |
296 | if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) |
356 | if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) |
297 | croak ("cannot set result pipe to nonblocking mode"); |
357 | croak ("cannot set result pipe to nonblocking mode"); |
298 | } |
|
|
299 | |
|
|
300 | static void atfork_prepare (void) |
|
|
301 | { |
|
|
302 | pthread_mutex_lock (&reqlock); |
|
|
303 | pthread_mutex_lock (&reslock); |
|
|
304 | } |
|
|
305 | |
|
|
306 | static void atfork_parent (void) |
|
|
307 | { |
|
|
308 | pthread_mutex_unlock (&reslock); |
|
|
309 | pthread_mutex_unlock (&reqlock); |
|
|
310 | } |
|
|
311 | |
|
|
312 | static void atfork_child (void) |
|
|
313 | { |
|
|
314 | aio_req prv; |
|
|
315 | |
|
|
316 | int restart = started; |
|
|
317 | started = 0; |
|
|
318 | |
|
|
319 | while (reqs) |
|
|
320 | { |
|
|
321 | prv = reqs; |
|
|
322 | reqs = prv->next; |
|
|
323 | free_req (prv); |
|
|
324 | } |
|
|
325 | |
|
|
326 | reqs = reqe = 0; |
|
|
327 | |
|
|
328 | while (ress) |
|
|
329 | { |
|
|
330 | prv = ress; |
|
|
331 | ress = prv->next; |
|
|
332 | free_req (prv); |
|
|
333 | } |
|
|
334 | |
|
|
335 | ress = rese = 0; |
|
|
336 | |
|
|
337 | close (respipe [0]); |
|
|
338 | close (respipe [1]); |
|
|
339 | create_pipe (); |
|
|
340 | |
|
|
341 | atfork_parent (); |
|
|
342 | |
|
|
343 | min_parallel (restart); |
|
|
344 | } |
358 | } |
345 | |
359 | |
346 | /*****************************************************************************/ |
360 | /*****************************************************************************/ |
347 | /* work around various missing functions */ |
361 | /* work around various missing functions */ |
348 | |
362 | |
… | |
… | |
353 | /* |
367 | /* |
354 | * make our pread/pwrite safe against themselves, but not against |
368 | * make our pread/pwrite safe against themselves, but not against |
355 | * normal read/write by using a mutex. slows down execution a lot, |
369 | * normal read/write by using a mutex. slows down execution a lot, |
356 | * but that's your problem, not mine. |
370 | * but that's your problem, not mine. |
357 | */ |
371 | */ |
358 | static pthread_mutex_t iolock = PTHREAD_MUTEX_INITIALIZER; |
372 | static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER; |
359 | |
373 | |
360 | static ssize_t |
374 | static ssize_t |
361 | pread (int fd, void *buf, size_t count, off_t offset) |
375 | pread (int fd, void *buf, size_t count, off_t offset) |
362 | { |
376 | { |
363 | ssize_t res; |
377 | ssize_t res; |
364 | off_t ooffset; |
378 | off_t ooffset; |
365 | |
379 | |
366 | pthread_mutex_lock (&iolock); |
380 | pthread_mutex_lock (&preadwritelock); |
367 | ooffset = lseek (fd, 0, SEEK_CUR); |
381 | ooffset = lseek (fd, 0, SEEK_CUR); |
368 | lseek (fd, offset, SEEK_SET); |
382 | lseek (fd, offset, SEEK_SET); |
369 | res = read (fd, buf, count); |
383 | res = read (fd, buf, count); |
370 | lseek (fd, ooffset, SEEK_SET); |
384 | lseek (fd, ooffset, SEEK_SET); |
371 | pthread_mutex_unlock (&iolock); |
385 | pthread_mutex_unlock (&preadwritelock); |
372 | |
386 | |
373 | return res; |
387 | return res; |
374 | } |
388 | } |
375 | |
389 | |
376 | static ssize_t |
390 | static ssize_t |
377 | pwrite (int fd, void *buf, size_t count, off_t offset) |
391 | pwrite (int fd, void *buf, size_t count, off_t offset) |
378 | { |
392 | { |
379 | ssize_t res; |
393 | ssize_t res; |
380 | off_t ooffset; |
394 | off_t ooffset; |
381 | |
395 | |
382 | pthread_mutex_lock (&iolock); |
396 | pthread_mutex_lock (&preadwritelock); |
383 | ooffset = lseek (fd, 0, SEEK_CUR); |
397 | ooffset = lseek (fd, 0, SEEK_CUR); |
384 | lseek (fd, offset, SEEK_SET); |
398 | lseek (fd, offset, SEEK_SET); |
385 | res = write (fd, buf, count); |
399 | res = write (fd, buf, count); |
386 | lseek (fd, offset, SEEK_SET); |
400 | lseek (fd, offset, SEEK_SET); |
387 | pthread_mutex_unlock (&iolock); |
401 | pthread_mutex_unlock (&preadwritelock); |
388 | |
402 | |
389 | return res; |
403 | return res; |
390 | } |
404 | } |
391 | #endif |
405 | #endif |
392 | |
406 | |
… | |
… | |
395 | #endif |
409 | #endif |
396 | |
410 | |
397 | #if !HAVE_READAHEAD |
411 | #if !HAVE_READAHEAD |
398 | # define readahead aio_readahead |
412 | # define readahead aio_readahead |
399 | |
413 | |
400 | static char readahead_buf[4096]; |
|
|
401 | |
|
|
402 | static ssize_t |
414 | static ssize_t |
403 | readahead (int fd, off_t offset, size_t count) |
415 | readahead (int fd, off_t offset, size_t count) |
404 | { |
416 | { |
|
|
417 | char readahead_buf[4096]; |
|
|
418 | |
405 | while (count > 0) |
419 | while (count > 0) |
406 | { |
420 | { |
407 | size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); |
421 | size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); |
408 | |
422 | |
409 | pread (fd, readahead_buf, len, offset); |
423 | pread (fd, readahead_buf, len, offset); |
… | |
… | |
412 | } |
426 | } |
413 | |
427 | |
414 | errno = 0; |
428 | errno = 0; |
415 | } |
429 | } |
416 | #endif |
430 | #endif |
|
|
431 | |
|
|
432 | #if !HAVE_READDIR_R |
|
|
433 | # define readdir_r aio_readdir_r |
|
|
434 | |
|
|
435 | static pthread_mutex_t readdirlock = PTHREAD_MUTEX_INITIALIZER; |
|
|
436 | |
|
|
437 | static int |
|
|
438 | readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) |
|
|
439 | { |
|
|
440 | struct dirent *e; |
|
|
441 | int errorno; |
|
|
442 | |
|
|
443 | pthread_mutex_lock (&readdirlock); |
|
|
444 | |
|
|
445 | e = readdir (dirp); |
|
|
446 | errorno = errno; |
|
|
447 | |
|
|
448 | if (e) |
|
|
449 | { |
|
|
450 | *res = ent; |
|
|
451 | strcpy (ent->d_name, e->d_name); |
|
|
452 | } |
|
|
453 | else |
|
|
454 | *res = 0; |
|
|
455 | |
|
|
456 | pthread_mutex_unlock (&readdirlock); |
|
|
457 | |
|
|
458 | errno = errorno; |
|
|
459 | return e ? 0 : -1; |
|
|
460 | } |
|
|
461 | #endif |
|
|
462 | |
|
|
463 | /* sendfile always needs emulation */ |
|
|
464 | static ssize_t |
|
|
465 | sendfile_ (int ofd, int ifd, off_t offset, size_t count) |
|
|
466 | { |
|
|
467 | ssize_t res; |
|
|
468 | |
|
|
469 | if (!count) |
|
|
470 | return 0; |
|
|
471 | |
|
|
472 | #if HAVE_SENDFILE |
|
|
473 | # if __linux |
|
|
474 | res = sendfile (ofd, ifd, &offset, count); |
|
|
475 | |
|
|
476 | # elif __freebsd |
|
|
477 | /* |
|
|
478 | * Of course, the freebsd sendfile is a dire hack with no thoughts |
|
|
479 | * wasted on making it similar to other I/O functions. |
|
|
480 | */ |
|
|
481 | { |
|
|
482 | off_t sbytes; |
|
|
483 | res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); |
|
|
484 | |
|
|
485 | if (res < 0 && sbytes) |
|
|
486 | /* maybe only on EAGAIN only: as usual, the manpage leaves you guessing */ |
|
|
487 | res = sbytes; |
|
|
488 | } |
|
|
489 | |
|
|
490 | # elif __hpux |
|
|
491 | res = sendfile (ofd, ifd, offset, count, 0, 0); |
|
|
492 | |
|
|
493 | # elif __solaris |
|
|
494 | { |
|
|
495 | struct sendfilevec vec; |
|
|
496 | size_t sbytes; |
|
|
497 | |
|
|
498 | vec.sfv_fd = ifd; |
|
|
499 | vec.sfv_flag = 0; |
|
|
500 | vec.sfv_off = offset; |
|
|
501 | vec.sfv_len = count; |
|
|
502 | |
|
|
503 | res = sendfilev (ofd, &vec, 1, &sbytes); |
|
|
504 | |
|
|
505 | if (res < 0 && sbytes) |
|
|
506 | res = sbytes; |
|
|
507 | } |
|
|
508 | |
|
|
509 | # endif |
|
|
510 | #else |
|
|
511 | res = -1; |
|
|
512 | errno = ENOSYS; |
|
|
513 | #endif |
|
|
514 | |
|
|
515 | if (res < 0 |
|
|
516 | && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK |
|
|
517 | #if __solaris |
|
|
518 | || errno == EAFNOSUPPORT || errno == EPROTOTYPE |
|
|
519 | #endif |
|
|
520 | ) |
|
|
521 | ) |
|
|
522 | { |
|
|
523 | /* emulate sendfile. this is a major pain in the ass */ |
|
|
524 | char buf[4096]; |
|
|
525 | res = 0; |
|
|
526 | |
|
|
527 | while (count) |
|
|
528 | { |
|
|
529 | ssize_t cnt; |
|
|
530 | |
|
|
531 | cnt = pread (ifd, buf, count > 4096 ? 4096 : count, offset); |
|
|
532 | |
|
|
533 | if (cnt <= 0) |
|
|
534 | { |
|
|
535 | if (cnt && !res) res = -1; |
|
|
536 | break; |
|
|
537 | } |
|
|
538 | |
|
|
539 | cnt = write (ofd, buf, cnt); |
|
|
540 | |
|
|
541 | if (cnt <= 0) |
|
|
542 | { |
|
|
543 | if (cnt && !res) res = -1; |
|
|
544 | break; |
|
|
545 | } |
|
|
546 | |
|
|
547 | offset += cnt; |
|
|
548 | res += cnt; |
|
|
549 | count -= cnt; |
|
|
550 | } |
|
|
551 | } |
|
|
552 | |
|
|
553 | return res; |
|
|
554 | } |
|
|
555 | |
|
|
556 | /* read a full directory */ |
|
|
557 | static int |
|
|
558 | scandir_ (const char *path, void **namesp) |
|
|
559 | { |
|
|
560 | DIR *dirp = opendir (path); |
|
|
561 | union |
|
|
562 | { |
|
|
563 | struct dirent d; |
|
|
564 | char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; |
|
|
565 | } u; |
|
|
566 | struct dirent *entp; |
|
|
567 | char *name, *names; |
|
|
568 | int memlen = 4096; |
|
|
569 | int memofs = 0; |
|
|
570 | int res = 0; |
|
|
571 | int errorno; |
|
|
572 | |
|
|
573 | if (!dirp) |
|
|
574 | return -1; |
|
|
575 | |
|
|
576 | names = malloc (memlen); |
|
|
577 | |
|
|
578 | for (;;) |
|
|
579 | { |
|
|
580 | errno = 0, readdir_r (dirp, &u.d, &entp); |
|
|
581 | |
|
|
582 | if (!entp) |
|
|
583 | break; |
|
|
584 | |
|
|
585 | name = entp->d_name; |
|
|
586 | |
|
|
587 | if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) |
|
|
588 | { |
|
|
589 | int len = strlen (name) + 1; |
|
|
590 | |
|
|
591 | res++; |
|
|
592 | |
|
|
593 | while (memofs + len > memlen) |
|
|
594 | { |
|
|
595 | memlen *= 2; |
|
|
596 | names = realloc (names, memlen); |
|
|
597 | if (!names) |
|
|
598 | break; |
|
|
599 | } |
|
|
600 | |
|
|
601 | memcpy (names + memofs, name, len); |
|
|
602 | memofs += len; |
|
|
603 | } |
|
|
604 | } |
|
|
605 | |
|
|
606 | errorno = errno; |
|
|
607 | closedir (dirp); |
|
|
608 | |
|
|
609 | if (errorno) |
|
|
610 | { |
|
|
611 | free (names); |
|
|
612 | errno = errorno; |
|
|
613 | res = -1; |
|
|
614 | } |
|
|
615 | |
|
|
616 | *namesp = (void *)names; |
|
|
617 | return res; |
|
|
618 | } |
417 | |
619 | |
418 | /*****************************************************************************/ |
620 | /*****************************************************************************/ |
419 | |
621 | |
420 | static void * |
622 | static void * |
421 | aio_proc (void *thr_arg) |
623 | aio_proc (void *thr_arg) |
… | |
… | |
453 | { |
655 | { |
454 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
656 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
455 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
657 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
456 | |
658 | |
457 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
659 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
|
|
660 | case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length); break; |
458 | |
661 | |
459 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
662 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
460 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
663 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
461 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
664 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
462 | |
665 | |
… | |
… | |
466 | case REQ_RMDIR: req->result = rmdir (req->dataptr); break; |
669 | case REQ_RMDIR: req->result = rmdir (req->dataptr); break; |
467 | case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; |
670 | case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; |
468 | |
671 | |
469 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
672 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
470 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
673 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
|
|
674 | case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break; |
471 | |
675 | |
472 | case REQ_QUIT: |
676 | case REQ_QUIT: |
473 | break; |
677 | break; |
474 | |
678 | |
475 | default: |
679 | default: |
… | |
… | |
499 | pthread_mutex_unlock (&reslock); |
703 | pthread_mutex_unlock (&reslock); |
500 | } |
704 | } |
501 | while (type != REQ_QUIT); |
705 | while (type != REQ_QUIT); |
502 | |
706 | |
503 | return 0; |
707 | return 0; |
|
|
708 | } |
|
|
709 | |
|
|
710 | /*****************************************************************************/ |
|
|
711 | |
|
|
712 | static void atfork_prepare (void) |
|
|
713 | { |
|
|
714 | pthread_mutex_lock (&reqlock); |
|
|
715 | pthread_mutex_lock (&reslock); |
|
|
716 | #if !HAVE_PREADWRITE |
|
|
717 | pthread_mutex_lock (&preadwritelock); |
|
|
718 | #endif |
|
|
719 | #if !HAVE_READDIR_R |
|
|
720 | pthread_mutex_lock (&readdirlock); |
|
|
721 | #endif |
|
|
722 | } |
|
|
723 | |
|
|
724 | static void atfork_parent (void) |
|
|
725 | { |
|
|
726 | #if !HAVE_READDIR_R |
|
|
727 | pthread_mutex_unlock (&readdirlock); |
|
|
728 | #endif |
|
|
729 | #if !HAVE_PREADWRITE |
|
|
730 | pthread_mutex_unlock (&preadwritelock); |
|
|
731 | #endif |
|
|
732 | pthread_mutex_unlock (&reslock); |
|
|
733 | pthread_mutex_unlock (&reqlock); |
|
|
734 | } |
|
|
735 | |
|
|
736 | static void atfork_child (void) |
|
|
737 | { |
|
|
738 | aio_req prv; |
|
|
739 | |
|
|
740 | started = 0; |
|
|
741 | |
|
|
742 | while (reqs) |
|
|
743 | { |
|
|
744 | prv = reqs; |
|
|
745 | reqs = prv->next; |
|
|
746 | free_req (prv); |
|
|
747 | } |
|
|
748 | |
|
|
749 | reqs = reqe = 0; |
|
|
750 | |
|
|
751 | while (ress) |
|
|
752 | { |
|
|
753 | prv = ress; |
|
|
754 | ress = prv->next; |
|
|
755 | free_req (prv); |
|
|
756 | } |
|
|
757 | |
|
|
758 | ress = rese = 0; |
|
|
759 | |
|
|
760 | close (respipe [0]); |
|
|
761 | close (respipe [1]); |
|
|
762 | create_pipe (); |
|
|
763 | |
|
|
764 | atfork_parent (); |
504 | } |
765 | } |
505 | |
766 | |
506 | #define dREQ \ |
767 | #define dREQ \ |
507 | aio_req req; \ |
768 | aio_req req; \ |
508 | \ |
769 | \ |
… | |
… | |
585 | |
846 | |
586 | void |
847 | void |
587 | aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
848 | aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
588 | SV * fh |
849 | SV * fh |
589 | UV offset |
850 | UV offset |
590 | IV length |
851 | UV length |
591 | SV * data |
852 | SV * data |
592 | IV dataoffset |
853 | UV dataoffset |
593 | SV * callback |
854 | SV * callback |
594 | ALIAS: |
855 | ALIAS: |
595 | aio_read = REQ_READ |
856 | aio_read = REQ_READ |
596 | aio_write = REQ_WRITE |
857 | aio_write = REQ_WRITE |
597 | PROTOTYPE: $$$$$;$ |
858 | PROTOTYPE: $$$$$;$ |
… | |
… | |
643 | req->data2ptr = (void *)data; |
904 | req->data2ptr = (void *)data; |
644 | } |
905 | } |
645 | |
906 | |
646 | send_req (req); |
907 | send_req (req); |
647 | } |
908 | } |
|
|
909 | } |
|
|
910 | |
|
|
911 | void |
|
|
912 | aio_sendfile(out_fh,in_fh,in_offset,length,callback=&PL_sv_undef) |
|
|
913 | SV * out_fh |
|
|
914 | SV * in_fh |
|
|
915 | UV in_offset |
|
|
916 | UV length |
|
|
917 | SV * callback |
|
|
918 | PROTOTYPE: $$$$;$ |
|
|
919 | CODE: |
|
|
920 | { |
|
|
921 | dREQ; |
|
|
922 | |
|
|
923 | req->type = REQ_SENDFILE; |
|
|
924 | req->fh = newSVsv (out_fh); |
|
|
925 | req->fd = PerlIO_fileno (IoIFP (sv_2io (out_fh))); |
|
|
926 | req->fh2 = newSVsv (in_fh); |
|
|
927 | req->fd2 = PerlIO_fileno (IoIFP (sv_2io (in_fh))); |
|
|
928 | req->offset = in_offset; |
|
|
929 | req->length = length; |
|
|
930 | |
|
|
931 | send_req (req); |
648 | } |
932 | } |
649 | |
933 | |
650 | void |
934 | void |
651 | aio_readahead(fh,offset,length,callback=&PL_sv_undef) |
935 | aio_readahead(fh,offset,length,callback=&PL_sv_undef) |
652 | SV * fh |
936 | SV * fh |
… | |
… | |
736 | |
1020 | |
737 | send_req (req); |
1021 | send_req (req); |
738 | } |
1022 | } |
739 | |
1023 | |
740 | void |
1024 | void |
|
|
1025 | aio_readdir(pathname,callback=&PL_sv_undef) |
|
|
1026 | SV * pathname |
|
|
1027 | SV * callback |
|
|
1028 | CODE: |
|
|
1029 | { |
|
|
1030 | dREQ; |
|
|
1031 | |
|
|
1032 | req->type = REQ_READDIR; |
|
|
1033 | req->data = newSVsv (pathname); |
|
|
1034 | req->dataptr = SvPVbyte_nolen (req->data); |
|
|
1035 | |
|
|
1036 | send_req (req); |
|
|
1037 | } |
|
|
1038 | |
|
|
1039 | void |
741 | flush() |
1040 | flush() |
742 | PROTOTYPE: |
1041 | PROTOTYPE: |
743 | CODE: |
1042 | CODE: |
744 | while (nreqs) |
1043 | while (nreqs) |
745 | { |
1044 | { |