ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/IO-AIO/AIO.xs
(Generate patch)

Comparing IO-AIO/AIO.xs (file contents):
Revision 1.4 by root, Sun Jul 10 20:57:00 2005 UTC vs.
Revision 1.15 by root, Sat Jul 23 18:19:56 2005 UTC

1#define PERL_NO_GET_CONTEXT
2
3#include "EXTERN.h" 1#include "EXTERN.h"
4#include "perl.h" 2#include "perl.h"
5#include "XSUB.h" 3#include "XSUB.h"
6 4
7#include <sys/types.h> 5#include <sys/types.h>
8#include <sys/stat.h> 6#include <sys/stat.h>
7
9#include <unistd.h> 8#include <unistd.h>
10#include <fcntl.h> 9#include <fcntl.h>
11#include <signal.h> 10#include <signal.h>
12#include <sched.h> 11#include <sched.h>
13#include <endian.h> 12#if __linux
13#include <sys/syscall.h>
14#endif
14 15
15#include <pthread.h> 16#include <pthread.h>
16#include <sys/syscall.h>
17 17
18typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ 18typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */
19typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ 19typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */
20typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ 20typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */
21 21
42 off_t offset; 42 off_t offset;
43 size_t length; 43 size_t length;
44 ssize_t result; 44 ssize_t result;
45 mode_t mode; /* open */ 45 mode_t mode; /* open */
46 int errorno; 46 int errorno;
47 SV *data, *callback; 47 SV *data, *callback, *fh;
48 void *dataptr; 48 void *dataptr;
49 STRLEN dataoffset; 49 STRLEN dataoffset;
50 50
51 Stat_t *statdata; 51 Stat_t *statdata;
52} aio_cb; 52} aio_cb;
53 53
54typedef aio_cb *aio_req; 54typedef aio_cb *aio_req;
55 55
56static int started; 56static int started;
57static int nreqs; 57static volatile int nreqs;
58static int max_outstanding = 1<<30; 58static int max_outstanding = 1<<30;
59static int respipe [2]; 59static int respipe [2];
60 60
61static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; 61static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER;
62static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; 62static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER;
66static volatile aio_req ress, rese; /* queue start, queue end */ 66static volatile aio_req ress, rese; /* queue start, queue end */
67 67
68static void 68static void
69poll_wait () 69poll_wait ()
70{ 70{
71 if (!nreqs) 71 if (nreqs && !ress)
72 return; 72 {
73
74 fd_set rfd; 73 fd_set rfd;
75 FD_ZERO(&rfd); 74 FD_ZERO(&rfd);
76 FD_SET(respipe [0], &rfd); 75 FD_SET(respipe [0], &rfd);
77 76
78 select (respipe [0] + 1, &rfd, 0, 0, 0); 77 select (respipe [0] + 1, &rfd, 0, 0, 0);
78 }
79} 79}
80 80
81static int 81static int
82poll_cb (pTHX) 82poll_cb ()
83{ 83{
84 dSP; 84 dSP;
85 int count = 0; 85 int count = 0;
86 aio_req req; 86 aio_req req, prv;
87 87
88 pthread_mutex_lock (&reslock);
89
88 { 90 {
89 /* read and signals sent by the worker threads */ 91 /* read any signals sent by the worker threads */
90 char buf [32]; 92 char buf [32];
91 while (read (respipe [0], buf, 32) > 0) 93 while (read (respipe [0], buf, 32) > 0)
92 ; 94 ;
93 } 95 }
94 96
95 for (;;) 97 req = ress;
98 ress = rese = 0;
99
100 pthread_mutex_unlock (&reslock);
101
102 while (req)
96 { 103 {
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--; 104 nreqs--;
113 105
114 if (req->type == REQ_QUIT) 106 if (req->type == REQ_QUIT)
115 started--; 107 started--;
116 else 108 else
123 + req->result > 0 ? req->result : 0); 115 + req->result > 0 ? req->result : 0);
124 116
125 if (req->data) 117 if (req->data)
126 SvREFCNT_dec (req->data); 118 SvREFCNT_dec (req->data);
127 119
120 if (req->fh)
121 SvREFCNT_dec (req->fh);
122
128 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) 123 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT)
129 { 124 {
130 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; 125 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
131 PL_laststatval = req->result; 126 PL_laststatval = req->result;
132 PL_statcache = *(req->statdata); 127 PL_statcache = *(req->statdata);
133 128
134 Safefree (req->statdata); 129 Safefree (req->statdata);
135 } 130 }
136 131
132 ENTER;
137 PUSHMARK (SP); 133 PUSHMARK (SP);
138 XPUSHs (sv_2mortal (newSViv (req->result))); 134 XPUSHs (sv_2mortal (newSViv (req->result)));
139 135
140 if (req->type == REQ_OPEN) 136 if (req->type == REQ_OPEN)
141 { 137 {
144 140
145 PUTBACK; 141 PUTBACK;
146 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); 142 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL);
147 SPAGAIN; 143 SPAGAIN;
148 144
149 fh = POPs; 145 fh = SvREFCNT_inc (POPs);
150 146
151 PUSHMARK (SP); 147 PUSHMARK (SP);
152 XPUSHs (fh); 148 XPUSHs (sv_2mortal (fh));
153 } 149 }
154 150
151 if (SvOK (req->callback))
152 {
155 PUTBACK; 153 PUTBACK;
156 call_sv (req->callback, G_VOID | G_EVAL); 154 call_sv (req->callback, G_VOID | G_EVAL);
157 SPAGAIN; 155 SPAGAIN;
156 }
157
158 LEAVE;
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
169 return count; 174 return count;
170} 175}
171 176
226 req->type = REQ_QUIT; 231 req->type = REQ_QUIT;
227 232
228 send_req (req); 233 send_req (req);
229} 234}
230 235
231static void
232read_write (pTHX_
233 int dowrite, int fd, off_t offset, size_t length,
234 SV *data, STRLEN dataoffset, SV *callback)
235{
236 aio_req req;
237 STRLEN svlen;
238 char *svptr = SvPV (data, svlen);
239
240 SvUPGRADE (data, SVt_PV);
241 SvPOK_on (data);
242
243 if (dataoffset < 0)
244 dataoffset += svlen;
245
246 if (dataoffset < 0 || dataoffset > svlen)
247 croak ("data offset outside of string");
248
249 if (dowrite)
250 {
251 /* write: check length and adjust. */
252 if (length < 0 || length + dataoffset > svlen)
253 length = svlen - dataoffset;
254 }
255 else
256 {
257 /* read: grow scalar as necessary */
258 svptr = SvGROW (data, length + dataoffset);
259 }
260
261 if (length < 0)
262 croak ("length must not be negative");
263
264 Newz (0, req, 1, aio_cb);
265
266 if (!req)
267 croak ("out of memory during aio_req allocation");
268
269 req->type = dowrite ? REQ_WRITE : REQ_READ;
270 req->fd = fd;
271 req->offset = offset;
272 req->length = length;
273 req->data = SvREFCNT_inc (data);
274 req->dataptr = (char *)svptr + dataoffset;
275 req->callback = SvREFCNT_inc (callback);
276
277 send_req (req);
278}
279
280static void * 236static void *
281aio_proc (void *thr_arg) 237aio_proc (void *thr_arg)
282{ 238{
283 aio_req req; 239 aio_req req;
284 int type; 240 int type;
309 265
310 type = req->type; 266 type = req->type;
311 267
312 switch (type) 268 switch (type)
313 { 269 {
314 case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, req->offset); break; 270 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; 271 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break;
316#if SYS_readahead 272#if SYS_readahead
317 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; 273 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break;
318#else 274#else
319 case REQ_READAHEAD: req->result = -1; errno = ENOSYS; break; 275 case REQ_READAHEAD: req->result = -1; errno = ENOSYS; break;
320#endif 276#endif
364 return 0; 320 return 0;
365} 321}
366 322
367MODULE = IO::AIO PACKAGE = IO::AIO 323MODULE = IO::AIO PACKAGE = IO::AIO
368 324
325PROTOTYPES: ENABLE
326
369BOOT: 327BOOT:
370{ 328{
371 if (pipe (respipe)) 329 if (pipe (respipe))
372 croak ("unable to initialize result pipe"); 330 croak ("unable to initialize result pipe");
373 331
400 } 358 }
401 359
402 while (started > nthreads) 360 while (started > nthreads)
403 { 361 {
404 poll_wait (); 362 poll_wait ();
405 poll_cb (aTHX); 363 poll_cb ();
406 } 364 }
407} 365}
408 366
409int 367int
410max_outstanding(nreqs) 368max_outstanding(nreqs)
413 CODE: 371 CODE:
414 RETVAL = max_outstanding; 372 RETVAL = max_outstanding;
415 max_outstanding = nreqs; 373 max_outstanding = nreqs;
416 374
417void 375void
418aio_open(pathname,flags,mode,callback) 376aio_open(pathname,flags,mode,callback=&PL_sv_undef)
419 SV * pathname 377 SV * pathname
420 int flags 378 int flags
421 int mode 379 int mode
422 SV * callback 380 SV * callback
423 PROTOTYPE: $$$$ 381 PROTOTYPE: $$$;$
424 CODE: 382 CODE:
425{ 383{
426 aio_req req; 384 aio_req req;
427 385
428 Newz (0, req, 1, aio_cb); 386 Newz (0, req, 1, aio_cb);
439 397
440 send_req (req); 398 send_req (req);
441} 399}
442 400
443void 401void
444aio_close(fh,callback) 402aio_close(fh,callback=&PL_sv_undef)
445 InputStream fh 403 SV * fh
446 SV * callback 404 SV * callback
447 PROTOTYPE: $$ 405 PROTOTYPE: $;$
448 ALIAS: 406 ALIAS:
449 aio_close = REQ_CLOSE 407 aio_close = REQ_CLOSE
450 aio_fsync = REQ_FSYNC 408 aio_fsync = REQ_FSYNC
451 aio_fdatasync = REQ_FDATASYNC 409 aio_fdatasync = REQ_FDATASYNC
452 CODE: 410 CODE:
457 415
458 if (!req) 416 if (!req)
459 croak ("out of memory during aio_req allocation"); 417 croak ("out of memory during aio_req allocation");
460 418
461 req->type = ix; 419 req->type = ix;
420 req->fh = newSVsv (fh);
462 req->fd = PerlIO_fileno (fh); 421 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh)));
463 req->callback = SvREFCNT_inc (callback); 422 req->callback = SvREFCNT_inc (callback);
464 423
465 send_req (req); 424 send_req (req);
466} 425}
467 426
468void 427void
469aio_read(fh,offset,length,data,dataoffset,callback) 428aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef)
470 InputStream fh 429 SV * fh
471 UV offset 430 UV offset
472 IV length 431 IV length
473 SV * data 432 SV * data
474 IV dataoffset 433 IV dataoffset
475 SV * callback 434 SV * callback
435 ALIAS:
436 aio_read = REQ_READ
437 aio_write = REQ_WRITE
476 PROTOTYPE: $$$$$$ 438 PROTOTYPE: $$$$$;$
477 CODE: 439 CODE:
478 read_write (aTHX_ 0, PerlIO_fileno (fh), offset, length, data, dataoffset, callback);
479
480void
481aio_write(fh,offset,length,data,dataoffset,callback)
482 OutputStream fh
483 UV offset
484 IV length
485 SV * data
486 IV dataoffset
487 SV * callback
488 PROTOTYPE: $$$$$$
489 CODE:
490 read_write (aTHX_ 1, PerlIO_fileno (fh), offset, length, data, dataoffset, callback);
491
492void
493aio_readahead(fh,offset,length,callback)
494 InputStream fh
495 UV offset
496 IV length
497 SV * callback
498 PROTOTYPE: $$$$
499 CODE:
500{ 440{
501 aio_req req; 441 aio_req req;
442 STRLEN svlen;
443 char *svptr = SvPV (data, svlen);
444
445 SvUPGRADE (data, SVt_PV);
446 SvPOK_on (data);
447
448 if (dataoffset < 0)
449 dataoffset += svlen;
450
451 if (dataoffset < 0 || dataoffset > svlen)
452 croak ("data offset outside of string");
453
454 if (ix == REQ_WRITE)
455 {
456 /* write: check length and adjust. */
457 if (length < 0 || length + dataoffset > svlen)
458 length = svlen - dataoffset;
459 }
460 else
461 {
462 /* read: grow scalar as necessary */
463 svptr = SvGROW (data, length + dataoffset);
464 }
502 465
503 if (length < 0) 466 if (length < 0)
504 croak ("length must not be negative"); 467 croak ("length must not be negative");
505 468
506 Newz (0, req, 1, aio_cb); 469 Newz (0, req, 1, aio_cb);
507 470
508 if (!req) 471 if (!req)
509 croak ("out of memory during aio_req allocation"); 472 croak ("out of memory during aio_req allocation");
510 473
474 req->type = ix;
475 req->fh = newSVsv (fh);
476 req->fd = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh))
477 : IoOFP (sv_2io (fh)));
478 req->offset = offset;
479 req->length = length;
480 req->data = SvREFCNT_inc (data);
481 req->dataptr = (char *)svptr + dataoffset;
482 req->callback = SvREFCNT_inc (callback);
483
484 send_req (req);
485}
486
487void
488aio_readahead(fh,offset,length,callback=&PL_sv_undef)
489 SV * fh
490 UV offset
491 IV length
492 SV * callback
493 PROTOTYPE: $$$;$
494 CODE:
495{
496 aio_req req;
497
498 if (length < 0)
499 croak ("length must not be negative");
500
501 Newz (0, req, 1, aio_cb);
502
503 if (!req)
504 croak ("out of memory during aio_req allocation");
505
511 req->type = REQ_READAHEAD; 506 req->type = REQ_READAHEAD;
507 req->fh = newSVsv (fh);
512 req->fd = PerlIO_fileno (fh); 508 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh)));
513 req->offset = offset; 509 req->offset = offset;
514 req->length = length; 510 req->length = length;
515 req->callback = SvREFCNT_inc (callback); 511 req->callback = SvREFCNT_inc (callback);
516 512
517 send_req (req); 513 send_req (req);
518} 514}
519 515
520void 516void
521aio_stat(fh_or_path,callback) 517aio_stat(fh_or_path,callback=&PL_sv_undef)
522 SV * fh_or_path 518 SV * fh_or_path
523 SV * callback 519 SV * callback
524 PROTOTYPE: $$
525 ALIAS: 520 ALIAS:
521 aio_stat = REQ_STAT
526 aio_lstat = 1 522 aio_lstat = REQ_LSTAT
527 CODE: 523 CODE:
528{ 524{
529 aio_req req; 525 aio_req req;
530 526
531 Newz (0, req, 1, aio_cb); 527 Newz (0, req, 1, aio_cb);
538 if (!req->statdata) 534 if (!req->statdata)
539 croak ("out of memory during aio_req->statdata allocation"); 535 croak ("out of memory during aio_req->statdata allocation");
540 536
541 if (SvPOK (fh_or_path)) 537 if (SvPOK (fh_or_path))
542 { 538 {
543 req->type = ix ? REQ_LSTAT : REQ_STAT; 539 req->type = ix;
544 req->data = newSVsv (fh_or_path); 540 req->data = newSVsv (fh_or_path);
545 req->dataptr = SvPV_nolen (req->data); 541 req->dataptr = SvPV_nolen (req->data);
546 } 542 }
547 else 543 else
548 { 544 {
549 req->type = REQ_FSTAT; 545 req->type = REQ_FSTAT;
546 req->fh = newSVsv (fh_or_path);
550 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); 547 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
551 } 548 }
552 549
553 req->callback = SvREFCNT_inc (callback); 550 req->callback = SvREFCNT_inc (callback);
554 551
555 send_req (req); 552 send_req (req);
556} 553}
557 554
558void 555void
559aio_unlink(pathname,callback) 556aio_unlink(pathname,callback=&PL_sv_undef)
560 SV * pathname 557 SV * pathname
561 SV * callback 558 SV * callback
562 PROTOTYPE: $$
563 CODE: 559 CODE:
564{ 560{
565 aio_req req; 561 aio_req req;
566 562
567 Newz (0, req, 1, aio_cb); 563 Newz (0, req, 1, aio_cb);
575 req->callback = SvREFCNT_inc (callback); 571 req->callback = SvREFCNT_inc (callback);
576 572
577 send_req (req); 573 send_req (req);
578} 574}
579 575
576void
577flush()
578 PROTOTYPE:
579 CODE:
580 while (nreqs)
581 {
582 poll_wait ();
583 poll_cb ();
584 }
585
586void
587poll()
588 PROTOTYPE:
589 CODE:
590 if (nreqs)
591 {
592 poll_wait ();
593 poll_cb ();
594 }
595
580int 596int
581poll_fileno() 597poll_fileno()
582 PROTOTYPE: 598 PROTOTYPE:
583 CODE: 599 CODE:
584 RETVAL = respipe [0]; 600 RETVAL = respipe [0];
587 603
588int 604int
589poll_cb(...) 605poll_cb(...)
590 PROTOTYPE: 606 PROTOTYPE:
591 CODE: 607 CODE:
592 RETVAL = poll_cb (aTHX); 608 RETVAL = poll_cb ();
593 OUTPUT: 609 OUTPUT:
594 RETVAL 610 RETVAL
595 611
596void 612void
597poll_wait() 613poll_wait()

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines