ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/IO-AIO/AIO.xs
(Generate patch)

Comparing IO-AIO/AIO.xs (file contents):
Revision 1.33 by root, Mon Aug 22 23:21:57 2005 UTC vs.
Revision 1.110 by root, Sun Dec 2 20:54:33 2007 UTC

1#define _REENTRANT 1 1#include "xthread.h"
2
2#include <errno.h> 3#include <errno.h>
3 4
4#include "EXTERN.h" 5#include "EXTERN.h"
5#include "perl.h" 6#include "perl.h"
6#include "XSUB.h" 7#include "XSUB.h"
7 8
8#include "autoconf/config.h"
9
10#include <pthread.h> 9#include <stddef.h>
11 10#include <stdlib.h>
11#include <errno.h>
12#include <sys/types.h> 12#include <sys/types.h>
13#include <sys/stat.h> 13#include <sys/stat.h>
14
15#include <unistd.h> 14#include <limits.h>
16#include <fcntl.h> 15#include <fcntl.h>
17#include <signal.h>
18#include <sched.h> 16#include <sched.h>
17
18#ifdef _WIN32
19
20# define SIGIO 0
21 typedef Direntry_t X_DIRENT;
22#undef malloc
23#undef free
24
25// perl overrides all those nice win32 functions
26# undef open
27# undef read
28# undef write
29# undef send
30# undef recv
31# undef stat
32# undef fstat
33# define lstat stat
34# undef truncate
35# undef ftruncate
36# undef open
37# undef close
38# undef unlink
39# undef rmdir
40# undef rename
41# undef lseek
42
43# define chown(a,b,c) (errno = ENOSYS, -1)
44# define fchown(a,b,c) (errno = ENOSYS, -1)
45# define fchmod(a,b) (errno = ENOSYS, -1)
46# define symlink(a,b) (errno = ENOSYS, -1)
47# define readlink(a,b,c) (errno = ENOSYS, -1)
48# define mknod(a,b,c) (errno = ENOSYS, -1)
49# define truncate(a,b) (errno = ENOSYS, -1)
50# define ftruncate(fd,o) chsize ((fd), (o))
51# define fsync(fd) _commit (fd)
52# define opendir(fd) (errno = ENOSYS, 0)
53# define readdir(fd) (errno = ENOSYS, -1)
54# define closedir(fd) (errno = ENOSYS, -1)
55# define mkdir(a,b) mkdir (a)
56
57#else
58
59# include "autoconf/config.h"
60# include <sys/time.h>
61# include <sys/select.h>
62# include <unistd.h>
63# include <utime.h>
64# include <signal.h>
65 typedef struct dirent X_DIRENT;
66
67#endif
19 68
20#if HAVE_SENDFILE 69#if HAVE_SENDFILE
21# if __linux 70# if __linux
22# include <sys/sendfile.h> 71# include <sys/sendfile.h>
23# elif __freebsd 72# elif __freebsd
24# include <sys/socket.h> 73# include <sys/socket.h>
25# include <sys/uio.h> 74# include <sys/uio.h>
26# elif __hpux 75# elif __hpux
27# include <sys/socket.h> 76# include <sys/socket.h>
77# elif __solaris /* not yet */
78# include <sys/sendfile.h>
79# else
80# error sendfile support requested but not available
28# endif 81# endif
29#endif 82#endif
30 83
31#if __ia64 84/* number of seconds after which idle threads exit */
85#define IDLE_TIMEOUT 10
86
87/* used for struct dirent, AIX doesn't provide it */
88#ifndef NAME_MAX
89# define NAME_MAX 4096
90#endif
91
92/* buffer size for various temporary buffers */
32# define STACKSIZE 65536 93#define AIO_BUFSIZE 65536
94
95/* use NV for 32 bit perls as it allows larger offsets */
96#if IVSIZE >= 8
97# define SvVAL64 SvIV
33#else 98#else
34# define STACKSIZE 4096 99# define SvVAL64 SvNV
35#endif 100#endif
101
102static HV *stash;
103
104#define dBUF \
105 char *aio_buf; \
106 X_LOCK (wrklock); \
107 self->dbuf = aio_buf = malloc (AIO_BUFSIZE); \
108 X_UNLOCK (wrklock); \
109 if (!aio_buf) \
110 return -1;
111
112typedef SV SV8; /* byte-sv, used for argument-checking */
36 113
37enum { 114enum {
38 REQ_QUIT, 115 REQ_QUIT,
39 REQ_OPEN, REQ_CLOSE, 116 REQ_OPEN, REQ_CLOSE,
40 REQ_READ, REQ_WRITE, REQ_READAHEAD, 117 REQ_READ, REQ_WRITE,
41 REQ_SENDFILE, 118 REQ_READAHEAD, REQ_SENDFILE,
42 REQ_STAT, REQ_LSTAT, REQ_FSTAT, 119 REQ_STAT, REQ_LSTAT, REQ_FSTAT,
120 REQ_TRUNCATE, REQ_FTRUNCATE,
121 REQ_UTIME, REQ_FUTIME,
122 REQ_CHMOD, REQ_FCHMOD,
123 REQ_CHOWN, REQ_FCHOWN,
43 REQ_FSYNC, REQ_FDATASYNC, 124 REQ_SYNC, REQ_FSYNC, REQ_FDATASYNC,
44 REQ_UNLINK, REQ_RMDIR, 125 REQ_UNLINK, REQ_RMDIR, REQ_MKDIR, REQ_RENAME,
45 REQ_SYMLINK, 126 REQ_MKNOD, REQ_READDIR,
127 REQ_LINK, REQ_SYMLINK, REQ_READLINK,
128 REQ_GROUP, REQ_NOP,
129 REQ_BUSY,
46}; 130};
47 131
132#define AIO_REQ_KLASS "IO::AIO::REQ"
133#define AIO_GRP_KLASS "IO::AIO::GRP"
134
48typedef struct aio_cb { 135typedef struct aio_cb
136{
49 struct aio_cb *volatile next; 137 struct aio_cb *volatile next;
50 138
139 SV *callback;
140 SV *sv1, *sv2;
141 void *ptr1, *ptr2;
142 off_t offs;
143 size_t size;
144 ssize_t result;
145 double nv1, nv2;
146
147 STRLEN stroffset;
51 int type; 148 int type;
52 149 int int1, int2, int3;
53 int fd, fd2; 150 int errorno;
54 off_t offset;
55 size_t length;
56 ssize_t result;
57 mode_t mode; /* open */ 151 mode_t mode; /* open */
58 int errorno;
59 SV *data, *callback;
60 SV *fh, *fh2;
61 void *dataptr, *data2ptr;
62 STRLEN dataoffset;
63 152
64 Stat_t *statdata; 153 unsigned char flags;
154 unsigned char pri;
155
156 SV *self; /* the perl counterpart of this request, if any */
157 struct aio_cb *grp, *grp_prev, *grp_next, *grp_first;
65} aio_cb; 158} aio_cb;
66 159
160enum {
161 FLAG_CANCELLED = 0x01, /* request was cancelled */
162 FLAG_SV2_RO_OFF = 0x40, /* data was set readonly */
163 FLAG_PTR2_FREE = 0x80, /* need to free(ptr2) */
164};
165
67typedef aio_cb *aio_req; 166typedef aio_cb *aio_req;
167typedef aio_cb *aio_req_ornot;
68 168
169enum {
170 PRI_MIN = -4,
171 PRI_MAX = 4,
172
173 DEFAULT_PRI = 0,
174 PRI_BIAS = -PRI_MIN,
175 NUM_PRI = PRI_MAX + PRI_BIAS + 1,
176};
177
178#define AIO_TICKS ((1000000 + 1023) >> 10)
179
180static unsigned int max_poll_time = 0;
181static unsigned int max_poll_reqs = 0;
182
183/* calculcate time difference in ~1/AIO_TICKS of a second */
184static int tvdiff (struct timeval *tv1, struct timeval *tv2)
185{
186 return (tv2->tv_sec - tv1->tv_sec ) * AIO_TICKS
187 + ((tv2->tv_usec - tv1->tv_usec) >> 10);
188}
189
190static thread_t main_tid;
191static int main_sig;
192static int block_sig_level;
193
194void block_sig ()
195{
196 sigset_t ss;
197
198 if (block_sig_level++)
199 return;
200
201 if (!main_sig)
202 return;
203
204 sigemptyset (&ss);
205 sigaddset (&ss, main_sig);
206 pthread_sigmask (SIG_BLOCK, &ss, 0);
207}
208
209void unblock_sig ()
210{
211 sigset_t ss;
212
213 if (--block_sig_level)
214 return;
215
216 if (!main_sig)
217 return;
218
219 sigemptyset (&ss);
220 sigaddset (&ss, main_sig);
221 pthread_sigmask (SIG_UNBLOCK, &ss, 0);
222}
223
224static int next_pri = DEFAULT_PRI + PRI_BIAS;
225
69static int started, wanted; 226static unsigned int started, idle, wanted;
70static volatile int nreqs;
71static int max_outstanding = 1<<30;
72static int respipe [2];
73 227
228/* worker threads management */
229static mutex_t wrklock = X_MUTEX_INIT;
230
231typedef struct worker {
232 /* locked by wrklock */
233 struct worker *prev, *next;
234
235 thread_t tid;
236
237 /* locked by reslock, reqlock or wrklock */
238 aio_req req; /* currently processed request */
239 void *dbuf;
240 DIR *dirp;
241} worker;
242
243static worker wrk_first = { &wrk_first, &wrk_first, 0 };
244
245static void worker_clear (worker *wrk)
246{
247 if (wrk->dirp)
248 {
249 closedir (wrk->dirp);
250 wrk->dirp = 0;
251 }
252
253 if (wrk->dbuf)
254 {
255 free (wrk->dbuf);
256 wrk->dbuf = 0;
257 }
258}
259
260static void worker_free (worker *wrk)
261{
262 wrk->next->prev = wrk->prev;
263 wrk->prev->next = wrk->next;
264
265 free (wrk);
266}
267
268static volatile unsigned int nreqs, nready, npending;
269static volatile unsigned int max_idle = 4;
270static volatile unsigned int max_outstanding = 0xffffffff;
271static int respipe_osf [2], respipe [2] = { -1, -1 };
272
74static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; 273static mutex_t reslock = X_MUTEX_INIT;
75static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; 274static mutex_t reqlock = X_MUTEX_INIT;
76static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; 275static cond_t reqwait = X_COND_INIT;
77 276
78static volatile aio_req reqs, reqe; /* queue start, queue end */ 277#if WORDACCESS_UNSAFE
79static volatile aio_req ress, rese; /* queue start, queue end */
80 278
279static unsigned int get_nready ()
280{
281 unsigned int retval;
282
283 X_LOCK (reqlock);
284 retval = nready;
285 X_UNLOCK (reqlock);
286
287 return retval;
288}
289
290static unsigned int get_npending ()
291{
292 unsigned int retval;
293
294 X_LOCK (reslock);
295 retval = npending;
296 X_UNLOCK (reslock);
297
298 return retval;
299}
300
301static unsigned int get_nthreads ()
302{
303 unsigned int retval;
304
305 X_LOCK (wrklock);
306 retval = started;
307 X_UNLOCK (wrklock);
308
309 return retval;
310}
311
312#else
313
314# define get_nready() nready
315# define get_npending() npending
316# define get_nthreads() started
317
318#endif
319
320/*
321 * a somewhat faster data structure might be nice, but
322 * with 8 priorities this actually needs <20 insns
323 * per shift, the most expensive operation.
324 */
325typedef struct {
326 aio_req qs[NUM_PRI], qe[NUM_PRI]; /* qstart, qend */
327 int size;
328} reqq;
329
330static reqq req_queue;
331static reqq res_queue;
332
333int reqq_push (reqq *q, aio_req req)
334{
335 int pri = req->pri;
336 req->next = 0;
337
338 if (q->qe[pri])
339 {
340 q->qe[pri]->next = req;
341 q->qe[pri] = req;
342 }
343 else
344 q->qe[pri] = q->qs[pri] = req;
345
346 return q->size++;
347}
348
349aio_req reqq_shift (reqq *q)
350{
351 int pri;
352
353 if (!q->size)
354 return 0;
355
356 --q->size;
357
358 for (pri = NUM_PRI; pri--; )
359 {
360 aio_req req = q->qs[pri];
361
362 if (req)
363 {
364 if (!(q->qs[pri] = req->next))
365 q->qe[pri] = 0;
366
367 return req;
368 }
369 }
370
371 abort ();
372}
373
374static int poll_cb ();
375static int req_invoke (aio_req req);
376static void req_destroy (aio_req req);
81static void free_req (aio_req req) 377static void req_cancel (aio_req req);
378
379/* must be called at most once */
380static SV *req_sv (aio_req req, const char *klass)
82{ 381{
382 if (!req->self)
383 {
384 req->self = (SV *)newHV ();
385 sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0);
386 }
387
388 return sv_2mortal (sv_bless (newRV_inc (req->self), gv_stashpv (klass, 1)));
389}
390
391static aio_req SvAIO_REQ (SV *sv)
392{
393 MAGIC *mg;
394
395 if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv))
396 croak ("object of class " AIO_REQ_KLASS " expected");
397
398 mg = mg_find (SvRV (sv), PERL_MAGIC_ext);
399
400 return mg ? (aio_req)mg->mg_ptr : 0;
401}
402
403static void aio_grp_feed (aio_req grp)
404{
405 block_sig ();
406
407 while (grp->size < grp->int2 && !(grp->flags & FLAG_CANCELLED))
408 {
409 int old_len = grp->size;
410
411 if (grp->sv2 && SvOK (grp->sv2))
412 {
413 dSP;
414
415 ENTER;
416 SAVETMPS;
417 PUSHMARK (SP);
418 XPUSHs (req_sv (grp, AIO_GRP_KLASS));
419 PUTBACK;
420 call_sv (grp->sv2, G_VOID | G_EVAL | G_KEEPERR);
421 SPAGAIN;
422 FREETMPS;
423 LEAVE;
424 }
425
426 /* stop if no progress has been made */
427 if (old_len == grp->size)
428 {
429 SvREFCNT_dec (grp->sv2);
430 grp->sv2 = 0;
431 break;
432 }
433 }
434
435 unblock_sig ();
436}
437
438static void aio_grp_dec (aio_req grp)
439{
440 --grp->size;
441
442 /* call feeder, if applicable */
443 aio_grp_feed (grp);
444
445 /* finish, if done */
446 if (!grp->size && grp->int1)
447 {
448 block_sig ();
449
450 if (!req_invoke (grp))
451 {
452 req_destroy (grp);
453 unblock_sig ();
454 croak (0);
455 }
456
457 req_destroy (grp);
458 unblock_sig ();
459 }
460}
461
462static int req_invoke (aio_req req)
463{
464 dSP;
465
466 if (req->flags & FLAG_SV2_RO_OFF)
467 SvREADONLY_off (req->sv2);
468
469 if (!(req->flags & FLAG_CANCELLED) && SvOK (req->callback))
470 {
471 ENTER;
472 SAVETMPS;
473 PUSHMARK (SP);
474 EXTEND (SP, 1);
475
476 switch (req->type)
477 {
478 case REQ_READDIR:
479 {
480 SV *rv = &PL_sv_undef;
481
482 if (req->result >= 0)
483 {
484 int i;
485 char *buf = req->ptr2;
486 AV *av = newAV ();
487
488 av_extend (av, req->result - 1);
489
490 for (i = 0; i < req->result; ++i)
491 {
492 SV *sv = newSVpv (buf, 0);
493
494 av_store (av, i, sv);
495 buf += SvCUR (sv) + 1;
496 }
497
498 rv = sv_2mortal (newRV_noinc ((SV *)av));
499 }
500
501 PUSHs (rv);
502 }
503 break;
504
505 case REQ_OPEN:
506 {
507 /* convert fd to fh */
508 SV *fh = &PL_sv_undef;
509
510 if (req->result >= 0)
511 {
512 GV *gv = (GV *)sv_newmortal ();
513 int flags = req->int1 & (O_RDONLY | O_WRONLY | O_RDWR);
514 char sym [64];
515 int symlen;
516
517 symlen = snprintf (sym, sizeof (sym), "fd#%d", req->result);
518 gv_init (gv, stash, sym, symlen, 0);
519
520 symlen = snprintf (
521 sym,
522 sizeof (sym),
523 "%s&=%d",
524 flags == O_RDONLY ? "<" : flags == O_WRONLY ? ">" : "+<",
525 req->result
526 );
527
528 if (do_open (gv, sym, symlen, 0, 0, 0, 0))
529 fh = (SV *)gv;
530 }
531
532 PUSHs (fh);
533 }
534 break;
535
536 case REQ_GROUP:
537 req->int1 = 2; /* mark group as finished */
538
539 if (req->sv1)
540 {
541 int i;
542 AV *av = (AV *)req->sv1;
543
544 EXTEND (SP, AvFILL (av) + 1);
545 for (i = 0; i <= AvFILL (av); ++i)
546 PUSHs (*av_fetch (av, i, 0));
547 }
548 break;
549
550 case REQ_NOP:
551 case REQ_BUSY:
552 break;
553
554 case REQ_READLINK:
555 if (req->result > 0)
556 {
557 SvCUR_set (req->sv2, req->result);
558 *SvEND (req->sv2) = 0;
559 PUSHs (req->sv2);
560 }
561 break;
562
563 case REQ_STAT:
564 case REQ_LSTAT:
565 case REQ_FSTAT:
566 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
567 PL_laststatval = req->result;
568 PL_statcache = *(Stat_t *)(req->ptr2);
569 PUSHs (sv_2mortal (newSViv (req->result)));
570 break;
571
572 case REQ_READ:
573 SvCUR_set (req->sv2, req->stroffset + (req->result > 0 ? req->result : 0));
574 *SvEND (req->sv2) = 0;
575 PUSHs (sv_2mortal (newSViv (req->result)));
576 break;
577
578 default:
579 PUSHs (sv_2mortal (newSViv (req->result)));
580 break;
581 }
582
583 errno = req->errorno;
584
585 PUTBACK;
586 call_sv (req->callback, G_VOID | G_EVAL | G_DISCARD);
587 SPAGAIN;
588
589 FREETMPS;
590 LEAVE;
591
592 PUTBACK;
593 }
594
83 if (req->data) 595 if (req->grp)
84 SvREFCNT_dec (req->data); 596 {
597 aio_req grp = req->grp;
85 598
599 /* unlink request */
600 if (req->grp_next) req->grp_next->grp_prev = req->grp_prev;
601 if (req->grp_prev) req->grp_prev->grp_next = req->grp_next;
602
603 if (grp->grp_first == req)
604 grp->grp_first = req->grp_next;
605
606 aio_grp_dec (grp);
607 }
608
609 return !SvTRUE (ERRSV);
610}
611
612static void req_destroy (aio_req req)
613{
86 if (req->fh) 614 if (req->self)
615 {
616 sv_unmagic (req->self, PERL_MAGIC_ext);
87 SvREFCNT_dec (req->fh); 617 SvREFCNT_dec (req->self);
618 }
88 619
89 if (req->fh2) 620 SvREFCNT_dec (req->sv1);
90 SvREFCNT_dec (req->fh2); 621 SvREFCNT_dec (req->sv2);
91
92 if (req->statdata)
93 Safefree (req->statdata);
94
95 if (req->callback)
96 SvREFCNT_dec (req->callback); 622 SvREFCNT_dec (req->callback);
623
624 if (req->flags & FLAG_PTR2_FREE)
625 free (req->ptr2);
97 626
98 Safefree (req); 627 Safefree (req);
99} 628}
100 629
630static void req_cancel_subs (aio_req grp)
631{
632 aio_req sub;
633
634 if (grp->type != REQ_GROUP)
635 return;
636
637 SvREFCNT_dec (grp->sv2);
638 grp->sv2 = 0;
639
640 for (sub = grp->grp_first; sub; sub = sub->grp_next)
641 req_cancel (sub);
642}
643
644static void req_cancel (aio_req req)
645{
646 req->flags |= FLAG_CANCELLED;
647
648 req_cancel_subs (req);
649}
650
651#ifdef USE_SOCKETS_AS_HANDLES
652# define TO_SOCKET(x) (win32_get_osfhandle (x))
653#else
654# define TO_SOCKET(x) (x)
655#endif
656
101static void 657static void
102poll_wait () 658create_respipe ()
103{ 659{
104 if (nreqs && !ress) 660 int old_readfd = respipe [0];
661
662 if (respipe [1] >= 0)
663 respipe_close (TO_SOCKET (respipe [1]));
664
665#ifdef _WIN32
666 if (PerlSock_socketpair (AF_UNIX, SOCK_STREAM, 0, respipe))
667#else
668 if (pipe (respipe))
669#endif
670 croak ("unable to initialize result pipe");
671
672 if (old_readfd >= 0)
105 { 673 {
106 fd_set rfd; 674 if (dup2 (TO_SOCKET (respipe [0]), TO_SOCKET (old_readfd)) < 0)
107 FD_ZERO(&rfd); 675 croak ("unable to initialize result pipe(2)");
108 FD_SET(respipe [0], &rfd);
109 676
110 select (respipe [0] + 1, &rfd, 0, 0, 0); 677 respipe_close (respipe [0]);
678 respipe [0] = old_readfd;
111 } 679 }
112}
113 680
114static int 681#ifdef _WIN32
115poll_cb () 682 int arg = 1;
683 if (ioctlsocket (TO_SOCKET (respipe [0]), FIONBIO, &arg)
684 || ioctlsocket (TO_SOCKET (respipe [1]), FIONBIO, &arg))
685#else
686 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)
687 || fcntl (respipe [1], F_SETFL, O_NONBLOCK))
688#endif
689 croak ("unable to initialize result pipe(3)");
690
691 respipe_osf [0] = TO_SOCKET (respipe [0]);
692 respipe_osf [1] = TO_SOCKET (respipe [1]);
693}
694
695X_THREAD_PROC (aio_proc);
696
697static void start_thread (void)
698{
699 worker *wrk = calloc (1, sizeof (worker));
700
701 if (!wrk)
702 croak ("unable to allocate worker thread data");
703
704 X_LOCK (wrklock);
705
706 if (thread_create (&wrk->tid, aio_proc, (void *)wrk))
707 {
708 wrk->prev = &wrk_first;
709 wrk->next = wrk_first.next;
710 wrk_first.next->prev = wrk;
711 wrk_first.next = wrk;
712 ++started;
713 }
714 else
715 free (wrk);
716
717 X_UNLOCK (wrklock);
718}
719
720static void maybe_start_thread ()
721{
722 if (get_nthreads () >= wanted)
723 return;
724
725 /* todo: maybe use idle here, but might be less exact */
726 if (0 <= (int)get_nthreads () + (int)get_npending () - (int)nreqs)
727 return;
728
729 start_thread ();
730}
731
732static void req_send (aio_req req)
733{
734 block_sig ();
735
736 ++nreqs;
737
738 X_LOCK (reqlock);
739 ++nready;
740 reqq_push (&req_queue, req);
741 X_COND_SIGNAL (reqwait);
742 X_UNLOCK (reqlock);
743
744 unblock_sig ();
745
746 maybe_start_thread ();
747}
748
749static void end_thread (void)
750{
751 aio_req req;
752
753 Newz (0, req, 1, aio_cb);
754
755 req->type = REQ_QUIT;
756 req->pri = PRI_MAX + PRI_BIAS;
757
758 X_LOCK (reqlock);
759 reqq_push (&req_queue, req);
760 X_COND_SIGNAL (reqwait);
761 X_UNLOCK (reqlock);
762
763 X_LOCK (wrklock);
764 --started;
765 X_UNLOCK (wrklock);
766}
767
768static void set_max_idle (int nthreads)
769{
770 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
771 max_idle = nthreads <= 0 ? 1 : nthreads;
772 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
773}
774
775static void min_parallel (int nthreads)
776{
777 if (wanted < nthreads)
778 wanted = nthreads;
779}
780
781static void max_parallel (int nthreads)
782{
783 if (wanted > nthreads)
784 wanted = nthreads;
785
786 while (started > wanted)
787 end_thread ();
788}
789
790static void poll_wait ()
791{
792 fd_set rfd;
793
794 while (nreqs)
795 {
796 int size;
797 if (WORDACCESS_UNSAFE) X_LOCK (reslock);
798 size = res_queue.size;
799 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock);
800
801 if (size)
802 return;
803
804 maybe_start_thread ();
805
806 FD_ZERO (&rfd);
807 FD_SET (respipe [0], &rfd);
808
809 PerlSock_select (respipe [0] + 1, &rfd, 0, 0, 0);
810 }
811}
812
813static int poll_cb ()
116{ 814{
117 dSP; 815 dSP;
118 int count = 0; 816 int count = 0;
817 int maxreqs = max_poll_reqs;
119 int do_croak = 0; 818 int do_croak = 0;
819 struct timeval tv_start, tv_now;
120 aio_req req; 820 aio_req req;
821
822 if (max_poll_time)
823 gettimeofday (&tv_start, 0);
824
825 block_sig ();
121 826
122 for (;;) 827 for (;;)
123 { 828 {
124 pthread_mutex_lock (&reslock); 829 for (;;)
125 req = ress;
126
127 if (req)
128 { 830 {
129 ress = req->next; 831 maybe_start_thread ();
130 832
833 X_LOCK (reslock);
834 req = reqq_shift (&res_queue);
835
131 if (!ress) 836 if (req)
132 { 837 {
838 --npending;
839
840 if (!res_queue.size)
841 {
133 /* read any signals sent by the worker threads */ 842 /* read any signals sent by the worker threads */
134 char buf [32]; 843 char buf [4];
135 while (read (respipe [0], buf, 32) == 32) 844 while (respipe_read (respipe [0], buf, 4) == 4)
845 ;
136 ; 846 }
847 }
137 848
849 X_UNLOCK (reslock);
850
851 if (!req)
852 break;
853
854 --nreqs;
855
856 if (req->type == REQ_GROUP && req->size)
857 {
858 req->int1 = 1; /* mark request as delayed */
859 continue;
860 }
861 else
862 {
863 if (!req_invoke (req))
864 {
865 req_destroy (req);
866 unblock_sig ();
867 croak (0);
868 }
869
870 count++;
871 }
872
873 req_destroy (req);
874
875 if (maxreqs && !--maxreqs)
876 break;
877
878 if (max_poll_time)
879 {
880 gettimeofday (&tv_now, 0);
881
882 if (tvdiff (&tv_start, &tv_now) >= max_poll_time)
138 rese = 0; 883 break;
139 } 884 }
140 } 885 }
141 886
142 pthread_mutex_unlock (&reslock); 887 if (nreqs <= max_outstanding)
143
144 if (!req)
145 break; 888 break;
146 889
147 nreqs--; 890 poll_wait ();
148 891
149 if (req->type == REQ_QUIT) 892 ++maxreqs;
150 started--;
151 else
152 {
153 int errorno = errno;
154 errno = req->errorno;
155
156 if (req->type == REQ_READ)
157 SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0));
158
159 if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE))
160 SvREADONLY_off (req->data);
161
162 if (req->statdata)
163 {
164 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
165 PL_laststatval = req->result;
166 PL_statcache = *(req->statdata);
167 }
168
169 ENTER;
170 PUSHMARK (SP);
171 XPUSHs (sv_2mortal (newSViv (req->result)));
172
173 if (req->type == REQ_OPEN)
174 {
175 /* convert fd to fh */
176 SV *fh;
177
178 PUTBACK;
179 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL);
180 SPAGAIN;
181
182 fh = SvREFCNT_inc (POPs);
183
184 PUSHMARK (SP);
185 XPUSHs (sv_2mortal (fh));
186 }
187
188 if (SvOK (req->callback))
189 {
190 PUTBACK;
191 call_sv (req->callback, G_VOID | G_EVAL);
192 SPAGAIN;
193
194 if (SvTRUE (ERRSV))
195 {
196 free_req (req);
197 croak (0);
198 }
199 }
200
201 LEAVE;
202
203 errno = errorno;
204 count++;
205 }
206
207 free_req (req);
208 } 893 }
209 894
895 unblock_sig ();
210 return count; 896 return count;
211}
212
213static void *aio_proc(void *arg);
214
215static void
216start_thread (void)
217{
218 sigset_t fullsigset, oldsigset;
219 pthread_t tid;
220 pthread_attr_t attr;
221
222 pthread_attr_init (&attr);
223 pthread_attr_setstacksize (&attr, STACKSIZE);
224 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
225
226 sigfillset (&fullsigset);
227 sigprocmask (SIG_SETMASK, &fullsigset, &oldsigset);
228
229 if (pthread_create (&tid, &attr, aio_proc, 0) == 0)
230 started++;
231
232 sigprocmask (SIG_SETMASK, &oldsigset, 0);
233}
234
235static void
236send_req (aio_req req)
237{
238 while (started < wanted && nreqs >= started)
239 start_thread ();
240
241 nreqs++;
242
243 pthread_mutex_lock (&reqlock);
244
245 req->next = 0;
246
247 if (reqe)
248 {
249 reqe->next = req;
250 reqe = req;
251 }
252 else
253 reqe = reqs = req;
254
255 pthread_cond_signal (&reqwait);
256 pthread_mutex_unlock (&reqlock);
257
258 if (nreqs > max_outstanding)
259 for (;;)
260 {
261 poll_cb ();
262
263 if (nreqs <= max_outstanding)
264 break;
265
266 poll_wait ();
267 }
268}
269
270static void
271end_thread (void)
272{
273 aio_req req;
274 Newz (0, req, 1, aio_cb);
275 req->type = REQ_QUIT;
276
277 send_req (req);
278}
279
280static void min_parallel (int nthreads)
281{
282 if (wanted < nthreads)
283 wanted = nthreads;
284}
285
286static void max_parallel (int nthreads)
287{
288 int cur = started;
289
290 if (wanted > nthreads)
291 wanted = nthreads;
292
293 while (cur > wanted)
294 {
295 end_thread ();
296 cur--;
297 }
298
299 while (started > wanted)
300 {
301 poll_wait ();
302 poll_cb ();
303 }
304}
305
306static void create_pipe ()
307{
308 if (pipe (respipe))
309 croak ("unable to initialize result pipe");
310
311 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK))
312 croak ("cannot set result pipe to nonblocking mode");
313
314 if (fcntl (respipe [1], F_SETFL, O_NONBLOCK))
315 croak ("cannot set result pipe to nonblocking mode");
316}
317
318static void atfork_prepare (void)
319{
320 pthread_mutex_lock (&reqlock);
321 pthread_mutex_lock (&reslock);
322}
323
324static void atfork_parent (void)
325{
326 pthread_mutex_unlock (&reslock);
327 pthread_mutex_unlock (&reqlock);
328}
329
330static void atfork_child (void)
331{
332 aio_req prv;
333
334 started = 0;
335
336 while (reqs)
337 {
338 prv = reqs;
339 reqs = prv->next;
340 free_req (prv);
341 }
342
343 reqs = reqe = 0;
344
345 while (ress)
346 {
347 prv = ress;
348 ress = prv->next;
349 free_req (prv);
350 }
351
352 ress = rese = 0;
353
354 close (respipe [0]);
355 close (respipe [1]);
356 create_pipe ();
357
358 atfork_parent ();
359} 897}
360 898
361/*****************************************************************************/ 899/*****************************************************************************/
362/* work around various missing functions */ 900/* work around various missing functions */
363 901
368/* 906/*
369 * make our pread/pwrite safe against themselves, but not against 907 * make our pread/pwrite safe against themselves, but not against
370 * normal read/write by using a mutex. slows down execution a lot, 908 * normal read/write by using a mutex. slows down execution a lot,
371 * but that's your problem, not mine. 909 * but that's your problem, not mine.
372 */ 910 */
373static pthread_mutex_t iolock = PTHREAD_MUTEX_INITIALIZER; 911static mutex_t preadwritelock = X_MUTEX_INIT;
374 912
375static ssize_t 913static ssize_t pread (int fd, void *buf, size_t count, off_t offset)
376pread (int fd, void *buf, size_t count, off_t offset)
377{ 914{
378 ssize_t res; 915 ssize_t res;
379 off_t ooffset; 916 off_t ooffset;
380 917
381 pthread_mutex_lock (&iolock); 918 X_LOCK (preadwritelock);
382 ooffset = lseek (fd, 0, SEEK_CUR); 919 ooffset = lseek (fd, 0, SEEK_CUR);
383 lseek (fd, offset, SEEK_SET); 920 lseek (fd, offset, SEEK_SET);
384 res = read (fd, buf, count); 921 res = read (fd, buf, count);
385 lseek (fd, ooffset, SEEK_SET); 922 lseek (fd, ooffset, SEEK_SET);
386 pthread_mutex_unlock (&iolock); 923 X_UNLOCK (preadwritelock);
387 924
388 return res; 925 return res;
389} 926}
390 927
391static ssize_t
392pwrite (int fd, void *buf, size_t count, off_t offset) 928static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset)
393{ 929{
394 ssize_t res; 930 ssize_t res;
395 off_t ooffset; 931 off_t ooffset;
396 932
397 pthread_mutex_lock (&iolock); 933 X_LOCK (preadwritelock);
398 ooffset = lseek (fd, 0, SEEK_CUR); 934 ooffset = lseek (fd, 0, SEEK_CUR);
399 lseek (fd, offset, SEEK_SET); 935 lseek (fd, offset, SEEK_SET);
400 res = write (fd, buf, count); 936 res = write (fd, buf, count);
401 lseek (fd, offset, SEEK_SET); 937 lseek (fd, offset, SEEK_SET);
402 pthread_mutex_unlock (&iolock); 938 X_UNLOCK (preadwritelock);
403 939
404 return res; 940 return res;
405} 941}
942#endif
943
944#ifndef HAVE_FUTIMES
945
946# define utimes(path,times) aio_utimes (path, times)
947# define futimes(fd,times) aio_futimes (fd, times)
948
949int aio_utimes (const char *filename, const struct timeval times[2])
950{
951 if (times)
952 {
953 struct utimbuf buf;
954
955 buf.actime = times[0].tv_sec;
956 buf.modtime = times[1].tv_sec;
957
958 return utime (filename, &buf);
959 }
960 else
961 return utime (filename, 0);
962}
963
964int aio_futimes (int fd, const struct timeval tv[2])
965{
966 errno = ENOSYS;
967 return -1;
968}
969
406#endif 970#endif
407 971
408#if !HAVE_FDATASYNC 972#if !HAVE_FDATASYNC
409# define fdatasync fsync 973# define fdatasync fsync
410#endif 974#endif
411 975
412#if !HAVE_READAHEAD 976#if !HAVE_READAHEAD
413# define readahead aio_readahead 977# define readahead(fd,offset,count) aio_readahead (fd, offset, count, self)
414 978
415static char readahead_buf[4096]; 979static ssize_t aio_readahead (int fd, off_t offset, size_t count, worker *self)
416
417static ssize_t
418readahead (int fd, off_t offset, size_t count)
419{ 980{
981 size_t todo = count;
982 dBUF;
983
420 while (count > 0) 984 while (todo > 0)
421 { 985 {
422 size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); 986 size_t len = todo < AIO_BUFSIZE ? todo : AIO_BUFSIZE;
423 987
424 pread (fd, readahead_buf, len, offset); 988 pread (fd, aio_buf, len, offset);
425 offset += len; 989 offset += len;
426 count -= len; 990 todo -= len;
427 } 991 }
428 992
429 errno = 0; 993 errno = 0;
994 return count;
430} 995}
996
431#endif 997#endif
432 998
999#if !HAVE_READDIR_R
1000# define readdir_r aio_readdir_r
1001
1002static mutex_t readdirlock = X_MUTEX_INIT;
1003
1004static int readdir_r (DIR *dirp, X_DIRENT *ent, X_DIRENT **res)
1005{
1006 X_DIRENT *e;
1007 int errorno;
1008
1009 X_LOCK (readdirlock);
1010
1011 e = readdir (dirp);
1012 errorno = errno;
1013
1014 if (e)
1015 {
1016 *res = ent;
1017 strcpy (ent->d_name, e->d_name);
1018 }
1019 else
1020 *res = 0;
1021
1022 X_UNLOCK (readdirlock);
1023
1024 errno = errorno;
1025 return e ? 0 : -1;
1026}
1027#endif
1028
433/* sendfile always needs emulation */ 1029/* sendfile always needs emulation */
434static ssize_t
435sendfile_ (int ofd, int ifd, off_t offset, size_t count) 1030static ssize_t sendfile_ (int ofd, int ifd, off_t offset, size_t count, worker *self)
436{ 1031{
437 ssize_t res; 1032 ssize_t res;
438 1033
439 if (!count) 1034 if (!count)
440 return 0; 1035 return 0;
441 1036
1037#if HAVE_SENDFILE
442#if __linux 1038# if __linux
443 res = sendfile (ofd, ifd, &offset, count); 1039 res = sendfile (ofd, ifd, &offset, count);
444 1040
445#elif __freebsd 1041# elif __freebsd
446 /* 1042 /*
447 * Of course, the freebsd sendfile is a dire hack with no thoughts 1043 * Of course, the freebsd sendfile is a dire hack with no thoughts
448 * wasted on making it similar to other i/o functions. 1044 * wasted on making it similar to other I/O functions.
449 */ 1045 */
450 { 1046 {
451 off_t sbytes; 1047 off_t sbytes;
452 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); 1048 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0);
453 1049
454 if (!res && errno == EAGAIN) 1050 if (res < 0 && sbytes)
455 /* maybe on others, too, as usual, the manpage leaves you guessing */ 1051 /* maybe only on EAGAIN: as usual, the manpage leaves you guessing */
456 res = sbytes; 1052 res = sbytes;
457 } 1053 }
458 1054
459#elif __hpux 1055# elif __hpux
460 res = sendfile (ofd, ifd, offset, count, 0, 0); 1056 res = sendfile (ofd, ifd, offset, count, 0, 0);
461 1057
1058# elif __solaris
1059 {
1060 struct sendfilevec vec;
1061 size_t sbytes;
1062
1063 vec.sfv_fd = ifd;
1064 vec.sfv_flag = 0;
1065 vec.sfv_off = offset;
1066 vec.sfv_len = count;
1067
1068 res = sendfilev (ofd, &vec, 1, &sbytes);
1069
1070 if (res < 0 && sbytes)
1071 res = sbytes;
1072 }
1073
1074# endif
462#else 1075#else
463 res = -1; 1076 res = -1;
464 errno = ENOSYS; 1077 errno = ENOSYS;
465#endif 1078#endif
466 1079
1080 if (res < 0
467 if (res < 0 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK)) 1081 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK
1082#if __solaris
1083 || errno == EAFNOSUPPORT || errno == EPROTOTYPE
1084#endif
1085 )
1086 )
468 { 1087 {
469 /* emulate sendfile. this is a major pain in the ass */ 1088 /* emulate sendfile. this is a major pain in the ass */
470 char *buf = malloc (4096); 1089 dBUF;
1090
471 res = 0; 1091 res = 0;
472 1092
473 for (;;) 1093 while (count)
474 { 1094 {
475 ssize_t cnt; 1095 ssize_t cnt;
476 1096
477 cnt = pread (ifd, buf, 4096, offset); 1097 cnt = pread (ifd, aio_buf, count > AIO_BUFSIZE ? AIO_BUFSIZE : count, offset);
478 1098
479 if (cnt <= 0) 1099 if (cnt <= 0)
480 { 1100 {
481 if (cnt && !res) res = -1; 1101 if (cnt && !res) res = -1;
482 break; 1102 break;
483 } 1103 }
484 1104
485 cnt = write (ofd, buf, cnt); 1105 cnt = write (ofd, aio_buf, cnt);
486 1106
487 if (cnt <= 0) 1107 if (cnt <= 0)
488 { 1108 {
489 if (cnt && !res) res = -1; 1109 if (cnt && !res) res = -1;
490 break; 1110 break;
491 } 1111 }
492 1112
493 offset += cnt; 1113 offset += cnt;
494 res += cnt; 1114 res += cnt;
1115 count -= cnt;
495 } 1116 }
496
497 {
498 int errorno = errno;
499 free (buf);
500 errno = errorno;
501 }
502 } 1117 }
503 1118
504 return res; 1119 return res;
1120}
1121
1122/* read a full directory */
1123static void scandir_ (aio_req req, worker *self)
1124{
1125 DIR *dirp;
1126 union
1127 {
1128 X_DIRENT d;
1129 char b [offsetof (X_DIRENT, d_name) + NAME_MAX + 1];
1130 } *u;
1131 X_DIRENT *entp;
1132 char *name, *names;
1133 int memlen = 4096;
1134 int memofs = 0;
1135 int res = 0;
1136
1137 X_LOCK (wrklock);
1138 self->dirp = dirp = opendir (req->ptr1);
1139 self->dbuf = u = malloc (sizeof (*u));
1140 req->flags |= FLAG_PTR2_FREE;
1141 req->ptr2 = names = malloc (memlen);
1142 X_UNLOCK (wrklock);
1143
1144 if (dirp && u && names)
1145 for (;;)
1146 {
1147 errno = 0;
1148 readdir_r (dirp, &u->d, &entp);
1149
1150 if (!entp)
1151 break;
1152
1153 name = entp->d_name;
1154
1155 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2])))
1156 {
1157 int len = strlen (name) + 1;
1158
1159 res++;
1160
1161 while (memofs + len > memlen)
1162 {
1163 memlen *= 2;
1164 X_LOCK (wrklock);
1165 req->ptr2 = names = realloc (names, memlen);
1166 X_UNLOCK (wrklock);
1167
1168 if (!names)
1169 break;
1170 }
1171
1172 memcpy (names + memofs, name, len);
1173 memofs += len;
1174 }
1175 }
1176
1177 if (errno)
1178 res = -1;
1179
1180 req->result = res;
505} 1181}
506 1182
507/*****************************************************************************/ 1183/*****************************************************************************/
508 1184
509static void * 1185X_THREAD_PROC (aio_proc)
510aio_proc (void *thr_arg)
511{ 1186{
1187 {//D
512 aio_req req; 1188 aio_req req;
513 int type; 1189 struct timespec ts;
1190 worker *self = (worker *)thr_arg;
514 1191
515 do 1192 /* try to distribute timeouts somewhat randomly */
1193 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL);
1194
1195 for (;;)
516 { 1196 {
517 pthread_mutex_lock (&reqlock); 1197 ts.tv_sec = time (0) + IDLE_TIMEOUT;
1198
1199 X_LOCK (reqlock);
518 1200
519 for (;;) 1201 for (;;)
520 { 1202 {
521 req = reqs; 1203 self->req = req = reqq_shift (&req_queue);
522
523 if (reqs)
524 {
525 reqs = reqs->next;
526 if (!reqs) reqe = 0;
527 }
528 1204
529 if (req) 1205 if (req)
530 break; 1206 break;
531 1207
532 pthread_cond_wait (&reqwait, &reqlock); 1208 ++idle;
1209
1210 if (X_COND_TIMEDWAIT (reqwait, reqlock, ts)
1211 == ETIMEDOUT)
1212 {
1213 if (idle > max_idle)
1214 {
1215 --idle;
1216 X_UNLOCK (reqlock);
1217 X_LOCK (wrklock);
1218 --started;
1219 X_UNLOCK (wrklock);
1220 goto quit;
1221 }
1222
1223 /* we are allowed to idle, so do so without any timeout */
1224 X_COND_WAIT (reqwait, reqlock);
1225 ts.tv_sec = time (0) + IDLE_TIMEOUT;
1226 }
1227
1228 --idle;
533 } 1229 }
534 1230
535 pthread_mutex_unlock (&reqlock); 1231 --nready;
1232
1233 X_UNLOCK (reqlock);
536 1234
537 errno = 0; /* strictly unnecessary */ 1235 errno = 0; /* strictly unnecessary */
538 1236
539 type = req->type; 1237 if (!(req->flags & FLAG_CANCELLED))
540
541 switch (type) 1238 switch (req->type)
1239 {
1240 case REQ_READ: req->result = req->offs >= 0
1241 ? pread (req->int1, req->ptr1, req->size, req->offs)
1242 : read (req->int1, req->ptr1, req->size); break;
1243 case REQ_WRITE: req->result = req->offs >= 0
1244 ? pwrite (req->int1, req->ptr1, req->size, req->offs)
1245 : write (req->int1, req->ptr1, req->size); break;
1246
1247 case REQ_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break;
1248 case REQ_SENDFILE: req->result = sendfile_ (req->int1, req->int2, req->offs, req->size, self); break;
1249
1250 case REQ_STAT: req->result = stat (req->ptr1, (Stat_t *)req->ptr2); break;
1251 case REQ_LSTAT: req->result = lstat (req->ptr1, (Stat_t *)req->ptr2); break;
1252 case REQ_FSTAT: req->result = fstat (req->int1, (Stat_t *)req->ptr2); break;
1253
1254 case REQ_CHOWN: req->result = chown (req->ptr1, req->int2, req->int3); break;
1255 case REQ_FCHOWN: req->result = fchown (req->int1, req->int2, req->int3); break;
1256 case REQ_CHMOD: req->result = chmod (req->ptr1, req->mode); break;
1257 case REQ_FCHMOD: req->result = fchmod (req->int1, req->mode); break;
1258 case REQ_TRUNCATE: req->result = truncate (req->ptr1, req->offs); break;
1259 case REQ_FTRUNCATE: req->result = ftruncate (req->int1, req->offs); break;
1260
1261 case REQ_OPEN: req->result = open (req->ptr1, req->int1, req->mode); break;
1262 case REQ_CLOSE: req->result = close (req->int1); break;
1263 case REQ_UNLINK: req->result = unlink (req->ptr1); break;
1264 case REQ_RMDIR: req->result = rmdir (req->ptr1); break;
1265 case REQ_MKDIR: req->result = mkdir (req->ptr1, req->mode); break;
1266 case REQ_RENAME: req->result = rename (req->ptr2, req->ptr1); break;
1267 case REQ_LINK: req->result = link (req->ptr2, req->ptr1); break;
1268 case REQ_SYMLINK: req->result = symlink (req->ptr2, req->ptr1); break;
1269 case REQ_MKNOD: req->result = mknod (req->ptr2, req->mode, (dev_t)req->offs); break;
1270 case REQ_READLINK: req->result = readlink (req->ptr2, req->ptr1, NAME_MAX); break;
1271
1272 case REQ_SYNC: req->result = 0; sync (); break;
1273 case REQ_FSYNC: req->result = fsync (req->int1); break;
1274 case REQ_FDATASYNC: req->result = fdatasync (req->int1); break;
1275
1276 case REQ_READDIR: scandir_ (req, self); break;
1277
1278 case REQ_BUSY:
1279#ifdef _WIN32
1280 Sleep (req->nv1 * 1000.);
1281#else
1282 {
1283 struct timeval tv;
1284
1285 tv.tv_sec = req->nv1;
1286 tv.tv_usec = (req->nv1 - tv.tv_sec) * 1000000.;
1287
1288 req->result = select (0, 0, 0, 0, &tv);
1289 }
1290#endif
1291 break;
1292
1293 case REQ_UTIME:
1294 case REQ_FUTIME:
1295 {
1296 struct timeval tv[2];
1297 struct timeval *times;
1298
1299 if (req->nv1 != -1. || req->nv2 != -1.)
1300 {
1301 tv[0].tv_sec = req->nv1;
1302 tv[0].tv_usec = (req->nv1 - tv[0].tv_sec) * 1000000.;
1303 tv[1].tv_sec = req->nv2;
1304 tv[1].tv_usec = (req->nv2 - tv[1].tv_sec) * 1000000.;
1305
1306 times = tv;
1307 }
1308 else
1309 times = 0;
1310
1311
1312 req->result = req->type == REQ_FUTIME
1313 ? futimes (req->int1, times)
1314 : utimes (req->ptr1, times);
1315 }
1316
1317 case REQ_GROUP:
1318 case REQ_NOP:
1319 break;
1320
1321 case REQ_QUIT:
1322 goto quit;
1323
1324 default:
1325 req->result = -1;
1326 break;
1327 }
1328
1329 req->errorno = errno;
1330
1331 X_LOCK (reslock);
1332
1333 ++npending;
1334
1335 if (!reqq_push (&res_queue, req))
542 { 1336 {
543 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; 1337 /* write a dummy byte to the pipe so fh becomes ready */
544 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; 1338 respipe_write (respipe_osf [1], (const void *)&respipe_osf, 1);
545 1339
546 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; 1340 /* optionally signal the main thread asynchronously */
547 case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length); break; 1341 if (main_sig)
548 1342 pthread_kill (main_tid, main_sig);
549 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break;
550 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break;
551 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break;
552
553 case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break;
554 case REQ_CLOSE: req->result = close (req->fd); break;
555 case REQ_UNLINK: req->result = unlink (req->dataptr); break;
556 case REQ_RMDIR: req->result = rmdir (req->dataptr); break;
557 case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break;
558
559 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break;
560 case REQ_FSYNC: req->result = fsync (req->fd); break;
561
562 case REQ_QUIT:
563 break;
564
565 default:
566 req->result = ENOSYS;
567 break;
568 } 1343 }
569 1344
570 req->errorno = errno; 1345 self->req = 0;
1346 worker_clear (self);
571 1347
572 pthread_mutex_lock (&reslock); 1348 X_UNLOCK (reslock);
573
574 req->next = 0;
575
576 if (rese)
577 {
578 rese->next = req;
579 rese = req;
580 }
581 else
582 {
583 rese = ress = req;
584
585 /* write a dummy byte to the pipe so fh becomes ready */
586 write (respipe [1], &respipe, 1);
587 }
588
589 pthread_mutex_unlock (&reslock);
590 } 1349 }
591 while (type != REQ_QUIT); 1350
1351quit:
1352 X_LOCK (wrklock);
1353 worker_free (self);
1354 X_UNLOCK (wrklock);
592 1355
593 return 0; 1356 return 0;
1357 }//D
1358}
1359
1360/*****************************************************************************/
1361
1362static void atfork_prepare (void)
1363{
1364 X_LOCK (wrklock);
1365 X_LOCK (reqlock);
1366 X_LOCK (reslock);
1367#if !HAVE_PREADWRITE
1368 X_LOCK (preadwritelock);
1369#endif
1370#if !HAVE_READDIR_R
1371 X_LOCK (readdirlock);
1372#endif
1373}
1374
1375static void atfork_parent (void)
1376{
1377#if !HAVE_READDIR_R
1378 X_UNLOCK (readdirlock);
1379#endif
1380#if !HAVE_PREADWRITE
1381 X_UNLOCK (preadwritelock);
1382#endif
1383 X_UNLOCK (reslock);
1384 X_UNLOCK (reqlock);
1385 X_UNLOCK (wrklock);
1386}
1387
1388static void atfork_child (void)
1389{
1390 aio_req prv;
1391
1392 while (prv = reqq_shift (&req_queue))
1393 req_destroy (prv);
1394
1395 while (prv = reqq_shift (&res_queue))
1396 req_destroy (prv);
1397
1398 while (wrk_first.next != &wrk_first)
1399 {
1400 worker *wrk = wrk_first.next;
1401
1402 if (wrk->req)
1403 req_destroy (wrk->req);
1404
1405 worker_clear (wrk);
1406 worker_free (wrk);
1407 }
1408
1409 started = 0;
1410 idle = 0;
1411 nreqs = 0;
1412 nready = 0;
1413 npending = 0;
1414
1415 create_respipe ();
1416
1417 atfork_parent ();
594} 1418}
595 1419
596#define dREQ \ 1420#define dREQ \
597 aio_req req; \ 1421 aio_req req; \
1422 int req_pri = next_pri; \
1423 next_pri = DEFAULT_PRI + PRI_BIAS; \
598 \ 1424 \
599 if (SvOK (callback) && !SvROK (callback)) \ 1425 if (SvOK (callback) && !SvROK (callback)) \
600 croak ("clalback must be undef or of reference type"); \ 1426 croak ("callback must be undef or of reference type"); \
601 \ 1427 \
602 Newz (0, req, 1, aio_cb); \ 1428 Newz (0, req, 1, aio_cb); \
603 if (!req) \ 1429 if (!req) \
604 croak ("out of memory during aio_req allocation"); \ 1430 croak ("out of memory during aio_req allocation"); \
605 \ 1431 \
606 req->callback = newSVsv (callback); 1432 req->callback = newSVsv (callback); \
1433 req->pri = req_pri
1434
1435#define REQ_SEND \
1436 req_send (req); \
1437 \
1438 if (GIMME_V != G_VOID) \
1439 XPUSHs (req_sv (req, AIO_REQ_KLASS));
607 1440
608MODULE = IO::AIO PACKAGE = IO::AIO 1441MODULE = IO::AIO PACKAGE = IO::AIO
609 1442
610PROTOTYPES: ENABLE 1443PROTOTYPES: ENABLE
611 1444
612BOOT: 1445BOOT:
613{ 1446{
614 create_pipe (); 1447 stash = gv_stashpv ("IO::AIO", 1);
1448
1449 newCONSTSUB (stash, "EXDEV", newSViv (EXDEV));
1450 newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY));
1451 newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY));
1452 newCONSTSUB (stash, "O_CREAT", newSViv (O_CREAT));
1453 newCONSTSUB (stash, "O_TRUNC", newSViv (O_TRUNC));
1454#ifdef _WIN32
1455 X_MUTEX_CHECK (wrklock);
1456 X_MUTEX_CHECK (reslock);
1457 X_MUTEX_CHECK (reqlock);
1458 X_MUTEX_CHECK (reqwait);
1459 X_MUTEX_CHECK (preadwritelock);
1460 X_MUTEX_CHECK (readdirlock);
1461
1462 X_COND_CHECK (reqwait);
1463#else
1464 newCONSTSUB (stash, "S_IFIFO", newSViv (S_IFIFO));
1465 newCONSTSUB (stash, "SIGIO", newSViv (SIGIO));
1466#endif
1467
1468 create_respipe ();
1469
615 pthread_atfork (atfork_prepare, atfork_parent, atfork_child); 1470 X_THREAD_ATFORK (atfork_prepare, atfork_parent, atfork_child);
616} 1471}
617 1472
618void 1473void
619min_parallel(nthreads) 1474max_poll_reqs (int nreqs)
620 int nthreads
621 PROTOTYPE: $ 1475 PROTOTYPE: $
1476 CODE:
1477 max_poll_reqs = nreqs;
622 1478
623void 1479void
624max_parallel(nthreads) 1480max_poll_time (double nseconds)
625 int nthreads
626 PROTOTYPE: $ 1481 PROTOTYPE: $
1482 CODE:
1483 max_poll_time = nseconds * AIO_TICKS;
1484
1485void
1486min_parallel (int nthreads)
1487 PROTOTYPE: $
1488
1489void
1490max_parallel (int nthreads)
1491 PROTOTYPE: $
1492
1493void
1494max_idle (int nthreads)
1495 PROTOTYPE: $
1496 CODE:
1497 set_max_idle (nthreads);
627 1498
628int 1499int
629max_outstanding(nreqs) 1500max_outstanding (int maxreqs)
630 int nreqs 1501 PROTOTYPE: $
631 PROTOTYPE: $
632 CODE: 1502 CODE:
633 RETVAL = max_outstanding; 1503 RETVAL = max_outstanding;
634 max_outstanding = nreqs; 1504 max_outstanding = maxreqs;
1505 OUTPUT:
1506 RETVAL
635 1507
636void 1508void
637aio_open(pathname,flags,mode,callback=&PL_sv_undef) 1509aio_open (SV8 *pathname, int flags, int mode, SV *callback=&PL_sv_undef)
638 SV * pathname
639 int flags
640 int mode
641 SV * callback
642 PROTOTYPE: $$$;$ 1510 PROTOTYPE: $$$;$
643 CODE: 1511 PPCODE:
644{ 1512{
645 dREQ; 1513 dREQ;
646 1514
647 req->type = REQ_OPEN; 1515 req->type = REQ_OPEN;
648 req->data = newSVsv (pathname); 1516 req->sv1 = newSVsv (pathname);
649 req->dataptr = SvPVbyte_nolen (req->data); 1517 req->ptr1 = SvPVbyte_nolen (req->sv1);
650 req->fd = flags; 1518 req->int1 = flags;
651 req->mode = mode; 1519 req->mode = mode;
652 1520
653 send_req (req); 1521 REQ_SEND;
654} 1522}
655 1523
656void 1524void
657aio_close(fh,callback=&PL_sv_undef) 1525aio_fsync (SV *fh, SV *callback=&PL_sv_undef)
658 SV * fh
659 SV * callback
660 PROTOTYPE: $;$ 1526 PROTOTYPE: $;$
661 ALIAS: 1527 ALIAS:
662 aio_close = REQ_CLOSE
663 aio_fsync = REQ_FSYNC 1528 aio_fsync = REQ_FSYNC
664 aio_fdatasync = REQ_FDATASYNC 1529 aio_fdatasync = REQ_FDATASYNC
665 CODE: 1530 PPCODE:
666{ 1531{
667 dREQ; 1532 dREQ;
668 1533
669 req->type = ix; 1534 req->type = ix;
670 req->fh = newSVsv (fh); 1535 req->sv1 = newSVsv (fh);
671 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); 1536 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh)));
672 1537
673 send_req (req); 1538 REQ_SEND (req);
674} 1539}
675 1540
1541int
1542_dup (int fd)
1543 PROTOTYPE: $
1544 CODE:
1545 RETVAL = dup (fd);
1546 OUTPUT:
1547 RETVAL
1548
676void 1549void
1550_aio_close (int fd, SV *callback=&PL_sv_undef)
1551 PROTOTYPE: $;$
1552 PPCODE:
1553{
1554 dREQ;
1555
1556 req->type = REQ_CLOSE;
1557 req->int1 = fd;
1558
1559 REQ_SEND (req);
1560}
1561
1562void
677aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) 1563aio_read (SV *fh, SV *offset, SV *length, SV8 *data, IV dataoffset, SV *callback=&PL_sv_undef)
678 SV * fh
679 UV offset
680 UV length
681 SV * data
682 UV dataoffset
683 SV * callback
684 ALIAS: 1564 ALIAS:
685 aio_read = REQ_READ 1565 aio_read = REQ_READ
686 aio_write = REQ_WRITE 1566 aio_write = REQ_WRITE
687 PROTOTYPE: $$$$$;$ 1567 PROTOTYPE: $$$$$;$
688 CODE: 1568 PPCODE:
689{ 1569{
690 aio_req req;
691 STRLEN svlen; 1570 STRLEN svlen;
692 char *svptr = SvPVbyte (data, svlen); 1571 char *svptr = SvPVbyte (data, svlen);
1572 UV len = SvUV (length);
693 1573
694 SvUPGRADE (data, SVt_PV); 1574 SvUPGRADE (data, SVt_PV);
695 SvPOK_on (data); 1575 SvPOK_on (data);
696 1576
697 if (dataoffset < 0) 1577 if (dataoffset < 0)
698 dataoffset += svlen; 1578 dataoffset += svlen;
699 1579
700 if (dataoffset < 0 || dataoffset > svlen) 1580 if (dataoffset < 0 || dataoffset > svlen)
701 croak ("data offset outside of string"); 1581 croak ("dataoffset outside of data scalar");
702 1582
703 if (ix == REQ_WRITE) 1583 if (ix == REQ_WRITE)
704 { 1584 {
705 /* write: check length and adjust. */ 1585 /* write: check length and adjust. */
706 if (length < 0 || length + dataoffset > svlen) 1586 if (!SvOK (length) || len + dataoffset > svlen)
707 length = svlen - dataoffset; 1587 len = svlen - dataoffset;
708 } 1588 }
709 else 1589 else
710 { 1590 {
711 /* read: grow scalar as necessary */ 1591 /* read: grow scalar as necessary */
712 svptr = SvGROW (data, length + dataoffset); 1592 svptr = SvGROW (data, len + dataoffset + 1);
713 } 1593 }
714 1594
715 if (length < 0) 1595 if (len < 0)
716 croak ("length must not be negative"); 1596 croak ("length must not be negative");
717 1597
718 { 1598 {
719 dREQ; 1599 dREQ;
720 1600
721 req->type = ix; 1601 req->type = ix;
722 req->fh = newSVsv (fh); 1602 req->sv1 = newSVsv (fh);
723 req->fd = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh)) 1603 req->int1 = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh))
724 : IoOFP (sv_2io (fh))); 1604 : IoOFP (sv_2io (fh)));
1605 req->offs = SvOK (offset) ? SvVAL64 (offset) : -1;
725 req->offset = offset; 1606 req->size = len;
726 req->length = length;
727 req->data = SvREFCNT_inc (data); 1607 req->sv2 = SvREFCNT_inc (data);
728 req->dataptr = (char *)svptr + dataoffset; 1608 req->ptr1 = (char *)svptr + dataoffset;
1609 req->stroffset = dataoffset;
729 1610
730 if (!SvREADONLY (data)) 1611 if (!SvREADONLY (data))
731 { 1612 {
732 SvREADONLY_on (data); 1613 SvREADONLY_on (data);
733 req->data2ptr = (void *)data; 1614 req->flags |= FLAG_SV2_RO_OFF;
734 } 1615 }
735 1616
736 send_req (req); 1617 REQ_SEND;
737 } 1618 }
738} 1619}
739 1620
740void 1621void
1622aio_readlink (SV8 *path, SV *callback=&PL_sv_undef)
1623 PROTOTYPE: $$;$
1624 PPCODE:
1625{
1626 SV *data;
1627 dREQ;
1628
1629 data = newSV (NAME_MAX);
1630 SvPOK_on (data);
1631
1632 req->type = REQ_READLINK;
1633 req->sv1 = newSVsv (path);
1634 req->ptr2 = SvPVbyte_nolen (req->sv1);
1635 req->sv2 = data;
1636 req->ptr1 = SvPVbyte_nolen (data);
1637
1638 REQ_SEND;
1639}
1640
1641void
741aio_sendfile(out_fh,in_fh,in_offset,length,callback=&PL_sv_undef) 1642aio_sendfile (SV *out_fh, SV *in_fh, SV *in_offset, UV length, SV *callback=&PL_sv_undef)
742 SV * out_fh
743 SV * in_fh
744 UV in_offset
745 UV length
746 SV * callback
747 PROTOTYPE: $$$$;$ 1643 PROTOTYPE: $$$$;$
748 CODE: 1644 PPCODE:
749{ 1645{
750 dREQ; 1646 dREQ;
751 1647
752 req->type = REQ_SENDFILE; 1648 req->type = REQ_SENDFILE;
753 req->fh = newSVsv (out_fh); 1649 req->sv1 = newSVsv (out_fh);
754 req->fd = PerlIO_fileno (IoIFP (sv_2io (out_fh))); 1650 req->int1 = PerlIO_fileno (IoIFP (sv_2io (out_fh)));
755 req->fh2 = newSVsv (in_fh); 1651 req->sv2 = newSVsv (in_fh);
756 req->fd2 = PerlIO_fileno (IoIFP (sv_2io (in_fh))); 1652 req->int2 = PerlIO_fileno (IoIFP (sv_2io (in_fh)));
757 req->offset = in_offset; 1653 req->offs = SvVAL64 (in_offset);
758 req->length = length; 1654 req->size = length;
759 1655
760 send_req (req); 1656 REQ_SEND;
761} 1657}
762 1658
763void 1659void
764aio_readahead(fh,offset,length,callback=&PL_sv_undef) 1660aio_readahead (SV *fh, SV *offset, IV length, SV *callback=&PL_sv_undef)
765 SV * fh
766 UV offset
767 IV length
768 SV * callback
769 PROTOTYPE: $$$;$ 1661 PROTOTYPE: $$$;$
770 CODE: 1662 PPCODE:
771{ 1663{
772 dREQ; 1664 dREQ;
773 1665
774 req->type = REQ_READAHEAD; 1666 req->type = REQ_READAHEAD;
775 req->fh = newSVsv (fh); 1667 req->sv1 = newSVsv (fh);
776 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); 1668 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh)));
777 req->offset = offset; 1669 req->offs = SvVAL64 (offset);
778 req->length = length; 1670 req->size = length;
779 1671
780 send_req (req); 1672 REQ_SEND;
781} 1673}
782 1674
783void 1675void
784aio_stat(fh_or_path,callback=&PL_sv_undef) 1676aio_stat (SV8 *fh_or_path, SV *callback=&PL_sv_undef)
785 SV * fh_or_path
786 SV * callback
787 ALIAS: 1677 ALIAS:
788 aio_stat = REQ_STAT 1678 aio_stat = REQ_STAT
789 aio_lstat = REQ_LSTAT 1679 aio_lstat = REQ_LSTAT
790 CODE: 1680 PPCODE:
791{ 1681{
792 dREQ; 1682 dREQ;
793 1683
794 New (0, req->statdata, 1, Stat_t); 1684 req->ptr2 = malloc (sizeof (Stat_t));
795 if (!req->statdata) 1685 if (!req->ptr2)
796 { 1686 {
797 free_req (req); 1687 req_destroy (req);
798 croak ("out of memory during aio_req->statdata allocation"); 1688 croak ("out of memory during aio_stat statdata allocation");
799 } 1689 }
1690
1691 req->flags |= FLAG_PTR2_FREE;
1692 req->sv1 = newSVsv (fh_or_path);
800 1693
801 if (SvPOK (fh_or_path)) 1694 if (SvPOK (fh_or_path))
802 { 1695 {
803 req->type = ix; 1696 req->type = ix;
804 req->data = newSVsv (fh_or_path);
805 req->dataptr = SvPVbyte_nolen (req->data); 1697 req->ptr1 = SvPVbyte_nolen (req->sv1);
806 } 1698 }
807 else 1699 else
808 { 1700 {
809 req->type = REQ_FSTAT; 1701 req->type = REQ_FSTAT;
810 req->fh = newSVsv (fh_or_path);
811 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); 1702 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
812 } 1703 }
813 1704
814 send_req (req); 1705 REQ_SEND;
815} 1706}
816 1707
817void 1708void
1709aio_utime (SV8 *fh_or_path, SV *atime, SV *mtime, SV *callback=&PL_sv_undef)
1710 PPCODE:
1711{
1712 dREQ;
1713
1714 req->nv1 = SvOK (atime) ? SvNV (atime) : -1.;
1715 req->nv2 = SvOK (mtime) ? SvNV (mtime) : -1.;
1716 req->sv1 = newSVsv (fh_or_path);
1717
1718 if (SvPOK (fh_or_path))
1719 {
1720 req->type = REQ_UTIME;
1721 req->ptr1 = SvPVbyte_nolen (req->sv1);
1722 }
1723 else
1724 {
1725 req->type = REQ_FUTIME;
1726 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
1727 }
1728
1729 REQ_SEND;
1730}
1731
1732void
1733aio_truncate (SV8 *fh_or_path, SV *offset, SV *callback=&PL_sv_undef)
1734 PPCODE:
1735{
1736 dREQ;
1737
1738 req->sv1 = newSVsv (fh_or_path);
1739 req->offs = SvOK (offset) ? SvVAL64 (offset) : -1;
1740
1741 if (SvPOK (fh_or_path))
1742 {
1743 req->type = REQ_TRUNCATE;
1744 req->ptr1 = SvPVbyte_nolen (req->sv1);
1745 }
1746 else
1747 {
1748 req->type = REQ_FTRUNCATE;
1749 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
1750 }
1751
1752 REQ_SEND;
1753}
1754
1755void
1756aio_chmod (SV8 *fh_or_path, int mode, SV *callback=&PL_sv_undef)
1757 PPCODE:
1758{
1759 dREQ;
1760
1761 req->mode = mode;
1762 req->sv1 = newSVsv (fh_or_path);
1763
1764 if (SvPOK (fh_or_path))
1765 {
1766 req->type = REQ_CHMOD;
1767 req->ptr1 = SvPVbyte_nolen (req->sv1);
1768 }
1769 else
1770 {
1771 req->type = REQ_FCHMOD;
1772 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
1773 }
1774
1775 REQ_SEND;
1776}
1777
1778void
1779aio_chown (SV8 *fh_or_path, SV *uid, SV *gid, SV *callback=&PL_sv_undef)
1780 PPCODE:
1781{
1782 dREQ;
1783
1784 req->int2 = SvOK (uid) ? SvIV (uid) : -1;
1785 req->int3 = SvOK (gid) ? SvIV (gid) : -1;
1786 req->sv1 = newSVsv (fh_or_path);
1787
1788 if (SvPOK (fh_or_path))
1789 {
1790 req->type = REQ_CHOWN;
1791 req->ptr1 = SvPVbyte_nolen (req->sv1);
1792 }
1793 else
1794 {
1795 req->type = REQ_FCHOWN;
1796 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
1797 }
1798
1799 REQ_SEND;
1800}
1801
1802void
818aio_unlink(pathname,callback=&PL_sv_undef) 1803aio_unlink (SV8 *pathname, SV *callback=&PL_sv_undef)
819 SV * pathname
820 SV * callback
821 ALIAS: 1804 ALIAS:
822 aio_unlink = REQ_UNLINK 1805 aio_unlink = REQ_UNLINK
823 aio_rmdir = REQ_RMDIR 1806 aio_rmdir = REQ_RMDIR
1807 aio_readdir = REQ_READDIR
824 CODE: 1808 PPCODE:
825{ 1809{
826 dREQ; 1810 dREQ;
827 1811
828 req->type = ix; 1812 req->type = ix;
829 req->data = newSVsv (pathname); 1813 req->sv1 = newSVsv (pathname);
830 req->dataptr = SvPVbyte_nolen (req->data); 1814 req->ptr1 = SvPVbyte_nolen (req->sv1);
831
832 send_req (req);
833}
834 1815
1816 REQ_SEND;
1817}
1818
835void 1819void
836aio_symlink(oldpath,newpath,callback=&PL_sv_undef) 1820aio_mkdir (SV8 *pathname, int mode, SV *callback=&PL_sv_undef)
837 SV * oldpath
838 SV * newpath
839 SV * callback
840 CODE: 1821 PPCODE:
841{ 1822{
842 dREQ; 1823 dREQ;
843 1824
844 req->type = REQ_SYMLINK; 1825 req->type = REQ_MKDIR;
845 req->fh = newSVsv (oldpath); 1826 req->sv1 = newSVsv (pathname);
846 req->data2ptr = SvPVbyte_nolen (req->fh); 1827 req->ptr1 = SvPVbyte_nolen (req->sv1);
847 req->data = newSVsv (newpath); 1828 req->mode = mode;
848 req->dataptr = SvPVbyte_nolen (req->data); 1829
1830 REQ_SEND;
1831}
1832
1833void
1834aio_link (SV8 *oldpath, SV8 *newpath, SV *callback=&PL_sv_undef)
1835 ALIAS:
1836 aio_link = REQ_LINK
1837 aio_symlink = REQ_SYMLINK
1838 aio_rename = REQ_RENAME
1839 PPCODE:
1840{
1841 dREQ;
849 1842
850 send_req (req); 1843 req->type = ix;
1844 req->sv2 = newSVsv (oldpath);
1845 req->ptr2 = SvPVbyte_nolen (req->sv2);
1846 req->sv1 = newSVsv (newpath);
1847 req->ptr1 = SvPVbyte_nolen (req->sv1);
1848
1849 REQ_SEND;
851} 1850}
852 1851
853void 1852void
1853aio_mknod (SV8 *pathname, int mode, UV dev, SV *callback=&PL_sv_undef)
1854 PPCODE:
1855{
1856 dREQ;
1857
1858 req->type = REQ_MKNOD;
1859 req->sv1 = newSVsv (pathname);
1860 req->ptr1 = SvPVbyte_nolen (req->sv1);
1861 req->mode = (mode_t)mode;
1862 req->offs = dev;
1863
1864 REQ_SEND;
1865}
1866
1867void
1868aio_busy (double delay, SV *callback=&PL_sv_undef)
1869 PPCODE:
1870{
1871 dREQ;
1872
1873 req->type = REQ_BUSY;
1874 req->nv1 = delay < 0. ? 0. : delay;
1875
1876 REQ_SEND;
1877}
1878
1879void
1880aio_group (SV *callback=&PL_sv_undef)
1881 PROTOTYPE: ;$
1882 PPCODE:
1883{
1884 dREQ;
1885
1886 req->type = REQ_GROUP;
1887
1888 req_send (req);
1889 XPUSHs (req_sv (req, AIO_GRP_KLASS));
1890}
1891
1892void
1893aio_nop (SV *callback=&PL_sv_undef)
1894 ALIAS:
1895 aio_nop = REQ_NOP
1896 aio_sync = REQ_SYNC
1897 PPCODE:
1898{
1899 dREQ;
1900
1901 req->type = ix;
1902
1903 REQ_SEND;
1904}
1905
1906int
1907aioreq_pri (int pri = 0)
1908 PROTOTYPE: ;$
1909 CODE:
1910 RETVAL = next_pri - PRI_BIAS;
1911 if (items > 0)
1912 {
1913 if (pri < PRI_MIN) pri = PRI_MIN;
1914 if (pri > PRI_MAX) pri = PRI_MAX;
1915 next_pri = pri + PRI_BIAS;
1916 }
1917 OUTPUT:
1918 RETVAL
1919
1920void
1921aioreq_nice (int nice = 0)
1922 CODE:
1923 nice = next_pri - nice;
1924 if (nice < PRI_MIN) nice = PRI_MIN;
1925 if (nice > PRI_MAX) nice = PRI_MAX;
1926 next_pri = nice + PRI_BIAS;
1927
1928void
854flush() 1929flush ()
855 PROTOTYPE: 1930 PROTOTYPE:
856 CODE: 1931 CODE:
857 while (nreqs) 1932 while (nreqs)
858 { 1933 {
859 poll_wait (); 1934 poll_wait ();
860 poll_cb (); 1935 poll_cb ();
861 } 1936 }
862 1937
863void 1938int
864poll() 1939poll()
865 PROTOTYPE: 1940 PROTOTYPE:
866 CODE: 1941 CODE:
867 if (nreqs)
868 {
869 poll_wait (); 1942 poll_wait ();
870 poll_cb (); 1943 RETVAL = poll_cb ();
871 } 1944 OUTPUT:
1945 RETVAL
872 1946
873int 1947int
874poll_fileno() 1948poll_fileno()
875 PROTOTYPE: 1949 PROTOTYPE:
876 CODE: 1950 CODE:
888 1962
889void 1963void
890poll_wait() 1964poll_wait()
891 PROTOTYPE: 1965 PROTOTYPE:
892 CODE: 1966 CODE:
893 if (nreqs)
894 poll_wait (); 1967 poll_wait ();
1968
1969void
1970setsig (int signum = SIGIO)
1971 PROTOTYPE: ;$
1972 CODE:
1973{
1974 if (block_sig_level)
1975 croak ("cannot call IO::AIO::setsig from within aio_block/callback");
1976
1977 X_LOCK (reslock);
1978 main_tid = pthread_self ();
1979 main_sig = signum;
1980 X_UNLOCK (reslock);
1981
1982 if (main_sig && npending)
1983 pthread_kill (main_tid, main_sig);
1984}
1985
1986void
1987aio_block (SV *cb)
1988 PROTOTYPE: &
1989 PPCODE:
1990{
1991 int count;
1992
1993 block_sig ();
1994 PUSHMARK (SP);
1995 PUTBACK;
1996 count = call_sv (cb, GIMME_V | G_NOARGS | G_EVAL);
1997 SPAGAIN;
1998 unblock_sig ();
1999
2000 if (SvTRUE (ERRSV))
2001 croak (0);
2002
2003 XSRETURN (count);
2004}
895 2005
896int 2006int
897nreqs() 2007nreqs()
898 PROTOTYPE: 2008 PROTOTYPE:
899 CODE: 2009 CODE:
900 RETVAL = nreqs; 2010 RETVAL = nreqs;
901 OUTPUT: 2011 OUTPUT:
902 RETVAL 2012 RETVAL
903 2013
2014int
2015nready()
2016 PROTOTYPE:
2017 CODE:
2018 RETVAL = get_nready ();
2019 OUTPUT:
2020 RETVAL
2021
2022int
2023npending()
2024 PROTOTYPE:
2025 CODE:
2026 RETVAL = get_npending ();
2027 OUTPUT:
2028 RETVAL
2029
2030int
2031nthreads()
2032 PROTOTYPE:
2033 CODE:
2034 if (WORDACCESS_UNSAFE) X_LOCK (wrklock);
2035 RETVAL = started;
2036 if (WORDACCESS_UNSAFE) X_UNLOCK (wrklock);
2037 OUTPUT:
2038 RETVAL
2039
2040PROTOTYPES: DISABLE
2041
2042MODULE = IO::AIO PACKAGE = IO::AIO::REQ
2043
2044void
2045cancel (aio_req_ornot req)
2046 CODE:
2047 req_cancel (req);
2048
2049void
2050cb (aio_req_ornot req, SV *callback=&PL_sv_undef)
2051 CODE:
2052 SvREFCNT_dec (req->callback);
2053 req->callback = newSVsv (callback);
2054
2055MODULE = IO::AIO PACKAGE = IO::AIO::GRP
2056
2057void
2058add (aio_req grp, ...)
2059 PPCODE:
2060{
2061 int i;
2062 aio_req req;
2063
2064 if (main_sig && !block_sig_level)
2065 croak ("aio_group->add called outside aio_block/callback context while IO::AIO::setsig is in use");
2066
2067 if (grp->int1 == 2)
2068 croak ("cannot add requests to IO::AIO::GRP after the group finished");
2069
2070 for (i = 1; i < items; ++i )
2071 {
2072 if (GIMME_V != G_VOID)
2073 XPUSHs (sv_2mortal (newSVsv (ST (i))));
2074
2075 req = SvAIO_REQ (ST (i));
2076
2077 if (req)
2078 {
2079 ++grp->size;
2080 req->grp = grp;
2081
2082 req->grp_prev = 0;
2083 req->grp_next = grp->grp_first;
2084
2085 if (grp->grp_first)
2086 grp->grp_first->grp_prev = req;
2087
2088 grp->grp_first = req;
2089 }
2090 }
2091}
2092
2093void
2094cancel_subs (aio_req_ornot req)
2095 CODE:
2096 req_cancel_subs (req);
2097
2098void
2099result (aio_req grp, ...)
2100 CODE:
2101{
2102 int i;
2103 AV *av;
2104
2105 grp->errorno = errno;
2106
2107 av = newAV ();
2108
2109 for (i = 1; i < items; ++i )
2110 av_push (av, newSVsv (ST (i)));
2111
2112 SvREFCNT_dec (grp->sv1);
2113 grp->sv1 = (SV *)av;
2114}
2115
2116void
2117errno (aio_req grp, int errorno = errno)
2118 CODE:
2119 grp->errorno = errorno;
2120
2121void
2122limit (aio_req grp, int limit)
2123 CODE:
2124 grp->int2 = limit;
2125 aio_grp_feed (grp);
2126
2127void
2128feed (aio_req grp, SV *callback=&PL_sv_undef)
2129 CODE:
2130{
2131 SvREFCNT_dec (grp->sv2);
2132 grp->sv2 = newSVsv (callback);
2133
2134 if (grp->int2 <= 0)
2135 grp->int2 = 2;
2136
2137 aio_grp_feed (grp);
2138}
2139

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines