… | |
… | |
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 | |
… | |
… | |
30 | # else |
32 | # else |
31 | # error sendfile support requested but not available |
33 | # error sendfile support requested but not available |
32 | # endif |
34 | # endif |
33 | #endif |
35 | #endif |
34 | |
36 | |
|
|
37 | /* used for struct dirent, AIX doesn't provide it */ |
|
|
38 | #ifndef NAME_MAX |
|
|
39 | # define NAME_MAX 4096 |
|
|
40 | #endif |
|
|
41 | |
35 | #if __ia64 |
42 | #if __ia64 |
36 | # define STACKSIZE 65536 |
43 | # define STACKSIZE 65536 |
37 | #else |
44 | #else |
38 | # define STACKSIZE 4096 |
45 | # define STACKSIZE 8192 |
39 | #endif |
46 | #endif |
40 | |
47 | |
41 | enum { |
48 | enum { |
42 | REQ_QUIT, |
49 | REQ_QUIT, |
43 | REQ_OPEN, REQ_CLOSE, |
50 | REQ_OPEN, REQ_CLOSE, |
44 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
51 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
45 | REQ_SENDFILE, |
52 | REQ_SENDFILE, |
46 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
53 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
47 | REQ_FSYNC, REQ_FDATASYNC, |
54 | REQ_FSYNC, REQ_FDATASYNC, |
48 | REQ_UNLINK, REQ_RMDIR, |
55 | REQ_UNLINK, REQ_RMDIR, REQ_RENAME, |
49 | REQ_SYMLINK, |
56 | REQ_READDIR, |
|
|
57 | REQ_LINK, REQ_SYMLINK, |
50 | }; |
58 | }; |
51 | |
59 | |
52 | typedef struct aio_cb { |
60 | typedef struct aio_cb { |
53 | struct aio_cb *volatile next; |
61 | struct aio_cb *volatile next; |
54 | |
62 | |
55 | int type; |
63 | int type; |
56 | |
64 | |
|
|
65 | /* should receive a cleanup, with unions */ |
57 | int fd, fd2; |
66 | int fd, fd2; |
58 | off_t offset; |
67 | off_t offset; |
59 | size_t length; |
68 | size_t length; |
60 | ssize_t result; |
69 | ssize_t result; |
61 | mode_t mode; /* open */ |
70 | mode_t mode; /* open */ |
… | |
… | |
97 | Safefree (req->statdata); |
106 | Safefree (req->statdata); |
98 | |
107 | |
99 | if (req->callback) |
108 | if (req->callback) |
100 | SvREFCNT_dec (req->callback); |
109 | SvREFCNT_dec (req->callback); |
101 | |
110 | |
|
|
111 | if (req->type == REQ_READDIR && req->result >= 0) |
|
|
112 | free (req->data2ptr); |
|
|
113 | |
102 | Safefree (req); |
114 | Safefree (req); |
103 | } |
115 | } |
104 | |
116 | |
105 | static void |
117 | static void |
106 | poll_wait () |
118 | poll_wait () |
… | |
… | |
170 | PL_statcache = *(req->statdata); |
182 | PL_statcache = *(req->statdata); |
171 | } |
183 | } |
172 | |
184 | |
173 | ENTER; |
185 | ENTER; |
174 | PUSHMARK (SP); |
186 | PUSHMARK (SP); |
175 | XPUSHs (sv_2mortal (newSViv (req->result))); |
|
|
176 | |
187 | |
177 | if (req->type == REQ_OPEN) |
188 | if (req->type == REQ_READDIR) |
178 | { |
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 | { |
179 | /* convert fd to fh */ |
217 | /* convert fd to fh */ |
180 | SV *fh; |
218 | SV *fh; |
181 | |
219 | |
182 | PUTBACK; |
220 | PUTBACK; |
183 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
221 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
184 | SPAGAIN; |
222 | SPAGAIN; |
185 | |
223 | |
186 | fh = SvREFCNT_inc (POPs); |
224 | fh = SvREFCNT_inc (POPs); |
187 | |
225 | |
188 | PUSHMARK (SP); |
226 | PUSHMARK (SP); |
189 | XPUSHs (sv_2mortal (fh)); |
227 | XPUSHs (sv_2mortal (fh)); |
|
|
228 | } |
190 | } |
229 | } |
191 | |
230 | |
192 | if (SvOK (req->callback)) |
231 | if (SvOK (req->callback)) |
193 | { |
232 | { |
194 | PUTBACK; |
233 | PUTBACK; |
… | |
… | |
317 | |
356 | |
318 | if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) |
357 | if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) |
319 | croak ("cannot set result pipe to nonblocking mode"); |
358 | croak ("cannot set result pipe to nonblocking mode"); |
320 | } |
359 | } |
321 | |
360 | |
322 | static void atfork_prepare (void) |
|
|
323 | { |
|
|
324 | pthread_mutex_lock (&reqlock); |
|
|
325 | pthread_mutex_lock (&reslock); |
|
|
326 | } |
|
|
327 | |
|
|
328 | static void atfork_parent (void) |
|
|
329 | { |
|
|
330 | pthread_mutex_unlock (&reslock); |
|
|
331 | pthread_mutex_unlock (&reqlock); |
|
|
332 | } |
|
|
333 | |
|
|
334 | static void atfork_child (void) |
|
|
335 | { |
|
|
336 | aio_req prv; |
|
|
337 | |
|
|
338 | started = 0; |
|
|
339 | |
|
|
340 | while (reqs) |
|
|
341 | { |
|
|
342 | prv = reqs; |
|
|
343 | reqs = prv->next; |
|
|
344 | free_req (prv); |
|
|
345 | } |
|
|
346 | |
|
|
347 | reqs = reqe = 0; |
|
|
348 | |
|
|
349 | while (ress) |
|
|
350 | { |
|
|
351 | prv = ress; |
|
|
352 | ress = prv->next; |
|
|
353 | free_req (prv); |
|
|
354 | } |
|
|
355 | |
|
|
356 | ress = rese = 0; |
|
|
357 | |
|
|
358 | close (respipe [0]); |
|
|
359 | close (respipe [1]); |
|
|
360 | create_pipe (); |
|
|
361 | |
|
|
362 | atfork_parent (); |
|
|
363 | } |
|
|
364 | |
|
|
365 | /*****************************************************************************/ |
361 | /*****************************************************************************/ |
366 | /* work around various missing functions */ |
362 | /* work around various missing functions */ |
367 | |
363 | |
368 | #if !HAVE_PREADWRITE |
364 | #if !HAVE_PREADWRITE |
369 | # define pread aio_pread |
365 | # define pread aio_pread |
… | |
… | |
372 | /* |
368 | /* |
373 | * make our pread/pwrite safe against themselves, but not against |
369 | * make our pread/pwrite safe against themselves, but not against |
374 | * 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, |
375 | * but that's your problem, not mine. |
371 | * but that's your problem, not mine. |
376 | */ |
372 | */ |
377 | static pthread_mutex_t iolock = PTHREAD_MUTEX_INITIALIZER; |
373 | static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER; |
378 | |
374 | |
379 | static ssize_t |
375 | static ssize_t |
380 | pread (int fd, void *buf, size_t count, off_t offset) |
376 | pread (int fd, void *buf, size_t count, off_t offset) |
381 | { |
377 | { |
382 | ssize_t res; |
378 | ssize_t res; |
383 | off_t ooffset; |
379 | off_t ooffset; |
384 | |
380 | |
385 | pthread_mutex_lock (&iolock); |
381 | pthread_mutex_lock (&preadwritelock); |
386 | ooffset = lseek (fd, 0, SEEK_CUR); |
382 | ooffset = lseek (fd, 0, SEEK_CUR); |
387 | lseek (fd, offset, SEEK_SET); |
383 | lseek (fd, offset, SEEK_SET); |
388 | res = read (fd, buf, count); |
384 | res = read (fd, buf, count); |
389 | lseek (fd, ooffset, SEEK_SET); |
385 | lseek (fd, ooffset, SEEK_SET); |
390 | pthread_mutex_unlock (&iolock); |
386 | pthread_mutex_unlock (&preadwritelock); |
391 | |
387 | |
392 | return res; |
388 | return res; |
393 | } |
389 | } |
394 | |
390 | |
395 | static ssize_t |
391 | static ssize_t |
396 | pwrite (int fd, void *buf, size_t count, off_t offset) |
392 | pwrite (int fd, void *buf, size_t count, off_t offset) |
397 | { |
393 | { |
398 | ssize_t res; |
394 | ssize_t res; |
399 | off_t ooffset; |
395 | off_t ooffset; |
400 | |
396 | |
401 | pthread_mutex_lock (&iolock); |
397 | pthread_mutex_lock (&preadwritelock); |
402 | ooffset = lseek (fd, 0, SEEK_CUR); |
398 | ooffset = lseek (fd, 0, SEEK_CUR); |
403 | lseek (fd, offset, SEEK_SET); |
399 | lseek (fd, offset, SEEK_SET); |
404 | res = write (fd, buf, count); |
400 | res = write (fd, buf, count); |
405 | lseek (fd, offset, SEEK_SET); |
401 | lseek (fd, offset, SEEK_SET); |
406 | pthread_mutex_unlock (&iolock); |
402 | pthread_mutex_unlock (&preadwritelock); |
407 | |
403 | |
408 | return res; |
404 | return res; |
409 | } |
405 | } |
410 | #endif |
406 | #endif |
411 | |
407 | |
… | |
… | |
414 | #endif |
410 | #endif |
415 | |
411 | |
416 | #if !HAVE_READAHEAD |
412 | #if !HAVE_READAHEAD |
417 | # define readahead aio_readahead |
413 | # define readahead aio_readahead |
418 | |
414 | |
419 | static char readahead_buf[4096]; |
|
|
420 | |
|
|
421 | static ssize_t |
415 | static ssize_t |
422 | readahead (int fd, off_t offset, size_t count) |
416 | readahead (int fd, off_t offset, size_t count) |
423 | { |
417 | { |
|
|
418 | char readahead_buf[4096]; |
|
|
419 | |
424 | while (count > 0) |
420 | while (count > 0) |
425 | { |
421 | { |
426 | size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); |
422 | size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); |
427 | |
423 | |
428 | pread (fd, readahead_buf, len, offset); |
424 | pread (fd, readahead_buf, len, offset); |
… | |
… | |
432 | |
428 | |
433 | errno = 0; |
429 | errno = 0; |
434 | } |
430 | } |
435 | #endif |
431 | #endif |
436 | |
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 | |
437 | /* sendfile always needs emulation */ |
464 | /* sendfile always needs emulation */ |
438 | static ssize_t |
465 | static ssize_t |
439 | sendfile_ (int ofd, int ifd, off_t offset, size_t count) |
466 | sendfile_ (int ofd, int ifd, off_t offset, size_t count) |
440 | { |
467 | { |
441 | ssize_t res; |
468 | ssize_t res; |
442 | |
469 | |
443 | if (!count) |
470 | if (!count) |
444 | return 0; |
471 | return 0; |
445 | |
472 | |
446 | #if HAVE_SENDFILE |
473 | #if HAVE_SENDFILE |
447 | # if __linux |
474 | # if __linux |
448 | res = sendfile (ofd, ifd, &offset, count); |
475 | res = sendfile (ofd, ifd, &offset, count); |
449 | |
476 | |
450 | # elif __freebsd |
477 | # elif __freebsd |
451 | /* |
478 | /* |
452 | * 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 |
453 | * wasted on making it similar to other I/O functions. |
480 | * wasted on making it similar to other I/O functions. |
454 | */ |
481 | */ |
455 | { |
482 | { |
456 | off_t sbytes; |
483 | off_t sbytes; |
457 | res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); |
484 | res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); |
458 | |
485 | |
459 | if (res < 0 && sbytes) |
486 | if (res < 0 && sbytes) |
460 | /* maybe only on EAGAIN only: as usual, the manpage leaves you guessing */ |
487 | /* maybe only on EAGAIN only: as usual, the manpage leaves you guessing */ |
461 | res = sbytes; |
488 | res = sbytes; |
462 | } |
489 | } |
463 | |
490 | |
464 | # elif __hpux |
491 | # elif __hpux |
465 | res = sendfile (ofd, ifd, offset, count, 0, 0); |
492 | res = sendfile (ofd, ifd, offset, count, 0, 0); |
466 | |
493 | |
467 | # elif __solaris |
494 | # elif __solaris |
468 | { |
495 | { |
469 | struct sendfilevec vec; |
496 | struct sendfilevec vec; |
470 | size_t sbytes; |
497 | size_t sbytes; |
471 | |
498 | |
472 | vec.sfv_fd = ifd; |
499 | vec.sfv_fd = ifd; |
473 | vec.sfv_flag = 0; |
500 | vec.sfv_flag = 0; |
474 | vec.sfv_off = offset; |
501 | vec.sfv_off = offset; |
475 | vec.sfv_len = count; |
502 | vec.sfv_len = count; |
476 | |
503 | |
477 | res = sendfilev (ofd, &vec, 1, &sbytes); |
504 | res = sendfilev (ofd, &vec, 1, &sbytes); |
478 | |
505 | |
479 | if (res < 0 && sbytes) |
506 | if (res < 0 && sbytes) |
480 | res = sbytes; |
507 | res = sbytes; |
481 | } |
508 | } |
482 | |
509 | |
483 | # else |
|
|
484 | res = -1; |
|
|
485 | errno = ENOSYS; |
|
|
486 | # endif |
510 | # endif |
|
|
511 | #else |
|
|
512 | res = -1; |
|
|
513 | errno = ENOSYS; |
487 | #endif |
514 | #endif |
488 | |
515 | |
489 | if (res < 0 |
516 | if (res < 0 |
490 | && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK |
517 | && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK |
491 | #if __solaris |
518 | #if __solaris |
492 | || errno == EAFNOSUPPORT || errno == EPROTOTYPE |
519 | || errno == EAFNOSUPPORT || errno == EPROTOTYPE |
493 | #endif |
520 | #endif |
494 | ) |
521 | ) |
495 | ) |
522 | ) |
496 | { |
523 | { |
497 | /* emulate sendfile. this is a major pain in the ass */ |
524 | /* emulate sendfile. this is a major pain in the ass */ |
498 | char *buf = malloc (4096); |
525 | char buf[4096]; |
499 | res = 0; |
526 | res = 0; |
500 | |
527 | |
501 | for (;;) |
528 | while (count) |
502 | { |
529 | { |
503 | ssize_t cnt; |
530 | ssize_t cnt; |
504 | |
531 | |
505 | cnt = pread (ifd, buf, 4096, offset); |
532 | cnt = pread (ifd, buf, count > 4096 ? 4096 : count, offset); |
506 | |
533 | |
507 | if (cnt <= 0) |
534 | if (cnt <= 0) |
508 | { |
535 | { |
509 | if (cnt && !res) res = -1; |
536 | if (cnt && !res) res = -1; |
510 | break; |
537 | break; |
511 | } |
538 | } |
512 | |
539 | |
513 | cnt = write (ofd, buf, cnt); |
540 | cnt = write (ofd, buf, cnt); |
514 | |
541 | |
515 | if (cnt <= 0) |
542 | if (cnt <= 0) |
516 | { |
543 | { |
517 | if (cnt && !res) res = -1; |
544 | if (cnt && !res) res = -1; |
518 | break; |
545 | break; |
519 | } |
546 | } |
520 | |
547 | |
521 | offset += cnt; |
548 | offset += cnt; |
522 | res += cnt; |
549 | res += cnt; |
|
|
550 | count -= cnt; |
523 | } |
551 | } |
524 | |
|
|
525 | { |
|
|
526 | int errorno = errno; |
|
|
527 | free (buf); |
|
|
528 | errno = errorno; |
|
|
529 | } |
|
|
530 | } |
552 | } |
531 | |
553 | |
532 | 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; |
533 | } |
619 | } |
534 | |
620 | |
535 | /*****************************************************************************/ |
621 | /*****************************************************************************/ |
536 | |
622 | |
537 | static void * |
623 | static void * |
… | |
… | |
580 | |
666 | |
581 | 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; |
582 | case REQ_CLOSE: req->result = close (req->fd); break; |
668 | case REQ_CLOSE: req->result = close (req->fd); break; |
583 | case REQ_UNLINK: req->result = unlink (req->dataptr); break; |
669 | case REQ_UNLINK: req->result = unlink (req->dataptr); break; |
584 | 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; |
585 | case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; |
673 | case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; |
586 | |
674 | |
587 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
675 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
588 | 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; |
589 | |
678 | |
590 | case REQ_QUIT: |
679 | case REQ_QUIT: |
591 | break; |
680 | break; |
592 | |
681 | |
593 | default: |
682 | default: |
… | |
… | |
617 | pthread_mutex_unlock (&reslock); |
706 | pthread_mutex_unlock (&reslock); |
618 | } |
707 | } |
619 | while (type != REQ_QUIT); |
708 | while (type != REQ_QUIT); |
620 | |
709 | |
621 | 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 (); |
622 | } |
768 | } |
623 | |
769 | |
624 | #define dREQ \ |
770 | #define dREQ \ |
625 | aio_req req; \ |
771 | aio_req req; \ |
626 | \ |
772 | \ |
… | |
… | |
637 | |
783 | |
638 | PROTOTYPES: ENABLE |
784 | PROTOTYPES: ENABLE |
639 | |
785 | |
640 | BOOT: |
786 | BOOT: |
641 | { |
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 | |
642 | create_pipe (); |
793 | create_pipe (); |
643 | pthread_atfork (atfork_prepare, atfork_parent, atfork_child); |
794 | pthread_atfork (atfork_prepare, atfork_parent, atfork_child); |
644 | } |
795 | } |
645 | |
796 | |
646 | void |
797 | void |
647 | min_parallel(nthreads) |
798 | min_parallel (nthreads) |
648 | int nthreads |
799 | int nthreads |
649 | PROTOTYPE: $ |
800 | PROTOTYPE: $ |
650 | |
801 | |
651 | void |
802 | void |
652 | max_parallel(nthreads) |
803 | max_parallel (nthreads) |
653 | int nthreads |
804 | int nthreads |
654 | PROTOTYPE: $ |
805 | PROTOTYPE: $ |
655 | |
806 | |
656 | int |
807 | int |
657 | max_outstanding(nreqs) |
808 | max_outstanding (nreqs) |
658 | int nreqs |
809 | int nreqs |
659 | PROTOTYPE: $ |
810 | PROTOTYPE: $ |
660 | CODE: |
811 | CODE: |
661 | RETVAL = max_outstanding; |
812 | RETVAL = max_outstanding; |
662 | max_outstanding = nreqs; |
813 | max_outstanding = nreqs; |
663 | |
814 | |
664 | void |
815 | void |
665 | aio_open(pathname,flags,mode,callback=&PL_sv_undef) |
816 | aio_open (pathname,flags,mode,callback=&PL_sv_undef) |
666 | SV * pathname |
817 | SV * pathname |
667 | int flags |
818 | int flags |
668 | int mode |
819 | int mode |
669 | SV * callback |
820 | SV * callback |
670 | PROTOTYPE: $$$;$ |
821 | PROTOTYPE: $$$;$ |
… | |
… | |
680 | |
831 | |
681 | send_req (req); |
832 | send_req (req); |
682 | } |
833 | } |
683 | |
834 | |
684 | void |
835 | void |
685 | aio_close(fh,callback=&PL_sv_undef) |
836 | aio_close (fh,callback=&PL_sv_undef) |
686 | SV * fh |
837 | SV * fh |
687 | SV * callback |
838 | SV * callback |
688 | PROTOTYPE: $;$ |
839 | PROTOTYPE: $;$ |
689 | ALIAS: |
840 | ALIAS: |
690 | aio_close = REQ_CLOSE |
841 | aio_close = REQ_CLOSE |
… | |
… | |
700 | |
851 | |
701 | send_req (req); |
852 | send_req (req); |
702 | } |
853 | } |
703 | |
854 | |
704 | void |
855 | void |
705 | aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
856 | aio_read (fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
706 | SV * fh |
857 | SV * fh |
707 | UV offset |
858 | UV offset |
708 | UV length |
859 | UV length |
709 | SV * data |
860 | SV * data |
710 | UV dataoffset |
861 | UV dataoffset |
… | |
… | |
764 | send_req (req); |
915 | send_req (req); |
765 | } |
916 | } |
766 | } |
917 | } |
767 | |
918 | |
768 | void |
919 | void |
769 | 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) |
770 | SV * out_fh |
921 | SV * out_fh |
771 | SV * in_fh |
922 | SV * in_fh |
772 | UV in_offset |
923 | UV in_offset |
773 | UV length |
924 | UV length |
774 | SV * callback |
925 | SV * callback |
… | |
… | |
787 | |
938 | |
788 | send_req (req); |
939 | send_req (req); |
789 | } |
940 | } |
790 | |
941 | |
791 | void |
942 | void |
792 | aio_readahead(fh,offset,length,callback=&PL_sv_undef) |
943 | aio_readahead (fh,offset,length,callback=&PL_sv_undef) |
793 | SV * fh |
944 | SV * fh |
794 | UV offset |
945 | UV offset |
795 | IV length |
946 | IV length |
796 | SV * callback |
947 | SV * callback |
797 | PROTOTYPE: $$$;$ |
948 | PROTOTYPE: $$$;$ |
… | |
… | |
807 | |
958 | |
808 | send_req (req); |
959 | send_req (req); |
809 | } |
960 | } |
810 | |
961 | |
811 | void |
962 | void |
812 | aio_stat(fh_or_path,callback=&PL_sv_undef) |
963 | aio_stat (fh_or_path,callback=&PL_sv_undef) |
813 | SV * fh_or_path |
964 | SV * fh_or_path |
814 | SV * callback |
965 | SV * callback |
815 | ALIAS: |
966 | ALIAS: |
816 | aio_stat = REQ_STAT |
967 | aio_stat = REQ_STAT |
817 | aio_lstat = REQ_LSTAT |
968 | aio_lstat = REQ_LSTAT |
… | |
… | |
841 | |
992 | |
842 | send_req (req); |
993 | send_req (req); |
843 | } |
994 | } |
844 | |
995 | |
845 | void |
996 | void |
846 | aio_unlink(pathname,callback=&PL_sv_undef) |
997 | aio_unlink (pathname,callback=&PL_sv_undef) |
847 | SV * pathname |
998 | SV * pathname |
848 | SV * callback |
999 | SV * callback |
849 | ALIAS: |
1000 | ALIAS: |
850 | aio_unlink = REQ_UNLINK |
1001 | aio_unlink = REQ_UNLINK |
851 | aio_rmdir = REQ_RMDIR |
1002 | aio_rmdir = REQ_RMDIR |
|
|
1003 | aio_readdir = REQ_READDIR |
852 | CODE: |
1004 | CODE: |
853 | { |
1005 | { |
854 | dREQ; |
1006 | dREQ; |
855 | |
1007 | |
856 | req->type = ix; |
1008 | req->type = ix; |
… | |
… | |
859 | |
1011 | |
860 | send_req (req); |
1012 | send_req (req); |
861 | } |
1013 | } |
862 | |
1014 | |
863 | void |
1015 | void |
864 | aio_symlink(oldpath,newpath,callback=&PL_sv_undef) |
1016 | aio_link (oldpath,newpath,callback=&PL_sv_undef) |
865 | SV * oldpath |
1017 | SV * oldpath |
866 | SV * newpath |
1018 | SV * newpath |
867 | SV * callback |
1019 | SV * callback |
|
|
1020 | ALIAS: |
|
|
1021 | aio_link = REQ_LINK |
|
|
1022 | aio_symlink = REQ_SYMLINK |
|
|
1023 | aio_rename = REQ_RENAME |
868 | CODE: |
1024 | CODE: |
869 | { |
1025 | { |
870 | dREQ; |
1026 | dREQ; |
871 | |
1027 | |
872 | req->type = REQ_SYMLINK; |
1028 | req->type = ix; |
873 | req->fh = newSVsv (oldpath); |
1029 | req->fh = newSVsv (oldpath); |
874 | req->data2ptr = SvPVbyte_nolen (req->fh); |
1030 | req->data2ptr = SvPVbyte_nolen (req->fh); |
875 | req->data = newSVsv (newpath); |
1031 | req->data = newSVsv (newpath); |
876 | req->dataptr = SvPVbyte_nolen (req->data); |
1032 | req->dataptr = SvPVbyte_nolen (req->data); |
877 | |
1033 | |
878 | send_req (req); |
1034 | send_req (req); |
879 | } |
1035 | } |
880 | |
1036 | |
881 | void |
1037 | void |
882 | flush() |
1038 | flush () |
883 | PROTOTYPE: |
1039 | PROTOTYPE: |
884 | CODE: |
1040 | CODE: |
885 | while (nreqs) |
1041 | while (nreqs) |
886 | { |
1042 | { |
887 | poll_wait (); |
1043 | poll_wait (); |