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.18 by root, Sun Jul 31 19:00:31 2005 UTC vs.
Revision 1.26 by root, Wed Aug 17 03:52:20 2005 UTC

1#define _REENTRANT 1
2#include <errno.h>
3
1#include "EXTERN.h" 4#include "EXTERN.h"
2#include "perl.h" 5#include "perl.h"
3#include "XSUB.h" 6#include "XSUB.h"
4 7
5#include "autoconf/config.h" 8#include "autoconf/config.h"
9 12
10#include <unistd.h> 13#include <unistd.h>
11#include <fcntl.h> 14#include <fcntl.h>
12#include <signal.h> 15#include <signal.h>
13#include <sched.h> 16#include <sched.h>
14
15#ifndef _REENTRANT
16# define _REENTRANT 1
17#endif
18#include <errno.h>
19 17
20#include <pthread.h> 18#include <pthread.h>
21 19
22typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ 20typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */
23typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ 21typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */
31 29
32enum { 30enum {
33 REQ_QUIT, 31 REQ_QUIT,
34 REQ_OPEN, REQ_CLOSE, 32 REQ_OPEN, REQ_CLOSE,
35 REQ_READ, REQ_WRITE, REQ_READAHEAD, 33 REQ_READ, REQ_WRITE, REQ_READAHEAD,
36 REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_UNLINK, 34 REQ_STAT, REQ_LSTAT, REQ_FSTAT,
37 REQ_FSYNC, REQ_FDATASYNC, 35 REQ_FSYNC, REQ_FDATASYNC,
36 REQ_UNLINK, REQ_RMDIR,
37 REQ_SYMLINK,
38}; 38};
39 39
40typedef struct aio_cb { 40typedef struct aio_cb {
41 struct aio_cb *volatile next; 41 struct aio_cb *volatile next;
42 42
47 size_t length; 47 size_t length;
48 ssize_t result; 48 ssize_t result;
49 mode_t mode; /* open */ 49 mode_t mode; /* open */
50 int errorno; 50 int errorno;
51 SV *data, *callback, *fh; 51 SV *data, *callback, *fh;
52 void *dataptr; 52 void *dataptr, *data2ptr;
53 STRLEN dataoffset; 53 STRLEN dataoffset;
54 54
55 Stat_t *statdata; 55 Stat_t *statdata;
56} aio_cb; 56} aio_cb;
57 57
62static int max_outstanding = 1<<30; 62static int max_outstanding = 1<<30;
63static int respipe [2]; 63static int respipe [2];
64 64
65static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; 65static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER;
66static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; 66static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER;
67static pthread_mutex_t frklock = PTHREAD_MUTEX_INITIALIZER;
67static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; 68static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER;
68 69
69static volatile aio_req reqs, reqe; /* queue start, queue end */ 70static volatile aio_req reqs, reqe; /* queue start, queue end */
70static volatile aio_req ress, rese; /* queue start, queue end */ 71static volatile aio_req ress, rese; /* queue start, queue end */
72
73static void free_req (aio_req req)
74{
75 if (req->data)
76 SvREFCNT_dec (req->data);
77
78 if (req->fh)
79 SvREFCNT_dec (req->fh);
80
81 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT)
82 Safefree (req->statdata);
83
84 if (req->callback)
85 SvREFCNT_dec (req->callback);
86
87 Safefree (req);
88}
71 89
72static void 90static void
73poll_wait () 91poll_wait ()
74{ 92{
75 if (nreqs && !ress) 93 if (nreqs && !ress)
85static int 103static int
86poll_cb () 104poll_cb ()
87{ 105{
88 dSP; 106 dSP;
89 int count = 0; 107 int count = 0;
108 int do_croak = 0;
90 aio_req req, prv; 109 aio_req req, prv;
91 110
92 pthread_mutex_lock (&reslock); 111 pthread_mutex_lock (&reslock);
93 112
94 { 113 {
95 /* read any signals sent by the worker threads */ 114 /* read any signals sent by the worker threads */
96 char buf [32]; 115 char buf [32];
97 while (read (respipe [0], buf, 32) > 0) 116 while (read (respipe [0], buf, 32) == 32)
98 ; 117 ;
99 } 118 }
100 119
101 req = ress; 120 req = ress;
102 ress = rese = 0; 121 ress = rese = 0;
116 135
117 if (req->type == REQ_READ) 136 if (req->type == REQ_READ)
118 SvCUR_set (req->data, req->dataoffset 137 SvCUR_set (req->data, req->dataoffset
119 + req->result > 0 ? req->result : 0); 138 + req->result > 0 ? req->result : 0);
120 139
121 if (req->data)
122 SvREFCNT_dec (req->data);
123
124 if (req->fh)
125 SvREFCNT_dec (req->fh);
126
127 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) 140 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT)
128 { 141 {
129 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; 142 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
130 PL_laststatval = req->result; 143 PL_laststatval = req->result;
131 PL_statcache = *(req->statdata); 144 PL_statcache = *(req->statdata);
132
133 Safefree (req->statdata);
134 } 145 }
135 146
136 ENTER; 147 ENTER;
137 PUSHMARK (SP); 148 PUSHMARK (SP);
138 XPUSHs (sv_2mortal (newSViv (req->result))); 149 XPUSHs (sv_2mortal (newSViv (req->result)));
158 call_sv (req->callback, G_VOID | G_EVAL); 169 call_sv (req->callback, G_VOID | G_EVAL);
159 SPAGAIN; 170 SPAGAIN;
160 } 171 }
161 172
162 LEAVE; 173 LEAVE;
174
175 do_croak = SvTRUE (ERRSV);
163 176
164 if (req->callback)
165 SvREFCNT_dec (req->callback);
166
167 errno = errorno; 177 errno = errorno;
168 count++; 178 count++;
169 } 179 }
170 180
171 prv = req; 181 prv = req;
172 req = req->next; 182 req = req->next;
173 Safefree (prv); 183 free_req (prv);
174 184
175 /* TODO: croak on errors? */ 185 if (do_croak)
186 croak (0);
176 } 187 }
177 188
178 return count; 189 return count;
179} 190}
180 191
229 240
230static void 241static void
231end_thread (void) 242end_thread (void)
232{ 243{
233 aio_req req; 244 aio_req req;
234 New (0, req, 1, aio_cb); 245 Newz (0, req, 1, aio_cb);
235 req->type = REQ_QUIT; 246 req->type = REQ_QUIT;
236 247
237 send_req (req); 248 send_req (req);
238} 249}
239 250
251
252static void min_parallel (int nthreads)
253{
254 while (nthreads > started)
255 start_thread ();
256}
257
258static void max_parallel (int nthreads)
259{
260 int cur = started;
261 while (cur > nthreads)
262 {
263 end_thread ();
264 cur--;
265 }
266
267 while (started > nthreads)
268 {
269 poll_wait ();
270 poll_cb ();
271 }
272}
273
274static void create_pipe ()
275{
276 if (pipe (respipe))
277 croak ("unable to initialize result pipe");
278
279 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK))
280 croak ("cannot set result pipe to nonblocking mode");
281
282 if (fcntl (respipe [1], F_SETFL, O_NONBLOCK))
283 croak ("cannot set result pipe to nonblocking mode");
284}
285
286static void atfork_prepare (void)
287{
288 pthread_mutex_lock (&frklock);
289 pthread_mutex_lock (&reqlock);
290 pthread_mutex_lock (&reslock);
291}
292
293static void atfork_parent (void)
294{
295 pthread_mutex_unlock (&reslock);
296 pthread_mutex_unlock (&reqlock);
297 pthread_mutex_unlock (&frklock);
298}
299
300static void atfork_child (void)
301{
302 int restart = started;
303 started = 0;
304
305 while (reqs)
306 {
307 free_req (reqs);
308 reqs = reqs->next;
309 }
310
311 reqs = reqe = 0;
312
313 while (ress)
314 {
315 free_req (ress);
316 ress = ress->next;
317 }
318
319 ress = rese = 0;
320
321 close (respipe [0]);
322 close (respipe [1]);
323
324 create_pipe ();
325
326 atfork_parent ();
327
328 min_parallel (restart);
329}
330
331/*****************************************************************************/
240/* work around various missing functions */ 332/* work around various missing functions */
241 333
242#if !HAVE_PREADWRITE 334#if !HAVE_PREADWRITE
243# define pread aio_pread 335# define pread aio_pread
244# define pwrite aio_pwrite 336# define pwrite aio_pwrite
306 398
307 errno = 0; 399 errno = 0;
308} 400}
309#endif 401#endif
310 402
403/*****************************************************************************/
404
311static void * 405static void *
312aio_proc (void *thr_arg) 406aio_proc (void *thr_arg)
313{ 407{
314 aio_req req; 408 aio_req req;
315 int type; 409 int type;
352 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; 446 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break;
353 447
354 case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; 448 case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break;
355 case REQ_CLOSE: req->result = close (req->fd); break; 449 case REQ_CLOSE: req->result = close (req->fd); break;
356 case REQ_UNLINK: req->result = unlink (req->dataptr); break; 450 case REQ_UNLINK: req->result = unlink (req->dataptr); break;
451 case REQ_RMDIR: req->result = rmdir (req->dataptr); break;
452 case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break;
357 453
358 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; 454 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break;
359 case REQ_FSYNC: req->result = fsync (req->fd); break; 455 case REQ_FSYNC: req->result = fsync (req->fd); break;
360 456
361 case REQ_QUIT: 457 case REQ_QUIT:
390 while (type != REQ_QUIT); 486 while (type != REQ_QUIT);
391 487
392 return 0; 488 return 0;
393} 489}
394 490
491#define dREQ \
492 aio_req req; \
493 \
494 if (SvOK (callback) && !SvROK (callback)) \
495 croak ("clalback must be undef or of reference type"); \
496 \
497 Newz (0, req, 1, aio_cb); \
498 if (!req) \
499 croak ("out of memory during aio_req allocation"); \
500 \
501 req->callback = SvREFCNT_inc (callback);
502
395MODULE = IO::AIO PACKAGE = IO::AIO 503MODULE = IO::AIO PACKAGE = IO::AIO
396 504
397PROTOTYPES: ENABLE 505PROTOTYPES: ENABLE
398 506
399BOOT: 507BOOT:
400{ 508{
401 if (pipe (respipe)) 509 create_pipe ();
402 croak ("unable to initialize result pipe"); 510 pthread_atfork (atfork_prepare, atfork_parent, atfork_child);
403
404 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK))
405 croak ("cannot set result pipe to nonblocking mode");
406
407 if (fcntl (respipe [1], F_SETFL, O_NONBLOCK))
408 croak ("cannot set result pipe to nonblocking mode");
409} 511}
410 512
411void 513void
412min_parallel(nthreads) 514min_parallel(nthreads)
413 int nthreads 515 int nthreads
414 PROTOTYPE: $ 516 PROTOTYPE: $
415 CODE:
416 while (nthreads > started)
417 start_thread ();
418 517
419void 518void
420max_parallel(nthreads) 519max_parallel(nthreads)
421 int nthreads 520 int nthreads
422 PROTOTYPE: $ 521 PROTOTYPE: $
423 CODE:
424{
425 int cur = started;
426 while (cur > nthreads)
427 {
428 end_thread ();
429 cur--;
430 }
431
432 while (started > nthreads)
433 {
434 poll_wait ();
435 poll_cb ();
436 }
437}
438 522
439int 523int
440max_outstanding(nreqs) 524max_outstanding(nreqs)
441 int nreqs 525 int nreqs
442 PROTOTYPE: $ 526 PROTOTYPE: $
451 int mode 535 int mode
452 SV * callback 536 SV * callback
453 PROTOTYPE: $$$;$ 537 PROTOTYPE: $$$;$
454 CODE: 538 CODE:
455{ 539{
456 aio_req req; 540 dREQ;
457
458 Newz (0, req, 1, aio_cb);
459
460 if (!req)
461 croak ("out of memory during aio_req allocation");
462 541
463 req->type = REQ_OPEN; 542 req->type = REQ_OPEN;
464 req->data = newSVsv (pathname); 543 req->data = newSVsv (pathname);
465 req->dataptr = SvPV_nolen (req->data); 544 req->dataptr = SvPVbyte_nolen (req->data);
466 req->fd = flags; 545 req->fd = flags;
467 req->mode = mode; 546 req->mode = mode;
468 req->callback = SvREFCNT_inc (callback);
469 547
470 send_req (req); 548 send_req (req);
471} 549}
472 550
473void 551void
479 aio_close = REQ_CLOSE 557 aio_close = REQ_CLOSE
480 aio_fsync = REQ_FSYNC 558 aio_fsync = REQ_FSYNC
481 aio_fdatasync = REQ_FDATASYNC 559 aio_fdatasync = REQ_FDATASYNC
482 CODE: 560 CODE:
483{ 561{
484 aio_req req; 562 dREQ;
485
486 Newz (0, req, 1, aio_cb);
487
488 if (!req)
489 croak ("out of memory during aio_req allocation");
490 563
491 req->type = ix; 564 req->type = ix;
492 req->fh = newSVsv (fh); 565 req->fh = newSVsv (fh);
493 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); 566 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh)));
494 req->callback = SvREFCNT_inc (callback);
495 567
496 send_req (req); 568 send_req (req);
497} 569}
498 570
499void 571void
510 PROTOTYPE: $$$$$;$ 582 PROTOTYPE: $$$$$;$
511 CODE: 583 CODE:
512{ 584{
513 aio_req req; 585 aio_req req;
514 STRLEN svlen; 586 STRLEN svlen;
515 char *svptr = SvPV (data, svlen); 587 char *svptr = SvPVbyte (data, svlen);
516 588
517 SvUPGRADE (data, SVt_PV); 589 SvUPGRADE (data, SVt_PV);
518 SvPOK_on (data); 590 SvPOK_on (data);
519 591
520 if (dataoffset < 0) 592 if (dataoffset < 0)
536 } 608 }
537 609
538 if (length < 0) 610 if (length < 0)
539 croak ("length must not be negative"); 611 croak ("length must not be negative");
540 612
541 Newz (0, req, 1, aio_cb); 613 {
614 dREQ;
542 615
543 if (!req)
544 croak ("out of memory during aio_req allocation");
545
546 req->type = ix; 616 req->type = ix;
547 req->fh = newSVsv (fh); 617 req->fh = newSVsv (fh);
548 req->fd = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh)) 618 req->fd = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh))
549 : IoOFP (sv_2io (fh))); 619 : IoOFP (sv_2io (fh)));
550 req->offset = offset; 620 req->offset = offset;
551 req->length = length; 621 req->length = length;
552 req->data = SvREFCNT_inc (data); 622 req->data = SvREFCNT_inc (data);
553 req->dataptr = (char *)svptr + dataoffset; 623 req->dataptr = (char *)svptr + dataoffset;
554 req->callback = SvREFCNT_inc (callback); 624 req->callback = SvREFCNT_inc (callback);
555 625
556 send_req (req); 626 send_req (req);
627 }
557} 628}
558 629
559void 630void
560aio_readahead(fh,offset,length,callback=&PL_sv_undef) 631aio_readahead(fh,offset,length,callback=&PL_sv_undef)
561 SV * fh 632 SV * fh
563 IV length 634 IV length
564 SV * callback 635 SV * callback
565 PROTOTYPE: $$$;$ 636 PROTOTYPE: $$$;$
566 CODE: 637 CODE:
567{ 638{
568 aio_req req; 639 dREQ;
569
570 if (length < 0)
571 croak ("length must not be negative");
572
573 Newz (0, req, 1, aio_cb);
574
575 if (!req)
576 croak ("out of memory during aio_req allocation");
577 640
578 req->type = REQ_READAHEAD; 641 req->type = REQ_READAHEAD;
579 req->fh = newSVsv (fh); 642 req->fh = newSVsv (fh);
580 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); 643 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh)));
581 req->offset = offset; 644 req->offset = offset;
582 req->length = length; 645 req->length = length;
583 req->callback = SvREFCNT_inc (callback);
584 646
585 send_req (req); 647 send_req (req);
586} 648}
587 649
588void 650void
592 ALIAS: 654 ALIAS:
593 aio_stat = REQ_STAT 655 aio_stat = REQ_STAT
594 aio_lstat = REQ_LSTAT 656 aio_lstat = REQ_LSTAT
595 CODE: 657 CODE:
596{ 658{
597 aio_req req; 659 dREQ;
598
599 Newz (0, req, 1, aio_cb);
600
601 if (!req)
602 croak ("out of memory during aio_req allocation");
603 660
604 New (0, req->statdata, 1, Stat_t); 661 New (0, req->statdata, 1, Stat_t);
605
606 if (!req->statdata) 662 if (!req->statdata)
607 croak ("out of memory during aio_req->statdata allocation"); 663 croak ("out of memory during aio_req->statdata allocation (sorry, i just leaked memory, too)");
608 664
609 if (SvPOK (fh_or_path)) 665 if (SvPOK (fh_or_path))
610 { 666 {
611 req->type = ix; 667 req->type = ix;
612 req->data = newSVsv (fh_or_path); 668 req->data = newSVsv (fh_or_path);
613 req->dataptr = SvPV_nolen (req->data); 669 req->dataptr = SvPVbyte_nolen (req->data);
614 } 670 }
615 else 671 else
616 { 672 {
617 req->type = REQ_FSTAT; 673 req->type = REQ_FSTAT;
618 req->fh = newSVsv (fh_or_path); 674 req->fh = newSVsv (fh_or_path);
619 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); 675 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
620 } 676 }
621 677
622 req->callback = SvREFCNT_inc (callback);
623
624 send_req (req); 678 send_req (req);
625} 679}
626 680
627void 681void
628aio_unlink(pathname,callback=&PL_sv_undef) 682aio_unlink(pathname,callback=&PL_sv_undef)
629 SV * pathname 683 SV * pathname
630 SV * callback 684 SV * callback
685 ALIAS:
686 aio_unlink = REQ_UNLINK
687 aio_rmdir = REQ_RMDIR
631 CODE: 688 CODE:
632{ 689{
633 aio_req req; 690 dREQ;
634 691
635 Newz (0, req, 1, aio_cb); 692 req->type = ix;
693 req->data = newSVsv (pathname);
694 req->dataptr = SvPVbyte_nolen (req->data);
636 695
637 if (!req) 696 send_req (req);
638 croak ("out of memory during aio_req allocation"); 697}
698
699void
700aio_symlink(oldpath,newpath,callback=&PL_sv_undef)
701 SV * oldpath
702 SV * newpath
703 SV * callback
704 CODE:
705{
706 dREQ;
639 707
640 req->type = REQ_UNLINK; 708 req->type = REQ_SYMLINK;
709 req->fh = newSVsv (oldpath);
710 req->data2ptr = SvPVbyte_nolen (req->fh);
641 req->data = newSVsv (pathname); 711 req->data = newSVsv (newpath);
642 req->dataptr = SvPV_nolen (req->data); 712 req->dataptr = SvPVbyte_nolen (req->data);
643 req->callback = SvREFCNT_inc (callback);
644 713
645 send_req (req); 714 send_req (req);
646} 715}
647 716
648void 717void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines