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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines