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.9 by root, Tue Jul 12 11:02:54 2005 UTC vs.
Revision 1.113 by root, Wed Apr 16 16:45:18 2008 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines