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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines