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.13 by root, Fri Jul 22 08:25:22 2005 UTC vs.
Revision 1.26 by root, Wed Aug 17 03:52:20 2005 UTC

1#define _XOPEN_SOURCE 500 1#define _REENTRANT 1
2#include <errno.h>
2 3
3#include "EXTERN.h" 4#include "EXTERN.h"
4#include "perl.h" 5#include "perl.h"
5#include "XSUB.h" 6#include "XSUB.h"
7
8#include "autoconf/config.h"
6 9
7#include <sys/types.h> 10#include <sys/types.h>
8#include <sys/stat.h> 11#include <sys/stat.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#if __linux
15#include <sys/syscall.h>
16#endif
17 17
18#include <pthread.h> 18#include <pthread.h>
19 19
20typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ 20typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */
21typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ 21typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */
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
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;
64static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; 66static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER;
67static pthread_mutex_t frklock = PTHREAD_MUTEX_INITIALIZER;
65static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; 68static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER;
66 69
67static volatile aio_req reqs, reqe; /* queue start, queue end */ 70static volatile aio_req reqs, reqe; /* queue start, queue end */
68static 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}
69 89
70static void 90static void
71poll_wait () 91poll_wait ()
72{ 92{
73 if (nreqs && !ress) 93 if (nreqs && !ress)
83static int 103static int
84poll_cb () 104poll_cb ()
85{ 105{
86 dSP; 106 dSP;
87 int count = 0; 107 int count = 0;
108 int do_croak = 0;
88 aio_req req, prv; 109 aio_req req, prv;
89 110
90 pthread_mutex_lock (&reslock); 111 pthread_mutex_lock (&reslock);
91 112
92 { 113 {
93 /* read any signals sent by the worker threads */ 114 /* read any signals sent by the worker threads */
94 char buf [32]; 115 char buf [32];
95 while (read (respipe [0], buf, 32) > 0) 116 while (read (respipe [0], buf, 32) == 32)
96 ; 117 ;
97 } 118 }
98 119
99 req = ress; 120 req = ress;
100 ress = rese = 0; 121 ress = rese = 0;
114 135
115 if (req->type == REQ_READ) 136 if (req->type == REQ_READ)
116 SvCUR_set (req->data, req->dataoffset 137 SvCUR_set (req->data, req->dataoffset
117 + req->result > 0 ? req->result : 0); 138 + req->result > 0 ? req->result : 0);
118 139
119 if (req->data)
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) 140 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT)
126 { 141 {
127 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; 142 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
128 PL_laststatval = req->result; 143 PL_laststatval = req->result;
129 PL_statcache = *(req->statdata); 144 PL_statcache = *(req->statdata);
130
131 Safefree (req->statdata);
132 } 145 }
133 146
134 ENTER; 147 ENTER;
135 PUSHMARK (SP); 148 PUSHMARK (SP);
136 XPUSHs (sv_2mortal (newSViv (req->result))); 149 XPUSHs (sv_2mortal (newSViv (req->result)));
156 call_sv (req->callback, G_VOID | G_EVAL); 169 call_sv (req->callback, G_VOID | G_EVAL);
157 SPAGAIN; 170 SPAGAIN;
158 } 171 }
159 172
160 LEAVE; 173 LEAVE;
174
175 do_croak = SvTRUE (ERRSV);
161 176
162 if (req->callback)
163 SvREFCNT_dec (req->callback);
164
165 errno = errorno; 177 errno = errorno;
166 count++; 178 count++;
167 } 179 }
168 180
169 prv = req; 181 prv = req;
170 req = req->next; 182 req = req->next;
171 Safefree (prv); 183 free_req (prv);
172 184
173 /* TODO: croak on errors? */ 185 if (do_croak)
186 croak (0);
174 } 187 }
175 188
176 return count; 189 return count;
177} 190}
178 191
227 240
228static void 241static void
229end_thread (void) 242end_thread (void)
230{ 243{
231 aio_req req; 244 aio_req req;
232 New (0, req, 1, aio_cb); 245 Newz (0, req, 1, aio_cb);
233 req->type = REQ_QUIT; 246 req->type = REQ_QUIT;
234 247
235 send_req (req); 248 send_req (req);
236} 249}
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/*****************************************************************************/
332/* work around various missing functions */
333
334#if !HAVE_PREADWRITE
335# define pread aio_pread
336# define pwrite aio_pwrite
337
338/*
339 * make our pread/pwrite safe against themselves, but not against
340 * normal read/write by using a mutex. slows down execution a lot,
341 * but that's your problem, not mine.
342 */
343static pthread_mutex_t iolock = PTHREAD_MUTEX_INITIALIZER;
344
345static ssize_t
346pread (int fd, void *buf, size_t count, off_t offset)
347{
348 ssize_t res;
349 off_t ooffset;
350
351 pthread_mutex_lock (&iolock);
352 ooffset = lseek (fd, 0, SEEK_CUR);
353 lseek (fd, offset, SEEK_SET);
354 res = read (fd, buf, count);
355 lseek (fd, ooffset, SEEK_SET);
356 pthread_mutex_unlock (&iolock);
357
358 return res;
359}
360
361static ssize_t
362pwrite (int fd, void *buf, size_t count, off_t offset)
363{
364 ssize_t res;
365 off_t ooffset;
366
367 pthread_mutex_lock (&iolock);
368 ooffset = lseek (fd, 0, SEEK_CUR);
369 lseek (fd, offset, SEEK_SET);
370 res = write (fd, buf, count);
371 lseek (fd, offset, SEEK_SET);
372 pthread_mutex_unlock (&iolock);
373
374 return res;
375}
376#endif
377
378#if !HAVE_FDATASYNC
379# define fdatasync fsync
380#endif
381
382#if !HAVE_READAHEAD
383# define readahead aio_readahead
384
385static char readahead_buf[4096];
386
387static ssize_t
388readahead (int fd, off_t offset, size_t count)
389{
390 while (count > 0)
391 {
392 size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf);
393
394 pread (fd, readahead_buf, len, offset);
395 offset += len;
396 count -= len;
397 }
398
399 errno = 0;
400}
401#endif
402
403/*****************************************************************************/
237 404
238static void * 405static void *
239aio_proc (void *thr_arg) 406aio_proc (void *thr_arg)
240{ 407{
241 aio_req req; 408 aio_req req;
269 436
270 switch (type) 437 switch (type)
271 { 438 {
272 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; 439 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break;
273 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; 440 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break;
274#if SYS_readahead 441
275 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; 442 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break;
276#else
277 case REQ_READAHEAD: req->result = -1; errno = ENOSYS; break;
278#endif
279 443
280 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; 444 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break;
281 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; 445 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break;
282 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; 446 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break;
283 447
284 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;
285 case REQ_CLOSE: req->result = close (req->fd); break; 449 case REQ_CLOSE: req->result = close (req->fd); break;
286 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;
287 453
454 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break;
288 case REQ_FSYNC: req->result = fsync (req->fd); break; 455 case REQ_FSYNC: req->result = fsync (req->fd); break;
289 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break;
290 456
291 case REQ_QUIT: 457 case REQ_QUIT:
292 break; 458 break;
293 459
294 default: 460 default:
320 while (type != REQ_QUIT); 486 while (type != REQ_QUIT);
321 487
322 return 0; 488 return 0;
323} 489}
324 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
325MODULE = IO::AIO PACKAGE = IO::AIO 503MODULE = IO::AIO PACKAGE = IO::AIO
326 504
327PROTOTYPES: ENABLE 505PROTOTYPES: ENABLE
328 506
329BOOT: 507BOOT:
330{ 508{
331 if (pipe (respipe)) 509 create_pipe ();
332 croak ("unable to initialize result pipe"); 510 pthread_atfork (atfork_prepare, atfork_parent, atfork_child);
333
334 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK))
335 croak ("cannot set result pipe to nonblocking mode");
336
337 if (fcntl (respipe [1], F_SETFL, O_NONBLOCK))
338 croak ("cannot set result pipe to nonblocking mode");
339} 511}
340 512
341void 513void
342min_parallel(nthreads) 514min_parallel(nthreads)
343 int nthreads 515 int nthreads
344 PROTOTYPE: $ 516 PROTOTYPE: $
345 CODE:
346 while (nthreads > started)
347 start_thread ();
348 517
349void 518void
350max_parallel(nthreads) 519max_parallel(nthreads)
351 int nthreads 520 int nthreads
352 PROTOTYPE: $ 521 PROTOTYPE: $
353 CODE:
354{
355 int cur = started;
356 while (cur > nthreads)
357 {
358 end_thread ();
359 cur--;
360 }
361
362 while (started > nthreads)
363 {
364 poll_wait ();
365 poll_cb ();
366 }
367}
368 522
369int 523int
370max_outstanding(nreqs) 524max_outstanding(nreqs)
371 int nreqs 525 int nreqs
372 PROTOTYPE: $ 526 PROTOTYPE: $
381 int mode 535 int mode
382 SV * callback 536 SV * callback
383 PROTOTYPE: $$$;$ 537 PROTOTYPE: $$$;$
384 CODE: 538 CODE:
385{ 539{
386 aio_req req; 540 dREQ;
387
388 Newz (0, req, 1, aio_cb);
389
390 if (!req)
391 croak ("out of memory during aio_req allocation");
392 541
393 req->type = REQ_OPEN; 542 req->type = REQ_OPEN;
394 req->data = newSVsv (pathname); 543 req->data = newSVsv (pathname);
395 req->dataptr = SvPV_nolen (req->data); 544 req->dataptr = SvPVbyte_nolen (req->data);
396 req->fd = flags; 545 req->fd = flags;
397 req->mode = mode; 546 req->mode = mode;
398 req->callback = SvREFCNT_inc (callback);
399 547
400 send_req (req); 548 send_req (req);
401} 549}
402 550
403void 551void
409 aio_close = REQ_CLOSE 557 aio_close = REQ_CLOSE
410 aio_fsync = REQ_FSYNC 558 aio_fsync = REQ_FSYNC
411 aio_fdatasync = REQ_FDATASYNC 559 aio_fdatasync = REQ_FDATASYNC
412 CODE: 560 CODE:
413{ 561{
414 aio_req req; 562 dREQ;
415
416 Newz (0, req, 1, aio_cb);
417
418 if (!req)
419 croak ("out of memory during aio_req allocation");
420 563
421 req->type = ix; 564 req->type = ix;
422 req->fh = newSVsv (fh); 565 req->fh = newSVsv (fh);
423 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); 566 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh)));
424 req->callback = SvREFCNT_inc (callback);
425 567
426 send_req (req); 568 send_req (req);
427} 569}
428 570
429void 571void
440 PROTOTYPE: $$$$$;$ 582 PROTOTYPE: $$$$$;$
441 CODE: 583 CODE:
442{ 584{
443 aio_req req; 585 aio_req req;
444 STRLEN svlen; 586 STRLEN svlen;
445 char *svptr = SvPV (data, svlen); 587 char *svptr = SvPVbyte (data, svlen);
446 588
447 SvUPGRADE (data, SVt_PV); 589 SvUPGRADE (data, SVt_PV);
448 SvPOK_on (data); 590 SvPOK_on (data);
449 591
450 if (dataoffset < 0) 592 if (dataoffset < 0)
466 } 608 }
467 609
468 if (length < 0) 610 if (length < 0)
469 croak ("length must not be negative"); 611 croak ("length must not be negative");
470 612
471 Newz (0, req, 1, aio_cb); 613 {
614 dREQ;
472 615
473 if (!req)
474 croak ("out of memory during aio_req allocation");
475
476 req->type = ix; 616 req->type = ix;
477 req->fh = newSVsv (fh); 617 req->fh = newSVsv (fh);
478 req->fd = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh)) 618 req->fd = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh))
479 : IoOFP (sv_2io (fh))); 619 : IoOFP (sv_2io (fh)));
480 req->offset = offset; 620 req->offset = offset;
481 req->length = length; 621 req->length = length;
482 req->data = SvREFCNT_inc (data); 622 req->data = SvREFCNT_inc (data);
483 req->dataptr = (char *)svptr + dataoffset; 623 req->dataptr = (char *)svptr + dataoffset;
484 req->callback = SvREFCNT_inc (callback); 624 req->callback = SvREFCNT_inc (callback);
485 625
486 send_req (req); 626 send_req (req);
627 }
487} 628}
488 629
489void 630void
490aio_readahead(fh,offset,length,callback=&PL_sv_undef) 631aio_readahead(fh,offset,length,callback=&PL_sv_undef)
491 SV * fh 632 SV * fh
493 IV length 634 IV length
494 SV * callback 635 SV * callback
495 PROTOTYPE: $$$;$ 636 PROTOTYPE: $$$;$
496 CODE: 637 CODE:
497{ 638{
498 aio_req req; 639 dREQ;
499
500 if (length < 0)
501 croak ("length must not be negative");
502
503 Newz (0, req, 1, aio_cb);
504
505 if (!req)
506 croak ("out of memory during aio_req allocation");
507 640
508 req->type = REQ_READAHEAD; 641 req->type = REQ_READAHEAD;
509 req->fh = newSVsv (fh); 642 req->fh = newSVsv (fh);
510 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); 643 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh)));
511 req->offset = offset; 644 req->offset = offset;
512 req->length = length; 645 req->length = length;
513 req->callback = SvREFCNT_inc (callback);
514 646
515 send_req (req); 647 send_req (req);
516} 648}
517 649
518void 650void
522 ALIAS: 654 ALIAS:
523 aio_stat = REQ_STAT 655 aio_stat = REQ_STAT
524 aio_lstat = REQ_LSTAT 656 aio_lstat = REQ_LSTAT
525 CODE: 657 CODE:
526{ 658{
527 aio_req req; 659 dREQ;
528
529 Newz (0, req, 1, aio_cb);
530
531 if (!req)
532 croak ("out of memory during aio_req allocation");
533 660
534 New (0, req->statdata, 1, Stat_t); 661 New (0, req->statdata, 1, Stat_t);
535
536 if (!req->statdata) 662 if (!req->statdata)
537 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)");
538 664
539 if (SvPOK (fh_or_path)) 665 if (SvPOK (fh_or_path))
540 { 666 {
541 req->type = ix; 667 req->type = ix;
542 req->data = newSVsv (fh_or_path); 668 req->data = newSVsv (fh_or_path);
543 req->dataptr = SvPV_nolen (req->data); 669 req->dataptr = SvPVbyte_nolen (req->data);
544 } 670 }
545 else 671 else
546 { 672 {
547 req->type = REQ_FSTAT; 673 req->type = REQ_FSTAT;
548 req->fh = newSVsv (fh_or_path); 674 req->fh = newSVsv (fh_or_path);
549 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); 675 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
550 } 676 }
551 677
552 req->callback = SvREFCNT_inc (callback);
553
554 send_req (req); 678 send_req (req);
555} 679}
556 680
557void 681void
558aio_unlink(pathname,callback=&PL_sv_undef) 682aio_unlink(pathname,callback=&PL_sv_undef)
559 SV * pathname 683 SV * pathname
560 SV * callback 684 SV * callback
685 ALIAS:
686 aio_unlink = REQ_UNLINK
687 aio_rmdir = REQ_RMDIR
561 CODE: 688 CODE:
562{ 689{
563 aio_req req; 690 dREQ;
564 691
565 Newz (0, req, 1, aio_cb); 692 req->type = ix;
693 req->data = newSVsv (pathname);
694 req->dataptr = SvPVbyte_nolen (req->data);
566 695
567 if (!req) 696 send_req (req);
568 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;
569 707
570 req->type = REQ_UNLINK; 708 req->type = REQ_SYMLINK;
709 req->fh = newSVsv (oldpath);
710 req->data2ptr = SvPVbyte_nolen (req->fh);
571 req->data = newSVsv (pathname); 711 req->data = newSVsv (newpath);
572 req->dataptr = SvPV_nolen (req->data); 712 req->dataptr = SvPVbyte_nolen (req->data);
573 req->callback = SvREFCNT_inc (callback);
574 713
575 send_req (req); 714 send_req (req);
576} 715}
577 716
578void 717void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines