1 | #define PERL_NO_GET_CONTEXT |
1 | #define _XOPEN_SOURCE 500 |
2 | |
2 | |
3 | #include "EXTERN.h" |
3 | #include "EXTERN.h" |
4 | #include "perl.h" |
4 | #include "perl.h" |
5 | #include "XSUB.h" |
5 | #include "XSUB.h" |
6 | |
6 | |
7 | #include <sys/types.h> |
7 | #include <sys/types.h> |
8 | #include <sys/stat.h> |
8 | #include <sys/stat.h> |
|
|
9 | |
9 | #include <unistd.h> |
10 | #include <unistd.h> |
10 | #include <fcntl.h> |
11 | #include <fcntl.h> |
11 | #include <signal.h> |
12 | #include <signal.h> |
12 | #include <sched.h> |
13 | #include <sched.h> |
13 | #include <endian.h> |
14 | #if __linux |
|
|
15 | #include <sys/syscall.h> |
|
|
16 | #endif |
14 | |
17 | |
15 | #include <pthread.h> |
18 | #include <pthread.h> |
16 | #include <sys/syscall.h> |
|
|
17 | |
19 | |
18 | typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
20 | typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
19 | typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
21 | typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
20 | typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
22 | typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
21 | |
23 | |
… | |
… | |
52 | } aio_cb; |
54 | } aio_cb; |
53 | |
55 | |
54 | typedef aio_cb *aio_req; |
56 | typedef aio_cb *aio_req; |
55 | |
57 | |
56 | static int started; |
58 | static int started; |
57 | static int nreqs; |
59 | static volatile int nreqs; |
58 | static int max_outstanding = 1<<30; |
60 | static int max_outstanding = 1<<30; |
59 | static int respipe [2]; |
61 | static int respipe [2]; |
60 | |
62 | |
61 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
63 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
62 | static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; |
64 | static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; |
… | |
… | |
66 | static volatile aio_req ress, rese; /* queue start, queue end */ |
68 | static volatile aio_req ress, rese; /* queue start, queue end */ |
67 | |
69 | |
68 | static void |
70 | static void |
69 | poll_wait () |
71 | poll_wait () |
70 | { |
72 | { |
71 | if (!nreqs) |
73 | if (nreqs && !ress) |
72 | return; |
74 | { |
73 | |
|
|
74 | fd_set rfd; |
75 | fd_set rfd; |
75 | FD_ZERO(&rfd); |
76 | FD_ZERO(&rfd); |
76 | FD_SET(respipe [0], &rfd); |
77 | FD_SET(respipe [0], &rfd); |
77 | |
78 | |
78 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
79 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
|
|
80 | } |
79 | } |
81 | } |
80 | |
82 | |
81 | static int |
83 | static int |
82 | poll_cb (pTHX) |
84 | poll_cb () |
83 | { |
85 | { |
84 | dSP; |
86 | dSP; |
85 | int count = 0; |
87 | int count = 0; |
86 | aio_req req; |
88 | aio_req req, prv; |
87 | |
89 | |
|
|
90 | static int rl;//D |
|
|
91 | //printf ("%d ENTER\n", ++rl);//D |
|
|
92 | |
|
|
93 | pthread_mutex_lock (&reslock); |
|
|
94 | |
88 | { |
95 | { |
89 | /* read and signals sent by the worker threads */ |
96 | /* read any signals sent by the worker threads */ |
90 | char buf [32]; |
97 | char buf [32]; |
91 | while (read (respipe [0], buf, 32) > 0) |
98 | while (read (respipe [0], buf, 32) > 0) |
92 | ; |
99 | ; |
93 | } |
100 | } |
94 | |
101 | |
95 | for (;;) |
102 | req = ress; |
|
|
103 | ress = rese = 0; |
|
|
104 | |
|
|
105 | pthread_mutex_unlock (&reslock); |
|
|
106 | |
|
|
107 | while (req) |
96 | { |
108 | { |
97 | pthread_mutex_lock (&reslock); |
|
|
98 | |
|
|
99 | req = ress; |
|
|
100 | |
|
|
101 | if (ress) |
|
|
102 | { |
|
|
103 | ress = ress->next; |
|
|
104 | if (!ress) rese = 0; |
|
|
105 | } |
|
|
106 | |
|
|
107 | pthread_mutex_unlock (&reslock); |
|
|
108 | |
|
|
109 | if (!req) |
|
|
110 | break; |
|
|
111 | |
|
|
112 | nreqs--; |
109 | nreqs--; |
|
|
110 | //printf ("%d count %d %p->%p\n", rl, count, req, req->next);//D |
113 | |
111 | |
114 | if (req->type == REQ_QUIT) |
112 | if (req->type == REQ_QUIT) |
115 | started--; |
113 | started--; |
116 | else |
114 | else |
117 | { |
115 | { |
… | |
… | |
150 | |
148 | |
151 | PUSHMARK (SP); |
149 | PUSHMARK (SP); |
152 | XPUSHs (fh); |
150 | XPUSHs (fh); |
153 | } |
151 | } |
154 | |
152 | |
|
|
153 | if (SvOK (req->callback)) |
|
|
154 | { |
155 | PUTBACK; |
155 | PUTBACK; |
156 | call_sv (req->callback, G_VOID | G_EVAL); |
156 | call_sv (req->callback, G_VOID | G_EVAL); |
157 | SPAGAIN; |
157 | SPAGAIN; |
|
|
158 | } |
158 | |
159 | |
159 | if (req->callback) |
160 | if (req->callback) |
160 | SvREFCNT_dec (req->callback); |
161 | SvREFCNT_dec (req->callback); |
161 | |
162 | |
162 | errno = errorno; |
163 | errno = errorno; |
163 | count++; |
164 | count++; |
164 | } |
165 | } |
165 | |
166 | |
|
|
167 | prv = req; |
|
|
168 | req = req->next; |
166 | Safefree (req); |
169 | Safefree (prv); |
|
|
170 | |
|
|
171 | /* TODO: croak on errors? */ |
167 | } |
172 | } |
168 | |
173 | |
|
|
174 | //printf ("%d LEAVE %p %p\n", rl--, ress, rese);//D |
169 | return count; |
175 | return count; |
170 | } |
176 | } |
171 | |
177 | |
172 | static void *aio_proc(void *arg); |
178 | static void *aio_proc(void *arg); |
173 | |
179 | |
… | |
… | |
227 | |
233 | |
228 | send_req (req); |
234 | send_req (req); |
229 | } |
235 | } |
230 | |
236 | |
231 | static void |
237 | static void |
232 | read_write (pTHX_ |
|
|
233 | int dowrite, int fd, off_t offset, size_t length, |
238 | read_write (int dowrite, int fd, off_t offset, size_t length, |
234 | SV *data, STRLEN dataoffset, SV *callback) |
239 | SV *data, STRLEN dataoffset, SV *callback) |
235 | { |
240 | { |
236 | aio_req req; |
241 | aio_req req; |
237 | STRLEN svlen; |
242 | STRLEN svlen; |
238 | char *svptr = SvPV (data, svlen); |
243 | char *svptr = SvPV (data, svlen); |
… | |
… | |
309 | |
314 | |
310 | type = req->type; |
315 | type = req->type; |
311 | |
316 | |
312 | switch (type) |
317 | switch (type) |
313 | { |
318 | { |
314 | case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, req->offset); break; |
319 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
315 | case REQ_WRITE: req->result = pwrite64 (req->fd, req->dataptr, req->length, req->offset); break; |
320 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
316 | #if SYS_readahead |
321 | #if SYS_readahead |
317 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
322 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
318 | #else |
323 | #else |
319 | case REQ_READAHEAD: req->result = -1; errno = ENOSYS; break; |
324 | case REQ_READAHEAD: req->result = -1; errno = ENOSYS; break; |
320 | #endif |
325 | #endif |
… | |
… | |
364 | return 0; |
369 | return 0; |
365 | } |
370 | } |
366 | |
371 | |
367 | MODULE = IO::AIO PACKAGE = IO::AIO |
372 | MODULE = IO::AIO PACKAGE = IO::AIO |
368 | |
373 | |
|
|
374 | PROTOTYPES: ENABLE |
|
|
375 | |
369 | BOOT: |
376 | BOOT: |
370 | { |
377 | { |
371 | if (pipe (respipe)) |
378 | if (pipe (respipe)) |
372 | croak ("unable to initialize result pipe"); |
379 | croak ("unable to initialize result pipe"); |
373 | |
380 | |
… | |
… | |
400 | } |
407 | } |
401 | |
408 | |
402 | while (started > nthreads) |
409 | while (started > nthreads) |
403 | { |
410 | { |
404 | poll_wait (); |
411 | poll_wait (); |
405 | poll_cb (aTHX); |
412 | poll_cb (); |
406 | } |
413 | } |
407 | } |
414 | } |
408 | |
415 | |
409 | int |
416 | int |
410 | max_outstanding(nreqs) |
417 | max_outstanding(nreqs) |
… | |
… | |
413 | CODE: |
420 | CODE: |
414 | RETVAL = max_outstanding; |
421 | RETVAL = max_outstanding; |
415 | max_outstanding = nreqs; |
422 | max_outstanding = nreqs; |
416 | |
423 | |
417 | void |
424 | void |
418 | aio_open(pathname,flags,mode,callback) |
425 | aio_open(pathname,flags,mode,callback=&PL_sv_undef) |
419 | SV * pathname |
426 | SV * pathname |
420 | int flags |
427 | int flags |
421 | int mode |
428 | int mode |
422 | SV * callback |
429 | SV * callback |
423 | PROTOTYPE: $$$$ |
430 | PROTOTYPE: $$$;$ |
424 | CODE: |
431 | CODE: |
425 | { |
432 | { |
426 | aio_req req; |
433 | aio_req req; |
427 | |
434 | |
428 | Newz (0, req, 1, aio_cb); |
435 | Newz (0, req, 1, aio_cb); |
… | |
… | |
439 | |
446 | |
440 | send_req (req); |
447 | send_req (req); |
441 | } |
448 | } |
442 | |
449 | |
443 | void |
450 | void |
444 | aio_close(fh,callback) |
451 | aio_close(fh,callback=&PL_sv_undef) |
445 | InputStream fh |
452 | InputStream fh |
446 | SV * callback |
453 | SV * callback |
447 | PROTOTYPE: $$ |
454 | PROTOTYPE: $;$ |
448 | ALIAS: |
455 | ALIAS: |
449 | aio_close = REQ_CLOSE |
456 | aio_close = REQ_CLOSE |
450 | aio_fsync = REQ_FSYNC |
457 | aio_fsync = REQ_FSYNC |
451 | aio_fdatasync = REQ_FDATASYNC |
458 | aio_fdatasync = REQ_FDATASYNC |
452 | CODE: |
459 | CODE: |
… | |
… | |
464 | |
471 | |
465 | send_req (req); |
472 | send_req (req); |
466 | } |
473 | } |
467 | |
474 | |
468 | void |
475 | void |
469 | aio_read(fh,offset,length,data,dataoffset,callback) |
476 | aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
470 | InputStream fh |
477 | InputStream fh |
471 | UV offset |
478 | UV offset |
472 | IV length |
479 | IV length |
473 | SV * data |
480 | SV * data |
474 | IV dataoffset |
481 | IV dataoffset |
475 | SV * callback |
482 | SV * callback |
476 | PROTOTYPE: $$$$$$ |
483 | PROTOTYPE: $$$$$;$ |
477 | CODE: |
484 | CODE: |
478 | read_write (aTHX_ 0, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
485 | read_write (0, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
479 | |
486 | |
480 | void |
487 | void |
481 | aio_write(fh,offset,length,data,dataoffset,callback) |
488 | aio_write(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
482 | OutputStream fh |
489 | OutputStream fh |
483 | UV offset |
490 | UV offset |
484 | IV length |
491 | IV length |
485 | SV * data |
492 | SV * data |
486 | IV dataoffset |
493 | IV dataoffset |
487 | SV * callback |
494 | SV * callback |
488 | PROTOTYPE: $$$$$$ |
495 | PROTOTYPE: $$$$$;$ |
489 | CODE: |
496 | CODE: |
490 | read_write (aTHX_ 1, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
497 | read_write (1, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
491 | |
498 | |
492 | void |
499 | void |
493 | aio_readahead(fh,offset,length,callback) |
500 | aio_readahead(fh,offset,length,callback=&PL_sv_undef) |
494 | InputStream fh |
501 | InputStream fh |
495 | UV offset |
502 | UV offset |
496 | IV length |
503 | IV length |
497 | SV * callback |
504 | SV * callback |
498 | PROTOTYPE: $$$$ |
505 | PROTOTYPE: $$$;$ |
499 | CODE: |
506 | CODE: |
500 | { |
507 | { |
501 | aio_req req; |
508 | aio_req req; |
502 | |
509 | |
503 | if (length < 0) |
510 | if (length < 0) |
… | |
… | |
516 | |
523 | |
517 | send_req (req); |
524 | send_req (req); |
518 | } |
525 | } |
519 | |
526 | |
520 | void |
527 | void |
521 | aio_stat(fh_or_path,callback) |
528 | aio_stat(fh_or_path,callback=&PL_sv_undef) |
522 | SV * fh_or_path |
529 | SV * fh_or_path |
523 | SV * callback |
530 | SV * callback |
524 | PROTOTYPE: $$ |
|
|
525 | ALIAS: |
531 | ALIAS: |
|
|
532 | aio_stat = REQ_STAT |
526 | aio_lstat = 1 |
533 | aio_lstat = REQ_LSTAT |
527 | CODE: |
534 | CODE: |
528 | { |
535 | { |
529 | aio_req req; |
536 | aio_req req; |
530 | |
537 | |
531 | Newz (0, req, 1, aio_cb); |
538 | Newz (0, req, 1, aio_cb); |
… | |
… | |
538 | if (!req->statdata) |
545 | if (!req->statdata) |
539 | croak ("out of memory during aio_req->statdata allocation"); |
546 | croak ("out of memory during aio_req->statdata allocation"); |
540 | |
547 | |
541 | if (SvPOK (fh_or_path)) |
548 | if (SvPOK (fh_or_path)) |
542 | { |
549 | { |
543 | req->type = ix ? REQ_LSTAT : REQ_STAT; |
550 | req->type = ix; |
544 | req->data = newSVsv (fh_or_path); |
551 | req->data = newSVsv (fh_or_path); |
545 | req->dataptr = SvPV_nolen (req->data); |
552 | req->dataptr = SvPV_nolen (req->data); |
546 | } |
553 | } |
547 | else |
554 | else |
548 | { |
555 | { |
… | |
… | |
554 | |
561 | |
555 | send_req (req); |
562 | send_req (req); |
556 | } |
563 | } |
557 | |
564 | |
558 | void |
565 | void |
559 | aio_unlink(pathname,callback) |
566 | aio_unlink(pathname,callback=&PL_sv_undef) |
560 | SV * pathname |
567 | SV * pathname |
561 | SV * callback |
568 | SV * callback |
562 | PROTOTYPE: $$ |
|
|
563 | CODE: |
569 | CODE: |
564 | { |
570 | { |
565 | aio_req req; |
571 | aio_req req; |
566 | |
572 | |
567 | Newz (0, req, 1, aio_cb); |
573 | Newz (0, req, 1, aio_cb); |
… | |
… | |
575 | req->callback = SvREFCNT_inc (callback); |
581 | req->callback = SvREFCNT_inc (callback); |
576 | |
582 | |
577 | send_req (req); |
583 | send_req (req); |
578 | } |
584 | } |
579 | |
585 | |
|
|
586 | void |
|
|
587 | flush() |
|
|
588 | PROTOTYPE: |
|
|
589 | CODE: |
|
|
590 | while (nreqs) |
|
|
591 | { |
|
|
592 | poll_wait (); |
|
|
593 | poll_cb (); |
|
|
594 | } |
|
|
595 | |
|
|
596 | void |
|
|
597 | poll() |
|
|
598 | PROTOTYPE: |
|
|
599 | CODE: |
|
|
600 | if (nreqs) |
|
|
601 | { |
|
|
602 | poll_wait (); |
|
|
603 | poll_cb (); |
|
|
604 | } |
|
|
605 | |
580 | int |
606 | int |
581 | poll_fileno() |
607 | poll_fileno() |
582 | PROTOTYPE: |
608 | PROTOTYPE: |
583 | CODE: |
609 | CODE: |
584 | RETVAL = respipe [0]; |
610 | RETVAL = respipe [0]; |
… | |
… | |
587 | |
613 | |
588 | int |
614 | int |
589 | poll_cb(...) |
615 | poll_cb(...) |
590 | PROTOTYPE: |
616 | PROTOTYPE: |
591 | CODE: |
617 | CODE: |
592 | RETVAL = poll_cb (aTHX); |
618 | RETVAL = poll_cb (); |
593 | OUTPUT: |
619 | OUTPUT: |
594 | RETVAL |
620 | RETVAL |
595 | |
621 | |
596 | void |
622 | void |
597 | poll_wait() |
623 | poll_wait() |