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.16 by root, Sun Jul 31 18:20:07 2005 UTC vs.
Revision 1.41 by root, Sat Jun 24 19:14:04 2006 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"
6 9
10#include <pthread.h>
11
12#include <stddef.h>
13#include <errno.h>
7#include <sys/types.h> 14#include <sys/types.h>
8#include <sys/stat.h> 15#include <sys/stat.h>
9 16#include <limits.h>
10#include <unistd.h> 17#include <unistd.h>
11#include <fcntl.h> 18#include <fcntl.h>
12#include <signal.h> 19#include <signal.h>
13#include <sched.h> 20#include <sched.h>
14 21
15#include <pthread.h> 22#if HAVE_SENDFILE
23# if __linux
24# include <sys/sendfile.h>
25# elif __freebsd
26# include <sys/socket.h>
27# include <sys/uio.h>
28# elif __hpux
29# include <sys/socket.h>
30# elif __solaris /* not yet */
31# include <sys/sendfile.h>
32# else
33# error sendfile support requested but not available
34# endif
35#endif
16 36
17typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ 37/* used for struct dirent, AIX doesn't provide it */
18typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ 38#ifndef NAME_MAX
19typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ 39# define NAME_MAX 4096
40#endif
20 41
21#if __ia64 42#if __ia64
22# define STACKSIZE 65536 43# define STACKSIZE 65536
23#else 44#else
24# define STACKSIZE 4096 45# define STACKSIZE 8192
25#endif 46#endif
26 47
27enum { 48enum {
28 REQ_QUIT, 49 REQ_QUIT,
29 REQ_OPEN, REQ_CLOSE, 50 REQ_OPEN, REQ_CLOSE,
30 REQ_READ, REQ_WRITE, REQ_READAHEAD, 51 REQ_READ, REQ_WRITE, REQ_READAHEAD,
52 REQ_SENDFILE,
31 REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_UNLINK, 53 REQ_STAT, REQ_LSTAT, REQ_FSTAT,
32 REQ_FSYNC, REQ_FDATASYNC, 54 REQ_FSYNC, REQ_FDATASYNC,
55 REQ_UNLINK, REQ_RMDIR, REQ_RENAME,
56 REQ_READDIR,
57 REQ_LINK, REQ_SYMLINK,
33}; 58};
34 59
35typedef struct aio_cb { 60typedef struct aio_cb {
36 struct aio_cb *volatile next; 61 struct aio_cb *volatile next;
37 62
38 int type; 63 int type;
39 64
65 /* should receive a cleanup, with unions */
40 int fd; 66 int fd, fd2;
41 off_t offset; 67 off_t offset;
42 size_t length; 68 size_t length;
43 ssize_t result; 69 ssize_t result;
44 mode_t mode; /* open */ 70 mode_t mode; /* open */
45 int errorno; 71 int errorno;
46 SV *data, *callback, *fh; 72 SV *data, *callback;
47 void *dataptr; 73 SV *fh, *fh2;
74 void *dataptr, *data2ptr;
48 STRLEN dataoffset; 75 STRLEN dataoffset;
49 76
50 Stat_t *statdata; 77 Stat_t *statdata;
51} aio_cb; 78} aio_cb;
52 79
53typedef aio_cb *aio_req; 80typedef aio_cb *aio_req;
54 81
55static int started; 82static int started, wanted;
56static volatile int nreqs; 83static volatile int nreqs;
57static int max_outstanding = 1<<30; 84static int max_outstanding = 1<<30;
58static int respipe [2]; 85static int respipe [2];
59 86
60static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; 87static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER;
62static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; 89static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER;
63 90
64static volatile aio_req reqs, reqe; /* queue start, queue end */ 91static volatile aio_req reqs, reqe; /* queue start, queue end */
65static volatile aio_req ress, rese; /* queue start, queue end */ 92static volatile aio_req ress, rese; /* queue start, queue end */
66 93
94static void free_req (aio_req req)
95{
96 if (req->data)
97 SvREFCNT_dec (req->data);
98
99 if (req->fh)
100 SvREFCNT_dec (req->fh);
101
102 if (req->fh2)
103 SvREFCNT_dec (req->fh2);
104
105 if (req->statdata)
106 Safefree (req->statdata);
107
108 if (req->callback)
109 SvREFCNT_dec (req->callback);
110
111 if (req->type == REQ_READDIR && req->result >= 0)
112 free (req->data2ptr);
113
114 Safefree (req);
115}
116
67static void 117static void
68poll_wait () 118poll_wait ()
69{ 119{
70 if (nreqs && !ress) 120 if (nreqs && !ress)
71 { 121 {
80static int 130static int
81poll_cb () 131poll_cb ()
82{ 132{
83 dSP; 133 dSP;
84 int count = 0; 134 int count = 0;
135 int do_croak = 0;
85 aio_req req, prv; 136 aio_req req;
86 137
138 for (;;)
139 {
87 pthread_mutex_lock (&reslock); 140 pthread_mutex_lock (&reslock);
141 req = ress;
88 142
89 { 143 if (req)
144 {
145 ress = req->next;
146
147 if (!ress)
148 {
90 /* read any signals sent by the worker threads */ 149 /* read any signals sent by the worker threads */
91 char buf [32]; 150 char buf [32];
92 while (read (respipe [0], buf, 32) > 0) 151 while (read (respipe [0], buf, 32) == 32)
152 ;
153
154 rese = 0;
155 }
93 ; 156 }
94 }
95 157
96 req = ress;
97 ress = rese = 0;
98
99 pthread_mutex_unlock (&reslock); 158 pthread_mutex_unlock (&reslock);
100 159
101 while (req) 160 if (!req)
102 { 161 break;
162
103 nreqs--; 163 nreqs--;
104 164
105 if (req->type == REQ_QUIT) 165 if (req->type == REQ_QUIT)
106 started--; 166 started--;
107 else 167 else
108 { 168 {
109 int errorno = errno; 169 int errorno = errno;
110 errno = req->errorno; 170 errno = req->errorno;
111 171
112 if (req->type == REQ_READ) 172 if (req->type == REQ_READ)
113 SvCUR_set (req->data, req->dataoffset 173 SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0));
114 + req->result > 0 ? req->result : 0);
115 174
175 if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE))
176 SvREADONLY_off (req->data);
177
116 if (req->data) 178 if (req->statdata)
117 SvREFCNT_dec (req->data);
118
119 if (req->fh)
120 SvREFCNT_dec (req->fh);
121
122 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT)
123 { 179 {
124 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; 180 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
125 PL_laststatval = req->result; 181 PL_laststatval = req->result;
126 PL_statcache = *(req->statdata); 182 PL_statcache = *(req->statdata);
127
128 Safefree (req->statdata);
129 } 183 }
130 184
131 ENTER; 185 ENTER;
132 PUSHMARK (SP); 186 PUSHMARK (SP);
133 XPUSHs (sv_2mortal (newSViv (req->result)));
134 187
135 if (req->type == REQ_OPEN) 188 if (req->type == REQ_READDIR)
136 { 189 {
190 SV *rv = &PL_sv_undef;
191
192 if (req->result >= 0)
193 {
194 char *buf = req->data2ptr;
195 AV *av = newAV ();
196
197 while (req->result)
198 {
199 SV *sv = newSVpv (buf, 0);
200
201 av_push (av, sv);
202 buf += SvCUR (sv) + 1;
203 req->result--;
204 }
205
206 rv = sv_2mortal (newRV_noinc ((SV *)av));
207 }
208
209 XPUSHs (rv);
210 }
211 else
212 {
213 XPUSHs (sv_2mortal (newSViv (req->result)));
214
215 if (req->type == REQ_OPEN)
216 {
137 /* convert fd to fh */ 217 /* convert fd to fh */
138 SV *fh; 218 SV *fh;
139 219
140 PUTBACK; 220 PUTBACK;
141 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); 221 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL);
142 SPAGAIN; 222 SPAGAIN;
143 223
144 fh = SvREFCNT_inc (POPs); 224 fh = SvREFCNT_inc (POPs);
145 225
146 PUSHMARK (SP); 226 PUSHMARK (SP);
147 XPUSHs (sv_2mortal (fh)); 227 XPUSHs (sv_2mortal (fh));
228 }
148 } 229 }
149 230
150 if (SvOK (req->callback)) 231 if (SvOK (req->callback))
151 { 232 {
152 PUTBACK; 233 PUTBACK;
153 call_sv (req->callback, G_VOID | G_EVAL); 234 call_sv (req->callback, G_VOID | G_EVAL);
154 SPAGAIN; 235 SPAGAIN;
236
237 if (SvTRUE (ERRSV))
238 {
239 free_req (req);
240 croak (0);
241 }
155 } 242 }
156 243
157 LEAVE; 244 LEAVE;
158
159 if (req->callback)
160 SvREFCNT_dec (req->callback);
161 245
162 errno = errorno; 246 errno = errorno;
163 count++; 247 count++;
164 } 248 }
165 249
166 prv = req; 250 free_req (req);
167 req = req->next;
168 Safefree (prv);
169
170 /* TODO: croak on errors? */
171 } 251 }
172 252
173 return count; 253 return count;
174} 254}
175 255
196} 276}
197 277
198static void 278static void
199send_req (aio_req req) 279send_req (aio_req req)
200{ 280{
281 while (started < wanted && nreqs >= started)
282 start_thread ();
283
201 nreqs++; 284 nreqs++;
202 285
203 pthread_mutex_lock (&reqlock); 286 pthread_mutex_lock (&reqlock);
204 287
205 req->next = 0; 288 req->next = 0;
213 reqe = reqs = req; 296 reqe = reqs = req;
214 297
215 pthread_cond_signal (&reqwait); 298 pthread_cond_signal (&reqwait);
216 pthread_mutex_unlock (&reqlock); 299 pthread_mutex_unlock (&reqlock);
217 300
218 while (nreqs > max_outstanding) 301 if (nreqs > max_outstanding)
302 for (;;)
303 {
304 poll_cb ();
305
306 if (nreqs <= max_outstanding)
307 break;
308
309 poll_wait ();
310 }
311}
312
313static void
314end_thread (void)
315{
316 aio_req req;
317 Newz (0, req, 1, aio_cb);
318 req->type = REQ_QUIT;
319
320 send_req (req);
321}
322
323static void min_parallel (int nthreads)
324{
325 if (wanted < nthreads)
326 wanted = nthreads;
327}
328
329static void max_parallel (int nthreads)
330{
331 int cur = started;
332
333 if (wanted > nthreads)
334 wanted = nthreads;
335
336 while (cur > wanted)
337 {
338 end_thread ();
339 cur--;
340 }
341
342 while (started > wanted)
219 { 343 {
220 poll_wait (); 344 poll_wait ();
221 poll_cb (); 345 poll_cb ();
222 } 346 }
223} 347}
224 348
349static void create_pipe ()
350{
351 if (pipe (respipe))
352 croak ("unable to initialize result pipe");
353
354 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK))
355 croak ("cannot set result pipe to nonblocking mode");
356
357 if (fcntl (respipe [1], F_SETFL, O_NONBLOCK))
358 croak ("cannot set result pipe to nonblocking mode");
359}
360
361/*****************************************************************************/
362/* work around various missing functions */
363
364#if !HAVE_PREADWRITE
365# define pread aio_pread
366# define pwrite aio_pwrite
367
368/*
369 * make our pread/pwrite safe against themselves, but not against
370 * normal read/write by using a mutex. slows down execution a lot,
371 * but that's your problem, not mine.
372 */
373static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER;
374
375static ssize_t
376pread (int fd, void *buf, size_t count, off_t offset)
377{
378 ssize_t res;
379 off_t ooffset;
380
381 pthread_mutex_lock (&preadwritelock);
382 ooffset = lseek (fd, 0, SEEK_CUR);
383 lseek (fd, offset, SEEK_SET);
384 res = read (fd, buf, count);
385 lseek (fd, ooffset, SEEK_SET);
386 pthread_mutex_unlock (&preadwritelock);
387
388 return res;
389}
390
391static ssize_t
392pwrite (int fd, void *buf, size_t count, off_t offset)
393{
394 ssize_t res;
395 off_t ooffset;
396
397 pthread_mutex_lock (&preadwritelock);
398 ooffset = lseek (fd, 0, SEEK_CUR);
399 lseek (fd, offset, SEEK_SET);
400 res = write (fd, buf, count);
401 lseek (fd, offset, SEEK_SET);
402 pthread_mutex_unlock (&preadwritelock);
403
404 return res;
405}
406#endif
407
408#if !HAVE_FDATASYNC
409# define fdatasync fsync
410#endif
411
412#if !HAVE_READAHEAD
413# define readahead aio_readahead
414
415static ssize_t
416readahead (int fd, off_t offset, size_t count)
417{
418 char readahead_buf[4096];
419
420 while (count > 0)
421 {
422 size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf);
423
424 pread (fd, readahead_buf, len, offset);
425 offset += len;
426 count -= len;
427 }
428
429 errno = 0;
430}
431#endif
432
433#if !HAVE_READDIR_R
434# define readdir_r aio_readdir_r
435
436static pthread_mutex_t readdirlock = PTHREAD_MUTEX_INITIALIZER;
437
225static void 438static int
226end_thread (void) 439readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res)
227{ 440{
228 aio_req req; 441 struct dirent *e;
229 New (0, req, 1, aio_cb); 442 int errorno;
230 req->type = REQ_QUIT;
231 443
232 send_req (req); 444 pthread_mutex_lock (&readdirlock);
445
446 e = readdir (dirp);
447 errorno = errno;
448
449 if (e)
450 {
451 *res = ent;
452 strcpy (ent->d_name, e->d_name);
453 }
454 else
455 *res = 0;
456
457 pthread_mutex_unlock (&readdirlock);
458
459 errno = errorno;
460 return e ? 0 : -1;
233} 461}
462#endif
463
464/* sendfile always needs emulation */
465static ssize_t
466sendfile_ (int ofd, int ifd, off_t offset, size_t count)
467{
468 ssize_t res;
469
470 if (!count)
471 return 0;
472
473#if HAVE_SENDFILE
474# if __linux
475 res = sendfile (ofd, ifd, &offset, count);
476
477# elif __freebsd
478 /*
479 * Of course, the freebsd sendfile is a dire hack with no thoughts
480 * wasted on making it similar to other I/O functions.
481 */
482 {
483 off_t sbytes;
484 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0);
485
486 if (res < 0 && sbytes)
487 /* maybe only on EAGAIN only: as usual, the manpage leaves you guessing */
488 res = sbytes;
489 }
490
491# elif __hpux
492 res = sendfile (ofd, ifd, offset, count, 0, 0);
493
494# elif __solaris
495 {
496 struct sendfilevec vec;
497 size_t sbytes;
498
499 vec.sfv_fd = ifd;
500 vec.sfv_flag = 0;
501 vec.sfv_off = offset;
502 vec.sfv_len = count;
503
504 res = sendfilev (ofd, &vec, 1, &sbytes);
505
506 if (res < 0 && sbytes)
507 res = sbytes;
508 }
509
510# endif
511#else
512 res = -1;
513 errno = ENOSYS;
514#endif
515
516 if (res < 0
517 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK
518#if __solaris
519 || errno == EAFNOSUPPORT || errno == EPROTOTYPE
520#endif
521 )
522 )
523 {
524 /* emulate sendfile. this is a major pain in the ass */
525 char buf[4096];
526 res = 0;
527
528 while (count)
529 {
530 ssize_t cnt;
531
532 cnt = pread (ifd, buf, count > 4096 ? 4096 : count, offset);
533
534 if (cnt <= 0)
535 {
536 if (cnt && !res) res = -1;
537 break;
538 }
539
540 cnt = write (ofd, buf, cnt);
541
542 if (cnt <= 0)
543 {
544 if (cnt && !res) res = -1;
545 break;
546 }
547
548 offset += cnt;
549 res += cnt;
550 count -= cnt;
551 }
552 }
553
554 return res;
555}
556
557/* read a full directory */
558static int
559scandir_ (const char *path, void **namesp)
560{
561 DIR *dirp = opendir (path);
562 union
563 {
564 struct dirent d;
565 char b [offsetof (struct dirent, d_name) + NAME_MAX + 1];
566 } u;
567 struct dirent *entp;
568 char *name, *names;
569 int memlen = 4096;
570 int memofs = 0;
571 int res = 0;
572 int errorno;
573
574 if (!dirp)
575 return -1;
576
577 names = malloc (memlen);
578
579 for (;;)
580 {
581 errno = 0, readdir_r (dirp, &u.d, &entp);
582
583 if (!entp)
584 break;
585
586 name = entp->d_name;
587
588 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2])))
589 {
590 int len = strlen (name) + 1;
591
592 res++;
593
594 while (memofs + len > memlen)
595 {
596 memlen *= 2;
597 names = realloc (names, memlen);
598 if (!names)
599 break;
600 }
601
602 memcpy (names + memofs, name, len);
603 memofs += len;
604 }
605 }
606
607 errorno = errno;
608 closedir (dirp);
609
610 if (errorno)
611 {
612 free (names);
613 errno = errorno;
614 res = -1;
615 }
616
617 *namesp = (void *)names;
618 return res;
619}
620
621/*****************************************************************************/
234 622
235static void * 623static void *
236aio_proc (void *thr_arg) 624aio_proc (void *thr_arg)
237{ 625{
238 aio_req req; 626 aio_req req;
264 652
265 type = req->type; 653 type = req->type;
266 654
267 switch (type) 655 switch (type)
268 { 656 {
269#if HAVE_PREADWRITE
270 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; 657 case REQ_READ: req->result = pread (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; 658 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break;
272#else
273# error "pread/pwrite cannot be emulated, fix your os"
274#endif
275 659
276#if HAVE_READAHEAD
277 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; 660 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break;
278#else 661 case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length); break;
279 // TODO: emulate
280 case REQ_READAHEAD: req->result = -1; errno = ENOSYS; break;
281#endif
282 662
283 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; 663 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break;
284 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; 664 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break;
285 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; 665 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break;
286 666
287 case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; 667 case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break;
288 case REQ_CLOSE: req->result = close (req->fd); break; 668 case REQ_CLOSE: req->result = close (req->fd); break;
289 case REQ_UNLINK: req->result = unlink (req->dataptr); break; 669 case REQ_UNLINK: req->result = unlink (req->dataptr); break;
670 case REQ_RMDIR: req->result = rmdir (req->dataptr); break;
671 case REQ_RENAME: req->result = rename (req->data2ptr, req->dataptr); break;
672 case REQ_LINK: req->result = link (req->data2ptr, req->dataptr); break;
673 case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break;
290 674
291 case REQ_FDATASYNC: 675 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break;
292#if HAVE_FDATASYNC
293 req->result = fdatasync (req->fd); break;
294#endif
295 case REQ_FSYNC: req->result = fsync (req->fd); break; 676 case REQ_FSYNC: req->result = fsync (req->fd); break;
677 case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break;
296 678
297 case REQ_QUIT: 679 case REQ_QUIT:
298 break; 680 break;
299 681
300 default: 682 default:
326 while (type != REQ_QUIT); 708 while (type != REQ_QUIT);
327 709
328 return 0; 710 return 0;
329} 711}
330 712
713/*****************************************************************************/
714
715static void atfork_prepare (void)
716{
717 pthread_mutex_lock (&reqlock);
718 pthread_mutex_lock (&reslock);
719#if !HAVE_PREADWRITE
720 pthread_mutex_lock (&preadwritelock);
721#endif
722#if !HAVE_READDIR_R
723 pthread_mutex_lock (&readdirlock);
724#endif
725}
726
727static void atfork_parent (void)
728{
729#if !HAVE_READDIR_R
730 pthread_mutex_unlock (&readdirlock);
731#endif
732#if !HAVE_PREADWRITE
733 pthread_mutex_unlock (&preadwritelock);
734#endif
735 pthread_mutex_unlock (&reslock);
736 pthread_mutex_unlock (&reqlock);
737}
738
739static void atfork_child (void)
740{
741 aio_req prv;
742
743 started = 0;
744
745 while (reqs)
746 {
747 prv = reqs;
748 reqs = prv->next;
749 free_req (prv);
750 }
751
752 reqs = reqe = 0;
753
754 while (ress)
755 {
756 prv = ress;
757 ress = prv->next;
758 free_req (prv);
759 }
760
761 ress = rese = 0;
762
763 close (respipe [0]);
764 close (respipe [1]);
765 create_pipe ();
766
767 atfork_parent ();
768}
769
770#define dREQ \
771 aio_req req; \
772 \
773 if (SvOK (callback) && !SvROK (callback)) \
774 croak ("clalback must be undef or of reference type"); \
775 \
776 Newz (0, req, 1, aio_cb); \
777 if (!req) \
778 croak ("out of memory during aio_req allocation"); \
779 \
780 req->callback = newSVsv (callback);
781
331MODULE = IO::AIO PACKAGE = IO::AIO 782MODULE = IO::AIO PACKAGE = IO::AIO
332 783
333PROTOTYPES: ENABLE 784PROTOTYPES: ENABLE
334 785
335BOOT: 786BOOT:
336{ 787{
337 if (pipe (respipe)) 788 HV *stash = gv_stashpv ("IO::AIO", 1);
338 croak ("unable to initialize result pipe"); 789 newCONSTSUB (stash, "EXDEV", newSViv (EXDEV));
790 newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY));
791 newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY));
339 792
340 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)) 793 create_pipe ();
341 croak ("cannot set result pipe to nonblocking mode"); 794 pthread_atfork (atfork_prepare, atfork_parent, atfork_child);
342
343 if (fcntl (respipe [1], F_SETFL, O_NONBLOCK))
344 croak ("cannot set result pipe to nonblocking mode");
345} 795}
346 796
347void 797void
348min_parallel(nthreads) 798min_parallel (nthreads)
349 int nthreads 799 int nthreads
350 PROTOTYPE: $ 800 PROTOTYPE: $
351 CODE:
352 while (nthreads > started)
353 start_thread ();
354 801
355void 802void
356max_parallel(nthreads) 803max_parallel (nthreads)
357 int nthreads 804 int nthreads
358 PROTOTYPE: $ 805 PROTOTYPE: $
359 CODE:
360{
361 int cur = started;
362 while (cur > nthreads)
363 {
364 end_thread ();
365 cur--;
366 }
367
368 while (started > nthreads)
369 {
370 poll_wait ();
371 poll_cb ();
372 }
373}
374 806
375int 807int
376max_outstanding(nreqs) 808max_outstanding (nreqs)
377 int nreqs 809 int nreqs
378 PROTOTYPE: $ 810 PROTOTYPE: $
379 CODE: 811 CODE:
380 RETVAL = max_outstanding; 812 RETVAL = max_outstanding;
381 max_outstanding = nreqs; 813 max_outstanding = nreqs;
382 814
383void 815void
384aio_open(pathname,flags,mode,callback=&PL_sv_undef) 816aio_open (pathname,flags,mode,callback=&PL_sv_undef)
385 SV * pathname 817 SV * pathname
386 int flags 818 int flags
387 int mode 819 int mode
388 SV * callback 820 SV * callback
389 PROTOTYPE: $$$;$ 821 PROTOTYPE: $$$;$
390 CODE: 822 CODE:
391{ 823{
392 aio_req req; 824 dREQ;
393
394 Newz (0, req, 1, aio_cb);
395
396 if (!req)
397 croak ("out of memory during aio_req allocation");
398 825
399 req->type = REQ_OPEN; 826 req->type = REQ_OPEN;
400 req->data = newSVsv (pathname); 827 req->data = newSVsv (pathname);
401 req->dataptr = SvPV_nolen (req->data); 828 req->dataptr = SvPVbyte_nolen (req->data);
402 req->fd = flags; 829 req->fd = flags;
403 req->mode = mode; 830 req->mode = mode;
404 req->callback = SvREFCNT_inc (callback);
405 831
406 send_req (req); 832 send_req (req);
407} 833}
408 834
409void 835void
410aio_close(fh,callback=&PL_sv_undef) 836aio_close (fh,callback=&PL_sv_undef)
411 SV * fh 837 SV * fh
412 SV * callback 838 SV * callback
413 PROTOTYPE: $;$ 839 PROTOTYPE: $;$
414 ALIAS: 840 ALIAS:
415 aio_close = REQ_CLOSE 841 aio_close = REQ_CLOSE
416 aio_fsync = REQ_FSYNC 842 aio_fsync = REQ_FSYNC
417 aio_fdatasync = REQ_FDATASYNC 843 aio_fdatasync = REQ_FDATASYNC
418 CODE: 844 CODE:
419{ 845{
420 aio_req req; 846 dREQ;
421
422 Newz (0, req, 1, aio_cb);
423
424 if (!req)
425 croak ("out of memory during aio_req allocation");
426 847
427 req->type = ix; 848 req->type = ix;
428 req->fh = newSVsv (fh); 849 req->fh = newSVsv (fh);
429 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); 850 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh)));
430 req->callback = SvREFCNT_inc (callback);
431 851
432 send_req (req); 852 send_req (req);
433} 853}
434 854
435void 855void
436aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) 856aio_read (fh,offset,length,data,dataoffset,callback=&PL_sv_undef)
437 SV * fh 857 SV * fh
438 UV offset 858 UV offset
439 IV length 859 UV length
440 SV * data 860 SV * data
441 IV dataoffset 861 UV dataoffset
442 SV * callback 862 SV * callback
443 ALIAS: 863 ALIAS:
444 aio_read = REQ_READ 864 aio_read = REQ_READ
445 aio_write = REQ_WRITE 865 aio_write = REQ_WRITE
446 PROTOTYPE: $$$$$;$ 866 PROTOTYPE: $$$$$;$
447 CODE: 867 CODE:
448{ 868{
449 aio_req req; 869 aio_req req;
450 STRLEN svlen; 870 STRLEN svlen;
451 char *svptr = SvPV (data, svlen); 871 char *svptr = SvPVbyte (data, svlen);
452 872
453 SvUPGRADE (data, SVt_PV); 873 SvUPGRADE (data, SVt_PV);
454 SvPOK_on (data); 874 SvPOK_on (data);
455 875
456 if (dataoffset < 0) 876 if (dataoffset < 0)
472 } 892 }
473 893
474 if (length < 0) 894 if (length < 0)
475 croak ("length must not be negative"); 895 croak ("length must not be negative");
476 896
477 Newz (0, req, 1, aio_cb); 897 {
898 dREQ;
478 899
479 if (!req)
480 croak ("out of memory during aio_req allocation");
481
482 req->type = ix; 900 req->type = ix;
483 req->fh = newSVsv (fh); 901 req->fh = newSVsv (fh);
484 req->fd = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh)) 902 req->fd = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh))
485 : IoOFP (sv_2io (fh))); 903 : IoOFP (sv_2io (fh)));
486 req->offset = offset; 904 req->offset = offset;
905 req->length = length;
906 req->data = SvREFCNT_inc (data);
907 req->dataptr = (char *)svptr + dataoffset;
908
909 if (!SvREADONLY (data))
910 {
911 SvREADONLY_on (data);
912 req->data2ptr = (void *)data;
913 }
914
915 send_req (req);
916 }
917}
918
919void
920aio_sendfile (out_fh,in_fh,in_offset,length,callback=&PL_sv_undef)
921 SV * out_fh
922 SV * in_fh
923 UV in_offset
924 UV length
925 SV * callback
926 PROTOTYPE: $$$$;$
927 CODE:
928{
929 dREQ;
930
931 req->type = REQ_SENDFILE;
932 req->fh = newSVsv (out_fh);
933 req->fd = PerlIO_fileno (IoIFP (sv_2io (out_fh)));
934 req->fh2 = newSVsv (in_fh);
935 req->fd2 = PerlIO_fileno (IoIFP (sv_2io (in_fh)));
936 req->offset = in_offset;
487 req->length = length; 937 req->length = length;
488 req->data = SvREFCNT_inc (data);
489 req->dataptr = (char *)svptr + dataoffset;
490 req->callback = SvREFCNT_inc (callback);
491 938
492 send_req (req); 939 send_req (req);
493} 940}
494 941
495void 942void
496aio_readahead(fh,offset,length,callback=&PL_sv_undef) 943aio_readahead (fh,offset,length,callback=&PL_sv_undef)
497 SV * fh 944 SV * fh
498 UV offset 945 UV offset
499 IV length 946 IV length
500 SV * callback 947 SV * callback
501 PROTOTYPE: $$$;$ 948 PROTOTYPE: $$$;$
502 CODE: 949 CODE:
503{ 950{
504 aio_req req; 951 dREQ;
505
506 if (length < 0)
507 croak ("length must not be negative");
508
509 Newz (0, req, 1, aio_cb);
510
511 if (!req)
512 croak ("out of memory during aio_req allocation");
513 952
514 req->type = REQ_READAHEAD; 953 req->type = REQ_READAHEAD;
515 req->fh = newSVsv (fh); 954 req->fh = newSVsv (fh);
516 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); 955 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh)));
517 req->offset = offset; 956 req->offset = offset;
518 req->length = length; 957 req->length = length;
519 req->callback = SvREFCNT_inc (callback);
520 958
521 send_req (req); 959 send_req (req);
522} 960}
523 961
524void 962void
525aio_stat(fh_or_path,callback=&PL_sv_undef) 963aio_stat (fh_or_path,callback=&PL_sv_undef)
526 SV * fh_or_path 964 SV * fh_or_path
527 SV * callback 965 SV * callback
528 ALIAS: 966 ALIAS:
529 aio_stat = REQ_STAT 967 aio_stat = REQ_STAT
530 aio_lstat = REQ_LSTAT 968 aio_lstat = REQ_LSTAT
531 CODE: 969 CODE:
532{ 970{
533 aio_req req; 971 dREQ;
534
535 Newz (0, req, 1, aio_cb);
536
537 if (!req)
538 croak ("out of memory during aio_req allocation");
539 972
540 New (0, req->statdata, 1, Stat_t); 973 New (0, req->statdata, 1, Stat_t);
541
542 if (!req->statdata) 974 if (!req->statdata)
975 {
976 free_req (req);
543 croak ("out of memory during aio_req->statdata allocation"); 977 croak ("out of memory during aio_req->statdata allocation");
978 }
544 979
545 if (SvPOK (fh_or_path)) 980 if (SvPOK (fh_or_path))
546 { 981 {
547 req->type = ix; 982 req->type = ix;
548 req->data = newSVsv (fh_or_path); 983 req->data = newSVsv (fh_or_path);
549 req->dataptr = SvPV_nolen (req->data); 984 req->dataptr = SvPVbyte_nolen (req->data);
550 } 985 }
551 else 986 else
552 { 987 {
553 req->type = REQ_FSTAT; 988 req->type = REQ_FSTAT;
554 req->fh = newSVsv (fh_or_path); 989 req->fh = newSVsv (fh_or_path);
555 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); 990 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
556 } 991 }
557 992
558 req->callback = SvREFCNT_inc (callback);
559
560 send_req (req); 993 send_req (req);
561} 994}
562 995
563void 996void
564aio_unlink(pathname,callback=&PL_sv_undef) 997aio_unlink (pathname,callback=&PL_sv_undef)
565 SV * pathname 998 SV * pathname
566 SV * callback 999 SV * callback
1000 ALIAS:
1001 aio_unlink = REQ_UNLINK
1002 aio_rmdir = REQ_RMDIR
1003 aio_readdir = REQ_READDIR
567 CODE: 1004 CODE:
568{ 1005{
569 aio_req req; 1006 dREQ;
570 1007
571 Newz (0, req, 1, aio_cb); 1008 req->type = ix;
572
573 if (!req)
574 croak ("out of memory during aio_req allocation");
575
576 req->type = REQ_UNLINK;
577 req->data = newSVsv (pathname); 1009 req->data = newSVsv (pathname);
578 req->dataptr = SvPV_nolen (req->data); 1010 req->dataptr = SvPVbyte_nolen (req->data);
579 req->callback = SvREFCNT_inc (callback);
580 1011
581 send_req (req); 1012 send_req (req);
582} 1013}
583 1014
584void 1015void
1016aio_link (oldpath,newpath,callback=&PL_sv_undef)
1017 SV * oldpath
1018 SV * newpath
1019 SV * callback
1020 ALIAS:
1021 aio_link = REQ_LINK
1022 aio_symlink = REQ_SYMLINK
1023 aio_rename = REQ_RENAME
1024 CODE:
1025{
1026 dREQ;
1027
1028 req->type = ix;
1029 req->fh = newSVsv (oldpath);
1030 req->data2ptr = SvPVbyte_nolen (req->fh);
1031 req->data = newSVsv (newpath);
1032 req->dataptr = SvPVbyte_nolen (req->data);
1033
1034 send_req (req);
1035}
1036
1037void
585flush() 1038flush ()
586 PROTOTYPE: 1039 PROTOTYPE:
587 CODE: 1040 CODE:
588 while (nreqs) 1041 while (nreqs)
589 { 1042 {
590 poll_wait (); 1043 poll_wait ();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines