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.26 by root, Wed Aug 17 03:52:20 2005 UTC vs.
Revision 1.40 by root, Sat Jun 24 16:27:02 2006 UTC

5#include "perl.h" 5#include "perl.h"
6#include "XSUB.h" 6#include "XSUB.h"
7 7
8#include "autoconf/config.h" 8#include "autoconf/config.h"
9 9
10#include <pthread.h>
11
12#include <stddef.h>
10#include <sys/types.h> 13#include <sys/types.h>
11#include <sys/stat.h> 14#include <sys/stat.h>
12 15#include <limits.h>
13#include <unistd.h> 16#include <unistd.h>
14#include <fcntl.h> 17#include <fcntl.h>
15#include <signal.h> 18#include <signal.h>
16#include <sched.h> 19#include <sched.h>
17 20
18#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
19 35
20typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ 36/* used for struct dirent, AIX doesn't provide it */
21typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ 37#ifndef NAME_MAX
22typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ 38# define NAME_MAX 4096
39#endif
23 40
24#if __ia64 41#if __ia64
25# define STACKSIZE 65536 42# define STACKSIZE 65536
26#else 43#else
27# define STACKSIZE 4096 44# define STACKSIZE 8192
28#endif 45#endif
29 46
30enum { 47enum {
31 REQ_QUIT, 48 REQ_QUIT,
32 REQ_OPEN, REQ_CLOSE, 49 REQ_OPEN, REQ_CLOSE,
33 REQ_READ, REQ_WRITE, REQ_READAHEAD, 50 REQ_READ, REQ_WRITE, REQ_READAHEAD,
51 REQ_SENDFILE,
34 REQ_STAT, REQ_LSTAT, REQ_FSTAT, 52 REQ_STAT, REQ_LSTAT, REQ_FSTAT,
35 REQ_FSYNC, REQ_FDATASYNC, 53 REQ_FSYNC, REQ_FDATASYNC,
36 REQ_UNLINK, REQ_RMDIR, 54 REQ_UNLINK, REQ_RMDIR, REQ_RENAME,
37 REQ_SYMLINK, 55 REQ_READDIR,
56 REQ_LINK, REQ_SYMLINK,
38}; 57};
39 58
40typedef struct aio_cb { 59typedef struct aio_cb {
41 struct aio_cb *volatile next; 60 struct aio_cb *volatile next;
42 61
43 int type; 62 int type;
44 63
64 /* should receive a cleanup, with unions */
45 int fd; 65 int fd, fd2;
46 off_t offset; 66 off_t offset;
47 size_t length; 67 size_t length;
48 ssize_t result; 68 ssize_t result;
49 mode_t mode; /* open */ 69 mode_t mode; /* open */
50 int errorno; 70 int errorno;
51 SV *data, *callback, *fh; 71 SV *data, *callback;
72 SV *fh, *fh2;
52 void *dataptr, *data2ptr; 73 void *dataptr, *data2ptr;
53 STRLEN dataoffset; 74 STRLEN dataoffset;
54 75
55 Stat_t *statdata; 76 Stat_t *statdata;
56} aio_cb; 77} aio_cb;
57 78
58typedef aio_cb *aio_req; 79typedef aio_cb *aio_req;
59 80
60static int started; 81static int started, wanted;
61static volatile int nreqs; 82static volatile int nreqs;
62static int max_outstanding = 1<<30; 83static int max_outstanding = 1<<30;
63static int respipe [2]; 84static int respipe [2];
64 85
65static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; 86static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER;
66static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; 87static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER;
67static pthread_mutex_t frklock = PTHREAD_MUTEX_INITIALIZER;
68static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; 88static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER;
69 89
70static volatile aio_req reqs, reqe; /* queue start, queue end */ 90static volatile aio_req reqs, reqe; /* queue start, queue end */
71static volatile aio_req ress, rese; /* queue start, queue end */ 91static volatile aio_req ress, rese; /* queue start, queue end */
72 92
76 SvREFCNT_dec (req->data); 96 SvREFCNT_dec (req->data);
77 97
78 if (req->fh) 98 if (req->fh)
79 SvREFCNT_dec (req->fh); 99 SvREFCNT_dec (req->fh);
80 100
81 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) 101 if (req->fh2)
102 SvREFCNT_dec (req->fh2);
103
104 if (req->statdata)
82 Safefree (req->statdata); 105 Safefree (req->statdata);
83 106
84 if (req->callback) 107 if (req->callback)
85 SvREFCNT_dec (req->callback); 108 SvREFCNT_dec (req->callback);
109
110 if (req->type == REQ_READDIR && req->result >= 0)
111 free (req->data2ptr);
86 112
87 Safefree (req); 113 Safefree (req);
88} 114}
89 115
90static void 116static void
104poll_cb () 130poll_cb ()
105{ 131{
106 dSP; 132 dSP;
107 int count = 0; 133 int count = 0;
108 int do_croak = 0; 134 int do_croak = 0;
109 aio_req req, prv; 135 aio_req req;
110 136
137 for (;;)
138 {
111 pthread_mutex_lock (&reslock); 139 pthread_mutex_lock (&reslock);
140 req = ress;
112 141
113 { 142 if (req)
143 {
144 ress = req->next;
145
146 if (!ress)
147 {
114 /* read any signals sent by the worker threads */ 148 /* read any signals sent by the worker threads */
115 char buf [32]; 149 char buf [32];
116 while (read (respipe [0], buf, 32) == 32) 150 while (read (respipe [0], buf, 32) == 32)
151 ;
152
153 rese = 0;
154 }
117 ; 155 }
118 }
119 156
120 req = ress;
121 ress = rese = 0;
122
123 pthread_mutex_unlock (&reslock); 157 pthread_mutex_unlock (&reslock);
124 158
125 while (req) 159 if (!req)
126 { 160 break;
161
127 nreqs--; 162 nreqs--;
128 163
129 if (req->type == REQ_QUIT) 164 if (req->type == REQ_QUIT)
130 started--; 165 started--;
131 else 166 else
132 { 167 {
133 int errorno = errno; 168 int errorno = errno;
134 errno = req->errorno; 169 errno = req->errorno;
135 170
136 if (req->type == REQ_READ) 171 if (req->type == REQ_READ)
137 SvCUR_set (req->data, req->dataoffset 172 SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0));
138 + req->result > 0 ? req->result : 0);
139 173
140 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) 174 if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE))
175 SvREADONLY_off (req->data);
176
177 if (req->statdata)
141 { 178 {
142 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; 179 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
143 PL_laststatval = req->result; 180 PL_laststatval = req->result;
144 PL_statcache = *(req->statdata); 181 PL_statcache = *(req->statdata);
145 } 182 }
146 183
147 ENTER; 184 ENTER;
148 PUSHMARK (SP); 185 PUSHMARK (SP);
149 XPUSHs (sv_2mortal (newSViv (req->result)));
150 186
151 if (req->type == REQ_OPEN) 187 if (req->type == REQ_READDIR)
152 { 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 {
153 /* convert fd to fh */ 216 /* convert fd to fh */
154 SV *fh; 217 SV *fh;
155 218
156 PUTBACK; 219 PUTBACK;
157 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); 220 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL);
158 SPAGAIN; 221 SPAGAIN;
159 222
160 fh = SvREFCNT_inc (POPs); 223 fh = SvREFCNT_inc (POPs);
161 224
162 PUSHMARK (SP); 225 PUSHMARK (SP);
163 XPUSHs (sv_2mortal (fh)); 226 XPUSHs (sv_2mortal (fh));
227 }
164 } 228 }
165 229
166 if (SvOK (req->callback)) 230 if (SvOK (req->callback))
167 { 231 {
168 PUTBACK; 232 PUTBACK;
169 call_sv (req->callback, G_VOID | G_EVAL); 233 call_sv (req->callback, G_VOID | G_EVAL);
170 SPAGAIN; 234 SPAGAIN;
235
236 if (SvTRUE (ERRSV))
237 {
238 free_req (req);
239 croak (0);
240 }
171 } 241 }
172 242
173 LEAVE; 243 LEAVE;
174 244
175 do_croak = SvTRUE (ERRSV);
176
177 errno = errorno; 245 errno = errorno;
178 count++; 246 count++;
179 } 247 }
180 248
181 prv = req;
182 req = req->next;
183 free_req (prv); 249 free_req (req);
184
185 if (do_croak)
186 croak (0);
187 } 250 }
188 251
189 return count; 252 return count;
190} 253}
191 254
212} 275}
213 276
214static void 277static void
215send_req (aio_req req) 278send_req (aio_req req)
216{ 279{
280 while (started < wanted && nreqs >= started)
281 start_thread ();
282
217 nreqs++; 283 nreqs++;
218 284
219 pthread_mutex_lock (&reqlock); 285 pthread_mutex_lock (&reqlock);
220 286
221 req->next = 0; 287 req->next = 0;
229 reqe = reqs = req; 295 reqe = reqs = req;
230 296
231 pthread_cond_signal (&reqwait); 297 pthread_cond_signal (&reqwait);
232 pthread_mutex_unlock (&reqlock); 298 pthread_mutex_unlock (&reqlock);
233 299
234 while (nreqs > max_outstanding) 300 if (nreqs > max_outstanding)
301 for (;;)
235 { 302 {
303 poll_cb ();
304
305 if (nreqs <= max_outstanding)
306 break;
307
236 poll_wait (); 308 poll_wait ();
237 poll_cb ();
238 } 309 }
239} 310}
240 311
241static void 312static void
242end_thread (void) 313end_thread (void)
243{ 314{
246 req->type = REQ_QUIT; 317 req->type = REQ_QUIT;
247 318
248 send_req (req); 319 send_req (req);
249} 320}
250 321
251
252static void min_parallel (int nthreads) 322static void min_parallel (int nthreads)
253{ 323{
254 while (nthreads > started) 324 if (wanted < nthreads)
255 start_thread (); 325 wanted = nthreads;
256} 326}
257 327
258static void max_parallel (int nthreads) 328static void max_parallel (int nthreads)
259{ 329{
260 int cur = started; 330 int cur = started;
331
332 if (wanted > nthreads)
333 wanted = nthreads;
334
261 while (cur > nthreads) 335 while (cur > wanted)
262 { 336 {
263 end_thread (); 337 end_thread ();
264 cur--; 338 cur--;
265 } 339 }
266 340
267 while (started > nthreads) 341 while (started > wanted)
268 { 342 {
269 poll_wait (); 343 poll_wait ();
270 poll_cb (); 344 poll_cb ();
271 } 345 }
272} 346}
279 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)) 353 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK))
280 croak ("cannot set result pipe to nonblocking mode"); 354 croak ("cannot set result pipe to nonblocking mode");
281 355
282 if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) 356 if (fcntl (respipe [1], F_SETFL, O_NONBLOCK))
283 croak ("cannot set result pipe to nonblocking mode"); 357 croak ("cannot set result pipe to nonblocking mode");
284}
285
286static void atfork_prepare (void)
287{
288 pthread_mutex_lock (&frklock);
289 pthread_mutex_lock (&reqlock);
290 pthread_mutex_lock (&reslock);
291}
292
293static void atfork_parent (void)
294{
295 pthread_mutex_unlock (&reslock);
296 pthread_mutex_unlock (&reqlock);
297 pthread_mutex_unlock (&frklock);
298}
299
300static void atfork_child (void)
301{
302 int restart = started;
303 started = 0;
304
305 while (reqs)
306 {
307 free_req (reqs);
308 reqs = reqs->next;
309 }
310
311 reqs = reqe = 0;
312
313 while (ress)
314 {
315 free_req (ress);
316 ress = ress->next;
317 }
318
319 ress = rese = 0;
320
321 close (respipe [0]);
322 close (respipe [1]);
323
324 create_pipe ();
325
326 atfork_parent ();
327
328 min_parallel (restart);
329} 358}
330 359
331/*****************************************************************************/ 360/*****************************************************************************/
332/* work around various missing functions */ 361/* work around various missing functions */
333 362
338/* 367/*
339 * make our pread/pwrite safe against themselves, but not against 368 * make our pread/pwrite safe against themselves, but not against
340 * normal read/write by using a mutex. slows down execution a lot, 369 * normal read/write by using a mutex. slows down execution a lot,
341 * but that's your problem, not mine. 370 * but that's your problem, not mine.
342 */ 371 */
343static pthread_mutex_t iolock = PTHREAD_MUTEX_INITIALIZER; 372static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER;
344 373
345static ssize_t 374static ssize_t
346pread (int fd, void *buf, size_t count, off_t offset) 375pread (int fd, void *buf, size_t count, off_t offset)
347{ 376{
348 ssize_t res; 377 ssize_t res;
349 off_t ooffset; 378 off_t ooffset;
350 379
351 pthread_mutex_lock (&iolock); 380 pthread_mutex_lock (&preadwritelock);
352 ooffset = lseek (fd, 0, SEEK_CUR); 381 ooffset = lseek (fd, 0, SEEK_CUR);
353 lseek (fd, offset, SEEK_SET); 382 lseek (fd, offset, SEEK_SET);
354 res = read (fd, buf, count); 383 res = read (fd, buf, count);
355 lseek (fd, ooffset, SEEK_SET); 384 lseek (fd, ooffset, SEEK_SET);
356 pthread_mutex_unlock (&iolock); 385 pthread_mutex_unlock (&preadwritelock);
357 386
358 return res; 387 return res;
359} 388}
360 389
361static ssize_t 390static ssize_t
362pwrite (int fd, void *buf, size_t count, off_t offset) 391pwrite (int fd, void *buf, size_t count, off_t offset)
363{ 392{
364 ssize_t res; 393 ssize_t res;
365 off_t ooffset; 394 off_t ooffset;
366 395
367 pthread_mutex_lock (&iolock); 396 pthread_mutex_lock (&preadwritelock);
368 ooffset = lseek (fd, 0, SEEK_CUR); 397 ooffset = lseek (fd, 0, SEEK_CUR);
369 lseek (fd, offset, SEEK_SET); 398 lseek (fd, offset, SEEK_SET);
370 res = write (fd, buf, count); 399 res = write (fd, buf, count);
371 lseek (fd, offset, SEEK_SET); 400 lseek (fd, offset, SEEK_SET);
372 pthread_mutex_unlock (&iolock); 401 pthread_mutex_unlock (&preadwritelock);
373 402
374 return res; 403 return res;
375} 404}
376#endif 405#endif
377 406
380#endif 409#endif
381 410
382#if !HAVE_READAHEAD 411#if !HAVE_READAHEAD
383# define readahead aio_readahead 412# define readahead aio_readahead
384 413
385static char readahead_buf[4096];
386
387static ssize_t 414static ssize_t
388readahead (int fd, off_t offset, size_t count) 415readahead (int fd, off_t offset, size_t count)
389{ 416{
417 char readahead_buf[4096];
418
390 while (count > 0) 419 while (count > 0)
391 { 420 {
392 size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); 421 size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf);
393 422
394 pread (fd, readahead_buf, len, offset); 423 pread (fd, readahead_buf, len, offset);
397 } 426 }
398 427
399 errno = 0; 428 errno = 0;
400} 429}
401#endif 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
437static int
438readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res)
439{
440 struct dirent *e;
441 int errorno;
442
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;
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}
402 619
403/*****************************************************************************/ 620/*****************************************************************************/
404 621
405static void * 622static void *
406aio_proc (void *thr_arg) 623aio_proc (void *thr_arg)
438 { 655 {
439 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;
440 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;
441 658
442 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;
660 case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length); break;
443 661
444 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; 662 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break;
445 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; 663 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break;
446 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; 664 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break;
447 665
448 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;
449 case REQ_CLOSE: req->result = close (req->fd); break; 667 case REQ_CLOSE: req->result = close (req->fd); break;
450 case REQ_UNLINK: req->result = unlink (req->dataptr); break; 668 case REQ_UNLINK: req->result = unlink (req->dataptr); break;
451 case REQ_RMDIR: req->result = rmdir (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;
452 case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; 672 case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break;
453 673
454 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; 674 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break;
455 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;
456 677
457 case REQ_QUIT: 678 case REQ_QUIT:
458 break; 679 break;
459 680
460 default: 681 default:
484 pthread_mutex_unlock (&reslock); 705 pthread_mutex_unlock (&reslock);
485 } 706 }
486 while (type != REQ_QUIT); 707 while (type != REQ_QUIT);
487 708
488 return 0; 709 return 0;
710}
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 ();
489} 767}
490 768
491#define dREQ \ 769#define dREQ \
492 aio_req req; \ 770 aio_req req; \
493 \ 771 \
496 \ 774 \
497 Newz (0, req, 1, aio_cb); \ 775 Newz (0, req, 1, aio_cb); \
498 if (!req) \ 776 if (!req) \
499 croak ("out of memory during aio_req allocation"); \ 777 croak ("out of memory during aio_req allocation"); \
500 \ 778 \
501 req->callback = SvREFCNT_inc (callback); 779 req->callback = newSVsv (callback);
502 780
503MODULE = IO::AIO PACKAGE = IO::AIO 781MODULE = IO::AIO PACKAGE = IO::AIO
504 782
505PROTOTYPES: ENABLE 783PROTOTYPES: ENABLE
506 784
509 create_pipe (); 787 create_pipe ();
510 pthread_atfork (atfork_prepare, atfork_parent, atfork_child); 788 pthread_atfork (atfork_prepare, atfork_parent, atfork_child);
511} 789}
512 790
513void 791void
514min_parallel(nthreads) 792min_parallel (nthreads)
515 int nthreads 793 int nthreads
516 PROTOTYPE: $ 794 PROTOTYPE: $
517 795
518void 796void
519max_parallel(nthreads) 797max_parallel (nthreads)
520 int nthreads 798 int nthreads
521 PROTOTYPE: $ 799 PROTOTYPE: $
522 800
523int 801int
524max_outstanding(nreqs) 802max_outstanding (nreqs)
525 int nreqs 803 int nreqs
526 PROTOTYPE: $ 804 PROTOTYPE: $
527 CODE: 805 CODE:
528 RETVAL = max_outstanding; 806 RETVAL = max_outstanding;
529 max_outstanding = nreqs; 807 max_outstanding = nreqs;
530 808
531void 809void
532aio_open(pathname,flags,mode,callback=&PL_sv_undef) 810aio_open (pathname,flags,mode,callback=&PL_sv_undef)
533 SV * pathname 811 SV * pathname
534 int flags 812 int flags
535 int mode 813 int mode
536 SV * callback 814 SV * callback
537 PROTOTYPE: $$$;$ 815 PROTOTYPE: $$$;$
547 825
548 send_req (req); 826 send_req (req);
549} 827}
550 828
551void 829void
552aio_close(fh,callback=&PL_sv_undef) 830aio_close (fh,callback=&PL_sv_undef)
553 SV * fh 831 SV * fh
554 SV * callback 832 SV * callback
555 PROTOTYPE: $;$ 833 PROTOTYPE: $;$
556 ALIAS: 834 ALIAS:
557 aio_close = REQ_CLOSE 835 aio_close = REQ_CLOSE
567 845
568 send_req (req); 846 send_req (req);
569} 847}
570 848
571void 849void
572aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) 850aio_read (fh,offset,length,data,dataoffset,callback=&PL_sv_undef)
573 SV * fh 851 SV * fh
574 UV offset 852 UV offset
575 IV length 853 UV length
576 SV * data 854 SV * data
577 IV dataoffset 855 UV dataoffset
578 SV * callback 856 SV * callback
579 ALIAS: 857 ALIAS:
580 aio_read = REQ_READ 858 aio_read = REQ_READ
581 aio_write = REQ_WRITE 859 aio_write = REQ_WRITE
582 PROTOTYPE: $$$$$;$ 860 PROTOTYPE: $$$$$;$
619 : IoOFP (sv_2io (fh))); 897 : IoOFP (sv_2io (fh)));
620 req->offset = offset; 898 req->offset = offset;
621 req->length = length; 899 req->length = length;
622 req->data = SvREFCNT_inc (data); 900 req->data = SvREFCNT_inc (data);
623 req->dataptr = (char *)svptr + dataoffset; 901 req->dataptr = (char *)svptr + dataoffset;
624 req->callback = SvREFCNT_inc (callback); 902
903 if (!SvREADONLY (data))
904 {
905 SvREADONLY_on (data);
906 req->data2ptr = (void *)data;
907 }
625 908
626 send_req (req); 909 send_req (req);
627 } 910 }
628} 911}
629 912
630void 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;
931 req->length = length;
932
933 send_req (req);
934}
935
936void
631aio_readahead(fh,offset,length,callback=&PL_sv_undef) 937aio_readahead (fh,offset,length,callback=&PL_sv_undef)
632 SV * fh 938 SV * fh
633 UV offset 939 UV offset
634 IV length 940 IV length
635 SV * callback 941 SV * callback
636 PROTOTYPE: $$$;$ 942 PROTOTYPE: $$$;$
646 952
647 send_req (req); 953 send_req (req);
648} 954}
649 955
650void 956void
651aio_stat(fh_or_path,callback=&PL_sv_undef) 957aio_stat (fh_or_path,callback=&PL_sv_undef)
652 SV * fh_or_path 958 SV * fh_or_path
653 SV * callback 959 SV * callback
654 ALIAS: 960 ALIAS:
655 aio_stat = REQ_STAT 961 aio_stat = REQ_STAT
656 aio_lstat = REQ_LSTAT 962 aio_lstat = REQ_LSTAT
658{ 964{
659 dREQ; 965 dREQ;
660 966
661 New (0, req->statdata, 1, Stat_t); 967 New (0, req->statdata, 1, Stat_t);
662 if (!req->statdata) 968 if (!req->statdata)
969 {
970 free_req (req);
663 croak ("out of memory during aio_req->statdata allocation (sorry, i just leaked memory, too)"); 971 croak ("out of memory during aio_req->statdata allocation");
972 }
664 973
665 if (SvPOK (fh_or_path)) 974 if (SvPOK (fh_or_path))
666 { 975 {
667 req->type = ix; 976 req->type = ix;
668 req->data = newSVsv (fh_or_path); 977 req->data = newSVsv (fh_or_path);
677 986
678 send_req (req); 987 send_req (req);
679} 988}
680 989
681void 990void
682aio_unlink(pathname,callback=&PL_sv_undef) 991aio_unlink (pathname,callback=&PL_sv_undef)
683 SV * pathname 992 SV * pathname
684 SV * callback 993 SV * callback
685 ALIAS: 994 ALIAS:
686 aio_unlink = REQ_UNLINK 995 aio_unlink = REQ_UNLINK
687 aio_rmdir = REQ_RMDIR 996 aio_rmdir = REQ_RMDIR
997 aio_readdir = REQ_READDIR
688 CODE: 998 CODE:
689{ 999{
690 dREQ; 1000 dREQ;
691 1001
692 req->type = ix; 1002 req->type = ix;
695 1005
696 send_req (req); 1006 send_req (req);
697} 1007}
698 1008
699void 1009void
700aio_symlink(oldpath,newpath,callback=&PL_sv_undef) 1010aio_link (oldpath,newpath,callback=&PL_sv_undef)
701 SV * oldpath 1011 SV * oldpath
702 SV * newpath 1012 SV * newpath
703 SV * callback 1013 SV * callback
1014 ALIAS:
1015 aio_link = REQ_LINK
1016 aio_symlink = REQ_SYMLINK
1017 aio_rename = REQ_RENAME
704 CODE: 1018 CODE:
705{ 1019{
706 dREQ; 1020 dREQ;
707 1021
708 req->type = REQ_SYMLINK; 1022 req->type = ix;
709 req->fh = newSVsv (oldpath); 1023 req->fh = newSVsv (oldpath);
710 req->data2ptr = SvPVbyte_nolen (req->fh); 1024 req->data2ptr = SvPVbyte_nolen (req->fh);
711 req->data = newSVsv (newpath); 1025 req->data = newSVsv (newpath);
712 req->dataptr = SvPVbyte_nolen (req->data); 1026 req->dataptr = SvPVbyte_nolen (req->data);
713 1027
714 send_req (req); 1028 send_req (req);
715} 1029}
716 1030
717void 1031void
718flush() 1032flush ()
719 PROTOTYPE: 1033 PROTOTYPE:
720 CODE: 1034 CODE:
721 while (nreqs) 1035 while (nreqs)
722 { 1036 {
723 poll_wait (); 1037 poll_wait ();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines