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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines