… | |
… | |
7 | |
7 | |
8 | #include "autoconf/config.h" |
8 | #include "autoconf/config.h" |
9 | |
9 | |
10 | #include <pthread.h> |
10 | #include <pthread.h> |
11 | |
11 | |
|
|
12 | #include <stddef.h> |
|
|
13 | #include <errno.h> |
12 | #include <sys/types.h> |
14 | #include <sys/types.h> |
13 | #include <sys/stat.h> |
15 | #include <sys/stat.h> |
14 | |
16 | #include <limits.h> |
15 | #include <unistd.h> |
17 | #include <unistd.h> |
16 | #include <fcntl.h> |
18 | #include <fcntl.h> |
17 | #include <signal.h> |
19 | #include <signal.h> |
18 | #include <sched.h> |
20 | #include <sched.h> |
19 | |
21 | |
… | |
… | |
23 | # elif __freebsd |
25 | # elif __freebsd |
24 | # include <sys/socket.h> |
26 | # include <sys/socket.h> |
25 | # include <sys/uio.h> |
27 | # include <sys/uio.h> |
26 | # elif __hpux |
28 | # elif __hpux |
27 | # include <sys/socket.h> |
29 | # include <sys/socket.h> |
|
|
30 | # elif __solaris /* not yet */ |
|
|
31 | # include <sys/sendfile.h> |
|
|
32 | # else |
|
|
33 | # error sendfile support requested but not available |
28 | # endif |
34 | # endif |
|
|
35 | #endif |
|
|
36 | |
|
|
37 | /* used for struct dirent, AIX doesn't provide it */ |
|
|
38 | #ifndef NAME_MAX |
|
|
39 | # define NAME_MAX 4096 |
29 | #endif |
40 | #endif |
30 | |
41 | |
31 | #if __ia64 |
42 | #if __ia64 |
32 | # define STACKSIZE 65536 |
43 | # define STACKSIZE 65536 |
33 | #else |
44 | #else |
34 | # define STACKSIZE 4096 |
45 | # define STACKSIZE 8192 |
35 | #endif |
46 | #endif |
36 | |
47 | |
37 | enum { |
48 | enum { |
38 | REQ_QUIT, |
49 | REQ_QUIT, |
39 | REQ_OPEN, REQ_CLOSE, |
50 | REQ_OPEN, REQ_CLOSE, |
40 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
51 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
41 | REQ_SENDFILE, |
52 | REQ_SENDFILE, |
42 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
53 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
43 | REQ_FSYNC, REQ_FDATASYNC, |
54 | REQ_FSYNC, REQ_FDATASYNC, |
44 | REQ_UNLINK, REQ_RMDIR, |
55 | REQ_UNLINK, REQ_RMDIR, REQ_RENAME, |
45 | REQ_SYMLINK, |
56 | REQ_READDIR, |
|
|
57 | REQ_LINK, REQ_SYMLINK, |
46 | }; |
58 | }; |
47 | |
59 | |
48 | typedef struct aio_cb { |
60 | typedef struct aio_cb { |
49 | struct aio_cb *volatile next; |
61 | struct aio_cb *volatile next; |
50 | |
62 | |
51 | int type; |
63 | int type; |
52 | |
64 | |
|
|
65 | /* should receive a cleanup, with unions */ |
53 | int fd, fd2; |
66 | int fd, fd2; |
54 | off_t offset; |
67 | off_t offset; |
55 | size_t length; |
68 | size_t length; |
56 | ssize_t result; |
69 | ssize_t result; |
57 | mode_t mode; /* open */ |
70 | mode_t mode; /* open */ |
… | |
… | |
93 | Safefree (req->statdata); |
106 | Safefree (req->statdata); |
94 | |
107 | |
95 | if (req->callback) |
108 | if (req->callback) |
96 | SvREFCNT_dec (req->callback); |
109 | SvREFCNT_dec (req->callback); |
97 | |
110 | |
|
|
111 | if (req->type == REQ_READDIR && req->result >= 0) |
|
|
112 | free (req->data2ptr); |
|
|
113 | |
98 | Safefree (req); |
114 | Safefree (req); |
99 | } |
115 | } |
100 | |
116 | |
101 | static void |
117 | static void |
102 | poll_wait () |
118 | poll_wait () |
… | |
… | |
166 | PL_statcache = *(req->statdata); |
182 | PL_statcache = *(req->statdata); |
167 | } |
183 | } |
168 | |
184 | |
169 | ENTER; |
185 | ENTER; |
170 | PUSHMARK (SP); |
186 | PUSHMARK (SP); |
171 | XPUSHs (sv_2mortal (newSViv (req->result))); |
|
|
172 | |
187 | |
173 | if (req->type == REQ_OPEN) |
188 | if (req->type == REQ_READDIR) |
174 | { |
189 | { |
|
|
190 | SV *rv = &PL_sv_undef; |
|
|
191 | |
|
|
192 | if (req->result >= 0) |
|
|
193 | { |
|
|
194 | char *buf = req->data2ptr; |
|
|
195 | AV *av = newAV (); |
|
|
196 | |
|
|
197 | while (req->result) |
|
|
198 | { |
|
|
199 | SV *sv = newSVpv (buf, 0); |
|
|
200 | |
|
|
201 | av_push (av, sv); |
|
|
202 | buf += SvCUR (sv) + 1; |
|
|
203 | req->result--; |
|
|
204 | } |
|
|
205 | |
|
|
206 | rv = sv_2mortal (newRV_noinc ((SV *)av)); |
|
|
207 | } |
|
|
208 | |
|
|
209 | XPUSHs (rv); |
|
|
210 | } |
|
|
211 | else |
|
|
212 | { |
|
|
213 | XPUSHs (sv_2mortal (newSViv (req->result))); |
|
|
214 | |
|
|
215 | if (req->type == REQ_OPEN) |
|
|
216 | { |
175 | /* convert fd to fh */ |
217 | /* convert fd to fh */ |
176 | SV *fh; |
218 | SV *fh; |
177 | |
219 | |
178 | PUTBACK; |
220 | PUTBACK; |
179 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
221 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
180 | SPAGAIN; |
222 | SPAGAIN; |
181 | |
223 | |
182 | fh = SvREFCNT_inc (POPs); |
224 | fh = SvREFCNT_inc (POPs); |
183 | |
225 | |
184 | PUSHMARK (SP); |
226 | PUSHMARK (SP); |
185 | XPUSHs (sv_2mortal (fh)); |
227 | XPUSHs (sv_2mortal (fh)); |
|
|
228 | } |
186 | } |
229 | } |
187 | |
230 | |
188 | if (SvOK (req->callback)) |
231 | if (SvOK (req->callback)) |
189 | { |
232 | { |
190 | PUTBACK; |
233 | PUTBACK; |
… | |
… | |
313 | |
356 | |
314 | if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) |
357 | if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) |
315 | croak ("cannot set result pipe to nonblocking mode"); |
358 | croak ("cannot set result pipe to nonblocking mode"); |
316 | } |
359 | } |
317 | |
360 | |
318 | static void atfork_prepare (void) |
|
|
319 | { |
|
|
320 | pthread_mutex_lock (&reqlock); |
|
|
321 | pthread_mutex_lock (&reslock); |
|
|
322 | } |
|
|
323 | |
|
|
324 | static void atfork_parent (void) |
|
|
325 | { |
|
|
326 | pthread_mutex_unlock (&reslock); |
|
|
327 | pthread_mutex_unlock (&reqlock); |
|
|
328 | } |
|
|
329 | |
|
|
330 | static void atfork_child (void) |
|
|
331 | { |
|
|
332 | aio_req prv; |
|
|
333 | |
|
|
334 | started = 0; |
|
|
335 | |
|
|
336 | while (reqs) |
|
|
337 | { |
|
|
338 | prv = reqs; |
|
|
339 | reqs = prv->next; |
|
|
340 | free_req (prv); |
|
|
341 | } |
|
|
342 | |
|
|
343 | reqs = reqe = 0; |
|
|
344 | |
|
|
345 | while (ress) |
|
|
346 | { |
|
|
347 | prv = ress; |
|
|
348 | ress = prv->next; |
|
|
349 | free_req (prv); |
|
|
350 | } |
|
|
351 | |
|
|
352 | ress = rese = 0; |
|
|
353 | |
|
|
354 | close (respipe [0]); |
|
|
355 | close (respipe [1]); |
|
|
356 | create_pipe (); |
|
|
357 | |
|
|
358 | atfork_parent (); |
|
|
359 | } |
|
|
360 | |
|
|
361 | /*****************************************************************************/ |
361 | /*****************************************************************************/ |
362 | /* work around various missing functions */ |
362 | /* work around various missing functions */ |
363 | |
363 | |
364 | #if !HAVE_PREADWRITE |
364 | #if !HAVE_PREADWRITE |
365 | # define pread aio_pread |
365 | # define pread aio_pread |
… | |
… | |
368 | /* |
368 | /* |
369 | * make our pread/pwrite safe against themselves, but not against |
369 | * make our pread/pwrite safe against themselves, but not against |
370 | * normal read/write by using a mutex. slows down execution a lot, |
370 | * normal read/write by using a mutex. slows down execution a lot, |
371 | * but that's your problem, not mine. |
371 | * but that's your problem, not mine. |
372 | */ |
372 | */ |
373 | static pthread_mutex_t iolock = PTHREAD_MUTEX_INITIALIZER; |
373 | static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER; |
374 | |
374 | |
375 | static ssize_t |
375 | static ssize_t |
376 | pread (int fd, void *buf, size_t count, off_t offset) |
376 | pread (int fd, void *buf, size_t count, off_t offset) |
377 | { |
377 | { |
378 | ssize_t res; |
378 | ssize_t res; |
379 | off_t ooffset; |
379 | off_t ooffset; |
380 | |
380 | |
381 | pthread_mutex_lock (&iolock); |
381 | pthread_mutex_lock (&preadwritelock); |
382 | ooffset = lseek (fd, 0, SEEK_CUR); |
382 | ooffset = lseek (fd, 0, SEEK_CUR); |
383 | lseek (fd, offset, SEEK_SET); |
383 | lseek (fd, offset, SEEK_SET); |
384 | res = read (fd, buf, count); |
384 | res = read (fd, buf, count); |
385 | lseek (fd, ooffset, SEEK_SET); |
385 | lseek (fd, ooffset, SEEK_SET); |
386 | pthread_mutex_unlock (&iolock); |
386 | pthread_mutex_unlock (&preadwritelock); |
387 | |
387 | |
388 | return res; |
388 | return res; |
389 | } |
389 | } |
390 | |
390 | |
391 | static ssize_t |
391 | static ssize_t |
392 | pwrite (int fd, void *buf, size_t count, off_t offset) |
392 | pwrite (int fd, void *buf, size_t count, off_t offset) |
393 | { |
393 | { |
394 | ssize_t res; |
394 | ssize_t res; |
395 | off_t ooffset; |
395 | off_t ooffset; |
396 | |
396 | |
397 | pthread_mutex_lock (&iolock); |
397 | pthread_mutex_lock (&preadwritelock); |
398 | ooffset = lseek (fd, 0, SEEK_CUR); |
398 | ooffset = lseek (fd, 0, SEEK_CUR); |
399 | lseek (fd, offset, SEEK_SET); |
399 | lseek (fd, offset, SEEK_SET); |
400 | res = write (fd, buf, count); |
400 | res = write (fd, buf, count); |
401 | lseek (fd, offset, SEEK_SET); |
401 | lseek (fd, offset, SEEK_SET); |
402 | pthread_mutex_unlock (&iolock); |
402 | pthread_mutex_unlock (&preadwritelock); |
403 | |
403 | |
404 | return res; |
404 | return res; |
405 | } |
405 | } |
406 | #endif |
406 | #endif |
407 | |
407 | |
… | |
… | |
410 | #endif |
410 | #endif |
411 | |
411 | |
412 | #if !HAVE_READAHEAD |
412 | #if !HAVE_READAHEAD |
413 | # define readahead aio_readahead |
413 | # define readahead aio_readahead |
414 | |
414 | |
415 | static char readahead_buf[4096]; |
|
|
416 | |
|
|
417 | static ssize_t |
415 | static ssize_t |
418 | readahead (int fd, off_t offset, size_t count) |
416 | readahead (int fd, off_t offset, size_t count) |
419 | { |
417 | { |
|
|
418 | char readahead_buf[4096]; |
|
|
419 | |
420 | while (count > 0) |
420 | while (count > 0) |
421 | { |
421 | { |
422 | size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); |
422 | size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); |
423 | |
423 | |
424 | pread (fd, readahead_buf, len, offset); |
424 | pread (fd, readahead_buf, len, offset); |
… | |
… | |
428 | |
428 | |
429 | errno = 0; |
429 | errno = 0; |
430 | } |
430 | } |
431 | #endif |
431 | #endif |
432 | |
432 | |
|
|
433 | #if !HAVE_READDIR_R |
|
|
434 | # define readdir_r aio_readdir_r |
|
|
435 | |
|
|
436 | static pthread_mutex_t readdirlock = PTHREAD_MUTEX_INITIALIZER; |
|
|
437 | |
|
|
438 | static int |
|
|
439 | readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) |
|
|
440 | { |
|
|
441 | struct dirent *e; |
|
|
442 | int errorno; |
|
|
443 | |
|
|
444 | pthread_mutex_lock (&readdirlock); |
|
|
445 | |
|
|
446 | e = readdir (dirp); |
|
|
447 | errorno = errno; |
|
|
448 | |
|
|
449 | if (e) |
|
|
450 | { |
|
|
451 | *res = ent; |
|
|
452 | strcpy (ent->d_name, e->d_name); |
|
|
453 | } |
|
|
454 | else |
|
|
455 | *res = 0; |
|
|
456 | |
|
|
457 | pthread_mutex_unlock (&readdirlock); |
|
|
458 | |
|
|
459 | errno = errorno; |
|
|
460 | return e ? 0 : -1; |
|
|
461 | } |
|
|
462 | #endif |
|
|
463 | |
433 | /* sendfile always needs emulation */ |
464 | /* sendfile always needs emulation */ |
434 | static ssize_t |
465 | static ssize_t |
435 | sendfile_ (int ofd, int ifd, off_t offset, size_t count) |
466 | sendfile_ (int ofd, int ifd, off_t offset, size_t count) |
436 | { |
467 | { |
437 | ssize_t res; |
468 | ssize_t res; |
438 | |
469 | |
439 | if (!count) |
470 | if (!count) |
440 | return 0; |
471 | return 0; |
441 | |
472 | |
|
|
473 | #if HAVE_SENDFILE |
442 | #if __linux |
474 | # if __linux |
443 | res = sendfile (ofd, ifd, &offset, count); |
475 | res = sendfile (ofd, ifd, &offset, count); |
444 | |
476 | |
445 | #elif __freebsd |
477 | # elif __freebsd |
446 | /* |
478 | /* |
447 | * Of course, the freebsd sendfile is a dire hack with no thoughts |
479 | * Of course, the freebsd sendfile is a dire hack with no thoughts |
448 | * wasted on making it similar to other i/o functions. |
480 | * wasted on making it similar to other I/O functions. |
449 | */ |
481 | */ |
450 | { |
482 | { |
451 | off_t sbytes; |
483 | off_t sbytes; |
452 | res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); |
484 | res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); |
453 | |
485 | |
454 | if (!res && errno == EAGAIN) |
486 | if (res < 0 && sbytes) |
455 | /* maybe on others, too, as usual, the manpage leaves you guessing */ |
487 | /* maybe only on EAGAIN only: as usual, the manpage leaves you guessing */ |
456 | res = sbytes; |
488 | res = sbytes; |
457 | } |
489 | } |
458 | |
490 | |
459 | #elif __hpux |
491 | # elif __hpux |
460 | res = sendfile (ofd, ifd, offset, count, 0, 0); |
492 | res = sendfile (ofd, ifd, offset, count, 0, 0); |
461 | |
493 | |
|
|
494 | # elif __solaris |
|
|
495 | { |
|
|
496 | struct sendfilevec vec; |
|
|
497 | size_t sbytes; |
|
|
498 | |
|
|
499 | vec.sfv_fd = ifd; |
|
|
500 | vec.sfv_flag = 0; |
|
|
501 | vec.sfv_off = offset; |
|
|
502 | vec.sfv_len = count; |
|
|
503 | |
|
|
504 | res = sendfilev (ofd, &vec, 1, &sbytes); |
|
|
505 | |
|
|
506 | if (res < 0 && sbytes) |
|
|
507 | res = sbytes; |
|
|
508 | } |
|
|
509 | |
|
|
510 | # endif |
462 | #else |
511 | #else |
463 | res = -1; |
512 | res = -1; |
464 | errno = ENOSYS; |
513 | errno = ENOSYS; |
465 | #endif |
514 | #endif |
466 | |
515 | |
|
|
516 | if (res < 0 |
467 | if (res < 0 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK)) |
517 | && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK |
|
|
518 | #if __solaris |
|
|
519 | || errno == EAFNOSUPPORT || errno == EPROTOTYPE |
|
|
520 | #endif |
|
|
521 | ) |
|
|
522 | ) |
468 | { |
523 | { |
469 | /* emulate sendfile. this is a major pain in the ass */ |
524 | /* emulate sendfile. this is a major pain in the ass */ |
470 | char *buf = malloc (4096); |
525 | char buf[4096]; |
471 | res = 0; |
526 | res = 0; |
472 | |
527 | |
473 | for (;;) |
528 | while (count) |
474 | { |
529 | { |
475 | ssize_t cnt; |
530 | ssize_t cnt; |
476 | |
531 | |
477 | cnt = pread (ifd, buf, 4096, offset); |
532 | cnt = pread (ifd, buf, count > 4096 ? 4096 : count, offset); |
478 | |
533 | |
479 | if (cnt <= 0) |
534 | if (cnt <= 0) |
480 | { |
535 | { |
481 | if (cnt && !res) res = -1; |
536 | if (cnt && !res) res = -1; |
482 | break; |
537 | break; |
483 | } |
538 | } |
484 | |
539 | |
485 | cnt = write (ofd, buf, cnt); |
540 | cnt = write (ofd, buf, cnt); |
486 | |
541 | |
487 | if (cnt <= 0) |
542 | if (cnt <= 0) |
488 | { |
543 | { |
489 | if (cnt && !res) res = -1; |
544 | if (cnt && !res) res = -1; |
490 | break; |
545 | break; |
491 | } |
546 | } |
492 | |
547 | |
493 | offset += cnt; |
548 | offset += cnt; |
494 | res += cnt; |
549 | res += cnt; |
|
|
550 | count -= cnt; |
495 | } |
551 | } |
496 | |
|
|
497 | { |
|
|
498 | int errorno = errno; |
|
|
499 | free (buf); |
|
|
500 | errno = errorno; |
|
|
501 | } |
|
|
502 | } |
552 | } |
503 | |
553 | |
504 | return res; |
554 | return res; |
|
|
555 | } |
|
|
556 | |
|
|
557 | /* read a full directory */ |
|
|
558 | static int |
|
|
559 | scandir_ (const char *path, void **namesp) |
|
|
560 | { |
|
|
561 | DIR *dirp = opendir (path); |
|
|
562 | union |
|
|
563 | { |
|
|
564 | struct dirent d; |
|
|
565 | char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; |
|
|
566 | } u; |
|
|
567 | struct dirent *entp; |
|
|
568 | char *name, *names; |
|
|
569 | int memlen = 4096; |
|
|
570 | int memofs = 0; |
|
|
571 | int res = 0; |
|
|
572 | int errorno; |
|
|
573 | |
|
|
574 | if (!dirp) |
|
|
575 | return -1; |
|
|
576 | |
|
|
577 | names = malloc (memlen); |
|
|
578 | |
|
|
579 | for (;;) |
|
|
580 | { |
|
|
581 | errno = 0, readdir_r (dirp, &u.d, &entp); |
|
|
582 | |
|
|
583 | if (!entp) |
|
|
584 | break; |
|
|
585 | |
|
|
586 | name = entp->d_name; |
|
|
587 | |
|
|
588 | if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) |
|
|
589 | { |
|
|
590 | int len = strlen (name) + 1; |
|
|
591 | |
|
|
592 | res++; |
|
|
593 | |
|
|
594 | while (memofs + len > memlen) |
|
|
595 | { |
|
|
596 | memlen *= 2; |
|
|
597 | names = realloc (names, memlen); |
|
|
598 | if (!names) |
|
|
599 | break; |
|
|
600 | } |
|
|
601 | |
|
|
602 | memcpy (names + memofs, name, len); |
|
|
603 | memofs += len; |
|
|
604 | } |
|
|
605 | } |
|
|
606 | |
|
|
607 | errorno = errno; |
|
|
608 | closedir (dirp); |
|
|
609 | |
|
|
610 | if (errorno) |
|
|
611 | { |
|
|
612 | free (names); |
|
|
613 | errno = errorno; |
|
|
614 | res = -1; |
|
|
615 | } |
|
|
616 | |
|
|
617 | *namesp = (void *)names; |
|
|
618 | return res; |
505 | } |
619 | } |
506 | |
620 | |
507 | /*****************************************************************************/ |
621 | /*****************************************************************************/ |
508 | |
622 | |
509 | static void * |
623 | static void * |
… | |
… | |
552 | |
666 | |
553 | case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
667 | case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
554 | case REQ_CLOSE: req->result = close (req->fd); break; |
668 | case REQ_CLOSE: req->result = close (req->fd); break; |
555 | case REQ_UNLINK: req->result = unlink (req->dataptr); break; |
669 | case REQ_UNLINK: req->result = unlink (req->dataptr); break; |
556 | case REQ_RMDIR: req->result = rmdir (req->dataptr); break; |
670 | case REQ_RMDIR: req->result = rmdir (req->dataptr); break; |
|
|
671 | case REQ_RENAME: req->result = rename (req->data2ptr, req->dataptr); break; |
|
|
672 | case REQ_LINK: req->result = link (req->data2ptr, req->dataptr); break; |
557 | case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; |
673 | case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; |
558 | |
674 | |
559 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
675 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
560 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
676 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
|
|
677 | case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break; |
561 | |
678 | |
562 | case REQ_QUIT: |
679 | case REQ_QUIT: |
563 | break; |
680 | break; |
564 | |
681 | |
565 | default: |
682 | default: |
… | |
… | |
589 | pthread_mutex_unlock (&reslock); |
706 | pthread_mutex_unlock (&reslock); |
590 | } |
707 | } |
591 | while (type != REQ_QUIT); |
708 | while (type != REQ_QUIT); |
592 | |
709 | |
593 | return 0; |
710 | return 0; |
|
|
711 | } |
|
|
712 | |
|
|
713 | /*****************************************************************************/ |
|
|
714 | |
|
|
715 | static void atfork_prepare (void) |
|
|
716 | { |
|
|
717 | pthread_mutex_lock (&reqlock); |
|
|
718 | pthread_mutex_lock (&reslock); |
|
|
719 | #if !HAVE_PREADWRITE |
|
|
720 | pthread_mutex_lock (&preadwritelock); |
|
|
721 | #endif |
|
|
722 | #if !HAVE_READDIR_R |
|
|
723 | pthread_mutex_lock (&readdirlock); |
|
|
724 | #endif |
|
|
725 | } |
|
|
726 | |
|
|
727 | static void atfork_parent (void) |
|
|
728 | { |
|
|
729 | #if !HAVE_READDIR_R |
|
|
730 | pthread_mutex_unlock (&readdirlock); |
|
|
731 | #endif |
|
|
732 | #if !HAVE_PREADWRITE |
|
|
733 | pthread_mutex_unlock (&preadwritelock); |
|
|
734 | #endif |
|
|
735 | pthread_mutex_unlock (&reslock); |
|
|
736 | pthread_mutex_unlock (&reqlock); |
|
|
737 | } |
|
|
738 | |
|
|
739 | static void atfork_child (void) |
|
|
740 | { |
|
|
741 | aio_req prv; |
|
|
742 | |
|
|
743 | started = 0; |
|
|
744 | |
|
|
745 | while (reqs) |
|
|
746 | { |
|
|
747 | prv = reqs; |
|
|
748 | reqs = prv->next; |
|
|
749 | free_req (prv); |
|
|
750 | } |
|
|
751 | |
|
|
752 | reqs = reqe = 0; |
|
|
753 | |
|
|
754 | while (ress) |
|
|
755 | { |
|
|
756 | prv = ress; |
|
|
757 | ress = prv->next; |
|
|
758 | free_req (prv); |
|
|
759 | } |
|
|
760 | |
|
|
761 | ress = rese = 0; |
|
|
762 | |
|
|
763 | close (respipe [0]); |
|
|
764 | close (respipe [1]); |
|
|
765 | create_pipe (); |
|
|
766 | |
|
|
767 | atfork_parent (); |
594 | } |
768 | } |
595 | |
769 | |
596 | #define dREQ \ |
770 | #define dREQ \ |
597 | aio_req req; \ |
771 | aio_req req; \ |
598 | \ |
772 | \ |
… | |
… | |
609 | |
783 | |
610 | PROTOTYPES: ENABLE |
784 | PROTOTYPES: ENABLE |
611 | |
785 | |
612 | BOOT: |
786 | BOOT: |
613 | { |
787 | { |
|
|
788 | HV *stash = gv_stashpv ("IO::AIO", 1); |
|
|
789 | newCONSTSUB (stash, "EXDEV", newSViv (EXDEV)); |
|
|
790 | newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY)); |
|
|
791 | newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY)); |
|
|
792 | |
614 | create_pipe (); |
793 | create_pipe (); |
615 | pthread_atfork (atfork_prepare, atfork_parent, atfork_child); |
794 | pthread_atfork (atfork_prepare, atfork_parent, atfork_child); |
616 | } |
795 | } |
617 | |
796 | |
618 | void |
797 | void |
619 | min_parallel(nthreads) |
798 | min_parallel (nthreads) |
620 | int nthreads |
799 | int nthreads |
621 | PROTOTYPE: $ |
800 | PROTOTYPE: $ |
622 | |
801 | |
623 | void |
802 | void |
624 | max_parallel(nthreads) |
803 | max_parallel (nthreads) |
625 | int nthreads |
804 | int nthreads |
626 | PROTOTYPE: $ |
805 | PROTOTYPE: $ |
627 | |
806 | |
628 | int |
807 | int |
629 | max_outstanding(nreqs) |
808 | max_outstanding (nreqs) |
630 | int nreqs |
809 | int nreqs |
631 | PROTOTYPE: $ |
810 | PROTOTYPE: $ |
632 | CODE: |
811 | CODE: |
633 | RETVAL = max_outstanding; |
812 | RETVAL = max_outstanding; |
634 | max_outstanding = nreqs; |
813 | max_outstanding = nreqs; |
635 | |
814 | |
636 | void |
815 | void |
637 | aio_open(pathname,flags,mode,callback=&PL_sv_undef) |
816 | aio_open (pathname,flags,mode,callback=&PL_sv_undef) |
638 | SV * pathname |
817 | SV * pathname |
639 | int flags |
818 | int flags |
640 | int mode |
819 | int mode |
641 | SV * callback |
820 | SV * callback |
642 | PROTOTYPE: $$$;$ |
821 | PROTOTYPE: $$$;$ |
… | |
… | |
652 | |
831 | |
653 | send_req (req); |
832 | send_req (req); |
654 | } |
833 | } |
655 | |
834 | |
656 | void |
835 | void |
657 | aio_close(fh,callback=&PL_sv_undef) |
836 | aio_close (fh,callback=&PL_sv_undef) |
658 | SV * fh |
837 | SV * fh |
659 | SV * callback |
838 | SV * callback |
660 | PROTOTYPE: $;$ |
839 | PROTOTYPE: $;$ |
661 | ALIAS: |
840 | ALIAS: |
662 | aio_close = REQ_CLOSE |
841 | aio_close = REQ_CLOSE |
… | |
… | |
672 | |
851 | |
673 | send_req (req); |
852 | send_req (req); |
674 | } |
853 | } |
675 | |
854 | |
676 | void |
855 | void |
677 | aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
856 | aio_read (fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
678 | SV * fh |
857 | SV * fh |
679 | UV offset |
858 | UV offset |
680 | UV length |
859 | UV length |
681 | SV * data |
860 | SV * data |
682 | UV dataoffset |
861 | UV dataoffset |
… | |
… | |
736 | send_req (req); |
915 | send_req (req); |
737 | } |
916 | } |
738 | } |
917 | } |
739 | |
918 | |
740 | void |
919 | void |
741 | aio_sendfile(out_fh,in_fh,in_offset,length,callback=&PL_sv_undef) |
920 | aio_sendfile (out_fh,in_fh,in_offset,length,callback=&PL_sv_undef) |
742 | SV * out_fh |
921 | SV * out_fh |
743 | SV * in_fh |
922 | SV * in_fh |
744 | UV in_offset |
923 | UV in_offset |
745 | UV length |
924 | UV length |
746 | SV * callback |
925 | SV * callback |
… | |
… | |
759 | |
938 | |
760 | send_req (req); |
939 | send_req (req); |
761 | } |
940 | } |
762 | |
941 | |
763 | void |
942 | void |
764 | aio_readahead(fh,offset,length,callback=&PL_sv_undef) |
943 | aio_readahead (fh,offset,length,callback=&PL_sv_undef) |
765 | SV * fh |
944 | SV * fh |
766 | UV offset |
945 | UV offset |
767 | IV length |
946 | IV length |
768 | SV * callback |
947 | SV * callback |
769 | PROTOTYPE: $$$;$ |
948 | PROTOTYPE: $$$;$ |
… | |
… | |
779 | |
958 | |
780 | send_req (req); |
959 | send_req (req); |
781 | } |
960 | } |
782 | |
961 | |
783 | void |
962 | void |
784 | aio_stat(fh_or_path,callback=&PL_sv_undef) |
963 | aio_stat (fh_or_path,callback=&PL_sv_undef) |
785 | SV * fh_or_path |
964 | SV * fh_or_path |
786 | SV * callback |
965 | SV * callback |
787 | ALIAS: |
966 | ALIAS: |
788 | aio_stat = REQ_STAT |
967 | aio_stat = REQ_STAT |
789 | aio_lstat = REQ_LSTAT |
968 | aio_lstat = REQ_LSTAT |
… | |
… | |
813 | |
992 | |
814 | send_req (req); |
993 | send_req (req); |
815 | } |
994 | } |
816 | |
995 | |
817 | void |
996 | void |
818 | aio_unlink(pathname,callback=&PL_sv_undef) |
997 | aio_unlink (pathname,callback=&PL_sv_undef) |
819 | SV * pathname |
998 | SV * pathname |
820 | SV * callback |
999 | SV * callback |
821 | ALIAS: |
1000 | ALIAS: |
822 | aio_unlink = REQ_UNLINK |
1001 | aio_unlink = REQ_UNLINK |
823 | aio_rmdir = REQ_RMDIR |
1002 | aio_rmdir = REQ_RMDIR |
|
|
1003 | aio_readdir = REQ_READDIR |
824 | CODE: |
1004 | CODE: |
825 | { |
1005 | { |
826 | dREQ; |
1006 | dREQ; |
827 | |
1007 | |
828 | req->type = ix; |
1008 | req->type = ix; |
… | |
… | |
831 | |
1011 | |
832 | send_req (req); |
1012 | send_req (req); |
833 | } |
1013 | } |
834 | |
1014 | |
835 | void |
1015 | void |
836 | aio_symlink(oldpath,newpath,callback=&PL_sv_undef) |
1016 | aio_link (oldpath,newpath,callback=&PL_sv_undef) |
837 | SV * oldpath |
1017 | SV * oldpath |
838 | SV * newpath |
1018 | SV * newpath |
839 | SV * callback |
1019 | SV * callback |
|
|
1020 | ALIAS: |
|
|
1021 | aio_link = REQ_LINK |
|
|
1022 | aio_symlink = REQ_SYMLINK |
|
|
1023 | aio_rename = REQ_RENAME |
840 | CODE: |
1024 | CODE: |
841 | { |
1025 | { |
842 | dREQ; |
1026 | dREQ; |
843 | |
1027 | |
844 | req->type = REQ_SYMLINK; |
1028 | req->type = ix; |
845 | req->fh = newSVsv (oldpath); |
1029 | req->fh = newSVsv (oldpath); |
846 | req->data2ptr = SvPVbyte_nolen (req->fh); |
1030 | req->data2ptr = SvPVbyte_nolen (req->fh); |
847 | req->data = newSVsv (newpath); |
1031 | req->data = newSVsv (newpath); |
848 | req->dataptr = SvPVbyte_nolen (req->data); |
1032 | req->dataptr = SvPVbyte_nolen (req->data); |
849 | |
1033 | |
850 | send_req (req); |
1034 | send_req (req); |
851 | } |
1035 | } |
852 | |
1036 | |
|
|
1037 | #if 0 |
|
|
1038 | |
|
|
1039 | # undocumented, because it does not cancel active requests |
853 | void |
1040 | void |
|
|
1041 | cancel_most_requests () |
|
|
1042 | PROTOTYPE: |
|
|
1043 | CODE: |
|
|
1044 | { |
|
|
1045 | aio_req *req; |
|
|
1046 | |
|
|
1047 | pthread_mutex_lock (&reqlock); |
|
|
1048 | for (req = reqs; req; req = req->next) |
|
|
1049 | req->flags |= 1; |
|
|
1050 | pthread_mutex_unlock (&reqlock); |
|
|
1051 | |
|
|
1052 | pthread_mutex_lock (&reslock); |
|
|
1053 | for (req = ress; req; req = req->next) |
|
|
1054 | req->flags |= 1; |
|
|
1055 | pthread_mutex_unlock (&reslock); |
|
|
1056 | } |
|
|
1057 | |
|
|
1058 | #endif |
|
|
1059 | |
|
|
1060 | void |
854 | flush() |
1061 | flush () |
855 | PROTOTYPE: |
1062 | PROTOTYPE: |
856 | CODE: |
1063 | CODE: |
857 | while (nreqs) |
1064 | while (nreqs) |
858 | { |
1065 | { |
859 | poll_wait (); |
1066 | poll_wait (); |