1 | /* solaris */ |
1 | /* solaris */ |
2 | #define _POSIX_PTHREAD_SEMANTICS 1 |
2 | #define _POSIX_PTHREAD_SEMANTICS 1 |
3 | |
3 | |
4 | #if __linux |
4 | #if __linux && !defined(_GNU_SOURCE) |
5 | # define _GNU_SOURCE |
5 | # define _GNU_SOURCE |
6 | #endif |
6 | #endif |
7 | |
7 | |
|
|
8 | /* just in case */ |
8 | #define _REENTRANT 1 |
9 | #define _REENTRANT 1 |
9 | |
10 | |
10 | #include <errno.h> |
11 | #include <errno.h> |
11 | |
12 | |
12 | #include "EXTERN.h" |
13 | #include "EXTERN.h" |
… | |
… | |
47 | /* used for struct dirent, AIX doesn't provide it */ |
48 | /* used for struct dirent, AIX doesn't provide it */ |
48 | #ifndef NAME_MAX |
49 | #ifndef NAME_MAX |
49 | # define NAME_MAX 4096 |
50 | # define NAME_MAX 4096 |
50 | #endif |
51 | #endif |
51 | |
52 | |
|
|
53 | #ifndef PTHREAD_STACK_MIN |
|
|
54 | /* care for broken platforms, e.g. windows */ |
|
|
55 | # define PTHREAD_STACK_MIN 16384 |
|
|
56 | #endif |
|
|
57 | |
52 | #if __ia64 |
58 | #if __ia64 |
53 | # define STACKSIZE 65536 |
59 | # define STACKSIZE 65536 |
54 | #elif __i386 || __x86_64 /* 16k is unreasonably high :( */ |
60 | #elif __i386 || __x86_64 /* 16k is unreasonably high :( */ |
55 | # define STACKSIZE PTHREAD_STACK_MIN |
61 | # define STACKSIZE PTHREAD_STACK_MIN |
56 | #else |
62 | #else |
… | |
… | |
59 | |
65 | |
60 | /* buffer size for various temporary buffers */ |
66 | /* buffer size for various temporary buffers */ |
61 | #define AIO_BUFSIZE 65536 |
67 | #define AIO_BUFSIZE 65536 |
62 | |
68 | |
63 | #define dBUF \ |
69 | #define dBUF \ |
|
|
70 | char *aio_buf; \ |
|
|
71 | LOCK (wrklock); \ |
64 | char *aio_buf = malloc (AIO_BUFSIZE); \ |
72 | self->dbuf = aio_buf = malloc (AIO_BUFSIZE); \ |
|
|
73 | UNLOCK (wrklock); \ |
65 | if (!aio_buf) \ |
74 | if (!aio_buf) \ |
66 | return -1; |
75 | return -1; |
67 | |
|
|
68 | #define fBUF free (aio_buf) |
|
|
69 | |
76 | |
70 | enum { |
77 | enum { |
71 | REQ_QUIT, |
78 | REQ_QUIT, |
72 | REQ_OPEN, REQ_CLOSE, |
79 | REQ_OPEN, REQ_CLOSE, |
73 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
80 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
… | |
… | |
125 | NUM_PRI = PRI_MAX + PRI_BIAS + 1, |
132 | NUM_PRI = PRI_MAX + PRI_BIAS + 1, |
126 | }; |
133 | }; |
127 | |
134 | |
128 | static int next_pri = DEFAULT_PRI + PRI_BIAS; |
135 | static int next_pri = DEFAULT_PRI + PRI_BIAS; |
129 | |
136 | |
130 | static int started, wanted; |
137 | static unsigned int started, wanted; |
131 | static volatile int nreqs; |
138 | static volatile unsigned int nreqs; |
132 | static int max_outstanding = 1<<30; |
139 | static volatile unsigned int max_outstanding = 0xffffffff; |
133 | static int respipe [2]; |
140 | static int respipe [2]; |
134 | |
141 | |
135 | #if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) |
142 | #if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) |
136 | # define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP |
143 | # define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP |
137 | #else |
144 | #else |
138 | # define AIO_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER |
145 | # define AIO_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER |
139 | #endif |
146 | #endif |
|
|
147 | |
|
|
148 | #define LOCK(mutex) pthread_mutex_lock (&(mutex)) |
|
|
149 | #define UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) |
|
|
150 | |
|
|
151 | /* worker threads management */ |
|
|
152 | static pthread_mutex_t wrklock = AIO_MUTEX_INIT; |
|
|
153 | |
|
|
154 | typedef struct worker { |
|
|
155 | /* locked by wrklock */ |
|
|
156 | struct worker *prev, *next; |
|
|
157 | |
|
|
158 | pthread_t tid; |
|
|
159 | |
|
|
160 | /* locked by reslock, reqlock or wrklock */ |
|
|
161 | aio_req req; /* currently processed request */ |
|
|
162 | void *dbuf; |
|
|
163 | DIR *dirp; |
|
|
164 | } worker; |
|
|
165 | |
|
|
166 | static worker wrk_first = { &wrk_first, &wrk_first, 0 }; |
|
|
167 | |
|
|
168 | static void worker_clear (worker *wrk) |
|
|
169 | { |
|
|
170 | if (wrk->dirp) |
|
|
171 | { |
|
|
172 | closedir (wrk->dirp); |
|
|
173 | wrk->dirp = 0; |
|
|
174 | } |
|
|
175 | |
|
|
176 | if (wrk->dbuf) |
|
|
177 | { |
|
|
178 | free (wrk->dbuf); |
|
|
179 | wrk->dbuf = 0; |
|
|
180 | } |
|
|
181 | } |
|
|
182 | |
|
|
183 | static void worker_free (worker *wrk) |
|
|
184 | { |
|
|
185 | wrk->next->prev = wrk->prev; |
|
|
186 | wrk->prev->next = wrk->next; |
|
|
187 | |
|
|
188 | free (wrk); |
|
|
189 | } |
140 | |
190 | |
141 | static pthread_mutex_t reslock = AIO_MUTEX_INIT; |
191 | static pthread_mutex_t reslock = AIO_MUTEX_INIT; |
142 | static pthread_mutex_t reqlock = AIO_MUTEX_INIT; |
192 | static pthread_mutex_t reqlock = AIO_MUTEX_INIT; |
143 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
193 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
144 | |
194 | |
… | |
… | |
194 | } |
244 | } |
195 | |
245 | |
196 | abort (); |
246 | abort (); |
197 | } |
247 | } |
198 | |
248 | |
|
|
249 | static int poll_cb (int max); |
199 | static void req_invoke (aio_req req); |
250 | static void req_invoke (aio_req req); |
200 | static void req_free (aio_req req); |
251 | static void req_free (aio_req req); |
|
|
252 | static void req_cancel (aio_req req); |
201 | |
253 | |
202 | /* must be called at most once */ |
254 | /* must be called at most once */ |
203 | static SV *req_sv (aio_req req, const char *klass) |
255 | static SV *req_sv (aio_req req, const char *klass) |
204 | { |
256 | { |
205 | if (!req->self) |
257 | if (!req->self) |
… | |
… | |
274 | fd_set rfd; |
326 | fd_set rfd; |
275 | |
327 | |
276 | while (nreqs) |
328 | while (nreqs) |
277 | { |
329 | { |
278 | int size; |
330 | int size; |
279 | #if !(__i386 || __x86_64) /* safe without sempahore on this archs */ |
331 | #if !(__i386 || __x86_64) /* safe without sempahore on these archs */ |
280 | pthread_mutex_lock (&reslock); |
332 | LOCK (reslock); |
281 | #endif |
333 | #endif |
282 | size = res_queue.size; |
334 | size = res_queue.size; |
283 | #if !(__i386 || __x86_64) /* safe without sempahore on this archs */ |
335 | #if !(__i386 || __x86_64) /* safe without sempahore on these archs */ |
284 | pthread_mutex_unlock (&reslock); |
336 | UNLOCK (reslock); |
285 | #endif |
337 | #endif |
286 | |
338 | |
287 | if (size) |
339 | if (size) |
288 | return; |
340 | return; |
289 | |
341 | |
… | |
… | |
313 | { |
365 | { |
314 | SV *rv = &PL_sv_undef; |
366 | SV *rv = &PL_sv_undef; |
315 | |
367 | |
316 | if (req->result >= 0) |
368 | if (req->result >= 0) |
317 | { |
369 | { |
|
|
370 | int i; |
318 | char *buf = req->data2ptr; |
371 | char *buf = req->data2ptr; |
319 | AV *av = newAV (); |
372 | AV *av = newAV (); |
320 | |
373 | |
321 | while (req->result) |
374 | av_extend (av, req->result - 1); |
|
|
375 | |
|
|
376 | for (i = 0; i < req->result; ++i) |
322 | { |
377 | { |
323 | SV *sv = newSVpv (buf, 0); |
378 | SV *sv = newSVpv (buf, 0); |
324 | |
379 | |
325 | av_push (av, sv); |
380 | av_store (av, i, sv); |
326 | buf += SvCUR (sv) + 1; |
381 | buf += SvCUR (sv) + 1; |
327 | req->result--; |
|
|
328 | } |
382 | } |
329 | |
383 | |
330 | rv = sv_2mortal (newRV_noinc ((SV *)av)); |
384 | rv = sv_2mortal (newRV_noinc ((SV *)av)); |
331 | } |
385 | } |
332 | |
386 | |
… | |
… | |
416 | SvREFCNT_dec (req->fh); |
470 | SvREFCNT_dec (req->fh); |
417 | SvREFCNT_dec (req->fh2); |
471 | SvREFCNT_dec (req->fh2); |
418 | SvREFCNT_dec (req->callback); |
472 | SvREFCNT_dec (req->callback); |
419 | Safefree (req->statdata); |
473 | Safefree (req->statdata); |
420 | |
474 | |
421 | if (req->type == REQ_READDIR && req->result >= 0) |
475 | if (req->type == REQ_READDIR) |
422 | free (req->data2ptr); |
476 | free (req->data2ptr); |
423 | |
477 | |
424 | Safefree (req); |
478 | Safefree (req); |
425 | } |
479 | } |
426 | |
480 | |
|
|
481 | static void req_cancel_subs (aio_req grp) |
|
|
482 | { |
|
|
483 | aio_req sub; |
|
|
484 | |
|
|
485 | if (grp->type != REQ_GROUP) |
|
|
486 | return; |
|
|
487 | |
|
|
488 | SvREFCNT_dec (grp->fh2); |
|
|
489 | grp->fh2 = 0; |
|
|
490 | |
|
|
491 | for (sub = grp->grp_first; sub; sub = sub->grp_next) |
|
|
492 | req_cancel (sub); |
|
|
493 | } |
|
|
494 | |
427 | static void req_cancel (aio_req req) |
495 | static void req_cancel (aio_req req) |
428 | { |
496 | { |
429 | req->flags |= FLAG_CANCELLED; |
497 | req->flags |= FLAG_CANCELLED; |
430 | |
498 | |
431 | if (req->type == REQ_GROUP) |
499 | req_cancel_subs (req); |
432 | { |
|
|
433 | aio_req sub; |
|
|
434 | |
|
|
435 | for (sub = req->grp_first; sub; sub = sub->grp_next) |
|
|
436 | req_cancel (sub); |
|
|
437 | } |
|
|
438 | } |
500 | } |
439 | |
501 | |
440 | static int poll_cb () |
502 | static int poll_cb (int max) |
441 | { |
503 | { |
442 | dSP; |
504 | dSP; |
443 | int count = 0; |
505 | int count = 0; |
444 | int do_croak = 0; |
506 | int do_croak = 0; |
445 | aio_req req; |
507 | aio_req req; |
446 | |
508 | |
447 | for (;;) |
509 | for (;;) |
448 | { |
510 | { |
449 | pthread_mutex_lock (&reslock); |
511 | while (max <= 0 || count < max) |
450 | req = reqq_shift (&res_queue); |
|
|
451 | |
|
|
452 | if (req) |
|
|
453 | { |
512 | { |
|
|
513 | LOCK (reslock); |
|
|
514 | req = reqq_shift (&res_queue); |
|
|
515 | |
454 | if (!res_queue.size) |
516 | if (req) |
455 | { |
517 | { |
|
|
518 | if (!res_queue.size) |
|
|
519 | { |
456 | /* read any signals sent by the worker threads */ |
520 | /* read any signals sent by the worker threads */ |
457 | char buf [32]; |
521 | char buf [32]; |
458 | while (read (respipe [0], buf, 32) == 32) |
522 | while (read (respipe [0], buf, 32) == 32) |
|
|
523 | ; |
459 | ; |
524 | } |
460 | } |
525 | } |
|
|
526 | |
|
|
527 | UNLOCK (reslock); |
|
|
528 | |
|
|
529 | if (!req) |
|
|
530 | break; |
|
|
531 | |
|
|
532 | --nreqs; |
|
|
533 | |
|
|
534 | if (req->type == REQ_QUIT) |
|
|
535 | --started; |
|
|
536 | else if (req->type == REQ_GROUP && req->length) |
|
|
537 | { |
|
|
538 | req->fd = 1; /* mark request as delayed */ |
|
|
539 | continue; |
|
|
540 | } |
|
|
541 | else |
|
|
542 | { |
|
|
543 | if (req->type == REQ_READ) |
|
|
544 | SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); |
|
|
545 | |
|
|
546 | if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE)) |
|
|
547 | SvREADONLY_off (req->data); |
|
|
548 | |
|
|
549 | if (req->statdata) |
|
|
550 | { |
|
|
551 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
|
|
552 | PL_laststatval = req->result; |
|
|
553 | PL_statcache = *(req->statdata); |
|
|
554 | } |
|
|
555 | |
|
|
556 | req_invoke (req); |
|
|
557 | |
|
|
558 | count++; |
|
|
559 | } |
|
|
560 | |
|
|
561 | req_free (req); |
461 | } |
562 | } |
462 | |
563 | |
463 | pthread_mutex_unlock (&reslock); |
564 | if (nreqs <= max_outstanding) |
464 | |
|
|
465 | if (!req) |
|
|
466 | break; |
565 | break; |
467 | |
566 | |
468 | --nreqs; |
567 | poll_wait (); |
469 | |
568 | |
470 | if (req->type == REQ_QUIT) |
569 | max = 0; |
471 | started--; |
|
|
472 | else if (req->type == REQ_GROUP && req->length) |
|
|
473 | { |
|
|
474 | req->fd = 1; /* mark request as delayed */ |
|
|
475 | continue; |
|
|
476 | } |
|
|
477 | else |
|
|
478 | { |
|
|
479 | if (req->type == REQ_READ) |
|
|
480 | SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); |
|
|
481 | |
|
|
482 | if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE)) |
|
|
483 | SvREADONLY_off (req->data); |
|
|
484 | |
|
|
485 | if (req->statdata) |
|
|
486 | { |
|
|
487 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
|
|
488 | PL_laststatval = req->result; |
|
|
489 | PL_statcache = *(req->statdata); |
|
|
490 | } |
|
|
491 | |
|
|
492 | req_invoke (req); |
|
|
493 | |
|
|
494 | count++; |
|
|
495 | } |
|
|
496 | |
|
|
497 | req_free (req); |
|
|
498 | } |
570 | } |
499 | |
571 | |
500 | return count; |
572 | return count; |
501 | } |
573 | } |
502 | |
574 | |
503 | static void *aio_proc(void *arg); |
575 | static void *aio_proc(void *arg); |
504 | |
576 | |
505 | static void start_thread (void) |
577 | static void start_thread (void) |
506 | { |
578 | { |
507 | sigset_t fullsigset, oldsigset; |
579 | sigset_t fullsigset, oldsigset; |
508 | pthread_t tid; |
|
|
509 | pthread_attr_t attr; |
580 | pthread_attr_t attr; |
|
|
581 | |
|
|
582 | worker *wrk = calloc (1, sizeof (worker)); |
|
|
583 | |
|
|
584 | if (!wrk) |
|
|
585 | croak ("unable to allocate worker thread data"); |
510 | |
586 | |
511 | pthread_attr_init (&attr); |
587 | pthread_attr_init (&attr); |
512 | pthread_attr_setstacksize (&attr, STACKSIZE); |
588 | pthread_attr_setstacksize (&attr, STACKSIZE); |
513 | pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); |
589 | pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); |
514 | |
590 | |
515 | sigfillset (&fullsigset); |
591 | sigfillset (&fullsigset); |
|
|
592 | |
|
|
593 | LOCK (wrklock); |
516 | sigprocmask (SIG_SETMASK, &fullsigset, &oldsigset); |
594 | sigprocmask (SIG_SETMASK, &fullsigset, &oldsigset); |
517 | |
595 | |
518 | if (pthread_create (&tid, &attr, aio_proc, 0) == 0) |
596 | if (pthread_create (&wrk->tid, &attr, aio_proc, (void *)wrk) == 0) |
|
|
597 | { |
|
|
598 | wrk->prev = &wrk_first; |
|
|
599 | wrk->next = wrk_first.next; |
|
|
600 | wrk_first.next->prev = wrk; |
|
|
601 | wrk_first.next = wrk; |
519 | started++; |
602 | ++started; |
|
|
603 | } |
|
|
604 | else |
|
|
605 | free (wrk); |
520 | |
606 | |
521 | sigprocmask (SIG_SETMASK, &oldsigset, 0); |
607 | sigprocmask (SIG_SETMASK, &oldsigset, 0); |
|
|
608 | UNLOCK (wrklock); |
522 | } |
609 | } |
523 | |
610 | |
524 | static void req_send (aio_req req) |
611 | static void req_send (aio_req req) |
525 | { |
612 | { |
526 | while (started < wanted && nreqs >= started) |
613 | while (started < wanted && nreqs >= started) |
527 | start_thread (); |
614 | start_thread (); |
528 | |
615 | |
529 | ++nreqs; |
616 | ++nreqs; |
530 | |
617 | |
531 | pthread_mutex_lock (&reqlock); |
618 | LOCK (reqlock); |
532 | reqq_push (&req_queue, req); |
619 | reqq_push (&req_queue, req); |
533 | pthread_cond_signal (&reqwait); |
620 | pthread_cond_signal (&reqwait); |
534 | pthread_mutex_unlock (&reqlock); |
621 | UNLOCK (reqlock); |
535 | |
|
|
536 | if (nreqs > max_outstanding) |
|
|
537 | for (;;) |
|
|
538 | { |
|
|
539 | poll_cb (); |
|
|
540 | |
|
|
541 | if (nreqs <= max_outstanding) |
|
|
542 | break; |
|
|
543 | |
|
|
544 | poll_wait (); |
|
|
545 | } |
|
|
546 | } |
622 | } |
547 | |
623 | |
548 | static void end_thread (void) |
624 | static void end_thread (void) |
549 | { |
625 | { |
550 | aio_req req; |
626 | aio_req req; |
… | |
… | |
577 | } |
653 | } |
578 | |
654 | |
579 | while (started > wanted) |
655 | while (started > wanted) |
580 | { |
656 | { |
581 | poll_wait (); |
657 | poll_wait (); |
582 | poll_cb (); |
658 | poll_cb (0); |
583 | } |
659 | } |
584 | } |
660 | } |
585 | |
661 | |
586 | static void create_pipe () |
662 | static void create_pipe () |
587 | { |
663 | { |
… | |
… | |
612 | static ssize_t pread (int fd, void *buf, size_t count, off_t offset) |
688 | static ssize_t pread (int fd, void *buf, size_t count, off_t offset) |
613 | { |
689 | { |
614 | ssize_t res; |
690 | ssize_t res; |
615 | off_t ooffset; |
691 | off_t ooffset; |
616 | |
692 | |
617 | pthread_mutex_lock (&preadwritelock); |
693 | LOCK (preadwritelock); |
618 | ooffset = lseek (fd, 0, SEEK_CUR); |
694 | ooffset = lseek (fd, 0, SEEK_CUR); |
619 | lseek (fd, offset, SEEK_SET); |
695 | lseek (fd, offset, SEEK_SET); |
620 | res = read (fd, buf, count); |
696 | res = read (fd, buf, count); |
621 | lseek (fd, ooffset, SEEK_SET); |
697 | lseek (fd, ooffset, SEEK_SET); |
622 | pthread_mutex_unlock (&preadwritelock); |
698 | UNLOCK (preadwritelock); |
623 | |
699 | |
624 | return res; |
700 | return res; |
625 | } |
701 | } |
626 | |
702 | |
627 | static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset) |
703 | static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset) |
628 | { |
704 | { |
629 | ssize_t res; |
705 | ssize_t res; |
630 | off_t ooffset; |
706 | off_t ooffset; |
631 | |
707 | |
632 | pthread_mutex_lock (&preadwritelock); |
708 | LOCK (preadwritelock); |
633 | ooffset = lseek (fd, 0, SEEK_CUR); |
709 | ooffset = lseek (fd, 0, SEEK_CUR); |
634 | lseek (fd, offset, SEEK_SET); |
710 | lseek (fd, offset, SEEK_SET); |
635 | res = write (fd, buf, count); |
711 | res = write (fd, buf, count); |
636 | lseek (fd, offset, SEEK_SET); |
712 | lseek (fd, offset, SEEK_SET); |
637 | pthread_mutex_unlock (&preadwritelock); |
713 | UNLOCK (preadwritelock); |
638 | |
714 | |
639 | return res; |
715 | return res; |
640 | } |
716 | } |
641 | #endif |
717 | #endif |
642 | |
718 | |
643 | #if !HAVE_FDATASYNC |
719 | #if !HAVE_FDATASYNC |
644 | # define fdatasync fsync |
720 | # define fdatasync fsync |
645 | #endif |
721 | #endif |
646 | |
722 | |
647 | #if !HAVE_READAHEAD |
723 | #if !HAVE_READAHEAD |
648 | # define readahead aio_readahead |
724 | # define readahead(fd,offset,count) aio_readahead (fd, offset, count, self) |
649 | |
725 | |
650 | static ssize_t readahead (int fd, off_t offset, size_t count) |
726 | static ssize_t aio_readahead (int fd, off_t offset, size_t count, worker *self) |
651 | { |
727 | { |
652 | dBUF; |
728 | dBUF; |
653 | |
729 | |
654 | while (count > 0) |
730 | while (count > 0) |
655 | { |
731 | { |
… | |
… | |
658 | pread (fd, aio_buf, len, offset); |
734 | pread (fd, aio_buf, len, offset); |
659 | offset += len; |
735 | offset += len; |
660 | count -= len; |
736 | count -= len; |
661 | } |
737 | } |
662 | |
738 | |
663 | fBUF; |
|
|
664 | |
|
|
665 | errno = 0; |
739 | errno = 0; |
666 | } |
740 | } |
|
|
741 | |
667 | #endif |
742 | #endif |
668 | |
743 | |
669 | #if !HAVE_READDIR_R |
744 | #if !HAVE_READDIR_R |
670 | # define readdir_r aio_readdir_r |
745 | # define readdir_r aio_readdir_r |
671 | |
746 | |
… | |
… | |
674 | static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) |
749 | static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) |
675 | { |
750 | { |
676 | struct dirent *e; |
751 | struct dirent *e; |
677 | int errorno; |
752 | int errorno; |
678 | |
753 | |
679 | pthread_mutex_lock (&readdirlock); |
754 | LOCK (readdirlock); |
680 | |
755 | |
681 | e = readdir (dirp); |
756 | e = readdir (dirp); |
682 | errorno = errno; |
757 | errorno = errno; |
683 | |
758 | |
684 | if (e) |
759 | if (e) |
… | |
… | |
687 | strcpy (ent->d_name, e->d_name); |
762 | strcpy (ent->d_name, e->d_name); |
688 | } |
763 | } |
689 | else |
764 | else |
690 | *res = 0; |
765 | *res = 0; |
691 | |
766 | |
692 | pthread_mutex_unlock (&readdirlock); |
767 | UNLOCK (readdirlock); |
693 | |
768 | |
694 | errno = errorno; |
769 | errno = errorno; |
695 | return e ? 0 : -1; |
770 | return e ? 0 : -1; |
696 | } |
771 | } |
697 | #endif |
772 | #endif |
698 | |
773 | |
699 | /* sendfile always needs emulation */ |
774 | /* sendfile always needs emulation */ |
700 | static ssize_t sendfile_ (int ofd, int ifd, off_t offset, size_t count) |
775 | static ssize_t sendfile_ (int ofd, int ifd, off_t offset, size_t count, worker *self) |
701 | { |
776 | { |
702 | ssize_t res; |
777 | ssize_t res; |
703 | |
778 | |
704 | if (!count) |
779 | if (!count) |
705 | return 0; |
780 | return 0; |
… | |
… | |
716 | { |
791 | { |
717 | off_t sbytes; |
792 | off_t sbytes; |
718 | res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); |
793 | res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); |
719 | |
794 | |
720 | if (res < 0 && sbytes) |
795 | if (res < 0 && sbytes) |
721 | /* maybe only on EAGAIN only: as usual, the manpage leaves you guessing */ |
796 | /* maybe only on EAGAIN: as usual, the manpage leaves you guessing */ |
722 | res = sbytes; |
797 | res = sbytes; |
723 | } |
798 | } |
724 | |
799 | |
725 | # elif __hpux |
800 | # elif __hpux |
726 | res = sendfile (ofd, ifd, offset, count, 0, 0); |
801 | res = sendfile (ofd, ifd, offset, count, 0, 0); |
… | |
… | |
782 | |
857 | |
783 | offset += cnt; |
858 | offset += cnt; |
784 | res += cnt; |
859 | res += cnt; |
785 | count -= cnt; |
860 | count -= cnt; |
786 | } |
861 | } |
787 | |
|
|
788 | fBUF; |
|
|
789 | } |
862 | } |
790 | |
863 | |
791 | return res; |
864 | return res; |
792 | } |
865 | } |
793 | |
866 | |
794 | /* read a full directory */ |
867 | /* read a full directory */ |
795 | static int scandir_ (const char *path, void **namesp) |
868 | static void scandir_ (aio_req req, worker *self) |
796 | { |
869 | { |
797 | DIR *dirp; |
870 | DIR *dirp; |
798 | union |
871 | union |
799 | { |
872 | { |
800 | struct dirent d; |
873 | struct dirent d; |
… | |
… | |
805 | int memlen = 4096; |
878 | int memlen = 4096; |
806 | int memofs = 0; |
879 | int memofs = 0; |
807 | int res = 0; |
880 | int res = 0; |
808 | int errorno; |
881 | int errorno; |
809 | |
882 | |
810 | dirp = opendir (path); |
883 | LOCK (wrklock); |
811 | if (!dirp) |
884 | self->dirp = dirp = opendir (req->dataptr); |
812 | return -1; |
|
|
813 | |
|
|
814 | u = malloc (sizeof (*u)); |
885 | self->dbuf = u = malloc (sizeof (*u)); |
815 | names = malloc (memlen); |
886 | req->data2ptr = names = malloc (memlen); |
|
|
887 | UNLOCK (wrklock); |
816 | |
888 | |
817 | if (u && names) |
889 | if (dirp && u && names) |
818 | for (;;) |
890 | for (;;) |
819 | { |
891 | { |
820 | errno = 0; |
892 | errno = 0; |
821 | readdir_r (dirp, &u->d, &entp); |
893 | readdir_r (dirp, &u->d, &entp); |
822 | |
894 | |
… | |
… | |
832 | res++; |
904 | res++; |
833 | |
905 | |
834 | while (memofs + len > memlen) |
906 | while (memofs + len > memlen) |
835 | { |
907 | { |
836 | memlen *= 2; |
908 | memlen *= 2; |
|
|
909 | LOCK (wrklock); |
837 | names = realloc (names, memlen); |
910 | req->data2ptr = names = realloc (names, memlen); |
|
|
911 | UNLOCK (wrklock); |
|
|
912 | |
838 | if (!names) |
913 | if (!names) |
839 | break; |
914 | break; |
840 | } |
915 | } |
841 | |
916 | |
842 | memcpy (names + memofs, name, len); |
917 | memcpy (names + memofs, name, len); |
843 | memofs += len; |
918 | memofs += len; |
844 | } |
919 | } |
845 | } |
920 | } |
846 | |
921 | |
847 | errorno = errno; |
|
|
848 | free (u); |
|
|
849 | closedir (dirp); |
|
|
850 | |
|
|
851 | if (errorno) |
922 | if (errno) |
852 | { |
|
|
853 | free (names); |
|
|
854 | errno = errorno; |
|
|
855 | res = -1; |
923 | res = -1; |
856 | } |
924 | |
857 | |
925 | req->result = res; |
858 | *namesp = (void *)names; |
|
|
859 | return res; |
|
|
860 | } |
926 | } |
861 | |
927 | |
862 | /*****************************************************************************/ |
928 | /*****************************************************************************/ |
863 | |
929 | |
864 | static void *aio_proc (void *thr_arg) |
930 | static void *aio_proc (void *thr_arg) |
865 | { |
931 | { |
866 | aio_req req; |
932 | aio_req req; |
867 | int type; |
933 | int type; |
|
|
934 | worker *self = (worker *)thr_arg; |
868 | |
935 | |
869 | do |
936 | do |
870 | { |
937 | { |
871 | pthread_mutex_lock (&reqlock); |
938 | LOCK (reqlock); |
872 | |
939 | |
873 | for (;;) |
940 | for (;;) |
874 | { |
941 | { |
875 | req = reqq_shift (&req_queue); |
942 | self->req = req = reqq_shift (&req_queue); |
876 | |
943 | |
877 | if (req) |
944 | if (req) |
878 | break; |
945 | break; |
879 | |
946 | |
880 | pthread_cond_wait (&reqwait, &reqlock); |
947 | pthread_cond_wait (&reqwait, &reqlock); |
881 | } |
948 | } |
882 | |
949 | |
883 | pthread_mutex_unlock (&reqlock); |
950 | UNLOCK (reqlock); |
884 | |
951 | |
885 | errno = 0; /* strictly unnecessary */ |
952 | errno = 0; /* strictly unnecessary */ |
886 | type = req->type; /* remember type for QUIT check */ |
953 | type = req->type; /* remember type for QUIT check */ |
887 | |
954 | |
888 | if (!(req->flags & FLAG_CANCELLED)) |
955 | if (!(req->flags & FLAG_CANCELLED)) |
… | |
… | |
890 | { |
957 | { |
891 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
958 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
892 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
959 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
893 | |
960 | |
894 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
961 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
895 | case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length); break; |
962 | case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length, self); break; |
896 | |
963 | |
897 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
964 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
898 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
965 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
899 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
966 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
900 | |
967 | |
… | |
… | |
906 | case REQ_LINK: req->result = link (req->data2ptr, req->dataptr); break; |
973 | case REQ_LINK: req->result = link (req->data2ptr, req->dataptr); break; |
907 | case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; |
974 | case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; |
908 | |
975 | |
909 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
976 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
910 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
977 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
911 | case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break; |
978 | case REQ_READDIR: scandir_ (req, self); break; |
912 | |
979 | |
913 | case REQ_BUSY: |
980 | case REQ_BUSY: |
914 | { |
981 | { |
915 | struct timeval tv; |
982 | struct timeval tv; |
916 | |
983 | |
… | |
… | |
930 | break; |
997 | break; |
931 | } |
998 | } |
932 | |
999 | |
933 | req->errorno = errno; |
1000 | req->errorno = errno; |
934 | |
1001 | |
935 | pthread_mutex_lock (&reslock); |
1002 | LOCK (reslock); |
936 | |
1003 | |
937 | if (!reqq_push (&res_queue, req)) |
1004 | if (!reqq_push (&res_queue, req)) |
938 | /* write a dummy byte to the pipe so fh becomes ready */ |
1005 | /* write a dummy byte to the pipe so fh becomes ready */ |
939 | write (respipe [1], &respipe, 1); |
1006 | write (respipe [1], &respipe, 1); |
940 | |
1007 | |
941 | pthread_mutex_unlock (&reslock); |
1008 | self->req = 0; |
|
|
1009 | worker_clear (self); |
|
|
1010 | |
|
|
1011 | UNLOCK (reslock); |
942 | } |
1012 | } |
943 | while (type != REQ_QUIT); |
1013 | while (type != REQ_QUIT); |
944 | |
1014 | |
|
|
1015 | LOCK (wrklock); |
|
|
1016 | worker_free (self); |
|
|
1017 | UNLOCK (wrklock); |
|
|
1018 | |
945 | return 0; |
1019 | return 0; |
946 | } |
1020 | } |
947 | |
1021 | |
948 | /*****************************************************************************/ |
1022 | /*****************************************************************************/ |
949 | |
1023 | |
950 | static void atfork_prepare (void) |
1024 | static void atfork_prepare (void) |
951 | { |
1025 | { |
952 | pthread_mutex_lock (&reqlock); |
1026 | LOCK (wrklock); |
953 | pthread_mutex_lock (&reslock); |
1027 | LOCK (reqlock); |
|
|
1028 | LOCK (reslock); |
954 | #if !HAVE_PREADWRITE |
1029 | #if !HAVE_PREADWRITE |
955 | pthread_mutex_lock (&preadwritelock); |
1030 | LOCK (preadwritelock); |
956 | #endif |
1031 | #endif |
957 | #if !HAVE_READDIR_R |
1032 | #if !HAVE_READDIR_R |
958 | pthread_mutex_lock (&readdirlock); |
1033 | LOCK (readdirlock); |
959 | #endif |
1034 | #endif |
960 | } |
1035 | } |
961 | |
1036 | |
962 | static void atfork_parent (void) |
1037 | static void atfork_parent (void) |
963 | { |
1038 | { |
964 | #if !HAVE_READDIR_R |
1039 | #if !HAVE_READDIR_R |
965 | pthread_mutex_unlock (&readdirlock); |
1040 | UNLOCK (readdirlock); |
966 | #endif |
1041 | #endif |
967 | #if !HAVE_PREADWRITE |
1042 | #if !HAVE_PREADWRITE |
968 | pthread_mutex_unlock (&preadwritelock); |
1043 | UNLOCK (preadwritelock); |
969 | #endif |
1044 | #endif |
970 | pthread_mutex_unlock (&reslock); |
1045 | UNLOCK (reslock); |
971 | pthread_mutex_unlock (&reqlock); |
1046 | UNLOCK (reqlock); |
|
|
1047 | UNLOCK (wrklock); |
972 | } |
1048 | } |
973 | |
1049 | |
974 | static void atfork_child (void) |
1050 | static void atfork_child (void) |
975 | { |
1051 | { |
976 | aio_req prv; |
1052 | aio_req prv; |
977 | |
|
|
978 | started = 0; |
|
|
979 | |
1053 | |
980 | while (prv = reqq_shift (&req_queue)) |
1054 | while (prv = reqq_shift (&req_queue)) |
981 | req_free (prv); |
1055 | req_free (prv); |
982 | |
1056 | |
983 | while (prv = reqq_shift (&res_queue)) |
1057 | while (prv = reqq_shift (&res_queue)) |
984 | req_free (prv); |
1058 | req_free (prv); |
985 | |
1059 | |
|
|
1060 | while (wrk_first.next != &wrk_first) |
|
|
1061 | { |
|
|
1062 | worker *wrk = wrk_first.next; |
|
|
1063 | |
|
|
1064 | if (wrk->req) |
|
|
1065 | req_free (wrk->req); |
|
|
1066 | |
|
|
1067 | worker_clear (wrk); |
|
|
1068 | worker_free (wrk); |
|
|
1069 | } |
|
|
1070 | |
|
|
1071 | started = 0; |
|
|
1072 | nreqs = 0; |
|
|
1073 | |
986 | close (respipe [0]); |
1074 | close (respipe [0]); |
987 | close (respipe [1]); |
1075 | close (respipe [1]); |
988 | create_pipe (); |
1076 | create_pipe (); |
989 | |
1077 | |
990 | atfork_parent (); |
1078 | atfork_parent (); |
… | |
… | |
1025 | create_pipe (); |
1113 | create_pipe (); |
1026 | pthread_atfork (atfork_prepare, atfork_parent, atfork_child); |
1114 | pthread_atfork (atfork_prepare, atfork_parent, atfork_child); |
1027 | } |
1115 | } |
1028 | |
1116 | |
1029 | void |
1117 | void |
1030 | min_parallel (nthreads) |
1118 | min_parallel (int nthreads) |
1031 | int nthreads |
|
|
1032 | PROTOTYPE: $ |
1119 | PROTOTYPE: $ |
1033 | |
1120 | |
1034 | void |
1121 | void |
1035 | max_parallel (nthreads) |
1122 | max_parallel (int nthreads) |
1036 | int nthreads |
|
|
1037 | PROTOTYPE: $ |
1123 | PROTOTYPE: $ |
1038 | |
1124 | |
1039 | int |
1125 | int |
1040 | max_outstanding (nreqs) |
1126 | max_outstanding (int maxreqs) |
1041 | int nreqs |
1127 | PROTOTYPE: $ |
1042 | PROTOTYPE: $ |
|
|
1043 | CODE: |
1128 | CODE: |
1044 | RETVAL = max_outstanding; |
1129 | RETVAL = max_outstanding; |
1045 | max_outstanding = nreqs; |
1130 | max_outstanding = maxreqs; |
|
|
1131 | OUTPUT: |
|
|
1132 | RETVAL |
1046 | |
1133 | |
1047 | void |
1134 | void |
1048 | aio_open (pathname,flags,mode,callback=&PL_sv_undef) |
1135 | aio_open (pathname,flags,mode,callback=&PL_sv_undef) |
1049 | SV * pathname |
1136 | SV * pathname |
1050 | int flags |
1137 | int flags |
… | |
… | |
1327 | PROTOTYPE: |
1414 | PROTOTYPE: |
1328 | CODE: |
1415 | CODE: |
1329 | while (nreqs) |
1416 | while (nreqs) |
1330 | { |
1417 | { |
1331 | poll_wait (); |
1418 | poll_wait (); |
1332 | poll_cb (); |
1419 | poll_cb (0); |
1333 | } |
1420 | } |
1334 | |
1421 | |
1335 | void |
1422 | void |
1336 | poll() |
1423 | poll() |
1337 | PROTOTYPE: |
1424 | PROTOTYPE: |
1338 | CODE: |
1425 | CODE: |
1339 | if (nreqs) |
1426 | if (nreqs) |
1340 | { |
1427 | { |
1341 | poll_wait (); |
1428 | poll_wait (); |
1342 | poll_cb (); |
1429 | poll_cb (0); |
1343 | } |
1430 | } |
1344 | |
1431 | |
1345 | int |
1432 | int |
1346 | poll_fileno() |
1433 | poll_fileno() |
1347 | PROTOTYPE: |
1434 | PROTOTYPE: |
… | |
… | |
1352 | |
1439 | |
1353 | int |
1440 | int |
1354 | poll_cb(...) |
1441 | poll_cb(...) |
1355 | PROTOTYPE: |
1442 | PROTOTYPE: |
1356 | CODE: |
1443 | CODE: |
1357 | RETVAL = poll_cb (); |
1444 | RETVAL = poll_cb (0); |
|
|
1445 | OUTPUT: |
|
|
1446 | RETVAL |
|
|
1447 | |
|
|
1448 | int |
|
|
1449 | poll_some(int max = 0) |
|
|
1450 | PROTOTYPE: $ |
|
|
1451 | CODE: |
|
|
1452 | RETVAL = poll_cb (max); |
1358 | OUTPUT: |
1453 | OUTPUT: |
1359 | RETVAL |
1454 | RETVAL |
1360 | |
1455 | |
1361 | void |
1456 | void |
1362 | poll_wait() |
1457 | poll_wait() |
… | |
… | |
1422 | } |
1517 | } |
1423 | } |
1518 | } |
1424 | } |
1519 | } |
1425 | |
1520 | |
1426 | void |
1521 | void |
|
|
1522 | cancel_subs (aio_req_ornot req) |
|
|
1523 | CODE: |
|
|
1524 | req_cancel_subs (req); |
|
|
1525 | |
|
|
1526 | void |
1427 | result (aio_req grp, ...) |
1527 | result (aio_req grp, ...) |
1428 | CODE: |
1528 | CODE: |
1429 | { |
1529 | { |
1430 | int i; |
1530 | int i; |
1431 | AV *av = newAV (); |
1531 | AV *av = newAV (); |