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.26 by root, Wed Aug 17 03:52:20 2005 UTC vs.
Revision 1.43 by root, Sat Oct 21 23:06:04 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines