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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines