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.19 by root, Sun Jul 31 19:04:45 2005 UTC vs.
Revision 1.43 by root, Sat Oct 21 23:06:04 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines