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.33 by root, Mon Aug 22 23:21:57 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>
34# else
35# error sendfile support requested but not available
28# endif 36# endif
37#endif
38
39/* used for struct dirent, AIX doesn't provide it */
40#ifndef NAME_MAX
41# define NAME_MAX 4096
29#endif 42#endif
30 43
31#if __ia64 44#if __ia64
32# define STACKSIZE 65536 45# define STACKSIZE 65536
33#else 46#else
34# define STACKSIZE 4096 47# define STACKSIZE 8192
35#endif 48#endif
36 49
37enum { 50enum {
38 REQ_QUIT, 51 REQ_QUIT,
39 REQ_OPEN, REQ_CLOSE, 52 REQ_OPEN, REQ_CLOSE,
40 REQ_READ, REQ_WRITE, REQ_READAHEAD, 53 REQ_READ, REQ_WRITE, REQ_READAHEAD,
41 REQ_SENDFILE, 54 REQ_SENDFILE,
42 REQ_STAT, REQ_LSTAT, REQ_FSTAT, 55 REQ_STAT, REQ_LSTAT, REQ_FSTAT,
43 REQ_FSYNC, REQ_FDATASYNC, 56 REQ_FSYNC, REQ_FDATASYNC,
44 REQ_UNLINK, REQ_RMDIR, 57 REQ_UNLINK, REQ_RMDIR, REQ_RENAME,
45 REQ_SYMLINK, 58 REQ_READDIR,
59 REQ_LINK, REQ_SYMLINK,
60 REQ_SLEEP,
61 REQ_GROUP,
46}; 62};
47 63
64#define AIO_REQ_KLASS "IO::AIO::REQ"
65#define AIO_GRP_KLASS "IO::AIO::GRP"
66
48typedef struct aio_cb { 67typedef struct aio_cb
68{
69 struct aio_cb *grp, *grp_prev, *grp_next;
70
49 struct aio_cb *volatile next; 71 struct aio_cb *volatile next;
50 72
51 int type; 73 SV *self; /* the perl counterpart of this request, if any */
52 74
53 int fd, fd2; 75 SV *data, *callback;
76 SV *fh, *fh2;
77 void *dataptr, *data2ptr;
78 Stat_t *statdata;
54 off_t offset; 79 off_t offset;
55 size_t length; 80 size_t length;
56 ssize_t result; 81 ssize_t result;
82
83 int type;
84 int fd, fd2;
85 int errorno;
86 STRLEN dataoffset;
57 mode_t mode; /* open */ 87 mode_t mode; /* open */
58 int errorno; 88 unsigned char cancelled;
59 SV *data, *callback;
60 SV *fh, *fh2;
61 void *dataptr, *data2ptr;
62 STRLEN dataoffset;
63
64 Stat_t *statdata;
65} aio_cb; 89} aio_cb;
66 90
67typedef aio_cb *aio_req; 91typedef aio_cb *aio_req;
92typedef aio_cb *aio_req_ornot;
68 93
69static int started, wanted; 94static int started, wanted;
70static volatile int nreqs; 95static volatile int nreqs;
71static int max_outstanding = 1<<30; 96static int max_outstanding = 1<<30;
72static int respipe [2]; 97static int respipe [2];
76static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; 101static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER;
77 102
78static volatile aio_req reqs, reqe; /* queue start, queue end */ 103static volatile aio_req reqs, reqe; /* queue start, queue end */
79static volatile aio_req ress, rese; /* queue start, queue end */ 104static volatile aio_req ress, rese; /* queue start, queue end */
80 105
81static void free_req (aio_req req) 106static void req_free (aio_req req);
82{
83 if (req->data)
84 SvREFCNT_dec (req->data);
85 107
86 if (req->fh) 108/* must be called at most once */
87 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);
88 113
89 if (req->fh2) 114 return sv_2mortal (sv_bless (newRV_inc (req->self), gv_stashpv (klass, 1)));
90 SvREFCNT_dec (req->fh2);
91
92 if (req->statdata)
93 Safefree (req->statdata);
94
95 if (req->callback)
96 SvREFCNT_dec (req->callback);
97
98 Safefree (req);
99} 115}
100 116
101static void 117static aio_req SvAIO_REQ (SV *sv)
102poll_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 ()
103{ 128{
104 if (nreqs && !ress) 129 if (nreqs && !ress)
105 { 130 {
106 fd_set rfd; 131 fd_set rfd;
107 FD_ZERO(&rfd); 132 FD_ZERO(&rfd);
109 134
110 select (respipe [0] + 1, &rfd, 0, 0, 0); 135 select (respipe [0] + 1, &rfd, 0, 0, 0);
111 } 136 }
112} 137}
113 138
114static int 139static void req_invoke (aio_req req)
115poll_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 ()
116{ 291{
117 dSP; 292 dSP;
118 int count = 0; 293 int count = 0;
119 int do_croak = 0; 294 int do_croak = 0;
120 aio_req req; 295 aio_req req;
146 321
147 nreqs--; 322 nreqs--;
148 323
149 if (req->type == REQ_QUIT) 324 if (req->type == REQ_QUIT)
150 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 }
151 else 331 else
152 { 332 {
153 int errorno = errno;
154 errno = req->errorno;
155
156 if (req->type == REQ_READ) 333 if (req->type == REQ_READ)
157 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));
158 335
159 if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE)) 336 if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE))
160 SvREADONLY_off (req->data); 337 SvREADONLY_off (req->data);
164 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; 341 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
165 PL_laststatval = req->result; 342 PL_laststatval = req->result;
166 PL_statcache = *(req->statdata); 343 PL_statcache = *(req->statdata);
167 } 344 }
168 345
169 ENTER; 346 req_invoke (req);
170 PUSHMARK (SP);
171 XPUSHs (sv_2mortal (newSViv (req->result)));
172 347
173 if (req->type == REQ_OPEN)
174 {
175 /* convert fd to fh */
176 SV *fh;
177
178 PUTBACK;
179 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL);
180 SPAGAIN;
181
182 fh = SvREFCNT_inc (POPs);
183
184 PUSHMARK (SP);
185 XPUSHs (sv_2mortal (fh));
186 }
187
188 if (SvOK (req->callback))
189 {
190 PUTBACK;
191 call_sv (req->callback, G_VOID | G_EVAL);
192 SPAGAIN;
193
194 if (SvTRUE (ERRSV))
195 {
196 free_req (req);
197 croak (0);
198 }
199 }
200
201 LEAVE;
202
203 errno = errorno;
204 count++; 348 count++;
205 } 349 }
206 350
207 free_req (req); 351 req_free (req);
208 } 352 }
209 353
210 return count; 354 return count;
211} 355}
212 356
213static void *aio_proc(void *arg); 357static void *aio_proc(void *arg);
214 358
215static void
216start_thread (void) 359static void start_thread (void)
217{ 360{
218 sigset_t fullsigset, oldsigset; 361 sigset_t fullsigset, oldsigset;
219 pthread_t tid; 362 pthread_t tid;
220 pthread_attr_t attr; 363 pthread_attr_t attr;
221 364
230 started++; 373 started++;
231 374
232 sigprocmask (SIG_SETMASK, &oldsigset, 0); 375 sigprocmask (SIG_SETMASK, &oldsigset, 0);
233} 376}
234 377
235static void 378static void req_send (aio_req req)
236send_req (aio_req req)
237{ 379{
238 while (started < wanted && nreqs >= started) 380 while (started < wanted && nreqs >= started)
239 start_thread (); 381 start_thread ();
240 382
241 nreqs++; 383 nreqs++;
265 407
266 poll_wait (); 408 poll_wait ();
267 } 409 }
268} 410}
269 411
270static void 412static void end_thread (void)
271end_thread (void)
272{ 413{
273 aio_req req; 414 aio_req req;
274 Newz (0, req, 1, aio_cb); 415 Newz (0, req, 1, aio_cb);
275 req->type = REQ_QUIT; 416 req->type = REQ_QUIT;
276 417
277 send_req (req); 418 req_send (req);
278} 419}
279 420
280static void min_parallel (int nthreads) 421static void min_parallel (int nthreads)
281{ 422{
282 if (wanted < nthreads) 423 if (wanted < nthreads)
311 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)) 452 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK))
312 croak ("cannot set result pipe to nonblocking mode"); 453 croak ("cannot set result pipe to nonblocking mode");
313 454
314 if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) 455 if (fcntl (respipe [1], F_SETFL, O_NONBLOCK))
315 croak ("cannot set result pipe to nonblocking mode"); 456 croak ("cannot set result pipe to nonblocking mode");
316}
317
318static void atfork_prepare (void)
319{
320 pthread_mutex_lock (&reqlock);
321 pthread_mutex_lock (&reslock);
322}
323
324static void atfork_parent (void)
325{
326 pthread_mutex_unlock (&reslock);
327 pthread_mutex_unlock (&reqlock);
328}
329
330static void atfork_child (void)
331{
332 aio_req prv;
333
334 started = 0;
335
336 while (reqs)
337 {
338 prv = reqs;
339 reqs = prv->next;
340 free_req (prv);
341 }
342
343 reqs = reqe = 0;
344
345 while (ress)
346 {
347 prv = ress;
348 ress = prv->next;
349 free_req (prv);
350 }
351
352 ress = rese = 0;
353
354 close (respipe [0]);
355 close (respipe [1]);
356 create_pipe ();
357
358 atfork_parent ();
359} 457}
360 458
361/*****************************************************************************/ 459/*****************************************************************************/
362/* work around various missing functions */ 460/* work around various missing functions */
363 461
368/* 466/*
369 * make our pread/pwrite safe against themselves, but not against 467 * make our pread/pwrite safe against themselves, but not against
370 * 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,
371 * but that's your problem, not mine. 469 * but that's your problem, not mine.
372 */ 470 */
373static pthread_mutex_t iolock = PTHREAD_MUTEX_INITIALIZER; 471static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER;
374 472
375static ssize_t 473static ssize_t pread (int fd, void *buf, size_t count, off_t offset)
376pread (int fd, void *buf, size_t count, off_t offset)
377{ 474{
378 ssize_t res; 475 ssize_t res;
379 off_t ooffset; 476 off_t ooffset;
380 477
381 pthread_mutex_lock (&iolock); 478 pthread_mutex_lock (&preadwritelock);
382 ooffset = lseek (fd, 0, SEEK_CUR); 479 ooffset = lseek (fd, 0, SEEK_CUR);
383 lseek (fd, offset, SEEK_SET); 480 lseek (fd, offset, SEEK_SET);
384 res = read (fd, buf, count); 481 res = read (fd, buf, count);
385 lseek (fd, ooffset, SEEK_SET); 482 lseek (fd, ooffset, SEEK_SET);
386 pthread_mutex_unlock (&iolock); 483 pthread_mutex_unlock (&preadwritelock);
387 484
388 return res; 485 return res;
389} 486}
390 487
391static ssize_t
392pwrite (int fd, void *buf, size_t count, off_t offset) 488static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset)
393{ 489{
394 ssize_t res; 490 ssize_t res;
395 off_t ooffset; 491 off_t ooffset;
396 492
397 pthread_mutex_lock (&iolock); 493 pthread_mutex_lock (&preadwritelock);
398 ooffset = lseek (fd, 0, SEEK_CUR); 494 ooffset = lseek (fd, 0, SEEK_CUR);
399 lseek (fd, offset, SEEK_SET); 495 lseek (fd, offset, SEEK_SET);
400 res = write (fd, buf, count); 496 res = write (fd, buf, count);
401 lseek (fd, offset, SEEK_SET); 497 lseek (fd, offset, SEEK_SET);
402 pthread_mutex_unlock (&iolock); 498 pthread_mutex_unlock (&preadwritelock);
403 499
404 return res; 500 return res;
405} 501}
406#endif 502#endif
407 503
410#endif 506#endif
411 507
412#if !HAVE_READAHEAD 508#if !HAVE_READAHEAD
413# define readahead aio_readahead 509# define readahead aio_readahead
414 510
511static ssize_t readahead (int fd, off_t offset, size_t count)
512{
415static char readahead_buf[4096]; 513 char readahead_buf[4096];
416 514
417static ssize_t
418readahead (int fd, off_t offset, size_t count)
419{
420 while (count > 0) 515 while (count > 0)
421 { 516 {
422 size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); 517 size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf);
423 518
424 pread (fd, readahead_buf, len, offset); 519 pread (fd, readahead_buf, len, offset);
428 523
429 errno = 0; 524 errno = 0;
430} 525}
431#endif 526#endif
432 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
433/* sendfile always needs emulation */ 558/* sendfile always needs emulation */
434static ssize_t
435sendfile_ (int ofd, int ifd, off_t offset, size_t count) 559static ssize_t sendfile_ (int ofd, int ifd, off_t offset, size_t count)
436{ 560{
437 ssize_t res; 561 ssize_t res;
438 562
439 if (!count) 563 if (!count)
440 return 0; 564 return 0;
441 565
566#if HAVE_SENDFILE
442#if __linux 567# if __linux
443 res = sendfile (ofd, ifd, &offset, count); 568 res = sendfile (ofd, ifd, &offset, count);
444 569
445#elif __freebsd 570# elif __freebsd
446 /* 571 /*
447 * 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
448 * wasted on making it similar to other i/o functions. 573 * wasted on making it similar to other I/O functions.
449 */ 574 */
450 { 575 {
451 off_t sbytes; 576 off_t sbytes;
452 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); 577 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0);
453 578
454 if (!res && errno == EAGAIN) 579 if (res < 0 && sbytes)
455 /* maybe on others, too, as usual, the manpage leaves you guessing */ 580 /* maybe only on EAGAIN only: as usual, the manpage leaves you guessing */
456 res = sbytes; 581 res = sbytes;
457 } 582 }
458 583
459#elif __hpux 584# elif __hpux
460 res = sendfile (ofd, ifd, offset, count, 0, 0); 585 res = sendfile (ofd, ifd, offset, count, 0, 0);
461 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
462#else 604#else
463 res = -1; 605 res = -1;
464 errno = ENOSYS; 606 errno = ENOSYS;
465#endif 607#endif
466 608
609 if (res < 0
467 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 )
468 { 616 {
469 /* emulate sendfile. this is a major pain in the ass */ 617 /* emulate sendfile. this is a major pain in the ass */
470 char *buf = malloc (4096); 618 char buf[4096];
471 res = 0; 619 res = 0;
472 620
473 for (;;) 621 while (count)
474 { 622 {
475 ssize_t cnt; 623 ssize_t cnt;
476 624
477 cnt = pread (ifd, buf, 4096, offset); 625 cnt = pread (ifd, buf, count > 4096 ? 4096 : count, offset);
478 626
479 if (cnt <= 0) 627 if (cnt <= 0)
480 { 628 {
481 if (cnt && !res) res = -1; 629 if (cnt && !res) res = -1;
482 break; 630 break;
483 } 631 }
484 632
485 cnt = write (ofd, buf, cnt); 633 cnt = write (ofd, buf, cnt);
486 634
487 if (cnt <= 0) 635 if (cnt <= 0)
488 { 636 {
489 if (cnt && !res) res = -1; 637 if (cnt && !res) res = -1;
490 break; 638 break;
491 } 639 }
492 640
493 offset += cnt; 641 offset += cnt;
494 res += cnt; 642 res += cnt;
643 count -= cnt;
495 } 644 }
496
497 {
498 int errorno = errno;
499 free (buf);
500 errno = errorno;
501 }
502 } 645 }
503 646
504 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;
505} 711}
506 712
507/*****************************************************************************/ 713/*****************************************************************************/
508 714
509static void *
510aio_proc (void *thr_arg) 715static void *aio_proc (void *thr_arg)
511{ 716{
512 aio_req req; 717 aio_req req;
513 int type; 718 int type;
514 719
515 do 720 do
534 739
535 pthread_mutex_unlock (&reqlock); 740 pthread_mutex_unlock (&reqlock);
536 741
537 errno = 0; /* strictly unnecessary */ 742 errno = 0; /* strictly unnecessary */
538 743
539 type = req->type; 744 if (!req->cancelled)
540
541 switch (type) 745 switch (req->type)
542 { 746 {
543 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;
544 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;
545 749
546 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;
547 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;
548 752
549 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; 753 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break;
550 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; 754 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break;
551 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; 755 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break;
552 756
553 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;
554 case REQ_CLOSE: req->result = close (req->fd); break; 758 case REQ_CLOSE: req->result = close (req->fd); break;
555 case REQ_UNLINK: req->result = unlink (req->dataptr); break; 759 case REQ_UNLINK: req->result = unlink (req->dataptr); break;
556 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;
557 case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; 763 case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break;
558 764
559 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; 765 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break;
560 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;
561 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
562 case REQ_QUIT: 779 case REQ_QUIT:
563 break; 780 break;
564 781
565 default: 782 default:
566 req->result = ENOSYS; 783 req->result = ENOSYS;
567 break; 784 break;
568 } 785 }
569 786
570 req->errorno = errno; 787 req->errorno = errno;
571 788
572 pthread_mutex_lock (&reslock); 789 pthread_mutex_lock (&reslock);
573 790
591 while (type != REQ_QUIT); 808 while (type != REQ_QUIT);
592 809
593 return 0; 810 return 0;
594} 811}
595 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
596#define dREQ \ 870#define dREQ \
597 aio_req req; \ 871 aio_req req; \
598 \ 872 \
599 if (SvOK (callback) && !SvROK (callback)) \ 873 if (SvOK (callback) && !SvROK (callback)) \
600 croak ("clalback must be undef or of reference type"); \ 874 croak ("callback must be undef or of reference type"); \
601 \ 875 \
602 Newz (0, req, 1, aio_cb); \ 876 Newz (0, req, 1, aio_cb); \
603 if (!req) \ 877 if (!req) \
604 croak ("out of memory during aio_req allocation"); \ 878 croak ("out of memory during aio_req allocation"); \
605 \ 879 \
606 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));
607 887
608MODULE = IO::AIO PACKAGE = IO::AIO 888MODULE = IO::AIO PACKAGE = IO::AIO
609 889
610PROTOTYPES: ENABLE 890PROTOTYPES: ENABLE
611 891
612BOOT: 892BOOT:
613{ 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
614 create_pipe (); 899 create_pipe ();
615 pthread_atfork (atfork_prepare, atfork_parent, atfork_child); 900 pthread_atfork (atfork_prepare, atfork_parent, atfork_child);
616} 901}
617 902
618void 903void
619min_parallel(nthreads) 904min_parallel (nthreads)
620 int nthreads 905 int nthreads
621 PROTOTYPE: $ 906 PROTOTYPE: $
622 907
623void 908void
624max_parallel(nthreads) 909max_parallel (nthreads)
625 int nthreads 910 int nthreads
626 PROTOTYPE: $ 911 PROTOTYPE: $
627 912
628int 913int
629max_outstanding(nreqs) 914max_outstanding (nreqs)
630 int nreqs 915 int nreqs
631 PROTOTYPE: $ 916 PROTOTYPE: $
632 CODE: 917 CODE:
633 RETVAL = max_outstanding; 918 RETVAL = max_outstanding;
634 max_outstanding = nreqs; 919 max_outstanding = nreqs;
635 920
636void 921void
637aio_open(pathname,flags,mode,callback=&PL_sv_undef) 922aio_open (pathname,flags,mode,callback=&PL_sv_undef)
638 SV * pathname 923 SV * pathname
639 int flags 924 int flags
640 int mode 925 int mode
641 SV * callback 926 SV * callback
642 PROTOTYPE: $$$;$ 927 PROTOTYPE: $$$;$
643 CODE: 928 PPCODE:
644{ 929{
645 dREQ; 930 dREQ;
646 931
647 req->type = REQ_OPEN; 932 req->type = REQ_OPEN;
648 req->data = newSVsv (pathname); 933 req->data = newSVsv (pathname);
649 req->dataptr = SvPVbyte_nolen (req->data); 934 req->dataptr = SvPVbyte_nolen (req->data);
650 req->fd = flags; 935 req->fd = flags;
651 req->mode = mode; 936 req->mode = mode;
652 937
653 send_req (req); 938 REQ_SEND;
654} 939}
655 940
656void 941void
657aio_close(fh,callback=&PL_sv_undef) 942aio_close (fh,callback=&PL_sv_undef)
658 SV * fh 943 SV * fh
659 SV * callback 944 SV * callback
660 PROTOTYPE: $;$ 945 PROTOTYPE: $;$
661 ALIAS: 946 ALIAS:
662 aio_close = REQ_CLOSE 947 aio_close = REQ_CLOSE
663 aio_fsync = REQ_FSYNC 948 aio_fsync = REQ_FSYNC
664 aio_fdatasync = REQ_FDATASYNC 949 aio_fdatasync = REQ_FDATASYNC
665 CODE: 950 PPCODE:
666{ 951{
667 dREQ; 952 dREQ;
668 953
669 req->type = ix; 954 req->type = ix;
670 req->fh = newSVsv (fh); 955 req->fh = newSVsv (fh);
671 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); 956 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh)));
672 957
673 send_req (req); 958 REQ_SEND (req);
674} 959}
675 960
676void 961void
677aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) 962aio_read (fh,offset,length,data,dataoffset,callback=&PL_sv_undef)
678 SV * fh 963 SV * fh
679 UV offset 964 UV offset
680 UV length 965 UV length
681 SV * data 966 SV * data
682 UV dataoffset 967 UV dataoffset
683 SV * callback 968 SV * callback
684 ALIAS: 969 ALIAS:
685 aio_read = REQ_READ 970 aio_read = REQ_READ
686 aio_write = REQ_WRITE 971 aio_write = REQ_WRITE
687 PROTOTYPE: $$$$$;$ 972 PROTOTYPE: $$$$$;$
688 CODE: 973 PPCODE:
689{ 974{
690 aio_req req; 975 aio_req req;
691 STRLEN svlen; 976 STRLEN svlen;
692 char *svptr = SvPVbyte (data, svlen); 977 char *svptr = SvPVbyte (data, svlen);
693 978
731 { 1016 {
732 SvREADONLY_on (data); 1017 SvREADONLY_on (data);
733 req->data2ptr = (void *)data; 1018 req->data2ptr = (void *)data;
734 } 1019 }
735 1020
736 send_req (req); 1021 REQ_SEND;
737 } 1022 }
738} 1023}
739 1024
740void 1025void
741aio_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)
742 SV * out_fh 1027 SV * out_fh
743 SV * in_fh 1028 SV * in_fh
744 UV in_offset 1029 UV in_offset
745 UV length 1030 UV length
746 SV * callback 1031 SV * callback
747 PROTOTYPE: $$$$;$ 1032 PROTOTYPE: $$$$;$
748 CODE: 1033 PPCODE:
749{ 1034{
750 dREQ; 1035 dREQ;
751 1036
752 req->type = REQ_SENDFILE; 1037 req->type = REQ_SENDFILE;
753 req->fh = newSVsv (out_fh); 1038 req->fh = newSVsv (out_fh);
755 req->fh2 = newSVsv (in_fh); 1040 req->fh2 = newSVsv (in_fh);
756 req->fd2 = PerlIO_fileno (IoIFP (sv_2io (in_fh))); 1041 req->fd2 = PerlIO_fileno (IoIFP (sv_2io (in_fh)));
757 req->offset = in_offset; 1042 req->offset = in_offset;
758 req->length = length; 1043 req->length = length;
759 1044
760 send_req (req); 1045 REQ_SEND;
761} 1046}
762 1047
763void 1048void
764aio_readahead(fh,offset,length,callback=&PL_sv_undef) 1049aio_readahead (fh,offset,length,callback=&PL_sv_undef)
765 SV * fh 1050 SV * fh
766 UV offset 1051 UV offset
767 IV length 1052 IV length
768 SV * callback 1053 SV * callback
769 PROTOTYPE: $$$;$ 1054 PROTOTYPE: $$$;$
770 CODE: 1055 PPCODE:
771{ 1056{
772 dREQ; 1057 dREQ;
773 1058
774 req->type = REQ_READAHEAD; 1059 req->type = REQ_READAHEAD;
775 req->fh = newSVsv (fh); 1060 req->fh = newSVsv (fh);
776 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); 1061 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh)));
777 req->offset = offset; 1062 req->offset = offset;
778 req->length = length; 1063 req->length = length;
779 1064
780 send_req (req); 1065 REQ_SEND;
781} 1066}
782 1067
783void 1068void
784aio_stat(fh_or_path,callback=&PL_sv_undef) 1069aio_stat (fh_or_path,callback=&PL_sv_undef)
785 SV * fh_or_path 1070 SV * fh_or_path
786 SV * callback 1071 SV * callback
787 ALIAS: 1072 ALIAS:
788 aio_stat = REQ_STAT 1073 aio_stat = REQ_STAT
789 aio_lstat = REQ_LSTAT 1074 aio_lstat = REQ_LSTAT
790 CODE: 1075 PPCODE:
791{ 1076{
792 dREQ; 1077 dREQ;
793 1078
794 New (0, req->statdata, 1, Stat_t); 1079 New (0, req->statdata, 1, Stat_t);
795 if (!req->statdata) 1080 if (!req->statdata)
796 { 1081 {
797 free_req (req); 1082 req_free (req);
798 croak ("out of memory during aio_req->statdata allocation"); 1083 croak ("out of memory during aio_req->statdata allocation");
799 } 1084 }
800 1085
801 if (SvPOK (fh_or_path)) 1086 if (SvPOK (fh_or_path))
802 { 1087 {
809 req->type = REQ_FSTAT; 1094 req->type = REQ_FSTAT;
810 req->fh = newSVsv (fh_or_path); 1095 req->fh = newSVsv (fh_or_path);
811 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); 1096 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
812 } 1097 }
813 1098
814 send_req (req); 1099 REQ_SEND;
815} 1100}
816 1101
817void 1102void
818aio_unlink(pathname,callback=&PL_sv_undef) 1103aio_unlink (pathname,callback=&PL_sv_undef)
819 SV * pathname 1104 SV * pathname
820 SV * callback 1105 SV * callback
821 ALIAS: 1106 ALIAS:
822 aio_unlink = REQ_UNLINK 1107 aio_unlink = REQ_UNLINK
823 aio_rmdir = REQ_RMDIR 1108 aio_rmdir = REQ_RMDIR
1109 aio_readdir = REQ_READDIR
824 CODE: 1110 PPCODE:
825{ 1111{
826 dREQ; 1112 dREQ;
827 1113
828 req->type = ix; 1114 req->type = ix;
829 req->data = newSVsv (pathname); 1115 req->data = newSVsv (pathname);
830 req->dataptr = SvPVbyte_nolen (req->data); 1116 req->dataptr = SvPVbyte_nolen (req->data);
831 1117
832 send_req (req); 1118 REQ_SEND;
833} 1119}
834 1120
835void 1121void
836aio_symlink(oldpath,newpath,callback=&PL_sv_undef) 1122aio_link (oldpath,newpath,callback=&PL_sv_undef)
837 SV * oldpath 1123 SV * oldpath
838 SV * newpath 1124 SV * newpath
839 SV * callback 1125 SV * callback
1126 ALIAS:
1127 aio_link = REQ_LINK
1128 aio_symlink = REQ_SYMLINK
1129 aio_rename = REQ_RENAME
840 CODE: 1130 PPCODE:
841{ 1131{
842 dREQ; 1132 dREQ;
843 1133
844 req->type = REQ_SYMLINK; 1134 req->type = ix;
845 req->fh = newSVsv (oldpath); 1135 req->fh = newSVsv (oldpath);
846 req->data2ptr = SvPVbyte_nolen (req->fh); 1136 req->data2ptr = SvPVbyte_nolen (req->fh);
847 req->data = newSVsv (newpath); 1137 req->data = newSVsv (newpath);
848 req->dataptr = SvPVbyte_nolen (req->data); 1138 req->dataptr = SvPVbyte_nolen (req->data);
849 1139
850 send_req (req); 1140 REQ_SEND;
851} 1141}
852 1142
853void 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
854flush() 1174flush ()
855 PROTOTYPE: 1175 PROTOTYPE:
856 CODE: 1176 CODE:
857 while (nreqs) 1177 while (nreqs)
858 { 1178 {
859 poll_wait (); 1179 poll_wait ();
899 CODE: 1219 CODE:
900 RETVAL = nreqs; 1220 RETVAL = nreqs;
901 OUTPUT: 1221 OUTPUT:
902 RETVAL 1222 RETVAL
903 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