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.34 by root, Tue Aug 23 00:03:14 2005 UTC vs.
Revision 1.48 by root, Sun Oct 22 10:33:19 2006 UTC

7 7
8#include "autoconf/config.h" 8#include "autoconf/config.h"
9 9
10#include <pthread.h> 10#include <pthread.h>
11 11
12#include <stddef.h>
13#include <errno.h>
14#include <sys/time.h>
15#include <sys/select.h>
12#include <sys/types.h> 16#include <sys/types.h>
13#include <sys/stat.h> 17#include <sys/stat.h>
14 18#include <limits.h>
15#include <unistd.h> 19#include <unistd.h>
16#include <fcntl.h> 20#include <fcntl.h>
17#include <signal.h> 21#include <signal.h>
18#include <sched.h> 22#include <sched.h>
19 23
23# elif __freebsd 27# elif __freebsd
24# include <sys/socket.h> 28# include <sys/socket.h>
25# include <sys/uio.h> 29# include <sys/uio.h>
26# elif __hpux 30# elif __hpux
27# include <sys/socket.h> 31# include <sys/socket.h>
32# elif __solaris /* not yet */
33# include <sys/sendfile.h>
28# else 34# else
29# error sendfile support requested but not available 35# error sendfile support requested but not available
30# endif 36# endif
31#endif 37#endif
32 38
39/* used for struct dirent, AIX doesn't provide it */
40#ifndef NAME_MAX
41# define NAME_MAX 4096
42#endif
43
33#if __ia64 44#if __ia64
34# define STACKSIZE 65536 45# define STACKSIZE 65536
35#else 46#else
36# define STACKSIZE 4096 47# define STACKSIZE 8192
37#endif 48#endif
38 49
39enum { 50enum {
40 REQ_QUIT, 51 REQ_QUIT,
41 REQ_OPEN, REQ_CLOSE, 52 REQ_OPEN, REQ_CLOSE,
42 REQ_READ, REQ_WRITE, REQ_READAHEAD, 53 REQ_READ, REQ_WRITE, REQ_READAHEAD,
43 REQ_SENDFILE, 54 REQ_SENDFILE,
44 REQ_STAT, REQ_LSTAT, REQ_FSTAT, 55 REQ_STAT, REQ_LSTAT, REQ_FSTAT,
45 REQ_FSYNC, REQ_FDATASYNC, 56 REQ_FSYNC, REQ_FDATASYNC,
46 REQ_UNLINK, REQ_RMDIR, 57 REQ_UNLINK, REQ_RMDIR, REQ_RENAME,
47 REQ_SYMLINK, 58 REQ_READDIR,
59 REQ_LINK, REQ_SYMLINK,
60 REQ_SLEEP,
61 REQ_GROUP,
48}; 62};
49 63
64#define AIO_REQ_KLASS "IO::AIO::REQ"
65#define AIO_GRP_KLASS "IO::AIO::GRP"
66
50typedef struct aio_cb { 67typedef struct aio_cb
68{
69 struct aio_cb *grp, *grp_prev, *grp_next;
70
51 struct aio_cb *volatile next; 71 struct aio_cb *volatile next;
52 72
53 int type; 73 SV *self; /* the perl counterpart of this request, if any */
54 74
55 int fd, fd2; 75 SV *data, *callback;
76 SV *fh, *fh2;
77 void *dataptr, *data2ptr;
78 Stat_t *statdata;
56 off_t offset; 79 off_t offset;
57 size_t length; 80 size_t length;
58 ssize_t result; 81 ssize_t result;
82
83 int type;
84 int fd, fd2;
85 int errorno;
86 STRLEN dataoffset;
59 mode_t mode; /* open */ 87 mode_t mode; /* open */
60 int errorno; 88 unsigned char cancelled;
61 SV *data, *callback;
62 SV *fh, *fh2;
63 void *dataptr, *data2ptr;
64 STRLEN dataoffset;
65
66 Stat_t *statdata;
67} aio_cb; 89} aio_cb;
68 90
69typedef aio_cb *aio_req; 91typedef aio_cb *aio_req;
92typedef aio_cb *aio_req_ornot;
70 93
71static int started, wanted; 94static int started, wanted;
72static volatile int nreqs; 95static volatile int nreqs;
73static int max_outstanding = 1<<30; 96static int max_outstanding = 1<<30;
74static int respipe [2]; 97static int respipe [2];
78static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; 101static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER;
79 102
80static volatile aio_req reqs, reqe; /* queue start, queue end */ 103static volatile aio_req reqs, reqe; /* queue start, queue end */
81static volatile aio_req ress, rese; /* queue start, queue end */ 104static volatile aio_req ress, rese; /* queue start, queue end */
82 105
83static void free_req (aio_req req) 106static void req_free (aio_req req);
84{
85 if (req->data)
86 SvREFCNT_dec (req->data);
87 107
88 if (req->fh) 108/* must be called at most once */
89 SvREFCNT_dec (req->fh); 109static SV *req_sv (aio_req req, const char *klass)
110{
111 req->self = (SV *)newHV ();
112 sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0);
90 113
91 if (req->fh2) 114 return sv_2mortal (sv_bless (newRV_inc (req->self), gv_stashpv (klass, 1)));
92 SvREFCNT_dec (req->fh2);
93
94 if (req->statdata)
95 Safefree (req->statdata);
96
97 if (req->callback)
98 SvREFCNT_dec (req->callback);
99
100 Safefree (req);
101} 115}
102 116
103static void 117static aio_req SvAIO_REQ (SV *sv)
104poll_wait () 118{
119 if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv))
120 croak ("object of class " AIO_REQ_KLASS " expected");
121
122 MAGIC *mg = mg_find (SvRV (sv), PERL_MAGIC_ext);
123
124 return mg ? (aio_req)mg->mg_ptr : 0;
125}
126
127static void poll_wait ()
105{ 128{
106 if (nreqs && !ress) 129 if (nreqs && !ress)
107 { 130 {
108 fd_set rfd; 131 fd_set rfd;
109 FD_ZERO(&rfd); 132 FD_ZERO(&rfd);
111 134
112 select (respipe [0] + 1, &rfd, 0, 0, 0); 135 select (respipe [0] + 1, &rfd, 0, 0, 0);
113 } 136 }
114} 137}
115 138
116static int 139static void req_invoke (aio_req req)
117poll_cb () 140{
141 dSP;
142 int errorno = errno;
143
144 if (req->cancelled || !SvOK (req->callback))
145 return;
146
147 errno = req->errorno;
148
149 ENTER;
150 PUSHMARK (SP);
151 EXTEND (SP, 1);
152
153 switch (req->type)
154 {
155 case REQ_READDIR:
156 {
157 SV *rv = &PL_sv_undef;
158
159 if (req->result >= 0)
160 {
161 char *buf = req->data2ptr;
162 AV *av = newAV ();
163
164 while (req->result)
165 {
166 SV *sv = newSVpv (buf, 0);
167
168 av_push (av, sv);
169 buf += SvCUR (sv) + 1;
170 req->result--;
171 }
172
173 rv = sv_2mortal (newRV_noinc ((SV *)av));
174 }
175
176 PUSHs (rv);
177 }
178 break;
179
180 case REQ_OPEN:
181 {
182 /* convert fd to fh */
183 SV *fh;
184
185 PUSHs (sv_2mortal (newSViv (req->result)));
186 PUTBACK;
187 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL);
188 SPAGAIN;
189
190 fh = SvREFCNT_inc (POPs);
191
192 PUSHMARK (SP);
193 XPUSHs (sv_2mortal (fh));
194 }
195 break;
196
197 case REQ_GROUP:
198 if (req->data)
199 {
200 int i;
201 AV *av = (AV *)req->data;
202
203 EXTEND (SP, AvFILL (av) + 1);
204 for (i = 0; i <= AvFILL (av); ++i)
205 PUSHs (*av_fetch (av, i, 0));
206 }
207 break;
208
209 case REQ_SLEEP:
210 break;
211
212 default:
213 PUSHs (sv_2mortal (newSViv (req->result)));
214 break;
215 }
216
217
218 PUTBACK;
219 call_sv (req->callback, G_VOID | G_EVAL);
220 SPAGAIN;
221
222 if (SvTRUE (ERRSV))
223 {
224 req_free (req);
225 croak (0);
226 }
227
228 LEAVE;
229
230 errno = errorno;
231}
232
233static void req_free (aio_req req)
234{
235 if (req->grp)
236 {
237 aio_req grp = req->grp;
238
239 /* unlink request */
240 req->grp_next->grp_prev = req->grp_prev;
241 req->grp_prev->grp_next = req->grp_next;
242
243 if (grp->grp_next == grp && grp->fd)
244 {
245 req_invoke (grp);
246 req_free (grp);
247 }
248 }
249
250 if (req->self)
251 {
252 sv_unmagic (req->self, PERL_MAGIC_ext);
253 SvREFCNT_dec (req->self);
254 }
255
256 if (req->data)
257 SvREFCNT_dec (req->data);
258
259 if (req->fh)
260 SvREFCNT_dec (req->fh);
261
262 if (req->fh2)
263 SvREFCNT_dec (req->fh2);
264
265 if (req->statdata)
266 Safefree (req->statdata);
267
268 if (req->callback)
269 SvREFCNT_dec (req->callback);
270
271 if (req->type == REQ_READDIR && req->result >= 0)
272 free (req->data2ptr);
273
274 Safefree (req);
275}
276
277static void req_cancel (aio_req req)
278{
279 req->cancelled = 1;
280
281 if (req->type == REQ_GROUP)
282 {
283 aio_req sub;
284
285 for (sub = req->grp_next; sub != req; sub = sub->grp_next)
286 req_cancel (sub);
287 }
288}
289
290static int poll_cb ()
118{ 291{
119 dSP; 292 dSP;
120 int count = 0; 293 int count = 0;
121 int do_croak = 0; 294 int do_croak = 0;
122 aio_req req; 295 aio_req req;
148 321
149 nreqs--; 322 nreqs--;
150 323
151 if (req->type == REQ_QUIT) 324 if (req->type == REQ_QUIT)
152 started--; 325 started--;
326 else if (req->type == REQ_GROUP && req->grp_next != req)
327 {
328 req->fd = 1; /* mark request as delayed */
329 continue;
330 }
153 else 331 else
154 { 332 {
155 int errorno = errno;
156 errno = req->errorno;
157
158 if (req->type == REQ_READ) 333 if (req->type == REQ_READ)
159 SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); 334 SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0));
160 335
161 if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE)) 336 if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE))
162 SvREADONLY_off (req->data); 337 SvREADONLY_off (req->data);
166 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; 341 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
167 PL_laststatval = req->result; 342 PL_laststatval = req->result;
168 PL_statcache = *(req->statdata); 343 PL_statcache = *(req->statdata);
169 } 344 }
170 345
171 ENTER; 346 req_invoke (req);
172 PUSHMARK (SP);
173 XPUSHs (sv_2mortal (newSViv (req->result)));
174 347
175 if (req->type == REQ_OPEN)
176 {
177 /* convert fd to fh */
178 SV *fh;
179
180 PUTBACK;
181 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL);
182 SPAGAIN;
183
184 fh = SvREFCNT_inc (POPs);
185
186 PUSHMARK (SP);
187 XPUSHs (sv_2mortal (fh));
188 }
189
190 if (SvOK (req->callback))
191 {
192 PUTBACK;
193 call_sv (req->callback, G_VOID | G_EVAL);
194 SPAGAIN;
195
196 if (SvTRUE (ERRSV))
197 {
198 free_req (req);
199 croak (0);
200 }
201 }
202
203 LEAVE;
204
205 errno = errorno;
206 count++; 348 count++;
207 } 349 }
208 350
209 free_req (req); 351 req_free (req);
210 } 352 }
211 353
212 return count; 354 return count;
213} 355}
214 356
215static void *aio_proc(void *arg); 357static void *aio_proc(void *arg);
216 358
217static void
218start_thread (void) 359static void start_thread (void)
219{ 360{
220 sigset_t fullsigset, oldsigset; 361 sigset_t fullsigset, oldsigset;
221 pthread_t tid; 362 pthread_t tid;
222 pthread_attr_t attr; 363 pthread_attr_t attr;
223 364
232 started++; 373 started++;
233 374
234 sigprocmask (SIG_SETMASK, &oldsigset, 0); 375 sigprocmask (SIG_SETMASK, &oldsigset, 0);
235} 376}
236 377
237static void 378static void req_send (aio_req req)
238send_req (aio_req req)
239{ 379{
240 while (started < wanted && nreqs >= started) 380 while (started < wanted && nreqs >= started)
241 start_thread (); 381 start_thread ();
242 382
243 nreqs++; 383 nreqs++;
267 407
268 poll_wait (); 408 poll_wait ();
269 } 409 }
270} 410}
271 411
272static void 412static void end_thread (void)
273end_thread (void)
274{ 413{
275 aio_req req; 414 aio_req req;
276 Newz (0, req, 1, aio_cb); 415 Newz (0, req, 1, aio_cb);
277 req->type = REQ_QUIT; 416 req->type = REQ_QUIT;
278 417
279 send_req (req); 418 req_send (req);
280} 419}
281 420
282static void min_parallel (int nthreads) 421static void min_parallel (int nthreads)
283{ 422{
284 if (wanted < nthreads) 423 if (wanted < nthreads)
313 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)) 452 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK))
314 croak ("cannot set result pipe to nonblocking mode"); 453 croak ("cannot set result pipe to nonblocking mode");
315 454
316 if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) 455 if (fcntl (respipe [1], F_SETFL, O_NONBLOCK))
317 croak ("cannot set result pipe to nonblocking mode"); 456 croak ("cannot set result pipe to nonblocking mode");
318}
319
320static void atfork_prepare (void)
321{
322 pthread_mutex_lock (&reqlock);
323 pthread_mutex_lock (&reslock);
324}
325
326static void atfork_parent (void)
327{
328 pthread_mutex_unlock (&reslock);
329 pthread_mutex_unlock (&reqlock);
330}
331
332static void atfork_child (void)
333{
334 aio_req prv;
335
336 started = 0;
337
338 while (reqs)
339 {
340 prv = reqs;
341 reqs = prv->next;
342 free_req (prv);
343 }
344
345 reqs = reqe = 0;
346
347 while (ress)
348 {
349 prv = ress;
350 ress = prv->next;
351 free_req (prv);
352 }
353
354 ress = rese = 0;
355
356 close (respipe [0]);
357 close (respipe [1]);
358 create_pipe ();
359
360 atfork_parent ();
361} 457}
362 458
363/*****************************************************************************/ 459/*****************************************************************************/
364/* work around various missing functions */ 460/* work around various missing functions */
365 461
370/* 466/*
371 * make our pread/pwrite safe against themselves, but not against 467 * make our pread/pwrite safe against themselves, but not against
372 * normal read/write by using a mutex. slows down execution a lot, 468 * normal read/write by using a mutex. slows down execution a lot,
373 * but that's your problem, not mine. 469 * but that's your problem, not mine.
374 */ 470 */
375static pthread_mutex_t iolock = PTHREAD_MUTEX_INITIALIZER; 471static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER;
376 472
377static ssize_t 473static ssize_t pread (int fd, void *buf, size_t count, off_t offset)
378pread (int fd, void *buf, size_t count, off_t offset)
379{ 474{
380 ssize_t res; 475 ssize_t res;
381 off_t ooffset; 476 off_t ooffset;
382 477
383 pthread_mutex_lock (&iolock); 478 pthread_mutex_lock (&preadwritelock);
384 ooffset = lseek (fd, 0, SEEK_CUR); 479 ooffset = lseek (fd, 0, SEEK_CUR);
385 lseek (fd, offset, SEEK_SET); 480 lseek (fd, offset, SEEK_SET);
386 res = read (fd, buf, count); 481 res = read (fd, buf, count);
387 lseek (fd, ooffset, SEEK_SET); 482 lseek (fd, ooffset, SEEK_SET);
388 pthread_mutex_unlock (&iolock); 483 pthread_mutex_unlock (&preadwritelock);
389 484
390 return res; 485 return res;
391} 486}
392 487
393static ssize_t
394pwrite (int fd, void *buf, size_t count, off_t offset) 488static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset)
395{ 489{
396 ssize_t res; 490 ssize_t res;
397 off_t ooffset; 491 off_t ooffset;
398 492
399 pthread_mutex_lock (&iolock); 493 pthread_mutex_lock (&preadwritelock);
400 ooffset = lseek (fd, 0, SEEK_CUR); 494 ooffset = lseek (fd, 0, SEEK_CUR);
401 lseek (fd, offset, SEEK_SET); 495 lseek (fd, offset, SEEK_SET);
402 res = write (fd, buf, count); 496 res = write (fd, buf, count);
403 lseek (fd, offset, SEEK_SET); 497 lseek (fd, offset, SEEK_SET);
404 pthread_mutex_unlock (&iolock); 498 pthread_mutex_unlock (&preadwritelock);
405 499
406 return res; 500 return res;
407} 501}
408#endif 502#endif
409 503
412#endif 506#endif
413 507
414#if !HAVE_READAHEAD 508#if !HAVE_READAHEAD
415# define readahead aio_readahead 509# define readahead aio_readahead
416 510
511static ssize_t readahead (int fd, off_t offset, size_t count)
512{
417static char readahead_buf[4096]; 513 char readahead_buf[4096];
418 514
419static ssize_t
420readahead (int fd, off_t offset, size_t count)
421{
422 while (count > 0) 515 while (count > 0)
423 { 516 {
424 size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); 517 size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf);
425 518
426 pread (fd, readahead_buf, len, offset); 519 pread (fd, readahead_buf, len, offset);
430 523
431 errno = 0; 524 errno = 0;
432} 525}
433#endif 526#endif
434 527
528#if !HAVE_READDIR_R
529# define readdir_r aio_readdir_r
530
531static pthread_mutex_t readdirlock = PTHREAD_MUTEX_INITIALIZER;
532
533static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res)
534{
535 struct dirent *e;
536 int errorno;
537
538 pthread_mutex_lock (&readdirlock);
539
540 e = readdir (dirp);
541 errorno = errno;
542
543 if (e)
544 {
545 *res = ent;
546 strcpy (ent->d_name, e->d_name);
547 }
548 else
549 *res = 0;
550
551 pthread_mutex_unlock (&readdirlock);
552
553 errno = errorno;
554 return e ? 0 : -1;
555}
556#endif
557
435/* sendfile always needs emulation */ 558/* sendfile always needs emulation */
436static ssize_t
437sendfile_ (int ofd, int ifd, off_t offset, size_t count) 559static ssize_t sendfile_ (int ofd, int ifd, off_t offset, size_t count)
438{ 560{
439 ssize_t res; 561 ssize_t res;
440 562
441 if (!count) 563 if (!count)
442 return 0; 564 return 0;
443 565
566#if HAVE_SENDFILE
444#if __linux 567# if __linux
445 res = sendfile (ofd, ifd, &offset, count); 568 res = sendfile (ofd, ifd, &offset, count);
446 569
447#elif __freebsd 570# elif __freebsd
448 /* 571 /*
449 * Of course, the freebsd sendfile is a dire hack with no thoughts 572 * Of course, the freebsd sendfile is a dire hack with no thoughts
450 * wasted on making it similar to other i/o functions. 573 * wasted on making it similar to other I/O functions.
451 */ 574 */
452 { 575 {
453 off_t sbytes; 576 off_t sbytes;
454 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); 577 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0);
455 578
456 if (!res && errno == EAGAIN) 579 if (res < 0 && sbytes)
457 /* maybe on others, too, as usual, the manpage leaves you guessing */ 580 /* maybe only on EAGAIN only: as usual, the manpage leaves you guessing */
458 res = sbytes; 581 res = sbytes;
459 } 582 }
460 583
461#elif __hpux 584# elif __hpux
462 res = sendfile (ofd, ifd, offset, count, 0, 0); 585 res = sendfile (ofd, ifd, offset, count, 0, 0);
463 586
587# elif __solaris
588 {
589 struct sendfilevec vec;
590 size_t sbytes;
591
592 vec.sfv_fd = ifd;
593 vec.sfv_flag = 0;
594 vec.sfv_off = offset;
595 vec.sfv_len = count;
596
597 res = sendfilev (ofd, &vec, 1, &sbytes);
598
599 if (res < 0 && sbytes)
600 res = sbytes;
601 }
602
603# endif
464#else 604#else
465 res = -1; 605 res = -1;
466 errno = ENOSYS; 606 errno = ENOSYS;
467#endif 607#endif
468 608
609 if (res < 0
469 if (res < 0 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK)) 610 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK
611#if __solaris
612 || errno == EAFNOSUPPORT || errno == EPROTOTYPE
613#endif
614 )
615 )
470 { 616 {
471 /* emulate sendfile. this is a major pain in the ass */ 617 /* emulate sendfile. this is a major pain in the ass */
472 char *buf = malloc (4096); 618 char buf[4096];
473 res = 0; 619 res = 0;
474 620
475 for (;;) 621 while (count)
476 { 622 {
477 ssize_t cnt; 623 ssize_t cnt;
478 624
479 cnt = pread (ifd, buf, 4096, offset); 625 cnt = pread (ifd, buf, count > 4096 ? 4096 : count, offset);
480 626
481 if (cnt <= 0) 627 if (cnt <= 0)
482 { 628 {
483 if (cnt && !res) res = -1; 629 if (cnt && !res) res = -1;
484 break; 630 break;
485 } 631 }
486 632
487 cnt = write (ofd, buf, cnt); 633 cnt = write (ofd, buf, cnt);
488 634
489 if (cnt <= 0) 635 if (cnt <= 0)
490 { 636 {
491 if (cnt && !res) res = -1; 637 if (cnt && !res) res = -1;
492 break; 638 break;
493 } 639 }
494 640
495 offset += cnt; 641 offset += cnt;
496 res += cnt; 642 res += cnt;
643 count -= cnt;
497 } 644 }
498
499 {
500 int errorno = errno;
501 free (buf);
502 errno = errorno;
503 }
504 } 645 }
505 646
506 return res; 647 return res;
648}
649
650/* read a full directory */
651static int scandir_ (const char *path, void **namesp)
652{
653 DIR *dirp = opendir (path);
654 union
655 {
656 struct dirent d;
657 char b [offsetof (struct dirent, d_name) + NAME_MAX + 1];
658 } u;
659 struct dirent *entp;
660 char *name, *names;
661 int memlen = 4096;
662 int memofs = 0;
663 int res = 0;
664 int errorno;
665
666 if (!dirp)
667 return -1;
668
669 names = malloc (memlen);
670
671 for (;;)
672 {
673 errno = 0, readdir_r (dirp, &u.d, &entp);
674
675 if (!entp)
676 break;
677
678 name = entp->d_name;
679
680 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2])))
681 {
682 int len = strlen (name) + 1;
683
684 res++;
685
686 while (memofs + len > memlen)
687 {
688 memlen *= 2;
689 names = realloc (names, memlen);
690 if (!names)
691 break;
692 }
693
694 memcpy (names + memofs, name, len);
695 memofs += len;
696 }
697 }
698
699 errorno = errno;
700 closedir (dirp);
701
702 if (errorno)
703 {
704 free (names);
705 errno = errorno;
706 res = -1;
707 }
708
709 *namesp = (void *)names;
710 return res;
507} 711}
508 712
509/*****************************************************************************/ 713/*****************************************************************************/
510 714
511static void *
512aio_proc (void *thr_arg) 715static void *aio_proc (void *thr_arg)
513{ 716{
514 aio_req req; 717 aio_req req;
515 int type; 718 int type;
516 719
517 do 720 do
536 739
537 pthread_mutex_unlock (&reqlock); 740 pthread_mutex_unlock (&reqlock);
538 741
539 errno = 0; /* strictly unnecessary */ 742 errno = 0; /* strictly unnecessary */
540 743
541 type = req->type; 744 if (!req->cancelled)
542
543 switch (type) 745 switch (req->type)
544 { 746 {
545 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; 747 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break;
546 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; 748 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break;
547 749
548 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; 750 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break;
549 case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length); break; 751 case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length); break;
550 752
551 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; 753 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break;
552 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; 754 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break;
553 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; 755 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break;
554 756
555 case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; 757 case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break;
556 case REQ_CLOSE: req->result = close (req->fd); break; 758 case REQ_CLOSE: req->result = close (req->fd); break;
557 case REQ_UNLINK: req->result = unlink (req->dataptr); break; 759 case REQ_UNLINK: req->result = unlink (req->dataptr); break;
558 case REQ_RMDIR: req->result = rmdir (req->dataptr); break; 760 case REQ_RMDIR: req->result = rmdir (req->dataptr); break;
761 case REQ_RENAME: req->result = rename (req->data2ptr, req->dataptr); break;
762 case REQ_LINK: req->result = link (req->data2ptr, req->dataptr); break;
559 case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; 763 case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break;
560 764
561 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; 765 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break;
562 case REQ_FSYNC: req->result = fsync (req->fd); break; 766 case REQ_FSYNC: req->result = fsync (req->fd); break;
767 case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break;
563 768
769 case REQ_SLEEP:
770 {
771 struct timeval tv;
772
773 tv.tv_sec = req->fd;
774 tv.tv_usec = req->fd2;
775
776 req->result = select (0, 0, 0, 0, &tv);
777 }
778
564 case REQ_QUIT: 779 case REQ_QUIT:
565 break; 780 break;
566 781
567 default: 782 default:
568 req->result = ENOSYS; 783 req->result = ENOSYS;
569 break; 784 break;
570 } 785 }
571 786
572 req->errorno = errno; 787 req->errorno = errno;
573 788
574 pthread_mutex_lock (&reslock); 789 pthread_mutex_lock (&reslock);
575 790
593 while (type != REQ_QUIT); 808 while (type != REQ_QUIT);
594 809
595 return 0; 810 return 0;
596} 811}
597 812
813/*****************************************************************************/
814
815static void atfork_prepare (void)
816{
817 pthread_mutex_lock (&reqlock);
818 pthread_mutex_lock (&reslock);
819#if !HAVE_PREADWRITE
820 pthread_mutex_lock (&preadwritelock);
821#endif
822#if !HAVE_READDIR_R
823 pthread_mutex_lock (&readdirlock);
824#endif
825}
826
827static void atfork_parent (void)
828{
829#if !HAVE_READDIR_R
830 pthread_mutex_unlock (&readdirlock);
831#endif
832#if !HAVE_PREADWRITE
833 pthread_mutex_unlock (&preadwritelock);
834#endif
835 pthread_mutex_unlock (&reslock);
836 pthread_mutex_unlock (&reqlock);
837}
838
839static void atfork_child (void)
840{
841 aio_req prv;
842
843 started = 0;
844
845 while (reqs)
846 {
847 prv = reqs;
848 reqs = prv->next;
849 req_free (prv);
850 }
851
852 reqs = reqe = 0;
853
854 while (ress)
855 {
856 prv = ress;
857 ress = prv->next;
858 req_free (prv);
859 }
860
861 ress = rese = 0;
862
863 close (respipe [0]);
864 close (respipe [1]);
865 create_pipe ();
866
867 atfork_parent ();
868}
869
598#define dREQ \ 870#define dREQ \
599 aio_req req; \ 871 aio_req req; \
600 \ 872 \
601 if (SvOK (callback) && !SvROK (callback)) \ 873 if (SvOK (callback) && !SvROK (callback)) \
602 croak ("clalback must be undef or of reference type"); \ 874 croak ("callback must be undef or of reference type"); \
603 \ 875 \
604 Newz (0, req, 1, aio_cb); \ 876 Newz (0, req, 1, aio_cb); \
605 if (!req) \ 877 if (!req) \
606 croak ("out of memory during aio_req allocation"); \ 878 croak ("out of memory during aio_req allocation"); \
607 \ 879 \
608 req->callback = newSVsv (callback); 880 req->callback = newSVsv (callback)
881
882#define REQ_SEND \
883 req_send (req); \
884 \
885 if (GIMME_V != G_VOID) \
886 XPUSHs (req_sv (req, AIO_REQ_KLASS));
609 887
610MODULE = IO::AIO PACKAGE = IO::AIO 888MODULE = IO::AIO PACKAGE = IO::AIO
611 889
612PROTOTYPES: ENABLE 890PROTOTYPES: ENABLE
613 891
614BOOT: 892BOOT:
615{ 893{
894 HV *stash = gv_stashpv ("IO::AIO", 1);
895 newCONSTSUB (stash, "EXDEV", newSViv (EXDEV));
896 newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY));
897 newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY));
898
616 create_pipe (); 899 create_pipe ();
617 pthread_atfork (atfork_prepare, atfork_parent, atfork_child); 900 pthread_atfork (atfork_prepare, atfork_parent, atfork_child);
618} 901}
619 902
620void 903void
621min_parallel(nthreads) 904min_parallel (nthreads)
622 int nthreads 905 int nthreads
623 PROTOTYPE: $ 906 PROTOTYPE: $
624 907
625void 908void
626max_parallel(nthreads) 909max_parallel (nthreads)
627 int nthreads 910 int nthreads
628 PROTOTYPE: $ 911 PROTOTYPE: $
629 912
630int 913int
631max_outstanding(nreqs) 914max_outstanding (nreqs)
632 int nreqs 915 int nreqs
633 PROTOTYPE: $ 916 PROTOTYPE: $
634 CODE: 917 CODE:
635 RETVAL = max_outstanding; 918 RETVAL = max_outstanding;
636 max_outstanding = nreqs; 919 max_outstanding = nreqs;
637 920
638void 921void
639aio_open(pathname,flags,mode,callback=&PL_sv_undef) 922aio_open (pathname,flags,mode,callback=&PL_sv_undef)
640 SV * pathname 923 SV * pathname
641 int flags 924 int flags
642 int mode 925 int mode
643 SV * callback 926 SV * callback
644 PROTOTYPE: $$$;$ 927 PROTOTYPE: $$$;$
645 CODE: 928 PPCODE:
646{ 929{
647 dREQ; 930 dREQ;
648 931
649 req->type = REQ_OPEN; 932 req->type = REQ_OPEN;
650 req->data = newSVsv (pathname); 933 req->data = newSVsv (pathname);
651 req->dataptr = SvPVbyte_nolen (req->data); 934 req->dataptr = SvPVbyte_nolen (req->data);
652 req->fd = flags; 935 req->fd = flags;
653 req->mode = mode; 936 req->mode = mode;
654 937
655 send_req (req); 938 REQ_SEND;
656} 939}
657 940
658void 941void
659aio_close(fh,callback=&PL_sv_undef) 942aio_close (fh,callback=&PL_sv_undef)
660 SV * fh 943 SV * fh
661 SV * callback 944 SV * callback
662 PROTOTYPE: $;$ 945 PROTOTYPE: $;$
663 ALIAS: 946 ALIAS:
664 aio_close = REQ_CLOSE 947 aio_close = REQ_CLOSE
665 aio_fsync = REQ_FSYNC 948 aio_fsync = REQ_FSYNC
666 aio_fdatasync = REQ_FDATASYNC 949 aio_fdatasync = REQ_FDATASYNC
667 CODE: 950 PPCODE:
668{ 951{
669 dREQ; 952 dREQ;
670 953
671 req->type = ix; 954 req->type = ix;
672 req->fh = newSVsv (fh); 955 req->fh = newSVsv (fh);
673 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); 956 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh)));
674 957
675 send_req (req); 958 REQ_SEND (req);
676} 959}
677 960
678void 961void
679aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) 962aio_read (fh,offset,length,data,dataoffset,callback=&PL_sv_undef)
680 SV * fh 963 SV * fh
681 UV offset 964 UV offset
682 UV length 965 UV length
683 SV * data 966 SV * data
684 UV dataoffset 967 UV dataoffset
685 SV * callback 968 SV * callback
686 ALIAS: 969 ALIAS:
687 aio_read = REQ_READ 970 aio_read = REQ_READ
688 aio_write = REQ_WRITE 971 aio_write = REQ_WRITE
689 PROTOTYPE: $$$$$;$ 972 PROTOTYPE: $$$$$;$
690 CODE: 973 PPCODE:
691{ 974{
692 aio_req req; 975 aio_req req;
693 STRLEN svlen; 976 STRLEN svlen;
694 char *svptr = SvPVbyte (data, svlen); 977 char *svptr = SvPVbyte (data, svlen);
695 978
733 { 1016 {
734 SvREADONLY_on (data); 1017 SvREADONLY_on (data);
735 req->data2ptr = (void *)data; 1018 req->data2ptr = (void *)data;
736 } 1019 }
737 1020
738 send_req (req); 1021 REQ_SEND;
739 } 1022 }
740} 1023}
741 1024
742void 1025void
743aio_sendfile(out_fh,in_fh,in_offset,length,callback=&PL_sv_undef) 1026aio_sendfile (out_fh,in_fh,in_offset,length,callback=&PL_sv_undef)
744 SV * out_fh 1027 SV * out_fh
745 SV * in_fh 1028 SV * in_fh
746 UV in_offset 1029 UV in_offset
747 UV length 1030 UV length
748 SV * callback 1031 SV * callback
749 PROTOTYPE: $$$$;$ 1032 PROTOTYPE: $$$$;$
750 CODE: 1033 PPCODE:
751{ 1034{
752 dREQ; 1035 dREQ;
753 1036
754 req->type = REQ_SENDFILE; 1037 req->type = REQ_SENDFILE;
755 req->fh = newSVsv (out_fh); 1038 req->fh = newSVsv (out_fh);
757 req->fh2 = newSVsv (in_fh); 1040 req->fh2 = newSVsv (in_fh);
758 req->fd2 = PerlIO_fileno (IoIFP (sv_2io (in_fh))); 1041 req->fd2 = PerlIO_fileno (IoIFP (sv_2io (in_fh)));
759 req->offset = in_offset; 1042 req->offset = in_offset;
760 req->length = length; 1043 req->length = length;
761 1044
762 send_req (req); 1045 REQ_SEND;
763} 1046}
764 1047
765void 1048void
766aio_readahead(fh,offset,length,callback=&PL_sv_undef) 1049aio_readahead (fh,offset,length,callback=&PL_sv_undef)
767 SV * fh 1050 SV * fh
768 UV offset 1051 UV offset
769 IV length 1052 IV length
770 SV * callback 1053 SV * callback
771 PROTOTYPE: $$$;$ 1054 PROTOTYPE: $$$;$
772 CODE: 1055 PPCODE:
773{ 1056{
774 dREQ; 1057 dREQ;
775 1058
776 req->type = REQ_READAHEAD; 1059 req->type = REQ_READAHEAD;
777 req->fh = newSVsv (fh); 1060 req->fh = newSVsv (fh);
778 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); 1061 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh)));
779 req->offset = offset; 1062 req->offset = offset;
780 req->length = length; 1063 req->length = length;
781 1064
782 send_req (req); 1065 REQ_SEND;
783} 1066}
784 1067
785void 1068void
786aio_stat(fh_or_path,callback=&PL_sv_undef) 1069aio_stat (fh_or_path,callback=&PL_sv_undef)
787 SV * fh_or_path 1070 SV * fh_or_path
788 SV * callback 1071 SV * callback
789 ALIAS: 1072 ALIAS:
790 aio_stat = REQ_STAT 1073 aio_stat = REQ_STAT
791 aio_lstat = REQ_LSTAT 1074 aio_lstat = REQ_LSTAT
792 CODE: 1075 PPCODE:
793{ 1076{
794 dREQ; 1077 dREQ;
795 1078
796 New (0, req->statdata, 1, Stat_t); 1079 New (0, req->statdata, 1, Stat_t);
797 if (!req->statdata) 1080 if (!req->statdata)
798 { 1081 {
799 free_req (req); 1082 req_free (req);
800 croak ("out of memory during aio_req->statdata allocation"); 1083 croak ("out of memory during aio_req->statdata allocation");
801 } 1084 }
802 1085
803 if (SvPOK (fh_or_path)) 1086 if (SvPOK (fh_or_path))
804 { 1087 {
811 req->type = REQ_FSTAT; 1094 req->type = REQ_FSTAT;
812 req->fh = newSVsv (fh_or_path); 1095 req->fh = newSVsv (fh_or_path);
813 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); 1096 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
814 } 1097 }
815 1098
816 send_req (req); 1099 REQ_SEND;
817} 1100}
818 1101
819void 1102void
820aio_unlink(pathname,callback=&PL_sv_undef) 1103aio_unlink (pathname,callback=&PL_sv_undef)
821 SV * pathname 1104 SV * pathname
822 SV * callback 1105 SV * callback
823 ALIAS: 1106 ALIAS:
824 aio_unlink = REQ_UNLINK 1107 aio_unlink = REQ_UNLINK
825 aio_rmdir = REQ_RMDIR 1108 aio_rmdir = REQ_RMDIR
1109 aio_readdir = REQ_READDIR
826 CODE: 1110 PPCODE:
827{ 1111{
828 dREQ; 1112 dREQ;
829 1113
830 req->type = ix; 1114 req->type = ix;
831 req->data = newSVsv (pathname); 1115 req->data = newSVsv (pathname);
832 req->dataptr = SvPVbyte_nolen (req->data); 1116 req->dataptr = SvPVbyte_nolen (req->data);
833 1117
834 send_req (req); 1118 REQ_SEND;
835} 1119}
836 1120
837void 1121void
838aio_symlink(oldpath,newpath,callback=&PL_sv_undef) 1122aio_link (oldpath,newpath,callback=&PL_sv_undef)
839 SV * oldpath 1123 SV * oldpath
840 SV * newpath 1124 SV * newpath
841 SV * callback 1125 SV * callback
1126 ALIAS:
1127 aio_link = REQ_LINK
1128 aio_symlink = REQ_SYMLINK
1129 aio_rename = REQ_RENAME
842 CODE: 1130 PPCODE:
843{ 1131{
844 dREQ; 1132 dREQ;
845 1133
846 req->type = REQ_SYMLINK; 1134 req->type = ix;
847 req->fh = newSVsv (oldpath); 1135 req->fh = newSVsv (oldpath);
848 req->data2ptr = SvPVbyte_nolen (req->fh); 1136 req->data2ptr = SvPVbyte_nolen (req->fh);
849 req->data = newSVsv (newpath); 1137 req->data = newSVsv (newpath);
850 req->dataptr = SvPVbyte_nolen (req->data); 1138 req->dataptr = SvPVbyte_nolen (req->data);
851 1139
852 send_req (req); 1140 REQ_SEND;
853} 1141}
854 1142
855void 1143void
1144aio_sleep (delay,callback=&PL_sv_undef)
1145 double delay
1146 SV * callback
1147 PPCODE:
1148{
1149 dREQ;
1150
1151 req->type = REQ_SLEEP;
1152 req->fd = delay < 0. ? 0 : delay;
1153 req->fd2 = delay < 0. ? 0 : 1000. * (delay - req->fd);
1154
1155 REQ_SEND;
1156}
1157
1158void
1159aio_group (callback=&PL_sv_undef)
1160 SV * callback
1161 PROTOTYPE: ;$
1162 PPCODE:
1163{
1164 dREQ;
1165 req->type = REQ_GROUP;
1166 req->grp_next = req;
1167 req->grp_prev = req;
1168
1169 req_send (req);
1170 XPUSHs (req_sv (req, AIO_GRP_KLASS));
1171}
1172
1173void
856flush() 1174flush ()
857 PROTOTYPE: 1175 PROTOTYPE:
858 CODE: 1176 CODE:
859 while (nreqs) 1177 while (nreqs)
860 { 1178 {
861 poll_wait (); 1179 poll_wait ();
901 CODE: 1219 CODE:
902 RETVAL = nreqs; 1220 RETVAL = nreqs;
903 OUTPUT: 1221 OUTPUT:
904 RETVAL 1222 RETVAL
905 1223
1224PROTOTYPES: DISABLE
1225
1226MODULE = IO::AIO PACKAGE = IO::AIO::REQ
1227
1228void
1229cancel (aio_req_ornot req)
1230 PROTOTYPE:
1231 CODE:
1232 req_cancel (req);
1233
1234MODULE = IO::AIO PACKAGE = IO::AIO::GRP
1235
1236void
1237add (aio_req grp, ...)
1238 PPCODE:
1239{
1240 int i;
1241
1242 for (i = 1; i < items; ++i )
1243 {
1244 if (GIMME_V != G_VOID)
1245 XPUSHs (sv_2mortal (newSVsv (ST (i))));
1246
1247 aio_req req = SvAIO_REQ (ST (i));
1248
1249 if (req)
1250 {
1251 req->grp_prev = grp;
1252 req->grp_next = grp->grp_next;
1253 grp->grp_next->grp_prev = req;
1254 grp->grp_next = req;
1255
1256 req->grp = grp;
1257 }
1258 }
1259}
1260
1261void
1262result (aio_req grp, ...)
1263 CODE:
1264{
1265 int i;
1266 AV *av = newAV ();
1267
1268 if (grp->data)
1269 SvREFCNT_dec (grp->data);
1270
1271 for (i = 1; i < items; ++i )
1272 av_push (av, newSVsv (ST (i)));
1273
1274 grp->data = (SV *)av;
1275}
1276

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines