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.36 by root, Tue Aug 23 01:18:04 2005 UTC vs.
Revision 1.53 by root, Mon Oct 23 00:17:07 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
30# else 34# else
31# error sendfile support requested but not available 35# error sendfile support requested but not available
32# endif 36# endif
33#endif 37#endif
34 38
39/* used for struct dirent, AIX doesn't provide it */
40#ifndef NAME_MAX
41# define NAME_MAX 4096
42#endif
43
35#if __ia64 44#if __ia64
36# define STACKSIZE 65536 45# define STACKSIZE 65536
37#else 46#else
38# define STACKSIZE 4096 47# define STACKSIZE 8192
39#endif 48#endif
40 49
41enum { 50enum {
42 REQ_QUIT, 51 REQ_QUIT,
43 REQ_OPEN, REQ_CLOSE, 52 REQ_OPEN, REQ_CLOSE,
44 REQ_READ, REQ_WRITE, REQ_READAHEAD, 53 REQ_READ, REQ_WRITE, REQ_READAHEAD,
45 REQ_SENDFILE, 54 REQ_SENDFILE,
46 REQ_STAT, REQ_LSTAT, REQ_FSTAT, 55 REQ_STAT, REQ_LSTAT, REQ_FSTAT,
47 REQ_FSYNC, REQ_FDATASYNC, 56 REQ_FSYNC, REQ_FDATASYNC,
48 REQ_UNLINK, REQ_RMDIR, 57 REQ_UNLINK, REQ_RMDIR, REQ_RENAME,
49 REQ_SYMLINK, 58 REQ_READDIR,
59 REQ_LINK, REQ_SYMLINK,
60 REQ_SLEEP,
61 REQ_GROUP,
50}; 62};
51 63
64#define AIO_REQ_KLASS "IO::AIO::REQ"
65#define AIO_GRP_KLASS "IO::AIO::GRP"
66
52typedef struct aio_cb { 67typedef struct aio_cb
68{
53 struct aio_cb *volatile next; 69 struct aio_cb *volatile next;
54 70
55 int type; 71 struct aio_cb *grp, *grp_prev, *grp_next, *grp_first;
56 72
57 int fd, fd2; 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;
58 off_t offset; 79 off_t offset;
59 size_t length; 80 size_t length;
60 ssize_t result; 81 ssize_t result;
82
83 int type;
84 int fd, fd2;
85 int errorno;
86 STRLEN dataoffset;
61 mode_t mode; /* open */ 87 mode_t mode; /* open */
62 int errorno; 88 unsigned char cancelled;
63 SV *data, *callback;
64 SV *fh, *fh2;
65 void *dataptr, *data2ptr;
66 STRLEN dataoffset;
67
68 Stat_t *statdata;
69} aio_cb; 89} aio_cb;
70 90
71typedef aio_cb *aio_req; 91typedef aio_cb *aio_req;
92typedef aio_cb *aio_req_ornot;
72 93
73static int started, wanted; 94static int started, wanted;
74static volatile int nreqs; 95static volatile int nreqs;
75static int max_outstanding = 1<<30; 96static int max_outstanding = 1<<30;
76static int respipe [2]; 97static int respipe [2];
80static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; 101static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER;
81 102
82static volatile aio_req reqs, reqe; /* queue start, queue end */ 103static volatile aio_req reqs, reqe; /* queue start, queue end */
83static volatile aio_req ress, rese; /* queue start, queue end */ 104static volatile aio_req ress, rese; /* queue start, queue end */
84 105
106static void req_invoke (aio_req req);
85static void free_req (aio_req req) 107static void req_free (aio_req req);
86{
87 if (req->data)
88 SvREFCNT_dec (req->data);
89 108
109/* must be called at most once */
110static SV *req_sv (aio_req req, const char *klass)
111{
90 if (req->fh) 112 if (!req->self)
91 SvREFCNT_dec (req->fh); 113 {
114 req->self = (SV *)newHV ();
115 sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0);
116 }
92 117
93 if (req->fh2) 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 {
94 SvREFCNT_dec (req->fh2); 157 SvREFCNT_dec (grp->fh2);
95 158 grp->fh2 = 0;
96 if (req->statdata) 159 break;
97 Safefree (req->statdata); 160 }
98 161 }
99 if (req->callback)
100 SvREFCNT_dec (req->callback);
101
102 Safefree (req);
103} 162}
104 163
105static void 164static void aio_grp_dec (aio_req grp)
106poll_wait () 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 ()
107{ 180{
108 if (nreqs && !ress) 181 if (nreqs && !ress)
109 { 182 {
110 fd_set rfd; 183 fd_set rfd;
111 FD_ZERO(&rfd); 184 FD_ZERO(&rfd);
113 186
114 select (respipe [0] + 1, &rfd, 0, 0, 0); 187 select (respipe [0] + 1, &rfd, 0, 0, 0);
115 } 188 }
116} 189}
117 190
118static int 191static void req_invoke (aio_req req)
119poll_cb () 192{
193 dSP;
194 int errorno = errno;
195
196 if (req->cancelled || !SvOK (req->callback))
197 return;
198
199 errno = req->errorno;
200
201 ENTER;
202 SAVETMPS;
203 PUSHMARK (SP);
204 EXTEND (SP, 1);
205
206 switch (req->type)
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 ()
120{ 337{
121 dSP; 338 dSP;
122 int count = 0; 339 int count = 0;
123 int do_croak = 0; 340 int do_croak = 0;
124 aio_req req; 341 aio_req req;
146 pthread_mutex_unlock (&reslock); 363 pthread_mutex_unlock (&reslock);
147 364
148 if (!req) 365 if (!req)
149 break; 366 break;
150 367
151 nreqs--; 368 --nreqs;
152 369
153 if (req->type == REQ_QUIT) 370 if (req->type == REQ_QUIT)
154 started--; 371 started--;
372 else if (req->type == REQ_GROUP && req->length)
373 {
374 req->fd = 1; /* mark request as delayed */
375 continue;
376 }
155 else 377 else
156 { 378 {
157 int errorno = errno;
158 errno = req->errorno;
159
160 if (req->type == REQ_READ) 379 if (req->type == REQ_READ)
161 SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); 380 SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0));
162 381
163 if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE)) 382 if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE))
164 SvREADONLY_off (req->data); 383 SvREADONLY_off (req->data);
168 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; 387 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
169 PL_laststatval = req->result; 388 PL_laststatval = req->result;
170 PL_statcache = *(req->statdata); 389 PL_statcache = *(req->statdata);
171 } 390 }
172 391
173 ENTER; 392 req_invoke (req);
174 PUSHMARK (SP);
175 XPUSHs (sv_2mortal (newSViv (req->result)));
176 393
177 if (req->type == REQ_OPEN)
178 {
179 /* convert fd to fh */
180 SV *fh;
181
182 PUTBACK;
183 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL);
184 SPAGAIN;
185
186 fh = SvREFCNT_inc (POPs);
187
188 PUSHMARK (SP);
189 XPUSHs (sv_2mortal (fh));
190 }
191
192 if (SvOK (req->callback))
193 {
194 PUTBACK;
195 call_sv (req->callback, G_VOID | G_EVAL);
196 SPAGAIN;
197
198 if (SvTRUE (ERRSV))
199 {
200 free_req (req);
201 croak (0);
202 }
203 }
204
205 LEAVE;
206
207 errno = errorno;
208 count++; 394 count++;
209 } 395 }
210 396
211 free_req (req); 397 req_free (req);
212 } 398 }
213 399
214 return count; 400 return count;
215} 401}
216 402
217static void *aio_proc(void *arg); 403static void *aio_proc(void *arg);
218 404
219static void
220start_thread (void) 405static void start_thread (void)
221{ 406{
222 sigset_t fullsigset, oldsigset; 407 sigset_t fullsigset, oldsigset;
223 pthread_t tid; 408 pthread_t tid;
224 pthread_attr_t attr; 409 pthread_attr_t attr;
225 410
234 started++; 419 started++;
235 420
236 sigprocmask (SIG_SETMASK, &oldsigset, 0); 421 sigprocmask (SIG_SETMASK, &oldsigset, 0);
237} 422}
238 423
239static void 424static void req_send (aio_req req)
240send_req (aio_req req)
241{ 425{
242 while (started < wanted && nreqs >= started) 426 while (started < wanted && nreqs >= started)
243 start_thread (); 427 start_thread ();
244 428
245 nreqs++; 429 ++nreqs;
246 430
247 pthread_mutex_lock (&reqlock); 431 pthread_mutex_lock (&reqlock);
248 432
249 req->next = 0; 433 req->next = 0;
250 434
269 453
270 poll_wait (); 454 poll_wait ();
271 } 455 }
272} 456}
273 457
274static void 458static void end_thread (void)
275end_thread (void)
276{ 459{
277 aio_req req; 460 aio_req req;
278 Newz (0, req, 1, aio_cb); 461 Newz (0, req, 1, aio_cb);
279 req->type = REQ_QUIT; 462 req->type = REQ_QUIT;
280 463
281 send_req (req); 464 req_send (req);
282} 465}
283 466
284static void min_parallel (int nthreads) 467static void min_parallel (int nthreads)
285{ 468{
286 if (wanted < nthreads) 469 if (wanted < nthreads)
315 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)) 498 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK))
316 croak ("cannot set result pipe to nonblocking mode"); 499 croak ("cannot set result pipe to nonblocking mode");
317 500
318 if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) 501 if (fcntl (respipe [1], F_SETFL, O_NONBLOCK))
319 croak ("cannot set result pipe to nonblocking mode"); 502 croak ("cannot set result pipe to nonblocking mode");
320}
321
322static void atfork_prepare (void)
323{
324 pthread_mutex_lock (&reqlock);
325 pthread_mutex_lock (&reslock);
326}
327
328static void atfork_parent (void)
329{
330 pthread_mutex_unlock (&reslock);
331 pthread_mutex_unlock (&reqlock);
332}
333
334static void atfork_child (void)
335{
336 aio_req prv;
337
338 started = 0;
339
340 while (reqs)
341 {
342 prv = reqs;
343 reqs = prv->next;
344 free_req (prv);
345 }
346
347 reqs = reqe = 0;
348
349 while (ress)
350 {
351 prv = ress;
352 ress = prv->next;
353 free_req (prv);
354 }
355
356 ress = rese = 0;
357
358 close (respipe [0]);
359 close (respipe [1]);
360 create_pipe ();
361
362 atfork_parent ();
363} 503}
364 504
365/*****************************************************************************/ 505/*****************************************************************************/
366/* work around various missing functions */ 506/* work around various missing functions */
367 507
372/* 512/*
373 * make our pread/pwrite safe against themselves, but not against 513 * make our pread/pwrite safe against themselves, but not against
374 * normal read/write by using a mutex. slows down execution a lot, 514 * normal read/write by using a mutex. slows down execution a lot,
375 * but that's your problem, not mine. 515 * but that's your problem, not mine.
376 */ 516 */
377static pthread_mutex_t iolock = PTHREAD_MUTEX_INITIALIZER; 517static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER;
378 518
379static ssize_t 519static ssize_t pread (int fd, void *buf, size_t count, off_t offset)
380pread (int fd, void *buf, size_t count, off_t offset)
381{ 520{
382 ssize_t res; 521 ssize_t res;
383 off_t ooffset; 522 off_t ooffset;
384 523
385 pthread_mutex_lock (&iolock); 524 pthread_mutex_lock (&preadwritelock);
386 ooffset = lseek (fd, 0, SEEK_CUR); 525 ooffset = lseek (fd, 0, SEEK_CUR);
387 lseek (fd, offset, SEEK_SET); 526 lseek (fd, offset, SEEK_SET);
388 res = read (fd, buf, count); 527 res = read (fd, buf, count);
389 lseek (fd, ooffset, SEEK_SET); 528 lseek (fd, ooffset, SEEK_SET);
390 pthread_mutex_unlock (&iolock); 529 pthread_mutex_unlock (&preadwritelock);
391 530
392 return res; 531 return res;
393} 532}
394 533
395static ssize_t
396pwrite (int fd, void *buf, size_t count, off_t offset) 534static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset)
397{ 535{
398 ssize_t res; 536 ssize_t res;
399 off_t ooffset; 537 off_t ooffset;
400 538
401 pthread_mutex_lock (&iolock); 539 pthread_mutex_lock (&preadwritelock);
402 ooffset = lseek (fd, 0, SEEK_CUR); 540 ooffset = lseek (fd, 0, SEEK_CUR);
403 lseek (fd, offset, SEEK_SET); 541 lseek (fd, offset, SEEK_SET);
404 res = write (fd, buf, count); 542 res = write (fd, buf, count);
405 lseek (fd, offset, SEEK_SET); 543 lseek (fd, offset, SEEK_SET);
406 pthread_mutex_unlock (&iolock); 544 pthread_mutex_unlock (&preadwritelock);
407 545
408 return res; 546 return res;
409} 547}
410#endif 548#endif
411 549
414#endif 552#endif
415 553
416#if !HAVE_READAHEAD 554#if !HAVE_READAHEAD
417# define readahead aio_readahead 555# define readahead aio_readahead
418 556
557static ssize_t readahead (int fd, off_t offset, size_t count)
558{
419static char readahead_buf[4096]; 559 char readahead_buf[4096];
420 560
421static ssize_t
422readahead (int fd, off_t offset, size_t count)
423{
424 while (count > 0) 561 while (count > 0)
425 { 562 {
426 size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); 563 size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf);
427 564
428 pread (fd, readahead_buf, len, offset); 565 pread (fd, readahead_buf, len, offset);
432 569
433 errno = 0; 570 errno = 0;
434} 571}
435#endif 572#endif
436 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);
593 }
594 else
595 *res = 0;
596
597 pthread_mutex_unlock (&readdirlock);
598
599 errno = errorno;
600 return e ? 0 : -1;
601}
602#endif
603
437/* sendfile always needs emulation */ 604/* sendfile always needs emulation */
438static ssize_t
439sendfile_ (int ofd, int ifd, off_t offset, size_t count) 605static ssize_t sendfile_ (int ofd, int ifd, off_t offset, size_t count)
440{ 606{
441 ssize_t res; 607 ssize_t res;
442 608
443 if (!count) 609 if (!count)
444 return 0; 610 return 0;
445 611
446#if HAVE_SENDFILE 612#if HAVE_SENDFILE
447# if __linux 613# if __linux
448 res = sendfile (ofd, ifd, &offset, count); 614 res = sendfile (ofd, ifd, &offset, count);
449 615
450# elif __freebsd 616# elif __freebsd
451 /* 617 /*
452 * Of course, the freebsd sendfile is a dire hack with no thoughts 618 * Of course, the freebsd sendfile is a dire hack with no thoughts
453 * wasted on making it similar to other I/O functions. 619 * wasted on making it similar to other I/O functions.
454 */ 620 */
455 { 621 {
456 off_t sbytes; 622 off_t sbytes;
457 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); 623 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0);
458 624
459 if (res < 0 && sbytes) 625 if (res < 0 && sbytes)
460 /* maybe only on EAGAIN only: as usual, the manpage leaves you guessing */ 626 /* maybe only on EAGAIN only: as usual, the manpage leaves you guessing */
461 res = sbytes; 627 res = sbytes;
462 } 628 }
463 629
464# elif __hpux 630# elif __hpux
465 res = sendfile (ofd, ifd, offset, count, 0, 0); 631 res = sendfile (ofd, ifd, offset, count, 0, 0);
466 632
467# elif __solaris 633# elif __solaris
468 { 634 {
469 struct sendfilevec vec; 635 struct sendfilevec vec;
470 size_t sbytes; 636 size_t sbytes;
471 637
472 vec.sfv_fd = ifd; 638 vec.sfv_fd = ifd;
473 vec.sfv_flag = 0; 639 vec.sfv_flag = 0;
474 vec.sfv_off = offset; 640 vec.sfv_off = offset;
475 vec.sfv_len = count; 641 vec.sfv_len = count;
476 642
477 res = sendfilev (ofd, &vec, 1, &sbytes); 643 res = sendfilev (ofd, &vec, 1, &sbytes);
478 644
479 if (res < 0 && sbytes) 645 if (res < 0 && sbytes)
480 res = sbytes; 646 res = sbytes;
481 } 647 }
482 648
483# else
484 res = -1;
485 errno = ENOSYS;
486# endif 649# endif
650#else
651 res = -1;
652 errno = ENOSYS;
487#endif 653#endif
488 654
489 if (res < 0 655 if (res < 0
490 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK 656 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK
491#if __solaris 657#if __solaris
492 || errno == EAFNOSUPPORT || errno == EPROTOTYPE 658 || errno == EAFNOSUPPORT || errno == EPROTOTYPE
493#endif 659#endif
494 ) 660 )
495 ) 661 )
496 { 662 {
497 /* emulate sendfile. this is a major pain in the ass */ 663 /* emulate sendfile. this is a major pain in the ass */
498 char *buf = malloc (4096); 664 char buf[4096];
499 res = 0; 665 res = 0;
500 666
501 for (;;) 667 while (count)
502 { 668 {
503 ssize_t cnt; 669 ssize_t cnt;
504 670
505 cnt = pread (ifd, buf, 4096, offset); 671 cnt = pread (ifd, buf, count > 4096 ? 4096 : count, offset);
506 672
507 if (cnt <= 0) 673 if (cnt <= 0)
508 { 674 {
509 if (cnt && !res) res = -1; 675 if (cnt && !res) res = -1;
510 break; 676 break;
511 } 677 }
512 678
513 cnt = write (ofd, buf, cnt); 679 cnt = write (ofd, buf, cnt);
514 680
515 if (cnt <= 0) 681 if (cnt <= 0)
516 { 682 {
517 if (cnt && !res) res = -1; 683 if (cnt && !res) res = -1;
518 break; 684 break;
519 } 685 }
520 686
521 offset += cnt; 687 offset += cnt;
522 res += cnt; 688 res += cnt;
689 count -= cnt;
523 } 690 }
524
525 {
526 int errorno = errno;
527 free (buf);
528 errno = errorno;
529 }
530 } 691 }
531 692
532 return res; 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
712 if (!dirp)
713 return -1;
714
715 names = malloc (memlen);
716
717 for (;;)
718 {
719 errno = 0, readdir_r (dirp, &u.d, &entp);
720
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;
533} 757}
534 758
535/*****************************************************************************/ 759/*****************************************************************************/
536 760
537static void *
538aio_proc (void *thr_arg) 761static void *aio_proc (void *thr_arg)
539{ 762{
540 aio_req req; 763 aio_req req;
541 int type; 764 int type;
542 765
543 do 766 do
562 785
563 pthread_mutex_unlock (&reqlock); 786 pthread_mutex_unlock (&reqlock);
564 787
565 errno = 0; /* strictly unnecessary */ 788 errno = 0; /* strictly unnecessary */
566 789
567 type = req->type; 790 if (!req->cancelled)
568 791 switch (type = req->type) /* remember type for QUIT check */
569 switch (type)
570 { 792 {
571 case REQ_READ: req->result = pread (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;
572 case REQ_WRITE: req->result = pwrite (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;
573 795
574 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;
575 case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length); break; 797 case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length); break;
576 798
577 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; 799 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break;
578 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; 800 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break;
579 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; 801 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break;
580 802
581 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;
582 case REQ_CLOSE: req->result = close (req->fd); break; 804 case REQ_CLOSE: req->result = close (req->fd); break;
583 case REQ_UNLINK: req->result = unlink (req->dataptr); break; 805 case REQ_UNLINK: req->result = unlink (req->dataptr); break;
584 case REQ_RMDIR: req->result = rmdir (req->dataptr); 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;
585 case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; 809 case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break;
586 810
587 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; 811 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break;
588 case REQ_FSYNC: req->result = fsync (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;
589 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
590 case REQ_QUIT: 825 case REQ_QUIT:
591 break; 826 break;
592 827
593 default: 828 default:
594 req->result = ENOSYS; 829 req->result = ENOSYS;
595 break; 830 break;
596 } 831 }
597 832
598 req->errorno = errno; 833 req->errorno = errno;
599 834
600 pthread_mutex_lock (&reslock); 835 pthread_mutex_lock (&reslock);
601 836
619 while (type != REQ_QUIT); 854 while (type != REQ_QUIT);
620 855
621 return 0; 856 return 0;
622} 857}
623 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
624#define dREQ \ 916#define dREQ \
625 aio_req req; \ 917 aio_req req; \
626 \ 918 \
627 if (SvOK (callback) && !SvROK (callback)) \ 919 if (SvOK (callback) && !SvROK (callback)) \
628 croak ("clalback must be undef or of reference type"); \ 920 croak ("callback must be undef or of reference type"); \
629 \ 921 \
630 Newz (0, req, 1, aio_cb); \ 922 Newz (0, req, 1, aio_cb); \
631 if (!req) \ 923 if (!req) \
632 croak ("out of memory during aio_req allocation"); \ 924 croak ("out of memory during aio_req allocation"); \
633 \ 925 \
634 req->callback = newSVsv (callback); 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));
635 933
636MODULE = IO::AIO PACKAGE = IO::AIO 934MODULE = IO::AIO PACKAGE = IO::AIO
637 935
638PROTOTYPES: ENABLE 936PROTOTYPES: ENABLE
639 937
640BOOT: 938BOOT:
641{ 939{
940 HV *stash = gv_stashpv ("IO::AIO", 1);
941 newCONSTSUB (stash, "EXDEV", newSViv (EXDEV));
942 newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY));
943 newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY));
944
642 create_pipe (); 945 create_pipe ();
643 pthread_atfork (atfork_prepare, atfork_parent, atfork_child); 946 pthread_atfork (atfork_prepare, atfork_parent, atfork_child);
644} 947}
645 948
646void 949void
647min_parallel(nthreads) 950min_parallel (nthreads)
648 int nthreads 951 int nthreads
649 PROTOTYPE: $ 952 PROTOTYPE: $
650 953
651void 954void
652max_parallel(nthreads) 955max_parallel (nthreads)
653 int nthreads 956 int nthreads
654 PROTOTYPE: $ 957 PROTOTYPE: $
655 958
656int 959int
657max_outstanding(nreqs) 960max_outstanding (nreqs)
658 int nreqs 961 int nreqs
659 PROTOTYPE: $ 962 PROTOTYPE: $
660 CODE: 963 CODE:
661 RETVAL = max_outstanding; 964 RETVAL = max_outstanding;
662 max_outstanding = nreqs; 965 max_outstanding = nreqs;
663 966
664void 967void
665aio_open(pathname,flags,mode,callback=&PL_sv_undef) 968aio_open (pathname,flags,mode,callback=&PL_sv_undef)
666 SV * pathname 969 SV * pathname
667 int flags 970 int flags
668 int mode 971 int mode
669 SV * callback 972 SV * callback
670 PROTOTYPE: $$$;$ 973 PROTOTYPE: $$$;$
671 CODE: 974 PPCODE:
672{ 975{
673 dREQ; 976 dREQ;
674 977
675 req->type = REQ_OPEN; 978 req->type = REQ_OPEN;
676 req->data = newSVsv (pathname); 979 req->data = newSVsv (pathname);
677 req->dataptr = SvPVbyte_nolen (req->data); 980 req->dataptr = SvPVbyte_nolen (req->data);
678 req->fd = flags; 981 req->fd = flags;
679 req->mode = mode; 982 req->mode = mode;
680 983
681 send_req (req); 984 REQ_SEND;
682} 985}
683 986
684void 987void
685aio_close(fh,callback=&PL_sv_undef) 988aio_close (fh,callback=&PL_sv_undef)
686 SV * fh 989 SV * fh
687 SV * callback 990 SV * callback
688 PROTOTYPE: $;$ 991 PROTOTYPE: $;$
689 ALIAS: 992 ALIAS:
690 aio_close = REQ_CLOSE 993 aio_close = REQ_CLOSE
691 aio_fsync = REQ_FSYNC 994 aio_fsync = REQ_FSYNC
692 aio_fdatasync = REQ_FDATASYNC 995 aio_fdatasync = REQ_FDATASYNC
693 CODE: 996 PPCODE:
694{ 997{
695 dREQ; 998 dREQ;
696 999
697 req->type = ix; 1000 req->type = ix;
698 req->fh = newSVsv (fh); 1001 req->fh = newSVsv (fh);
699 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); 1002 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh)));
700 1003
701 send_req (req); 1004 REQ_SEND (req);
702} 1005}
703 1006
704void 1007void
705aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) 1008aio_read (fh,offset,length,data,dataoffset,callback=&PL_sv_undef)
706 SV * fh 1009 SV * fh
707 UV offset 1010 UV offset
708 UV length 1011 UV length
709 SV * data 1012 SV * data
710 UV dataoffset 1013 UV dataoffset
711 SV * callback 1014 SV * callback
712 ALIAS: 1015 ALIAS:
713 aio_read = REQ_READ 1016 aio_read = REQ_READ
714 aio_write = REQ_WRITE 1017 aio_write = REQ_WRITE
715 PROTOTYPE: $$$$$;$ 1018 PROTOTYPE: $$$$$;$
716 CODE: 1019 PPCODE:
717{ 1020{
718 aio_req req; 1021 aio_req req;
719 STRLEN svlen; 1022 STRLEN svlen;
720 char *svptr = SvPVbyte (data, svlen); 1023 char *svptr = SvPVbyte (data, svlen);
721 1024
759 { 1062 {
760 SvREADONLY_on (data); 1063 SvREADONLY_on (data);
761 req->data2ptr = (void *)data; 1064 req->data2ptr = (void *)data;
762 } 1065 }
763 1066
764 send_req (req); 1067 REQ_SEND;
765 } 1068 }
766} 1069}
767 1070
768void 1071void
769aio_sendfile(out_fh,in_fh,in_offset,length,callback=&PL_sv_undef) 1072aio_sendfile (out_fh,in_fh,in_offset,length,callback=&PL_sv_undef)
770 SV * out_fh 1073 SV * out_fh
771 SV * in_fh 1074 SV * in_fh
772 UV in_offset 1075 UV in_offset
773 UV length 1076 UV length
774 SV * callback 1077 SV * callback
775 PROTOTYPE: $$$$;$ 1078 PROTOTYPE: $$$$;$
776 CODE: 1079 PPCODE:
777{ 1080{
778 dREQ; 1081 dREQ;
779 1082
780 req->type = REQ_SENDFILE; 1083 req->type = REQ_SENDFILE;
781 req->fh = newSVsv (out_fh); 1084 req->fh = newSVsv (out_fh);
783 req->fh2 = newSVsv (in_fh); 1086 req->fh2 = newSVsv (in_fh);
784 req->fd2 = PerlIO_fileno (IoIFP (sv_2io (in_fh))); 1087 req->fd2 = PerlIO_fileno (IoIFP (sv_2io (in_fh)));
785 req->offset = in_offset; 1088 req->offset = in_offset;
786 req->length = length; 1089 req->length = length;
787 1090
788 send_req (req); 1091 REQ_SEND;
789} 1092}
790 1093
791void 1094void
792aio_readahead(fh,offset,length,callback=&PL_sv_undef) 1095aio_readahead (fh,offset,length,callback=&PL_sv_undef)
793 SV * fh 1096 SV * fh
794 UV offset 1097 UV offset
795 IV length 1098 IV length
796 SV * callback 1099 SV * callback
797 PROTOTYPE: $$$;$ 1100 PROTOTYPE: $$$;$
798 CODE: 1101 PPCODE:
799{ 1102{
800 dREQ; 1103 dREQ;
801 1104
802 req->type = REQ_READAHEAD; 1105 req->type = REQ_READAHEAD;
803 req->fh = newSVsv (fh); 1106 req->fh = newSVsv (fh);
804 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); 1107 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh)));
805 req->offset = offset; 1108 req->offset = offset;
806 req->length = length; 1109 req->length = length;
807 1110
808 send_req (req); 1111 REQ_SEND;
809} 1112}
810 1113
811void 1114void
812aio_stat(fh_or_path,callback=&PL_sv_undef) 1115aio_stat (fh_or_path,callback=&PL_sv_undef)
813 SV * fh_or_path 1116 SV * fh_or_path
814 SV * callback 1117 SV * callback
815 ALIAS: 1118 ALIAS:
816 aio_stat = REQ_STAT 1119 aio_stat = REQ_STAT
817 aio_lstat = REQ_LSTAT 1120 aio_lstat = REQ_LSTAT
818 CODE: 1121 PPCODE:
819{ 1122{
820 dREQ; 1123 dREQ;
821 1124
822 New (0, req->statdata, 1, Stat_t); 1125 New (0, req->statdata, 1, Stat_t);
823 if (!req->statdata) 1126 if (!req->statdata)
824 { 1127 {
825 free_req (req); 1128 req_free (req);
826 croak ("out of memory during aio_req->statdata allocation"); 1129 croak ("out of memory during aio_req->statdata allocation");
827 } 1130 }
828 1131
829 if (SvPOK (fh_or_path)) 1132 if (SvPOK (fh_or_path))
830 { 1133 {
837 req->type = REQ_FSTAT; 1140 req->type = REQ_FSTAT;
838 req->fh = newSVsv (fh_or_path); 1141 req->fh = newSVsv (fh_or_path);
839 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); 1142 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
840 } 1143 }
841 1144
842 send_req (req); 1145 REQ_SEND;
843} 1146}
844 1147
845void 1148void
846aio_unlink(pathname,callback=&PL_sv_undef) 1149aio_unlink (pathname,callback=&PL_sv_undef)
847 SV * pathname 1150 SV * pathname
848 SV * callback 1151 SV * callback
849 ALIAS: 1152 ALIAS:
850 aio_unlink = REQ_UNLINK 1153 aio_unlink = REQ_UNLINK
851 aio_rmdir = REQ_RMDIR 1154 aio_rmdir = REQ_RMDIR
1155 aio_readdir = REQ_READDIR
852 CODE: 1156 PPCODE:
853{ 1157{
854 dREQ; 1158 dREQ;
855 1159
856 req->type = ix; 1160 req->type = ix;
857 req->data = newSVsv (pathname); 1161 req->data = newSVsv (pathname);
858 req->dataptr = SvPVbyte_nolen (req->data); 1162 req->dataptr = SvPVbyte_nolen (req->data);
859 1163
860 send_req (req); 1164 REQ_SEND;
861} 1165}
862 1166
863void 1167void
864aio_symlink(oldpath,newpath,callback=&PL_sv_undef) 1168aio_link (oldpath,newpath,callback=&PL_sv_undef)
865 SV * oldpath 1169 SV * oldpath
866 SV * newpath 1170 SV * newpath
867 SV * callback 1171 SV * callback
1172 ALIAS:
1173 aio_link = REQ_LINK
1174 aio_symlink = REQ_SYMLINK
1175 aio_rename = REQ_RENAME
868 CODE: 1176 PPCODE:
869{ 1177{
870 dREQ; 1178 dREQ;
871 1179
872 req->type = REQ_SYMLINK; 1180 req->type = ix;
873 req->fh = newSVsv (oldpath); 1181 req->fh = newSVsv (oldpath);
874 req->data2ptr = SvPVbyte_nolen (req->fh); 1182 req->data2ptr = SvPVbyte_nolen (req->fh);
875 req->data = newSVsv (newpath); 1183 req->data = newSVsv (newpath);
876 req->dataptr = SvPVbyte_nolen (req->data); 1184 req->dataptr = SvPVbyte_nolen (req->data);
877 1185
878 send_req (req); 1186 REQ_SEND;
879} 1187}
880 1188
881void 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
882flush() 1217flush ()
883 PROTOTYPE: 1218 PROTOTYPE:
884 CODE: 1219 CODE:
885 while (nreqs) 1220 while (nreqs)
886 { 1221 {
887 poll_wait (); 1222 poll_wait ();
927 CODE: 1262 CODE:
928 RETVAL = nreqs; 1263 RETVAL = nreqs;
929 OUTPUT: 1264 OUTPUT:
930 RETVAL 1265 RETVAL
931 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