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.7 by root, Mon Jul 11 01:49:14 2005 UTC vs.
Revision 1.53 by root, Mon Oct 23 00:17:07 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines