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.20 by root, Sun Jul 31 19:08:39 2005 UTC vs.
Revision 1.31 by root, Thu Aug 18 16:34:53 2005 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines