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" |
… | |
… | |
42 | # else |
43 | # else |
43 | # error sendfile support requested but not available |
44 | # error sendfile support requested but not available |
44 | # endif |
45 | # endif |
45 | #endif |
46 | #endif |
46 | |
47 | |
|
|
48 | /* number of seconds after which idle threads exit */ |
|
|
49 | #define IDLE_TIMEOUT 10 |
|
|
50 | |
47 | /* used for struct dirent, AIX doesn't provide it */ |
51 | /* used for struct dirent, AIX doesn't provide it */ |
48 | #ifndef NAME_MAX |
52 | #ifndef NAME_MAX |
49 | # define NAME_MAX 4096 |
53 | # define NAME_MAX 4096 |
|
|
54 | #endif |
|
|
55 | |
|
|
56 | #ifndef PTHREAD_STACK_MIN |
|
|
57 | /* care for broken platforms, e.g. windows */ |
|
|
58 | # define PTHREAD_STACK_MIN 16384 |
50 | #endif |
59 | #endif |
51 | |
60 | |
52 | #if __ia64 |
61 | #if __ia64 |
53 | # define STACKSIZE 65536 |
62 | # define STACKSIZE 65536 |
54 | #elif __i386 || __x86_64 /* 16k is unreasonably high :( */ |
63 | #elif __i386 || __x86_64 /* 16k is unreasonably high :( */ |
55 | # define STACKSIZE PTHREAD_STACK_MIN |
64 | # define STACKSIZE PTHREAD_STACK_MIN |
56 | #else |
65 | #else |
57 | # define STACKSIZE 16384 |
66 | # define STACKSIZE 16384 |
58 | #endif |
67 | #endif |
59 | |
68 | |
|
|
69 | /* wether word reads are potentially non-atomic. |
|
|
70 | * this is conservatice, likely most arches this runs |
|
|
71 | * on have atomic word read/writes. |
|
|
72 | */ |
|
|
73 | #ifndef WORDREAD_UNSAFE |
|
|
74 | # if __i386 || __x86_64 |
|
|
75 | # define WORDREAD_UNSAFE 0 |
|
|
76 | # else |
|
|
77 | # define WORDREAD_UNSAFE 1 |
|
|
78 | # endif |
|
|
79 | #endif |
|
|
80 | |
60 | /* buffer size for various temporary buffers */ |
81 | /* buffer size for various temporary buffers */ |
61 | #define AIO_BUFSIZE 65536 |
82 | #define AIO_BUFSIZE 65536 |
62 | |
83 | |
63 | #define dBUF \ |
84 | #define dBUF \ |
|
|
85 | char *aio_buf; \ |
|
|
86 | LOCK (wrklock); \ |
64 | char *aio_buf = malloc (AIO_BUFSIZE); \ |
87 | self->dbuf = aio_buf = malloc (AIO_BUFSIZE); \ |
|
|
88 | UNLOCK (wrklock); \ |
65 | if (!aio_buf) \ |
89 | if (!aio_buf) \ |
66 | return -1; |
90 | return -1; |
67 | |
|
|
68 | #define fBUF free (aio_buf) |
|
|
69 | |
91 | |
70 | enum { |
92 | enum { |
71 | REQ_QUIT, |
93 | REQ_QUIT, |
72 | REQ_OPEN, REQ_CLOSE, |
94 | REQ_OPEN, REQ_CLOSE, |
73 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
95 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
74 | REQ_SENDFILE, |
96 | REQ_SENDFILE, |
75 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
97 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
76 | REQ_FSYNC, REQ_FDATASYNC, |
98 | REQ_FSYNC, REQ_FDATASYNC, |
77 | REQ_UNLINK, REQ_RMDIR, REQ_RENAME, |
99 | REQ_UNLINK, REQ_RMDIR, REQ_RENAME, |
78 | REQ_READDIR, |
100 | REQ_MKNOD, REQ_READDIR, |
79 | REQ_LINK, REQ_SYMLINK, |
101 | REQ_LINK, REQ_SYMLINK, |
80 | REQ_GROUP, REQ_NOP, |
102 | REQ_GROUP, REQ_NOP, |
81 | REQ_BUSY, |
103 | REQ_BUSY, |
82 | }; |
104 | }; |
83 | |
105 | |
… | |
… | |
125 | NUM_PRI = PRI_MAX + PRI_BIAS + 1, |
147 | NUM_PRI = PRI_MAX + PRI_BIAS + 1, |
126 | }; |
148 | }; |
127 | |
149 | |
128 | static int next_pri = DEFAULT_PRI + PRI_BIAS; |
150 | static int next_pri = DEFAULT_PRI + PRI_BIAS; |
129 | |
151 | |
130 | static int started, wanted; |
152 | static unsigned int started, idle, wanted; |
131 | static volatile int nreqs; |
|
|
132 | static int max_outstanding = 1<<30; |
|
|
133 | static int respipe [2]; |
|
|
134 | |
153 | |
135 | #if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) |
154 | #if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) |
136 | # define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP |
155 | # define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP |
137 | #else |
156 | #else |
138 | # define AIO_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER |
157 | # define AIO_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER |
139 | #endif |
158 | #endif |
140 | |
159 | |
|
|
160 | #define LOCK(mutex) pthread_mutex_lock (&(mutex)) |
|
|
161 | #define UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) |
|
|
162 | |
|
|
163 | /* worker threads management */ |
|
|
164 | static pthread_mutex_t wrklock = AIO_MUTEX_INIT; |
|
|
165 | |
|
|
166 | typedef struct worker { |
|
|
167 | /* locked by wrklock */ |
|
|
168 | struct worker *prev, *next; |
|
|
169 | |
|
|
170 | pthread_t tid; |
|
|
171 | |
|
|
172 | /* locked by reslock, reqlock or wrklock */ |
|
|
173 | aio_req req; /* currently processed request */ |
|
|
174 | void *dbuf; |
|
|
175 | DIR *dirp; |
|
|
176 | } worker; |
|
|
177 | |
|
|
178 | static worker wrk_first = { &wrk_first, &wrk_first, 0 }; |
|
|
179 | |
|
|
180 | static void worker_clear (worker *wrk) |
|
|
181 | { |
|
|
182 | if (wrk->dirp) |
|
|
183 | { |
|
|
184 | closedir (wrk->dirp); |
|
|
185 | wrk->dirp = 0; |
|
|
186 | } |
|
|
187 | |
|
|
188 | if (wrk->dbuf) |
|
|
189 | { |
|
|
190 | free (wrk->dbuf); |
|
|
191 | wrk->dbuf = 0; |
|
|
192 | } |
|
|
193 | } |
|
|
194 | |
|
|
195 | static void worker_free (worker *wrk) |
|
|
196 | { |
|
|
197 | wrk->next->prev = wrk->prev; |
|
|
198 | wrk->prev->next = wrk->next; |
|
|
199 | |
|
|
200 | free (wrk); |
|
|
201 | } |
|
|
202 | |
|
|
203 | static volatile unsigned int nreqs, nready, npending; |
|
|
204 | static volatile unsigned int max_idle = 4; |
|
|
205 | static volatile unsigned int max_outstanding = 0xffffffff; |
|
|
206 | static int respipe [2]; |
|
|
207 | |
141 | static pthread_mutex_t reslock = AIO_MUTEX_INIT; |
208 | static pthread_mutex_t reslock = AIO_MUTEX_INIT; |
142 | static pthread_mutex_t reqlock = AIO_MUTEX_INIT; |
209 | static pthread_mutex_t reqlock = AIO_MUTEX_INIT; |
143 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
210 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
|
|
211 | |
|
|
212 | #if WORDREAD_UNSAFE |
|
|
213 | |
|
|
214 | static unsigned int get_nready () |
|
|
215 | { |
|
|
216 | unsigned int retval; |
|
|
217 | |
|
|
218 | LOCK (reqlock); |
|
|
219 | retval = nready; |
|
|
220 | UNLOCK (reqlock); |
|
|
221 | |
|
|
222 | return retval; |
|
|
223 | } |
|
|
224 | |
|
|
225 | static unsigned int get_npending () |
|
|
226 | { |
|
|
227 | unsigned int retval; |
|
|
228 | |
|
|
229 | LOCK (reslock); |
|
|
230 | retval = npending; |
|
|
231 | UNLOCK (reslock); |
|
|
232 | |
|
|
233 | return retval; |
|
|
234 | } |
|
|
235 | |
|
|
236 | #else |
|
|
237 | |
|
|
238 | # define get_nready() nready |
|
|
239 | # define get_npending() npending |
|
|
240 | |
|
|
241 | #endif |
144 | |
242 | |
145 | /* |
243 | /* |
146 | * a somewhat faster data structure might be nice, but |
244 | * a somewhat faster data structure might be nice, but |
147 | * with 8 priorities this actually needs <20 insns |
245 | * with 8 priorities this actually needs <20 insns |
148 | * per shift, the most expensive operation. |
246 | * per shift, the most expensive operation. |
… | |
… | |
194 | } |
292 | } |
195 | |
293 | |
196 | abort (); |
294 | abort (); |
197 | } |
295 | } |
198 | |
296 | |
|
|
297 | static int poll_cb (int max); |
199 | static void req_invoke (aio_req req); |
298 | static void req_invoke (aio_req req); |
200 | static void req_free (aio_req req); |
299 | static void req_free (aio_req req); |
|
|
300 | static void req_cancel (aio_req req); |
201 | |
301 | |
202 | /* must be called at most once */ |
302 | /* must be called at most once */ |
203 | static SV *req_sv (aio_req req, const char *klass) |
303 | static SV *req_sv (aio_req req, const char *klass) |
204 | { |
304 | { |
205 | if (!req->self) |
305 | if (!req->self) |
… | |
… | |
267 | req_invoke (grp); |
367 | req_invoke (grp); |
268 | req_free (grp); |
368 | req_free (grp); |
269 | } |
369 | } |
270 | } |
370 | } |
271 | |
371 | |
272 | static void poll_wait () |
|
|
273 | { |
|
|
274 | fd_set rfd; |
|
|
275 | |
|
|
276 | while (nreqs) |
|
|
277 | { |
|
|
278 | int size; |
|
|
279 | #if !(__i386 || __x86_64) /* safe without sempahore on this archs */ |
|
|
280 | pthread_mutex_lock (&reslock); |
|
|
281 | #endif |
|
|
282 | size = res_queue.size; |
|
|
283 | #if !(__i386 || __x86_64) /* safe without sempahore on this archs */ |
|
|
284 | pthread_mutex_unlock (&reslock); |
|
|
285 | #endif |
|
|
286 | |
|
|
287 | if (size) |
|
|
288 | return; |
|
|
289 | |
|
|
290 | FD_ZERO(&rfd); |
|
|
291 | FD_SET(respipe [0], &rfd); |
|
|
292 | |
|
|
293 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
|
|
294 | } |
|
|
295 | } |
|
|
296 | |
|
|
297 | static void req_invoke (aio_req req) |
372 | static void req_invoke (aio_req req) |
298 | { |
373 | { |
299 | dSP; |
374 | dSP; |
300 | |
375 | |
301 | if (!(req->flags & FLAG_CANCELLED) && SvOK (req->callback)) |
376 | if (!(req->flags & FLAG_CANCELLED) && SvOK (req->callback)) |
302 | { |
377 | { |
303 | errno = req->errorno; |
|
|
304 | |
|
|
305 | ENTER; |
378 | ENTER; |
306 | SAVETMPS; |
379 | SAVETMPS; |
307 | PUSHMARK (SP); |
380 | PUSHMARK (SP); |
308 | EXTEND (SP, 1); |
381 | EXTEND (SP, 1); |
309 | |
382 | |
… | |
… | |
313 | { |
386 | { |
314 | SV *rv = &PL_sv_undef; |
387 | SV *rv = &PL_sv_undef; |
315 | |
388 | |
316 | if (req->result >= 0) |
389 | if (req->result >= 0) |
317 | { |
390 | { |
|
|
391 | int i; |
318 | char *buf = req->data2ptr; |
392 | char *buf = req->data2ptr; |
319 | AV *av = newAV (); |
393 | AV *av = newAV (); |
320 | |
394 | |
321 | while (req->result) |
395 | av_extend (av, req->result - 1); |
|
|
396 | |
|
|
397 | for (i = 0; i < req->result; ++i) |
322 | { |
398 | { |
323 | SV *sv = newSVpv (buf, 0); |
399 | SV *sv = newSVpv (buf, 0); |
324 | |
400 | |
325 | av_push (av, sv); |
401 | av_store (av, i, sv); |
326 | buf += SvCUR (sv) + 1; |
402 | buf += SvCUR (sv) + 1; |
327 | req->result--; |
|
|
328 | } |
403 | } |
329 | |
404 | |
330 | rv = sv_2mortal (newRV_noinc ((SV *)av)); |
405 | rv = sv_2mortal (newRV_noinc ((SV *)av)); |
331 | } |
406 | } |
332 | |
407 | |
… | |
… | |
372 | default: |
447 | default: |
373 | PUSHs (sv_2mortal (newSViv (req->result))); |
448 | PUSHs (sv_2mortal (newSViv (req->result))); |
374 | break; |
449 | break; |
375 | } |
450 | } |
376 | |
451 | |
|
|
452 | errno = req->errorno; |
377 | |
453 | |
378 | PUTBACK; |
454 | PUTBACK; |
379 | call_sv (req->callback, G_VOID | G_EVAL); |
455 | call_sv (req->callback, G_VOID | G_EVAL); |
380 | SPAGAIN; |
456 | SPAGAIN; |
381 | |
457 | |
… | |
… | |
416 | SvREFCNT_dec (req->fh); |
492 | SvREFCNT_dec (req->fh); |
417 | SvREFCNT_dec (req->fh2); |
493 | SvREFCNT_dec (req->fh2); |
418 | SvREFCNT_dec (req->callback); |
494 | SvREFCNT_dec (req->callback); |
419 | Safefree (req->statdata); |
495 | Safefree (req->statdata); |
420 | |
496 | |
421 | if (req->type == REQ_READDIR && req->result >= 0) |
497 | if (req->type == REQ_READDIR) |
422 | free (req->data2ptr); |
498 | free (req->data2ptr); |
423 | |
499 | |
424 | Safefree (req); |
500 | Safefree (req); |
425 | } |
501 | } |
426 | |
502 | |
|
|
503 | static void req_cancel_subs (aio_req grp) |
|
|
504 | { |
|
|
505 | aio_req sub; |
|
|
506 | |
|
|
507 | if (grp->type != REQ_GROUP) |
|
|
508 | return; |
|
|
509 | |
|
|
510 | SvREFCNT_dec (grp->fh2); |
|
|
511 | grp->fh2 = 0; |
|
|
512 | |
|
|
513 | for (sub = grp->grp_first; sub; sub = sub->grp_next) |
|
|
514 | req_cancel (sub); |
|
|
515 | } |
|
|
516 | |
427 | static void req_cancel (aio_req req) |
517 | static void req_cancel (aio_req req) |
428 | { |
518 | { |
429 | req->flags |= FLAG_CANCELLED; |
519 | req->flags |= FLAG_CANCELLED; |
430 | |
520 | |
431 | if (req->type == REQ_GROUP) |
521 | req_cancel_subs (req); |
432 | { |
522 | } |
433 | aio_req sub; |
|
|
434 | |
523 | |
435 | for (sub = req->grp_first; sub; sub = sub->grp_next) |
524 | static void *aio_proc(void *arg); |
436 | req_cancel (sub); |
525 | |
|
|
526 | static void start_thread (void) |
|
|
527 | { |
|
|
528 | sigset_t fullsigset, oldsigset; |
|
|
529 | pthread_attr_t attr; |
|
|
530 | |
|
|
531 | worker *wrk = calloc (1, sizeof (worker)); |
|
|
532 | |
|
|
533 | if (!wrk) |
|
|
534 | croak ("unable to allocate worker thread data"); |
|
|
535 | |
|
|
536 | pthread_attr_init (&attr); |
|
|
537 | pthread_attr_setstacksize (&attr, STACKSIZE); |
|
|
538 | pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); |
|
|
539 | #ifdef PTHREAD_SCOPE_PROCESS |
|
|
540 | pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS); |
|
|
541 | #endif |
|
|
542 | |
|
|
543 | sigfillset (&fullsigset); |
|
|
544 | |
|
|
545 | LOCK (wrklock); |
|
|
546 | sigprocmask (SIG_SETMASK, &fullsigset, &oldsigset); |
|
|
547 | |
|
|
548 | if (pthread_create (&wrk->tid, &attr, aio_proc, (void *)wrk) == 0) |
437 | } |
549 | { |
438 | } |
550 | wrk->prev = &wrk_first; |
|
|
551 | wrk->next = wrk_first.next; |
|
|
552 | wrk_first.next->prev = wrk; |
|
|
553 | wrk_first.next = wrk; |
|
|
554 | ++started; |
|
|
555 | } |
|
|
556 | else |
|
|
557 | free (wrk); |
439 | |
558 | |
|
|
559 | sigprocmask (SIG_SETMASK, &oldsigset, 0); |
|
|
560 | UNLOCK (wrklock); |
|
|
561 | } |
|
|
562 | |
|
|
563 | static void maybe_start_thread () |
|
|
564 | { |
|
|
565 | if (started >= wanted) |
|
|
566 | return; |
|
|
567 | |
|
|
568 | /* todo: maybe use idle here, but might be less exact */ |
|
|
569 | if ((int)nready <= (int)started - (int)(nreqs - get_nready () - get_npending ())) |
|
|
570 | return; |
|
|
571 | |
|
|
572 | start_thread (); |
|
|
573 | } |
|
|
574 | |
|
|
575 | static void req_send (aio_req req) |
|
|
576 | { |
|
|
577 | ++nreqs; |
|
|
578 | |
|
|
579 | LOCK (reqlock); |
|
|
580 | ++nready; |
|
|
581 | reqq_push (&req_queue, req); |
|
|
582 | pthread_cond_signal (&reqwait); |
|
|
583 | UNLOCK (reqlock); |
|
|
584 | |
|
|
585 | maybe_start_thread (); |
|
|
586 | } |
|
|
587 | |
|
|
588 | static void end_thread (void) |
|
|
589 | { |
|
|
590 | aio_req req; |
|
|
591 | |
|
|
592 | Newz (0, req, 1, aio_cb); |
|
|
593 | |
|
|
594 | req->type = REQ_QUIT; |
|
|
595 | req->pri = PRI_MAX + PRI_BIAS; |
|
|
596 | |
|
|
597 | LOCK (reqlock); |
|
|
598 | reqq_push (&req_queue, req); |
|
|
599 | pthread_cond_signal (&reqwait); |
|
|
600 | UNLOCK (reqlock); |
|
|
601 | |
|
|
602 | LOCK (wrklock); |
|
|
603 | --started; |
|
|
604 | UNLOCK (wrklock); |
|
|
605 | } |
|
|
606 | |
|
|
607 | static void min_parallel (int nthreads) |
|
|
608 | { |
|
|
609 | if (wanted < nthreads) |
|
|
610 | wanted = nthreads; |
|
|
611 | } |
|
|
612 | |
|
|
613 | static void max_parallel (int nthreads) |
|
|
614 | { |
|
|
615 | if (wanted > nthreads) |
|
|
616 | wanted = nthreads; |
|
|
617 | |
|
|
618 | while (started > wanted) |
|
|
619 | end_thread (); |
|
|
620 | } |
|
|
621 | |
|
|
622 | static void poll_wait () |
|
|
623 | { |
|
|
624 | fd_set rfd; |
|
|
625 | |
|
|
626 | while (nreqs) |
|
|
627 | { |
|
|
628 | int size; |
|
|
629 | if (WORDREAD_UNSAFE) LOCK (reslock); |
|
|
630 | size = res_queue.size; |
|
|
631 | if (WORDREAD_UNSAFE) UNLOCK (reslock); |
|
|
632 | |
|
|
633 | if (size) |
|
|
634 | return; |
|
|
635 | |
|
|
636 | maybe_start_thread (); |
|
|
637 | |
|
|
638 | FD_ZERO(&rfd); |
|
|
639 | FD_SET(respipe [0], &rfd); |
|
|
640 | |
|
|
641 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
|
|
642 | } |
|
|
643 | } |
|
|
644 | |
440 | static int poll_cb () |
645 | static int poll_cb (int max) |
441 | { |
646 | { |
442 | dSP; |
647 | dSP; |
443 | int count = 0; |
648 | int count = 0; |
444 | int do_croak = 0; |
649 | int do_croak = 0; |
445 | aio_req req; |
650 | aio_req req; |
446 | |
651 | |
447 | for (;;) |
652 | for (;;) |
448 | { |
653 | { |
449 | pthread_mutex_lock (&reslock); |
654 | while (max <= 0 || count < max) |
450 | req = reqq_shift (&res_queue); |
|
|
451 | |
|
|
452 | if (req) |
|
|
453 | { |
655 | { |
|
|
656 | maybe_start_thread (); |
|
|
657 | |
|
|
658 | LOCK (reslock); |
|
|
659 | req = reqq_shift (&res_queue); |
|
|
660 | |
454 | if (!res_queue.size) |
661 | if (req) |
455 | { |
662 | { |
|
|
663 | --npending; |
|
|
664 | |
|
|
665 | if (!res_queue.size) |
|
|
666 | { |
456 | /* read any signals sent by the worker threads */ |
667 | /* read any signals sent by the worker threads */ |
457 | char buf [32]; |
668 | char buf [32]; |
458 | while (read (respipe [0], buf, 32) == 32) |
669 | while (read (respipe [0], buf, 32) == 32) |
|
|
670 | ; |
459 | ; |
671 | } |
460 | } |
672 | } |
|
|
673 | |
|
|
674 | UNLOCK (reslock); |
|
|
675 | |
|
|
676 | if (!req) |
|
|
677 | break; |
|
|
678 | |
|
|
679 | --nreqs; |
|
|
680 | |
|
|
681 | if (req->type == REQ_GROUP && req->length) |
|
|
682 | { |
|
|
683 | req->fd = 1; /* mark request as delayed */ |
|
|
684 | continue; |
|
|
685 | } |
|
|
686 | else |
|
|
687 | { |
|
|
688 | if (req->type == REQ_READ) |
|
|
689 | SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); |
|
|
690 | |
|
|
691 | if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE)) |
|
|
692 | SvREADONLY_off (req->data); |
|
|
693 | |
|
|
694 | if (req->statdata) |
|
|
695 | { |
|
|
696 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
|
|
697 | PL_laststatval = req->result; |
|
|
698 | PL_statcache = *(req->statdata); |
|
|
699 | } |
|
|
700 | |
|
|
701 | req_invoke (req); |
|
|
702 | |
|
|
703 | count++; |
|
|
704 | } |
|
|
705 | |
|
|
706 | req_free (req); |
461 | } |
707 | } |
462 | |
708 | |
463 | pthread_mutex_unlock (&reslock); |
709 | if (nreqs <= max_outstanding) |
464 | |
|
|
465 | if (!req) |
|
|
466 | break; |
710 | break; |
467 | |
711 | |
468 | --nreqs; |
712 | poll_wait (); |
469 | |
713 | |
470 | if (req->type == REQ_QUIT) |
714 | 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 | } |
715 | } |
499 | |
716 | |
500 | return count; |
717 | return count; |
501 | } |
|
|
502 | |
|
|
503 | static void *aio_proc(void *arg); |
|
|
504 | |
|
|
505 | static void start_thread (void) |
|
|
506 | { |
|
|
507 | sigset_t fullsigset, oldsigset; |
|
|
508 | pthread_t tid; |
|
|
509 | pthread_attr_t attr; |
|
|
510 | |
|
|
511 | pthread_attr_init (&attr); |
|
|
512 | pthread_attr_setstacksize (&attr, STACKSIZE); |
|
|
513 | pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); |
|
|
514 | |
|
|
515 | sigfillset (&fullsigset); |
|
|
516 | sigprocmask (SIG_SETMASK, &fullsigset, &oldsigset); |
|
|
517 | |
|
|
518 | if (pthread_create (&tid, &attr, aio_proc, 0) == 0) |
|
|
519 | started++; |
|
|
520 | |
|
|
521 | sigprocmask (SIG_SETMASK, &oldsigset, 0); |
|
|
522 | } |
|
|
523 | |
|
|
524 | static void req_send (aio_req req) |
|
|
525 | { |
|
|
526 | while (started < wanted && nreqs >= started) |
|
|
527 | start_thread (); |
|
|
528 | |
|
|
529 | ++nreqs; |
|
|
530 | |
|
|
531 | pthread_mutex_lock (&reqlock); |
|
|
532 | reqq_push (&req_queue, req); |
|
|
533 | pthread_cond_signal (&reqwait); |
|
|
534 | pthread_mutex_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 | } |
|
|
547 | |
|
|
548 | static void end_thread (void) |
|
|
549 | { |
|
|
550 | aio_req req; |
|
|
551 | |
|
|
552 | Newz (0, req, 1, aio_cb); |
|
|
553 | |
|
|
554 | req->type = REQ_QUIT; |
|
|
555 | req->pri = PRI_MAX + PRI_BIAS; |
|
|
556 | |
|
|
557 | req_send (req); |
|
|
558 | } |
|
|
559 | |
|
|
560 | static void min_parallel (int nthreads) |
|
|
561 | { |
|
|
562 | if (wanted < nthreads) |
|
|
563 | wanted = nthreads; |
|
|
564 | } |
|
|
565 | |
|
|
566 | static void max_parallel (int nthreads) |
|
|
567 | { |
|
|
568 | int cur = started; |
|
|
569 | |
|
|
570 | if (wanted > nthreads) |
|
|
571 | wanted = nthreads; |
|
|
572 | |
|
|
573 | while (cur > wanted) |
|
|
574 | { |
|
|
575 | end_thread (); |
|
|
576 | cur--; |
|
|
577 | } |
|
|
578 | |
|
|
579 | while (started > wanted) |
|
|
580 | { |
|
|
581 | poll_wait (); |
|
|
582 | poll_cb (); |
|
|
583 | } |
|
|
584 | } |
718 | } |
585 | |
719 | |
586 | static void create_pipe () |
720 | static void create_pipe () |
587 | { |
721 | { |
588 | if (pipe (respipe)) |
722 | if (pipe (respipe)) |
… | |
… | |
612 | static ssize_t pread (int fd, void *buf, size_t count, off_t offset) |
746 | static ssize_t pread (int fd, void *buf, size_t count, off_t offset) |
613 | { |
747 | { |
614 | ssize_t res; |
748 | ssize_t res; |
615 | off_t ooffset; |
749 | off_t ooffset; |
616 | |
750 | |
617 | pthread_mutex_lock (&preadwritelock); |
751 | LOCK (preadwritelock); |
618 | ooffset = lseek (fd, 0, SEEK_CUR); |
752 | ooffset = lseek (fd, 0, SEEK_CUR); |
619 | lseek (fd, offset, SEEK_SET); |
753 | lseek (fd, offset, SEEK_SET); |
620 | res = read (fd, buf, count); |
754 | res = read (fd, buf, count); |
621 | lseek (fd, ooffset, SEEK_SET); |
755 | lseek (fd, ooffset, SEEK_SET); |
622 | pthread_mutex_unlock (&preadwritelock); |
756 | UNLOCK (preadwritelock); |
623 | |
757 | |
624 | return res; |
758 | return res; |
625 | } |
759 | } |
626 | |
760 | |
627 | static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset) |
761 | static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset) |
628 | { |
762 | { |
629 | ssize_t res; |
763 | ssize_t res; |
630 | off_t ooffset; |
764 | off_t ooffset; |
631 | |
765 | |
632 | pthread_mutex_lock (&preadwritelock); |
766 | LOCK (preadwritelock); |
633 | ooffset = lseek (fd, 0, SEEK_CUR); |
767 | ooffset = lseek (fd, 0, SEEK_CUR); |
634 | lseek (fd, offset, SEEK_SET); |
768 | lseek (fd, offset, SEEK_SET); |
635 | res = write (fd, buf, count); |
769 | res = write (fd, buf, count); |
636 | lseek (fd, offset, SEEK_SET); |
770 | lseek (fd, offset, SEEK_SET); |
637 | pthread_mutex_unlock (&preadwritelock); |
771 | UNLOCK (preadwritelock); |
638 | |
772 | |
639 | return res; |
773 | return res; |
640 | } |
774 | } |
641 | #endif |
775 | #endif |
642 | |
776 | |
643 | #if !HAVE_FDATASYNC |
777 | #if !HAVE_FDATASYNC |
644 | # define fdatasync fsync |
778 | # define fdatasync fsync |
645 | #endif |
779 | #endif |
646 | |
780 | |
647 | #if !HAVE_READAHEAD |
781 | #if !HAVE_READAHEAD |
648 | # define readahead aio_readahead |
782 | # define readahead(fd,offset,count) aio_readahead (fd, offset, count, self) |
649 | |
783 | |
650 | static ssize_t readahead (int fd, off_t offset, size_t count) |
784 | static ssize_t aio_readahead (int fd, off_t offset, size_t count, worker *self) |
651 | { |
785 | { |
652 | dBUF; |
786 | dBUF; |
653 | |
787 | |
654 | while (count > 0) |
788 | while (count > 0) |
655 | { |
789 | { |
… | |
… | |
658 | pread (fd, aio_buf, len, offset); |
792 | pread (fd, aio_buf, len, offset); |
659 | offset += len; |
793 | offset += len; |
660 | count -= len; |
794 | count -= len; |
661 | } |
795 | } |
662 | |
796 | |
663 | fBUF; |
|
|
664 | |
|
|
665 | errno = 0; |
797 | errno = 0; |
666 | } |
798 | } |
|
|
799 | |
667 | #endif |
800 | #endif |
668 | |
801 | |
669 | #if !HAVE_READDIR_R |
802 | #if !HAVE_READDIR_R |
670 | # define readdir_r aio_readdir_r |
803 | # define readdir_r aio_readdir_r |
671 | |
804 | |
… | |
… | |
674 | static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) |
807 | static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) |
675 | { |
808 | { |
676 | struct dirent *e; |
809 | struct dirent *e; |
677 | int errorno; |
810 | int errorno; |
678 | |
811 | |
679 | pthread_mutex_lock (&readdirlock); |
812 | LOCK (readdirlock); |
680 | |
813 | |
681 | e = readdir (dirp); |
814 | e = readdir (dirp); |
682 | errorno = errno; |
815 | errorno = errno; |
683 | |
816 | |
684 | if (e) |
817 | if (e) |
… | |
… | |
687 | strcpy (ent->d_name, e->d_name); |
820 | strcpy (ent->d_name, e->d_name); |
688 | } |
821 | } |
689 | else |
822 | else |
690 | *res = 0; |
823 | *res = 0; |
691 | |
824 | |
692 | pthread_mutex_unlock (&readdirlock); |
825 | UNLOCK (readdirlock); |
693 | |
826 | |
694 | errno = errorno; |
827 | errno = errorno; |
695 | return e ? 0 : -1; |
828 | return e ? 0 : -1; |
696 | } |
829 | } |
697 | #endif |
830 | #endif |
698 | |
831 | |
699 | /* sendfile always needs emulation */ |
832 | /* sendfile always needs emulation */ |
700 | static ssize_t sendfile_ (int ofd, int ifd, off_t offset, size_t count) |
833 | static ssize_t sendfile_ (int ofd, int ifd, off_t offset, size_t count, worker *self) |
701 | { |
834 | { |
702 | ssize_t res; |
835 | ssize_t res; |
703 | |
836 | |
704 | if (!count) |
837 | if (!count) |
705 | return 0; |
838 | return 0; |
… | |
… | |
716 | { |
849 | { |
717 | off_t sbytes; |
850 | off_t sbytes; |
718 | res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); |
851 | res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); |
719 | |
852 | |
720 | if (res < 0 && sbytes) |
853 | if (res < 0 && sbytes) |
721 | /* maybe only on EAGAIN only: as usual, the manpage leaves you guessing */ |
854 | /* maybe only on EAGAIN: as usual, the manpage leaves you guessing */ |
722 | res = sbytes; |
855 | res = sbytes; |
723 | } |
856 | } |
724 | |
857 | |
725 | # elif __hpux |
858 | # elif __hpux |
726 | res = sendfile (ofd, ifd, offset, count, 0, 0); |
859 | res = sendfile (ofd, ifd, offset, count, 0, 0); |
… | |
… | |
782 | |
915 | |
783 | offset += cnt; |
916 | offset += cnt; |
784 | res += cnt; |
917 | res += cnt; |
785 | count -= cnt; |
918 | count -= cnt; |
786 | } |
919 | } |
787 | |
|
|
788 | fBUF; |
|
|
789 | } |
920 | } |
790 | |
921 | |
791 | return res; |
922 | return res; |
792 | } |
923 | } |
793 | |
924 | |
794 | /* read a full directory */ |
925 | /* read a full directory */ |
795 | static int scandir_ (const char *path, void **namesp) |
926 | static void scandir_ (aio_req req, worker *self) |
796 | { |
927 | { |
797 | DIR *dirp; |
928 | DIR *dirp; |
798 | union |
929 | union |
799 | { |
930 | { |
800 | struct dirent d; |
931 | struct dirent d; |
… | |
… | |
805 | int memlen = 4096; |
936 | int memlen = 4096; |
806 | int memofs = 0; |
937 | int memofs = 0; |
807 | int res = 0; |
938 | int res = 0; |
808 | int errorno; |
939 | int errorno; |
809 | |
940 | |
810 | dirp = opendir (path); |
941 | LOCK (wrklock); |
811 | if (!dirp) |
942 | self->dirp = dirp = opendir (req->dataptr); |
812 | return -1; |
|
|
813 | |
|
|
814 | u = malloc (sizeof (*u)); |
943 | self->dbuf = u = malloc (sizeof (*u)); |
815 | names = malloc (memlen); |
944 | req->data2ptr = names = malloc (memlen); |
|
|
945 | UNLOCK (wrklock); |
816 | |
946 | |
817 | if (u && names) |
947 | if (dirp && u && names) |
818 | for (;;) |
948 | for (;;) |
819 | { |
949 | { |
820 | errno = 0; |
950 | errno = 0; |
821 | readdir_r (dirp, &u->d, &entp); |
951 | readdir_r (dirp, &u->d, &entp); |
822 | |
952 | |
… | |
… | |
832 | res++; |
962 | res++; |
833 | |
963 | |
834 | while (memofs + len > memlen) |
964 | while (memofs + len > memlen) |
835 | { |
965 | { |
836 | memlen *= 2; |
966 | memlen *= 2; |
|
|
967 | LOCK (wrklock); |
837 | names = realloc (names, memlen); |
968 | req->data2ptr = names = realloc (names, memlen); |
|
|
969 | UNLOCK (wrklock); |
|
|
970 | |
838 | if (!names) |
971 | if (!names) |
839 | break; |
972 | break; |
840 | } |
973 | } |
841 | |
974 | |
842 | memcpy (names + memofs, name, len); |
975 | memcpy (names + memofs, name, len); |
843 | memofs += len; |
976 | memofs += len; |
844 | } |
977 | } |
845 | } |
978 | } |
846 | |
979 | |
847 | errorno = errno; |
|
|
848 | free (u); |
|
|
849 | closedir (dirp); |
|
|
850 | |
|
|
851 | if (errorno) |
980 | if (errno) |
852 | { |
|
|
853 | free (names); |
|
|
854 | errno = errorno; |
|
|
855 | res = -1; |
981 | res = -1; |
856 | } |
982 | |
857 | |
983 | req->result = res; |
858 | *namesp = (void *)names; |
|
|
859 | return res; |
|
|
860 | } |
984 | } |
861 | |
985 | |
862 | /*****************************************************************************/ |
986 | /*****************************************************************************/ |
863 | |
987 | |
864 | static void *aio_proc (void *thr_arg) |
988 | static void *aio_proc (void *thr_arg) |
865 | { |
989 | { |
866 | aio_req req; |
990 | aio_req req; |
867 | int type; |
991 | struct timespec ts; |
|
|
992 | worker *self = (worker *)thr_arg; |
868 | |
993 | |
869 | do |
994 | /* try to distribute timeouts somewhat evenly */ |
|
|
995 | ts.tv_nsec = (((unsigned long)self + (unsigned long)ts.tv_sec) & 1023UL) |
|
|
996 | * (1000000000UL / 1024UL); |
|
|
997 | |
|
|
998 | for (;;) |
870 | { |
999 | { |
871 | pthread_mutex_lock (&reqlock); |
1000 | ts.tv_sec = time (0) + IDLE_TIMEOUT; |
|
|
1001 | |
|
|
1002 | LOCK (reqlock); |
872 | |
1003 | |
873 | for (;;) |
1004 | for (;;) |
874 | { |
1005 | { |
875 | req = reqq_shift (&req_queue); |
1006 | self->req = req = reqq_shift (&req_queue); |
876 | |
1007 | |
877 | if (req) |
1008 | if (req) |
878 | break; |
1009 | break; |
879 | |
1010 | |
|
|
1011 | ++idle; |
|
|
1012 | |
|
|
1013 | if (pthread_cond_timedwait (&reqwait, &reqlock, &ts) |
|
|
1014 | == ETIMEDOUT) |
|
|
1015 | { |
|
|
1016 | if (idle > max_idle) |
|
|
1017 | { |
|
|
1018 | --idle; |
|
|
1019 | UNLOCK (reqlock); |
|
|
1020 | LOCK (wrklock); |
|
|
1021 | --started; |
|
|
1022 | UNLOCK (wrklock); |
|
|
1023 | goto quit; |
|
|
1024 | } |
|
|
1025 | |
|
|
1026 | /* we are allowed to idle, so do so without any timeout */ |
880 | pthread_cond_wait (&reqwait, &reqlock); |
1027 | pthread_cond_wait (&reqwait, &reqlock); |
|
|
1028 | ts.tv_sec = time (0) + IDLE_TIMEOUT; |
|
|
1029 | } |
|
|
1030 | |
|
|
1031 | --idle; |
881 | } |
1032 | } |
882 | |
1033 | |
883 | pthread_mutex_unlock (&reqlock); |
1034 | --nready; |
|
|
1035 | |
|
|
1036 | UNLOCK (reqlock); |
884 | |
1037 | |
885 | errno = 0; /* strictly unnecessary */ |
1038 | errno = 0; /* strictly unnecessary */ |
886 | type = req->type; /* remember type for QUIT check */ |
|
|
887 | |
1039 | |
888 | if (!(req->flags & FLAG_CANCELLED)) |
1040 | if (!(req->flags & FLAG_CANCELLED)) |
889 | switch (type) |
1041 | switch (req->type) |
890 | { |
1042 | { |
891 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
1043 | 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; |
1044 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
893 | |
1045 | |
894 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
1046 | 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; |
1047 | case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length, self); break; |
896 | |
1048 | |
897 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
1049 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
898 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
1050 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
899 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
1051 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
900 | |
1052 | |
… | |
… | |
903 | case REQ_UNLINK: req->result = unlink (req->dataptr); break; |
1055 | case REQ_UNLINK: req->result = unlink (req->dataptr); break; |
904 | case REQ_RMDIR: req->result = rmdir (req->dataptr); break; |
1056 | case REQ_RMDIR: req->result = rmdir (req->dataptr); break; |
905 | case REQ_RENAME: req->result = rename (req->data2ptr, req->dataptr); break; |
1057 | case REQ_RENAME: req->result = rename (req->data2ptr, req->dataptr); break; |
906 | case REQ_LINK: req->result = link (req->data2ptr, req->dataptr); break; |
1058 | case REQ_LINK: req->result = link (req->data2ptr, req->dataptr); break; |
907 | case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; |
1059 | case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; |
|
|
1060 | case REQ_MKNOD: req->result = mknod (req->data2ptr, req->mode, (dev_t)req->offset); break; |
908 | |
1061 | |
909 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
1062 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
910 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
1063 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
911 | case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break; |
1064 | case REQ_READDIR: scandir_ (req, self); break; |
912 | |
1065 | |
913 | case REQ_BUSY: |
1066 | case REQ_BUSY: |
914 | { |
1067 | { |
915 | struct timeval tv; |
1068 | struct timeval tv; |
916 | |
1069 | |
… | |
… | |
920 | req->result = select (0, 0, 0, 0, &tv); |
1073 | req->result = select (0, 0, 0, 0, &tv); |
921 | } |
1074 | } |
922 | |
1075 | |
923 | case REQ_GROUP: |
1076 | case REQ_GROUP: |
924 | case REQ_NOP: |
1077 | case REQ_NOP: |
|
|
1078 | break; |
|
|
1079 | |
925 | case REQ_QUIT: |
1080 | case REQ_QUIT: |
926 | break; |
1081 | goto quit; |
927 | |
1082 | |
928 | default: |
1083 | default: |
929 | req->result = ENOSYS; |
1084 | req->result = ENOSYS; |
930 | break; |
1085 | break; |
931 | } |
1086 | } |
932 | |
1087 | |
933 | req->errorno = errno; |
1088 | req->errorno = errno; |
934 | |
1089 | |
935 | pthread_mutex_lock (&reslock); |
1090 | LOCK (reslock); |
|
|
1091 | |
|
|
1092 | ++npending; |
936 | |
1093 | |
937 | if (!reqq_push (&res_queue, req)) |
1094 | if (!reqq_push (&res_queue, req)) |
938 | /* write a dummy byte to the pipe so fh becomes ready */ |
1095 | /* write a dummy byte to the pipe so fh becomes ready */ |
939 | write (respipe [1], &respipe, 1); |
1096 | write (respipe [1], &respipe, 1); |
940 | |
1097 | |
941 | pthread_mutex_unlock (&reslock); |
1098 | self->req = 0; |
|
|
1099 | worker_clear (self); |
|
|
1100 | |
|
|
1101 | UNLOCK (reslock); |
942 | } |
1102 | } |
943 | while (type != REQ_QUIT); |
1103 | |
|
|
1104 | quit: |
|
|
1105 | LOCK (wrklock); |
|
|
1106 | worker_free (self); |
|
|
1107 | UNLOCK (wrklock); |
944 | |
1108 | |
945 | return 0; |
1109 | return 0; |
946 | } |
1110 | } |
947 | |
1111 | |
948 | /*****************************************************************************/ |
1112 | /*****************************************************************************/ |
949 | |
1113 | |
950 | static void atfork_prepare (void) |
1114 | static void atfork_prepare (void) |
951 | { |
1115 | { |
952 | pthread_mutex_lock (&reqlock); |
1116 | LOCK (wrklock); |
953 | pthread_mutex_lock (&reslock); |
1117 | LOCK (reqlock); |
|
|
1118 | LOCK (reslock); |
954 | #if !HAVE_PREADWRITE |
1119 | #if !HAVE_PREADWRITE |
955 | pthread_mutex_lock (&preadwritelock); |
1120 | LOCK (preadwritelock); |
956 | #endif |
1121 | #endif |
957 | #if !HAVE_READDIR_R |
1122 | #if !HAVE_READDIR_R |
958 | pthread_mutex_lock (&readdirlock); |
1123 | LOCK (readdirlock); |
959 | #endif |
1124 | #endif |
960 | } |
1125 | } |
961 | |
1126 | |
962 | static void atfork_parent (void) |
1127 | static void atfork_parent (void) |
963 | { |
1128 | { |
964 | #if !HAVE_READDIR_R |
1129 | #if !HAVE_READDIR_R |
965 | pthread_mutex_unlock (&readdirlock); |
1130 | UNLOCK (readdirlock); |
966 | #endif |
1131 | #endif |
967 | #if !HAVE_PREADWRITE |
1132 | #if !HAVE_PREADWRITE |
968 | pthread_mutex_unlock (&preadwritelock); |
1133 | UNLOCK (preadwritelock); |
969 | #endif |
1134 | #endif |
970 | pthread_mutex_unlock (&reslock); |
1135 | UNLOCK (reslock); |
971 | pthread_mutex_unlock (&reqlock); |
1136 | UNLOCK (reqlock); |
|
|
1137 | UNLOCK (wrklock); |
972 | } |
1138 | } |
973 | |
1139 | |
974 | static void atfork_child (void) |
1140 | static void atfork_child (void) |
975 | { |
1141 | { |
976 | aio_req prv; |
1142 | aio_req prv; |
977 | |
|
|
978 | started = 0; |
|
|
979 | |
1143 | |
980 | while (prv = reqq_shift (&req_queue)) |
1144 | while (prv = reqq_shift (&req_queue)) |
981 | req_free (prv); |
1145 | req_free (prv); |
982 | |
1146 | |
983 | while (prv = reqq_shift (&res_queue)) |
1147 | while (prv = reqq_shift (&res_queue)) |
984 | req_free (prv); |
1148 | req_free (prv); |
985 | |
1149 | |
|
|
1150 | while (wrk_first.next != &wrk_first) |
|
|
1151 | { |
|
|
1152 | worker *wrk = wrk_first.next; |
|
|
1153 | |
|
|
1154 | if (wrk->req) |
|
|
1155 | req_free (wrk->req); |
|
|
1156 | |
|
|
1157 | worker_clear (wrk); |
|
|
1158 | worker_free (wrk); |
|
|
1159 | } |
|
|
1160 | |
|
|
1161 | started = 0; |
|
|
1162 | idle = 0; |
|
|
1163 | nreqs = 0; |
|
|
1164 | nready = 0; |
|
|
1165 | npending = 0; |
|
|
1166 | |
986 | close (respipe [0]); |
1167 | close (respipe [0]); |
987 | close (respipe [1]); |
1168 | close (respipe [1]); |
988 | create_pipe (); |
1169 | create_pipe (); |
989 | |
1170 | |
990 | atfork_parent (); |
1171 | atfork_parent (); |
… | |
… | |
1016 | PROTOTYPES: ENABLE |
1197 | PROTOTYPES: ENABLE |
1017 | |
1198 | |
1018 | BOOT: |
1199 | BOOT: |
1019 | { |
1200 | { |
1020 | HV *stash = gv_stashpv ("IO::AIO", 1); |
1201 | HV *stash = gv_stashpv ("IO::AIO", 1); |
|
|
1202 | |
1021 | newCONSTSUB (stash, "EXDEV", newSViv (EXDEV)); |
1203 | newCONSTSUB (stash, "EXDEV", newSViv (EXDEV)); |
1022 | newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY)); |
1204 | newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY)); |
1023 | newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY)); |
1205 | newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY)); |
|
|
1206 | newCONSTSUB (stash, "O_CREAT", newSViv (O_CREAT)); |
|
|
1207 | newCONSTSUB (stash, "O_TRUNC", newSViv (O_TRUNC)); |
|
|
1208 | newCONSTSUB (stash, "S_IFIFO", newSViv (S_IFIFO)); |
1024 | |
1209 | |
1025 | create_pipe (); |
1210 | create_pipe (); |
1026 | pthread_atfork (atfork_prepare, atfork_parent, atfork_child); |
1211 | pthread_atfork (atfork_prepare, atfork_parent, atfork_child); |
1027 | } |
1212 | } |
1028 | |
1213 | |
1029 | void |
1214 | void |
1030 | min_parallel (nthreads) |
1215 | min_parallel (int nthreads) |
1031 | int nthreads |
|
|
1032 | PROTOTYPE: $ |
1216 | PROTOTYPE: $ |
1033 | |
1217 | |
1034 | void |
1218 | void |
1035 | max_parallel (nthreads) |
1219 | max_parallel (int nthreads) |
1036 | int nthreads |
|
|
1037 | PROTOTYPE: $ |
1220 | PROTOTYPE: $ |
1038 | |
1221 | |
1039 | int |
1222 | int |
1040 | max_outstanding (nreqs) |
1223 | max_outstanding (int maxreqs) |
1041 | int nreqs |
1224 | PROTOTYPE: $ |
1042 | PROTOTYPE: $ |
|
|
1043 | CODE: |
1225 | CODE: |
1044 | RETVAL = max_outstanding; |
1226 | RETVAL = max_outstanding; |
1045 | max_outstanding = nreqs; |
1227 | max_outstanding = maxreqs; |
|
|
1228 | OUTPUT: |
|
|
1229 | RETVAL |
1046 | |
1230 | |
1047 | void |
1231 | void |
1048 | aio_open (pathname,flags,mode,callback=&PL_sv_undef) |
1232 | aio_open (pathname,flags,mode,callback=&PL_sv_undef) |
1049 | SV * pathname |
1233 | SV * pathname |
1050 | int flags |
1234 | int flags |
… | |
… | |
1265 | |
1449 | |
1266 | REQ_SEND; |
1450 | REQ_SEND; |
1267 | } |
1451 | } |
1268 | |
1452 | |
1269 | void |
1453 | void |
|
|
1454 | aio_mknod (pathname,mode,dev,callback=&PL_sv_undef) |
|
|
1455 | SV * pathname |
|
|
1456 | SV * callback |
|
|
1457 | UV mode |
|
|
1458 | UV dev |
|
|
1459 | PPCODE: |
|
|
1460 | { |
|
|
1461 | dREQ; |
|
|
1462 | |
|
|
1463 | req->type = REQ_MKNOD; |
|
|
1464 | req->data = newSVsv (pathname); |
|
|
1465 | req->dataptr = SvPVbyte_nolen (req->data); |
|
|
1466 | req->mode = (mode_t)mode; |
|
|
1467 | req->offset = dev; |
|
|
1468 | |
|
|
1469 | REQ_SEND; |
|
|
1470 | } |
|
|
1471 | |
|
|
1472 | void |
1270 | aio_busy (delay,callback=&PL_sv_undef) |
1473 | aio_busy (delay,callback=&PL_sv_undef) |
1271 | double delay |
1474 | double delay |
1272 | SV * callback |
1475 | SV * callback |
1273 | PPCODE: |
1476 | PPCODE: |
1274 | { |
1477 | { |
… | |
… | |
1305 | req->type = REQ_NOP; |
1508 | req->type = REQ_NOP; |
1306 | |
1509 | |
1307 | REQ_SEND; |
1510 | REQ_SEND; |
1308 | } |
1511 | } |
1309 | |
1512 | |
1310 | void |
1513 | int |
1311 | aioreq_pri (int pri = DEFAULT_PRI) |
1514 | aioreq_pri (int pri = 0) |
1312 | CODE: |
1515 | PROTOTYPE: ;$ |
|
|
1516 | CODE: |
|
|
1517 | RETVAL = next_pri - PRI_BIAS; |
|
|
1518 | if (items > 0) |
|
|
1519 | { |
1313 | if (pri < PRI_MIN) pri = PRI_MIN; |
1520 | if (pri < PRI_MIN) pri = PRI_MIN; |
1314 | if (pri > PRI_MAX) pri = PRI_MAX; |
1521 | if (pri > PRI_MAX) pri = PRI_MAX; |
1315 | next_pri = pri + PRI_BIAS; |
1522 | next_pri = pri + PRI_BIAS; |
|
|
1523 | } |
|
|
1524 | OUTPUT: |
|
|
1525 | RETVAL |
1316 | |
1526 | |
1317 | void |
1527 | void |
1318 | aioreq_nice (int nice = 0) |
1528 | aioreq_nice (int nice = 0) |
1319 | CODE: |
1529 | CODE: |
1320 | nice = next_pri - nice; |
1530 | nice = next_pri - nice; |
1321 | if (nice < PRI_MIN) nice = PRI_MIN; |
1531 | if (nice < PRI_MIN) nice = PRI_MIN; |
1322 | if (nice > PRI_MAX) nice = PRI_MAX; |
1532 | if (nice > PRI_MAX) nice = PRI_MAX; |
1323 | next_pri = nice + PRI_BIAS; |
1533 | next_pri = nice + PRI_BIAS; |
1324 | |
1534 | |
1325 | void |
1535 | void |
1326 | flush () |
1536 | flush () |
1327 | PROTOTYPE: |
1537 | PROTOTYPE: |
1328 | CODE: |
1538 | CODE: |
1329 | while (nreqs) |
1539 | while (nreqs) |
1330 | { |
1540 | { |
1331 | poll_wait (); |
1541 | poll_wait (); |
1332 | poll_cb (); |
1542 | poll_cb (0); |
1333 | } |
1543 | } |
1334 | |
1544 | |
1335 | void |
1545 | void |
1336 | poll() |
1546 | poll() |
1337 | PROTOTYPE: |
1547 | PROTOTYPE: |
1338 | CODE: |
1548 | CODE: |
1339 | if (nreqs) |
1549 | if (nreqs) |
1340 | { |
1550 | { |
1341 | poll_wait (); |
1551 | poll_wait (); |
1342 | poll_cb (); |
1552 | poll_cb (0); |
1343 | } |
1553 | } |
1344 | |
1554 | |
1345 | int |
1555 | int |
1346 | poll_fileno() |
1556 | poll_fileno() |
1347 | PROTOTYPE: |
1557 | PROTOTYPE: |
… | |
… | |
1352 | |
1562 | |
1353 | int |
1563 | int |
1354 | poll_cb(...) |
1564 | poll_cb(...) |
1355 | PROTOTYPE: |
1565 | PROTOTYPE: |
1356 | CODE: |
1566 | CODE: |
1357 | RETVAL = poll_cb (); |
1567 | RETVAL = poll_cb (0); |
|
|
1568 | OUTPUT: |
|
|
1569 | RETVAL |
|
|
1570 | |
|
|
1571 | int |
|
|
1572 | poll_some(int max = 0) |
|
|
1573 | PROTOTYPE: $ |
|
|
1574 | CODE: |
|
|
1575 | RETVAL = poll_cb (max); |
1358 | OUTPUT: |
1576 | OUTPUT: |
1359 | RETVAL |
1577 | RETVAL |
1360 | |
1578 | |
1361 | void |
1579 | void |
1362 | poll_wait() |
1580 | poll_wait() |
… | |
… | |
1371 | CODE: |
1589 | CODE: |
1372 | RETVAL = nreqs; |
1590 | RETVAL = nreqs; |
1373 | OUTPUT: |
1591 | OUTPUT: |
1374 | RETVAL |
1592 | RETVAL |
1375 | |
1593 | |
|
|
1594 | int |
|
|
1595 | nready() |
|
|
1596 | PROTOTYPE: |
|
|
1597 | CODE: |
|
|
1598 | RETVAL = get_nready (); |
|
|
1599 | OUTPUT: |
|
|
1600 | RETVAL |
|
|
1601 | |
|
|
1602 | int |
|
|
1603 | npending() |
|
|
1604 | PROTOTYPE: |
|
|
1605 | CODE: |
|
|
1606 | RETVAL = get_npending (); |
|
|
1607 | OUTPUT: |
|
|
1608 | RETVAL |
|
|
1609 | |
1376 | PROTOTYPES: DISABLE |
1610 | PROTOTYPES: DISABLE |
1377 | |
1611 | |
1378 | MODULE = IO::AIO PACKAGE = IO::AIO::REQ |
1612 | MODULE = IO::AIO PACKAGE = IO::AIO::REQ |
1379 | |
1613 | |
1380 | void |
1614 | void |
… | |
… | |
1422 | } |
1656 | } |
1423 | } |
1657 | } |
1424 | } |
1658 | } |
1425 | |
1659 | |
1426 | void |
1660 | void |
|
|
1661 | cancel_subs (aio_req_ornot req) |
|
|
1662 | CODE: |
|
|
1663 | req_cancel_subs (req); |
|
|
1664 | |
|
|
1665 | void |
1427 | result (aio_req grp, ...) |
1666 | result (aio_req grp, ...) |
1428 | CODE: |
1667 | CODE: |
1429 | { |
1668 | { |
1430 | int i; |
1669 | int i; |
|
|
1670 | AV *av; |
|
|
1671 | |
|
|
1672 | grp->errorno = errno; |
|
|
1673 | |
1431 | AV *av = newAV (); |
1674 | av = newAV (); |
1432 | |
1675 | |
1433 | for (i = 1; i < items; ++i ) |
1676 | for (i = 1; i < items; ++i ) |
1434 | av_push (av, newSVsv (ST (i))); |
1677 | av_push (av, newSVsv (ST (i))); |
1435 | |
1678 | |
1436 | SvREFCNT_dec (grp->data); |
1679 | SvREFCNT_dec (grp->data); |
1437 | grp->data = (SV *)av; |
1680 | grp->data = (SV *)av; |
1438 | } |
1681 | } |
|
|
1682 | |
|
|
1683 | void |
|
|
1684 | errno (aio_req grp, int errorno = errno) |
|
|
1685 | CODE: |
|
|
1686 | grp->errorno = errorno; |
1439 | |
1687 | |
1440 | void |
1688 | void |
1441 | limit (aio_req grp, int limit) |
1689 | limit (aio_req grp, int limit) |
1442 | CODE: |
1690 | CODE: |
1443 | grp->fd2 = limit; |
1691 | grp->fd2 = limit; |