|
|
1 | #define _XOPEN_SOURCE 500 |
|
|
2 | |
1 | #include "EXTERN.h" |
3 | #include "EXTERN.h" |
2 | #include "perl.h" |
4 | #include "perl.h" |
3 | #include "XSUB.h" |
5 | #include "XSUB.h" |
4 | |
6 | |
5 | #include <sys/types.h> |
7 | #include <sys/types.h> |
6 | #include <sys/stat.h> |
8 | #include <sys/stat.h> |
|
|
9 | |
7 | #include <unistd.h> |
10 | #include <unistd.h> |
8 | #include <fcntl.h> |
11 | #include <fcntl.h> |
9 | #include <signal.h> |
12 | #include <signal.h> |
10 | #include <sched.h> |
13 | #include <sched.h> |
11 | #include <endian.h> |
14 | #if __linux |
|
|
15 | #include <sys/syscall.h> |
|
|
16 | #endif |
12 | |
17 | |
13 | #include <pthread.h> |
18 | #include <pthread.h> |
14 | #include <sys/syscall.h> |
|
|
15 | |
19 | |
16 | 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 ;) */ |
17 | 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 ;) */ |
18 | 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 ;) */ |
19 | |
23 | |
… | |
… | |
50 | } aio_cb; |
54 | } aio_cb; |
51 | |
55 | |
52 | typedef aio_cb *aio_req; |
56 | typedef aio_cb *aio_req; |
53 | |
57 | |
54 | static int started; |
58 | static int started; |
55 | static int nreqs; |
59 | static volatile int nreqs; |
56 | static int max_outstanding = 1<<30; |
60 | static int max_outstanding = 1<<30; |
57 | static int respipe [2]; |
61 | static int respipe [2]; |
58 | |
62 | |
59 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
63 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
60 | static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; |
64 | static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; |
… | |
… | |
64 | static volatile aio_req ress, rese; /* queue start, queue end */ |
68 | static volatile aio_req ress, rese; /* queue start, queue end */ |
65 | |
69 | |
66 | static void |
70 | static void |
67 | poll_wait () |
71 | poll_wait () |
68 | { |
72 | { |
69 | if (!nreqs) |
73 | if (nreqs && !ress) |
70 | return; |
74 | { |
71 | |
|
|
72 | fd_set rfd; |
75 | fd_set rfd; |
73 | FD_ZERO(&rfd); |
76 | FD_ZERO(&rfd); |
74 | FD_SET(respipe [0], &rfd); |
77 | FD_SET(respipe [0], &rfd); |
75 | |
78 | |
76 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
79 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
|
|
80 | } |
77 | } |
81 | } |
78 | |
82 | |
79 | static int |
83 | static int |
80 | poll_cb () |
84 | poll_cb () |
81 | { |
85 | { |
82 | dSP; |
86 | dSP; |
83 | int count = 0; |
87 | int count = 0; |
84 | aio_req req; |
88 | aio_req req, prv; |
85 | |
89 | |
|
|
90 | static int rl;//D |
|
|
91 | //printf ("%d ENTER\n", ++rl);//D |
|
|
92 | |
|
|
93 | pthread_mutex_lock (&reslock); |
|
|
94 | |
86 | { |
95 | { |
87 | /* read and signals sent by the worker threads */ |
96 | /* read any signals sent by the worker threads */ |
88 | char buf [32]; |
97 | char buf [32]; |
89 | while (read (respipe [0], buf, 32) > 0) |
98 | while (read (respipe [0], buf, 32) > 0) |
90 | ; |
99 | ; |
91 | } |
100 | } |
92 | |
101 | |
93 | for (;;) |
102 | req = ress; |
|
|
103 | ress = rese = 0; |
|
|
104 | |
|
|
105 | pthread_mutex_unlock (&reslock); |
|
|
106 | |
|
|
107 | while (req) |
94 | { |
108 | { |
95 | pthread_mutex_lock (&reslock); |
|
|
96 | |
|
|
97 | req = ress; |
|
|
98 | |
|
|
99 | if (ress) |
|
|
100 | { |
|
|
101 | ress = ress->next; |
|
|
102 | if (!ress) rese = 0; |
|
|
103 | } |
|
|
104 | |
|
|
105 | pthread_mutex_unlock (&reslock); |
|
|
106 | |
|
|
107 | if (!req) |
|
|
108 | break; |
|
|
109 | |
|
|
110 | nreqs--; |
109 | nreqs--; |
|
|
110 | //printf ("%d count %d %p->%p\n", rl, count, req, req->next);//D |
111 | |
111 | |
112 | if (req->type == REQ_QUIT) |
112 | if (req->type == REQ_QUIT) |
113 | started--; |
113 | started--; |
114 | else |
114 | else |
115 | { |
115 | { |
… | |
… | |
148 | |
148 | |
149 | PUSHMARK (SP); |
149 | PUSHMARK (SP); |
150 | XPUSHs (fh); |
150 | XPUSHs (fh); |
151 | } |
151 | } |
152 | |
152 | |
|
|
153 | if (SvOK (req->callback)) |
|
|
154 | { |
153 | PUTBACK; |
155 | PUTBACK; |
154 | call_sv (req->callback, G_VOID | G_EVAL); |
156 | call_sv (req->callback, G_VOID | G_EVAL); |
155 | SPAGAIN; |
157 | SPAGAIN; |
|
|
158 | } |
156 | |
159 | |
157 | if (req->callback) |
160 | if (req->callback) |
158 | SvREFCNT_dec (req->callback); |
161 | SvREFCNT_dec (req->callback); |
159 | |
162 | |
160 | errno = errorno; |
163 | errno = errorno; |
161 | count++; |
164 | count++; |
162 | } |
165 | } |
163 | |
166 | |
|
|
167 | prv = req; |
|
|
168 | req = req->next; |
164 | Safefree (req); |
169 | Safefree (prv); |
|
|
170 | |
|
|
171 | /* TODO: croak on errors? */ |
165 | } |
172 | } |
166 | |
173 | |
|
|
174 | //printf ("%d LEAVE %p %p\n", rl--, ress, rese);//D |
167 | return count; |
175 | return count; |
168 | } |
176 | } |
169 | |
177 | |
170 | static void *aio_proc(void *arg); |
178 | static void *aio_proc(void *arg); |
171 | |
179 | |
… | |
… | |
306 | |
314 | |
307 | type = req->type; |
315 | type = req->type; |
308 | |
316 | |
309 | switch (type) |
317 | switch (type) |
310 | { |
318 | { |
311 | 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; |
312 | 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; |
313 | #if SYS_readahead |
321 | #if SYS_readahead |
314 | 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; |
315 | #else |
323 | #else |
316 | case REQ_READAHEAD: req->result = -1; errno = ENOSYS; break; |
324 | case REQ_READAHEAD: req->result = -1; errno = ENOSYS; break; |
317 | #endif |
325 | #endif |
… | |
… | |
361 | return 0; |
369 | return 0; |
362 | } |
370 | } |
363 | |
371 | |
364 | MODULE = IO::AIO PACKAGE = IO::AIO |
372 | MODULE = IO::AIO PACKAGE = IO::AIO |
365 | |
373 | |
|
|
374 | PROTOTYPES: ENABLE |
|
|
375 | |
366 | BOOT: |
376 | BOOT: |
367 | { |
377 | { |
368 | if (pipe (respipe)) |
378 | if (pipe (respipe)) |
369 | croak ("unable to initialize result pipe"); |
379 | croak ("unable to initialize result pipe"); |
370 | |
380 | |
… | |
… | |
410 | CODE: |
420 | CODE: |
411 | RETVAL = max_outstanding; |
421 | RETVAL = max_outstanding; |
412 | max_outstanding = nreqs; |
422 | max_outstanding = nreqs; |
413 | |
423 | |
414 | void |
424 | void |
415 | aio_open(pathname,flags,mode,callback) |
425 | aio_open(pathname,flags,mode,callback=&PL_sv_undef) |
416 | SV * pathname |
426 | SV * pathname |
417 | int flags |
427 | int flags |
418 | int mode |
428 | int mode |
419 | SV * callback |
429 | SV * callback |
420 | PROTOTYPE: $$$$ |
430 | PROTOTYPE: $$$;$ |
421 | CODE: |
431 | CODE: |
422 | { |
432 | { |
423 | aio_req req; |
433 | aio_req req; |
424 | |
434 | |
425 | Newz (0, req, 1, aio_cb); |
435 | Newz (0, req, 1, aio_cb); |
… | |
… | |
436 | |
446 | |
437 | send_req (req); |
447 | send_req (req); |
438 | } |
448 | } |
439 | |
449 | |
440 | void |
450 | void |
441 | aio_close(fh,callback) |
451 | aio_close(fh,callback=&PL_sv_undef) |
442 | InputStream fh |
452 | InputStream fh |
443 | SV * callback |
453 | SV * callback |
444 | PROTOTYPE: $$ |
454 | PROTOTYPE: $;$ |
445 | ALIAS: |
455 | ALIAS: |
446 | aio_close = REQ_CLOSE |
456 | aio_close = REQ_CLOSE |
447 | aio_fsync = REQ_FSYNC |
457 | aio_fsync = REQ_FSYNC |
448 | aio_fdatasync = REQ_FDATASYNC |
458 | aio_fdatasync = REQ_FDATASYNC |
449 | CODE: |
459 | CODE: |
… | |
… | |
461 | |
471 | |
462 | send_req (req); |
472 | send_req (req); |
463 | } |
473 | } |
464 | |
474 | |
465 | void |
475 | void |
466 | aio_read(fh,offset,length,data,dataoffset,callback) |
476 | aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
467 | InputStream fh |
477 | InputStream fh |
468 | UV offset |
478 | UV offset |
469 | IV length |
479 | IV length |
470 | SV * data |
480 | SV * data |
471 | IV dataoffset |
481 | IV dataoffset |
472 | SV * callback |
482 | SV * callback |
473 | PROTOTYPE: $$$$$$ |
483 | PROTOTYPE: $$$$$;$ |
474 | CODE: |
484 | CODE: |
475 | read_write (0, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
485 | read_write (0, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
476 | |
486 | |
477 | void |
487 | void |
478 | aio_write(fh,offset,length,data,dataoffset,callback) |
488 | aio_write(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
479 | OutputStream fh |
489 | OutputStream fh |
480 | UV offset |
490 | UV offset |
481 | IV length |
491 | IV length |
482 | SV * data |
492 | SV * data |
483 | IV dataoffset |
493 | IV dataoffset |
484 | SV * callback |
494 | SV * callback |
485 | PROTOTYPE: $$$$$$ |
495 | PROTOTYPE: $$$$$;$ |
486 | CODE: |
496 | CODE: |
487 | read_write (1, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
497 | read_write (1, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
488 | |
498 | |
489 | void |
499 | void |
490 | aio_readahead(fh,offset,length,callback) |
500 | aio_readahead(fh,offset,length,callback=&PL_sv_undef) |
491 | InputStream fh |
501 | InputStream fh |
492 | UV offset |
502 | UV offset |
493 | IV length |
503 | IV length |
494 | SV * callback |
504 | SV * callback |
495 | PROTOTYPE: $$$$ |
505 | PROTOTYPE: $$$;$ |
496 | CODE: |
506 | CODE: |
497 | { |
507 | { |
498 | aio_req req; |
508 | aio_req req; |
499 | |
509 | |
500 | if (length < 0) |
510 | if (length < 0) |
… | |
… | |
513 | |
523 | |
514 | send_req (req); |
524 | send_req (req); |
515 | } |
525 | } |
516 | |
526 | |
517 | void |
527 | void |
518 | aio_stat(fh_or_path,callback) |
528 | aio_stat(fh_or_path,callback=&PL_sv_undef) |
519 | SV * fh_or_path |
529 | SV * fh_or_path |
520 | SV * callback |
530 | SV * callback |
521 | PROTOTYPE: $$ |
|
|
522 | ALIAS: |
531 | ALIAS: |
|
|
532 | aio_stat = REQ_STAT |
523 | aio_lstat = 1 |
533 | aio_lstat = REQ_LSTAT |
524 | CODE: |
534 | CODE: |
525 | { |
535 | { |
526 | aio_req req; |
536 | aio_req req; |
527 | |
537 | |
528 | Newz (0, req, 1, aio_cb); |
538 | Newz (0, req, 1, aio_cb); |
… | |
… | |
535 | if (!req->statdata) |
545 | if (!req->statdata) |
536 | croak ("out of memory during aio_req->statdata allocation"); |
546 | croak ("out of memory during aio_req->statdata allocation"); |
537 | |
547 | |
538 | if (SvPOK (fh_or_path)) |
548 | if (SvPOK (fh_or_path)) |
539 | { |
549 | { |
540 | req->type = ix ? REQ_LSTAT : REQ_STAT; |
550 | req->type = ix; |
541 | req->data = newSVsv (fh_or_path); |
551 | req->data = newSVsv (fh_or_path); |
542 | req->dataptr = SvPV_nolen (req->data); |
552 | req->dataptr = SvPV_nolen (req->data); |
543 | } |
553 | } |
544 | else |
554 | else |
545 | { |
555 | { |
… | |
… | |
551 | |
561 | |
552 | send_req (req); |
562 | send_req (req); |
553 | } |
563 | } |
554 | |
564 | |
555 | void |
565 | void |
556 | aio_unlink(pathname,callback) |
566 | aio_unlink(pathname,callback=&PL_sv_undef) |
557 | SV * pathname |
567 | SV * pathname |
558 | SV * callback |
568 | SV * callback |
559 | PROTOTYPE: $$ |
|
|
560 | CODE: |
569 | CODE: |
561 | { |
570 | { |
562 | aio_req req; |
571 | aio_req req; |
563 | |
572 | |
564 | Newz (0, req, 1, aio_cb); |
573 | Newz (0, req, 1, aio_cb); |