ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/IO-AIO/AIO.xs
Revision: 1.109
Committed: Sat Oct 6 14:05:19 2007 UTC (17 years, 1 month ago) by root
Branch: MAIN
CVS Tags: rel-2_51
Changes since 1.108: +18 -19 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 #include "xthread.h"
2
3 #include <errno.h>
4
5 #include "EXTERN.h"
6 #include "perl.h"
7 #include "XSUB.h"
8
9 #include <stddef.h>
10 #include <stdlib.h>
11 #include <errno.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <limits.h>
15 #include <fcntl.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
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
102 static 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
112 typedef SV SV8; /* byte-sv, used for argument-checking */
113
114 enum {
115 REQ_QUIT,
116 REQ_OPEN, REQ_CLOSE,
117 REQ_READ, REQ_WRITE,
118 REQ_READAHEAD, REQ_SENDFILE,
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,
124 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,
130 };
131
132 #define AIO_REQ_KLASS "IO::AIO::REQ"
133 #define AIO_GRP_KLASS "IO::AIO::GRP"
134
135 typedef struct aio_cb
136 {
137 struct aio_cb *volatile next;
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;
148 int type;
149 int int1, int2, int3;
150 int errorno;
151 mode_t mode; /* open */
152
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;
158 } aio_cb;
159
160 enum {
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
166 typedef aio_cb *aio_req;
167 typedef aio_cb *aio_req_ornot;
168
169 enum {
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
180 static unsigned int max_poll_time = 0;
181 static unsigned int max_poll_reqs = 0;
182
183 /* calculcate time difference in ~1/AIO_TICKS of a second */
184 static 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
190 static thread_t main_tid;
191 static int main_sig;
192 static int block_sig_level;
193
194 void 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
209 void 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
224 static int next_pri = DEFAULT_PRI + PRI_BIAS;
225
226 static unsigned int started, idle, wanted;
227
228 /* worker threads management */
229 static mutex_t wrklock = X_MUTEX_INIT;
230
231 typedef 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
243 static worker wrk_first = { &wrk_first, &wrk_first, 0 };
244
245 static 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
260 static void worker_free (worker *wrk)
261 {
262 wrk->next->prev = wrk->prev;
263 wrk->prev->next = wrk->next;
264
265 free (wrk);
266 }
267
268 static volatile unsigned int nreqs, nready, npending;
269 static volatile unsigned int max_idle = 4;
270 static volatile unsigned int max_outstanding = 0xffffffff;
271 static int respipe_osf [2], respipe [2] = { -1, -1 };
272
273 static mutex_t reslock = X_MUTEX_INIT;
274 static mutex_t reqlock = X_MUTEX_INIT;
275 static cond_t reqwait = X_COND_INIT;
276
277 #if WORDACCESS_UNSAFE
278
279 static 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
290 static 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
301 static 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 */
325 typedef struct {
326 aio_req qs[NUM_PRI], qe[NUM_PRI]; /* qstart, qend */
327 int size;
328 } reqq;
329
330 static reqq req_queue;
331 static reqq res_queue;
332
333 int 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
349 aio_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
374 static int poll_cb ();
375 static int req_invoke (aio_req req);
376 static void req_destroy (aio_req req);
377 static void req_cancel (aio_req req);
378
379 /* must be called at most once */
380 static 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
391 static 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
403 static 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
438 static 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
462 static 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
612 static 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
630 static 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
644 static 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
657 static void
658 create_respipe ()
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
695 X_THREAD_PROC (aio_proc);
696
697 static 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
720 static 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
732 static 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
749 static 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
768 static 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
775 static void min_parallel (int nthreads)
776 {
777 if (wanted < nthreads)
778 wanted = nthreads;
779 }
780
781 static void max_parallel (int nthreads)
782 {
783 if (wanted > nthreads)
784 wanted = nthreads;
785
786 while (started > wanted)
787 end_thread ();
788 }
789
790 static 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
813 static int poll_cb ()
814 {
815 dSP;
816 int count = 0;
817 int maxreqs = max_poll_reqs;
818 int do_croak = 0;
819 struct timeval tv_start, tv_now;
820 aio_req req;
821
822 if (max_poll_time)
823 gettimeofday (&tv_start, 0);
824
825 block_sig ();
826
827 for (;;)
828 {
829 for (;;)
830 {
831 maybe_start_thread ();
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 }
885 }
886
887 if (nreqs <= max_outstanding)
888 break;
889
890 poll_wait ();
891
892 ++maxreqs;
893 }
894
895 unblock_sig ();
896 return count;
897 }
898
899 /*****************************************************************************/
900 /* work around various missing functions */
901
902 #if !HAVE_PREADWRITE
903 # define pread aio_pread
904 # define pwrite aio_pwrite
905
906 /*
907 * make our pread/pwrite safe against themselves, but not against
908 * normal read/write by using a mutex. slows down execution a lot,
909 * but that's your problem, not mine.
910 */
911 static mutex_t preadwritelock = X_MUTEX_INIT;
912
913 static ssize_t pread (int fd, void *buf, size_t count, off_t offset)
914 {
915 ssize_t res;
916 off_t ooffset;
917
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);
924
925 return res;
926 }
927
928 static 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
949 int 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
964 int aio_futimes (int fd, const struct timeval tv[2])
965 {
966 errno = ENOSYS;
967 return -1;
968 }
969
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
979 static 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)
985 {
986 size_t len = todo < AIO_BUFSIZE ? todo : AIO_BUFSIZE;
987
988 pread (fd, aio_buf, len, offset);
989 offset += len;
990 todo -= len;
991 }
992
993 errno = 0;
994 return count;
995 }
996
997 #endif
998
999 #if !HAVE_READDIR_R
1000 # define readdir_r aio_readdir_r
1001
1002 static mutex_t readdirlock = X_MUTEX_INIT;
1003
1004 static 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
1029 /* sendfile always needs emulation */
1030 static 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 )
1087 {
1088 /* emulate sendfile. this is a major pain in the ass */
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 }
1117 }
1118
1119 return res;
1120 }
1121
1122 /* read a full directory */
1123 static 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
1183 /*****************************************************************************/
1184
1185 X_THREAD_PROC (aio_proc)
1186 {
1187 {//D
1188 aio_req req;
1189 struct timespec ts;
1190 worker *self = (worker *)thr_arg;
1191
1192 /* try to distribute timeouts somewhat randomly */
1193 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL);
1194
1195 for (;;)
1196 {
1197 ts.tv_sec = time (0) + IDLE_TIMEOUT;
1198
1199 X_LOCK (reqlock);
1200
1201 for (;;)
1202 {
1203 self->req = req = reqq_shift (&req_queue);
1204
1205 if (req)
1206 break;
1207
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;
1229 }
1230
1231 --nready;
1232
1233 X_UNLOCK (reqlock);
1234
1235 errno = 0; /* strictly unnecessary */
1236
1237 if (!(req->flags & FLAG_CANCELLED))
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_FDATASYNC: req->result = fdatasync (req->int1); break;
1273 case REQ_FSYNC: req->result = fsync (req->int1); break;
1274 case REQ_READDIR: scandir_ (req, self); break;
1275
1276 case REQ_BUSY:
1277 #ifdef _WIN32
1278 Sleep (req->nv1 * 1000.);
1279 #else
1280 {
1281 struct timeval tv;
1282
1283 tv.tv_sec = req->nv1;
1284 tv.tv_usec = (req->nv1 - tv.tv_sec) * 1000000.;
1285
1286 req->result = select (0, 0, 0, 0, &tv);
1287 }
1288 #endif
1289 break;
1290
1291 case REQ_UTIME:
1292 case REQ_FUTIME:
1293 {
1294 struct timeval tv[2];
1295 struct timeval *times;
1296
1297 if (req->nv1 != -1. || req->nv2 != -1.)
1298 {
1299 tv[0].tv_sec = req->nv1;
1300 tv[0].tv_usec = (req->nv1 - tv[0].tv_sec) * 1000000.;
1301 tv[1].tv_sec = req->nv2;
1302 tv[1].tv_usec = (req->nv2 - tv[1].tv_sec) * 1000000.;
1303
1304 times = tv;
1305 }
1306 else
1307 times = 0;
1308
1309
1310 req->result = req->type == REQ_FUTIME
1311 ? futimes (req->int1, times)
1312 : utimes (req->ptr1, times);
1313 }
1314
1315 case REQ_GROUP:
1316 case REQ_NOP:
1317 break;
1318
1319 case REQ_QUIT:
1320 goto quit;
1321
1322 default:
1323 req->result = -1;
1324 break;
1325 }
1326
1327 req->errorno = errno;
1328
1329 X_LOCK (reslock);
1330
1331 ++npending;
1332
1333 if (!reqq_push (&res_queue, req))
1334 {
1335 /* write a dummy byte to the pipe so fh becomes ready */
1336 respipe_write (respipe_osf [1], (const void *)&respipe_osf, 1);
1337
1338 /* optionally signal the main thread asynchronously */
1339 if (main_sig)
1340 pthread_kill (main_tid, main_sig);
1341 }
1342
1343 self->req = 0;
1344 worker_clear (self);
1345
1346 X_UNLOCK (reslock);
1347 }
1348
1349 quit:
1350 X_LOCK (wrklock);
1351 worker_free (self);
1352 X_UNLOCK (wrklock);
1353
1354 return 0;
1355 }//D
1356 }
1357
1358 /*****************************************************************************/
1359
1360 static void atfork_prepare (void)
1361 {
1362 X_LOCK (wrklock);
1363 X_LOCK (reqlock);
1364 X_LOCK (reslock);
1365 #if !HAVE_PREADWRITE
1366 X_LOCK (preadwritelock);
1367 #endif
1368 #if !HAVE_READDIR_R
1369 X_LOCK (readdirlock);
1370 #endif
1371 }
1372
1373 static void atfork_parent (void)
1374 {
1375 #if !HAVE_READDIR_R
1376 X_UNLOCK (readdirlock);
1377 #endif
1378 #if !HAVE_PREADWRITE
1379 X_UNLOCK (preadwritelock);
1380 #endif
1381 X_UNLOCK (reslock);
1382 X_UNLOCK (reqlock);
1383 X_UNLOCK (wrklock);
1384 }
1385
1386 static void atfork_child (void)
1387 {
1388 aio_req prv;
1389
1390 while (prv = reqq_shift (&req_queue))
1391 req_destroy (prv);
1392
1393 while (prv = reqq_shift (&res_queue))
1394 req_destroy (prv);
1395
1396 while (wrk_first.next != &wrk_first)
1397 {
1398 worker *wrk = wrk_first.next;
1399
1400 if (wrk->req)
1401 req_destroy (wrk->req);
1402
1403 worker_clear (wrk);
1404 worker_free (wrk);
1405 }
1406
1407 started = 0;
1408 idle = 0;
1409 nreqs = 0;
1410 nready = 0;
1411 npending = 0;
1412
1413 create_respipe ();
1414
1415 atfork_parent ();
1416 }
1417
1418 #define dREQ \
1419 aio_req req; \
1420 int req_pri = next_pri; \
1421 next_pri = DEFAULT_PRI + PRI_BIAS; \
1422 \
1423 if (SvOK (callback) && !SvROK (callback)) \
1424 croak ("callback must be undef or of reference type"); \
1425 \
1426 Newz (0, req, 1, aio_cb); \
1427 if (!req) \
1428 croak ("out of memory during aio_req allocation"); \
1429 \
1430 req->callback = newSVsv (callback); \
1431 req->pri = req_pri
1432
1433 #define REQ_SEND \
1434 req_send (req); \
1435 \
1436 if (GIMME_V != G_VOID) \
1437 XPUSHs (req_sv (req, AIO_REQ_KLASS));
1438
1439 MODULE = IO::AIO PACKAGE = IO::AIO
1440
1441 PROTOTYPES: ENABLE
1442
1443 BOOT:
1444 {
1445 stash = gv_stashpv ("IO::AIO", 1);
1446
1447 newCONSTSUB (stash, "EXDEV", newSViv (EXDEV));
1448 newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY));
1449 newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY));
1450 newCONSTSUB (stash, "O_CREAT", newSViv (O_CREAT));
1451 newCONSTSUB (stash, "O_TRUNC", newSViv (O_TRUNC));
1452 #ifdef _WIN32
1453 X_MUTEX_CHECK (wrklock);
1454 X_MUTEX_CHECK (reslock);
1455 X_MUTEX_CHECK (reqlock);
1456 X_MUTEX_CHECK (reqwait);
1457 X_MUTEX_CHECK (preadwritelock);
1458 X_MUTEX_CHECK (readdirlock);
1459
1460 X_COND_CHECK (reqwait);
1461 #else
1462 newCONSTSUB (stash, "S_IFIFO", newSViv (S_IFIFO));
1463 newCONSTSUB (stash, "SIGIO", newSViv (SIGIO));
1464 #endif
1465
1466 create_respipe ();
1467
1468 X_THREAD_ATFORK (atfork_prepare, atfork_parent, atfork_child);
1469 }
1470
1471 void
1472 max_poll_reqs (int nreqs)
1473 PROTOTYPE: $
1474 CODE:
1475 max_poll_reqs = nreqs;
1476
1477 void
1478 max_poll_time (double nseconds)
1479 PROTOTYPE: $
1480 CODE:
1481 max_poll_time = nseconds * AIO_TICKS;
1482
1483 void
1484 min_parallel (int nthreads)
1485 PROTOTYPE: $
1486
1487 void
1488 max_parallel (int nthreads)
1489 PROTOTYPE: $
1490
1491 void
1492 max_idle (int nthreads)
1493 PROTOTYPE: $
1494 CODE:
1495 set_max_idle (nthreads);
1496
1497 int
1498 max_outstanding (int maxreqs)
1499 PROTOTYPE: $
1500 CODE:
1501 RETVAL = max_outstanding;
1502 max_outstanding = maxreqs;
1503 OUTPUT:
1504 RETVAL
1505
1506 void
1507 aio_open (SV8 *pathname, int flags, int mode, SV *callback=&PL_sv_undef)
1508 PROTOTYPE: $$$;$
1509 PPCODE:
1510 {
1511 dREQ;
1512
1513 req->type = REQ_OPEN;
1514 req->sv1 = newSVsv (pathname);
1515 req->ptr1 = SvPVbyte_nolen (req->sv1);
1516 req->int1 = flags;
1517 req->mode = mode;
1518
1519 REQ_SEND;
1520 }
1521
1522 void
1523 aio_fsync (SV *fh, SV *callback=&PL_sv_undef)
1524 PROTOTYPE: $;$
1525 ALIAS:
1526 aio_fsync = REQ_FSYNC
1527 aio_fdatasync = REQ_FDATASYNC
1528 PPCODE:
1529 {
1530 dREQ;
1531
1532 req->type = ix;
1533 req->sv1 = newSVsv (fh);
1534 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh)));
1535
1536 REQ_SEND (req);
1537 }
1538
1539 int
1540 _dup (int fd)
1541 PROTOTYPE: $
1542 CODE:
1543 RETVAL = dup (fd);
1544 OUTPUT:
1545 RETVAL
1546
1547 void
1548 _aio_close (int fd, SV *callback=&PL_sv_undef)
1549 PROTOTYPE: $;$
1550 PPCODE:
1551 {
1552 dREQ;
1553
1554 req->type = REQ_CLOSE;
1555 req->int1 = fd;
1556
1557 REQ_SEND (req);
1558 }
1559
1560 void
1561 aio_read (SV *fh, SV *offset, SV *length, SV8 *data, IV dataoffset, SV *callback=&PL_sv_undef)
1562 ALIAS:
1563 aio_read = REQ_READ
1564 aio_write = REQ_WRITE
1565 PROTOTYPE: $$$$$;$
1566 PPCODE:
1567 {
1568 STRLEN svlen;
1569 char *svptr = SvPVbyte (data, svlen);
1570 UV len = SvUV (length);
1571
1572 SvUPGRADE (data, SVt_PV);
1573 SvPOK_on (data);
1574
1575 if (dataoffset < 0)
1576 dataoffset += svlen;
1577
1578 if (dataoffset < 0 || dataoffset > svlen)
1579 croak ("dataoffset outside of data scalar");
1580
1581 if (ix == REQ_WRITE)
1582 {
1583 /* write: check length and adjust. */
1584 if (!SvOK (length) || len + dataoffset > svlen)
1585 len = svlen - dataoffset;
1586 }
1587 else
1588 {
1589 /* read: grow scalar as necessary */
1590 svptr = SvGROW (data, len + dataoffset + 1);
1591 }
1592
1593 if (len < 0)
1594 croak ("length must not be negative");
1595
1596 {
1597 dREQ;
1598
1599 req->type = ix;
1600 req->sv1 = newSVsv (fh);
1601 req->int1 = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh))
1602 : IoOFP (sv_2io (fh)));
1603 req->offs = SvOK (offset) ? SvVAL64 (offset) : -1;
1604 req->size = len;
1605 req->sv2 = SvREFCNT_inc (data);
1606 req->ptr1 = (char *)svptr + dataoffset;
1607 req->stroffset = dataoffset;
1608
1609 if (!SvREADONLY (data))
1610 {
1611 SvREADONLY_on (data);
1612 req->flags |= FLAG_SV2_RO_OFF;
1613 }
1614
1615 REQ_SEND;
1616 }
1617 }
1618
1619 void
1620 aio_readlink (SV8 *path, SV *callback=&PL_sv_undef)
1621 PROTOTYPE: $$;$
1622 PPCODE:
1623 {
1624 SV *data;
1625 dREQ;
1626
1627 data = newSV (NAME_MAX);
1628 SvPOK_on (data);
1629
1630 req->type = REQ_READLINK;
1631 req->sv1 = newSVsv (path);
1632 req->ptr2 = SvPVbyte_nolen (req->sv1);
1633 req->sv2 = data;
1634 req->ptr1 = SvPVbyte_nolen (data);
1635
1636 REQ_SEND;
1637 }
1638
1639 void
1640 aio_sendfile (SV *out_fh, SV *in_fh, SV *in_offset, UV length, SV *callback=&PL_sv_undef)
1641 PROTOTYPE: $$$$;$
1642 PPCODE:
1643 {
1644 dREQ;
1645
1646 req->type = REQ_SENDFILE;
1647 req->sv1 = newSVsv (out_fh);
1648 req->int1 = PerlIO_fileno (IoIFP (sv_2io (out_fh)));
1649 req->sv2 = newSVsv (in_fh);
1650 req->int2 = PerlIO_fileno (IoIFP (sv_2io (in_fh)));
1651 req->offs = SvVAL64 (in_offset);
1652 req->size = length;
1653
1654 REQ_SEND;
1655 }
1656
1657 void
1658 aio_readahead (SV *fh, SV *offset, IV length, SV *callback=&PL_sv_undef)
1659 PROTOTYPE: $$$;$
1660 PPCODE:
1661 {
1662 dREQ;
1663
1664 req->type = REQ_READAHEAD;
1665 req->sv1 = newSVsv (fh);
1666 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh)));
1667 req->offs = SvVAL64 (offset);
1668 req->size = length;
1669
1670 REQ_SEND;
1671 }
1672
1673 void
1674 aio_stat (SV8 *fh_or_path, SV *callback=&PL_sv_undef)
1675 ALIAS:
1676 aio_stat = REQ_STAT
1677 aio_lstat = REQ_LSTAT
1678 PPCODE:
1679 {
1680 dREQ;
1681
1682 req->ptr2 = malloc (sizeof (Stat_t));
1683 if (!req->ptr2)
1684 {
1685 req_destroy (req);
1686 croak ("out of memory during aio_stat statdata allocation");
1687 }
1688
1689 req->flags |= FLAG_PTR2_FREE;
1690 req->sv1 = newSVsv (fh_or_path);
1691
1692 if (SvPOK (fh_or_path))
1693 {
1694 req->type = ix;
1695 req->ptr1 = SvPVbyte_nolen (req->sv1);
1696 }
1697 else
1698 {
1699 req->type = REQ_FSTAT;
1700 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
1701 }
1702
1703 REQ_SEND;
1704 }
1705
1706 void
1707 aio_utime (SV8 *fh_or_path, SV *atime, SV *mtime, SV *callback=&PL_sv_undef)
1708 PPCODE:
1709 {
1710 dREQ;
1711
1712 req->nv1 = SvOK (atime) ? SvNV (atime) : -1.;
1713 req->nv2 = SvOK (mtime) ? SvNV (mtime) : -1.;
1714 req->sv1 = newSVsv (fh_or_path);
1715
1716 if (SvPOK (fh_or_path))
1717 {
1718 req->type = REQ_UTIME;
1719 req->ptr1 = SvPVbyte_nolen (req->sv1);
1720 }
1721 else
1722 {
1723 req->type = REQ_FUTIME;
1724 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
1725 }
1726
1727 REQ_SEND;
1728 }
1729
1730 void
1731 aio_truncate (SV8 *fh_or_path, SV *offset, SV *callback=&PL_sv_undef)
1732 PPCODE:
1733 {
1734 dREQ;
1735
1736 req->sv1 = newSVsv (fh_or_path);
1737 req->offs = SvOK (offset) ? SvVAL64 (offset) : -1;
1738
1739 if (SvPOK (fh_or_path))
1740 {
1741 req->type = REQ_TRUNCATE;
1742 req->ptr1 = SvPVbyte_nolen (req->sv1);
1743 }
1744 else
1745 {
1746 req->type = REQ_FTRUNCATE;
1747 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
1748 }
1749
1750 REQ_SEND;
1751 }
1752
1753 void
1754 aio_chmod (SV8 *fh_or_path, int mode, SV *callback=&PL_sv_undef)
1755 PPCODE:
1756 {
1757 dREQ;
1758
1759 req->mode = mode;
1760 req->sv1 = newSVsv (fh_or_path);
1761
1762 if (SvPOK (fh_or_path))
1763 {
1764 req->type = REQ_CHMOD;
1765 req->ptr1 = SvPVbyte_nolen (req->sv1);
1766 }
1767 else
1768 {
1769 req->type = REQ_FCHMOD;
1770 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
1771 }
1772
1773 REQ_SEND;
1774 }
1775
1776 void
1777 aio_chown (SV8 *fh_or_path, SV *uid, SV *gid, SV *callback=&PL_sv_undef)
1778 PPCODE:
1779 {
1780 dREQ;
1781
1782 req->int2 = SvOK (uid) ? SvIV (uid) : -1;
1783 req->int3 = SvOK (gid) ? SvIV (gid) : -1;
1784 req->sv1 = newSVsv (fh_or_path);
1785
1786 if (SvPOK (fh_or_path))
1787 {
1788 req->type = REQ_CHOWN;
1789 req->ptr1 = SvPVbyte_nolen (req->sv1);
1790 }
1791 else
1792 {
1793 req->type = REQ_FCHOWN;
1794 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
1795 }
1796
1797 REQ_SEND;
1798 }
1799
1800 void
1801 aio_unlink (SV8 *pathname, SV *callback=&PL_sv_undef)
1802 ALIAS:
1803 aio_unlink = REQ_UNLINK
1804 aio_rmdir = REQ_RMDIR
1805 aio_readdir = REQ_READDIR
1806 PPCODE:
1807 {
1808 dREQ;
1809
1810 req->type = ix;
1811 req->sv1 = newSVsv (pathname);
1812 req->ptr1 = SvPVbyte_nolen (req->sv1);
1813
1814 REQ_SEND;
1815 }
1816
1817 void
1818 aio_mkdir (SV8 *pathname, int mode, SV *callback=&PL_sv_undef)
1819 PPCODE:
1820 {
1821 dREQ;
1822
1823 req->type = REQ_MKDIR;
1824 req->sv1 = newSVsv (pathname);
1825 req->ptr1 = SvPVbyte_nolen (req->sv1);
1826 req->mode = mode;
1827
1828 REQ_SEND;
1829 }
1830
1831 void
1832 aio_link (SV8 *oldpath, SV8 *newpath, SV *callback=&PL_sv_undef)
1833 ALIAS:
1834 aio_link = REQ_LINK
1835 aio_symlink = REQ_SYMLINK
1836 aio_rename = REQ_RENAME
1837 PPCODE:
1838 {
1839 dREQ;
1840
1841 req->type = ix;
1842 req->sv2 = newSVsv (oldpath);
1843 req->ptr2 = SvPVbyte_nolen (req->sv2);
1844 req->sv1 = newSVsv (newpath);
1845 req->ptr1 = SvPVbyte_nolen (req->sv1);
1846
1847 REQ_SEND;
1848 }
1849
1850 void
1851 aio_mknod (SV8 *pathname, int mode, UV dev, SV *callback=&PL_sv_undef)
1852 PPCODE:
1853 {
1854 dREQ;
1855
1856 req->type = REQ_MKNOD;
1857 req->sv1 = newSVsv (pathname);
1858 req->ptr1 = SvPVbyte_nolen (req->sv1);
1859 req->mode = (mode_t)mode;
1860 req->offs = dev;
1861
1862 REQ_SEND;
1863 }
1864
1865 void
1866 aio_busy (double delay, SV *callback=&PL_sv_undef)
1867 PPCODE:
1868 {
1869 dREQ;
1870
1871 req->type = REQ_BUSY;
1872 req->nv1 = delay < 0. ? 0. : delay;
1873
1874 REQ_SEND;
1875 }
1876
1877 void
1878 aio_group (SV *callback=&PL_sv_undef)
1879 PROTOTYPE: ;$
1880 PPCODE:
1881 {
1882 dREQ;
1883
1884 req->type = REQ_GROUP;
1885
1886 req_send (req);
1887 XPUSHs (req_sv (req, AIO_GRP_KLASS));
1888 }
1889
1890 void
1891 aio_nop (SV *callback=&PL_sv_undef)
1892 PPCODE:
1893 {
1894 dREQ;
1895
1896 req->type = REQ_NOP;
1897
1898 REQ_SEND;
1899 }
1900
1901 int
1902 aioreq_pri (int pri = 0)
1903 PROTOTYPE: ;$
1904 CODE:
1905 RETVAL = next_pri - PRI_BIAS;
1906 if (items > 0)
1907 {
1908 if (pri < PRI_MIN) pri = PRI_MIN;
1909 if (pri > PRI_MAX) pri = PRI_MAX;
1910 next_pri = pri + PRI_BIAS;
1911 }
1912 OUTPUT:
1913 RETVAL
1914
1915 void
1916 aioreq_nice (int nice = 0)
1917 CODE:
1918 nice = next_pri - nice;
1919 if (nice < PRI_MIN) nice = PRI_MIN;
1920 if (nice > PRI_MAX) nice = PRI_MAX;
1921 next_pri = nice + PRI_BIAS;
1922
1923 void
1924 flush ()
1925 PROTOTYPE:
1926 CODE:
1927 while (nreqs)
1928 {
1929 poll_wait ();
1930 poll_cb ();
1931 }
1932
1933 int
1934 poll()
1935 PROTOTYPE:
1936 CODE:
1937 poll_wait ();
1938 RETVAL = poll_cb ();
1939 OUTPUT:
1940 RETVAL
1941
1942 int
1943 poll_fileno()
1944 PROTOTYPE:
1945 CODE:
1946 RETVAL = respipe [0];
1947 OUTPUT:
1948 RETVAL
1949
1950 int
1951 poll_cb(...)
1952 PROTOTYPE:
1953 CODE:
1954 RETVAL = poll_cb ();
1955 OUTPUT:
1956 RETVAL
1957
1958 void
1959 poll_wait()
1960 PROTOTYPE:
1961 CODE:
1962 poll_wait ();
1963
1964 void
1965 setsig (int signum = SIGIO)
1966 PROTOTYPE: ;$
1967 CODE:
1968 {
1969 if (block_sig_level)
1970 croak ("cannot call IO::AIO::setsig from within aio_block/callback");
1971
1972 X_LOCK (reslock);
1973 main_tid = pthread_self ();
1974 main_sig = signum;
1975 X_UNLOCK (reslock);
1976
1977 if (main_sig && npending)
1978 pthread_kill (main_tid, main_sig);
1979 }
1980
1981 void
1982 aio_block (SV *cb)
1983 PROTOTYPE: &
1984 PPCODE:
1985 {
1986 int count;
1987
1988 block_sig ();
1989 PUSHMARK (SP);
1990 PUTBACK;
1991 count = call_sv (cb, GIMME_V | G_NOARGS | G_EVAL);
1992 SPAGAIN;
1993 unblock_sig ();
1994
1995 if (SvTRUE (ERRSV))
1996 croak (0);
1997
1998 XSRETURN (count);
1999 }
2000
2001 int
2002 nreqs()
2003 PROTOTYPE:
2004 CODE:
2005 RETVAL = nreqs;
2006 OUTPUT:
2007 RETVAL
2008
2009 int
2010 nready()
2011 PROTOTYPE:
2012 CODE:
2013 RETVAL = get_nready ();
2014 OUTPUT:
2015 RETVAL
2016
2017 int
2018 npending()
2019 PROTOTYPE:
2020 CODE:
2021 RETVAL = get_npending ();
2022 OUTPUT:
2023 RETVAL
2024
2025 int
2026 nthreads()
2027 PROTOTYPE:
2028 CODE:
2029 if (WORDACCESS_UNSAFE) X_LOCK (wrklock);
2030 RETVAL = started;
2031 if (WORDACCESS_UNSAFE) X_UNLOCK (wrklock);
2032 OUTPUT:
2033 RETVAL
2034
2035 PROTOTYPES: DISABLE
2036
2037 MODULE = IO::AIO PACKAGE = IO::AIO::REQ
2038
2039 void
2040 cancel (aio_req_ornot req)
2041 CODE:
2042 req_cancel (req);
2043
2044 void
2045 cb (aio_req_ornot req, SV *callback=&PL_sv_undef)
2046 CODE:
2047 SvREFCNT_dec (req->callback);
2048 req->callback = newSVsv (callback);
2049
2050 MODULE = IO::AIO PACKAGE = IO::AIO::GRP
2051
2052 void
2053 add (aio_req grp, ...)
2054 PPCODE:
2055 {
2056 int i;
2057 aio_req req;
2058
2059 if (main_sig && !block_sig_level)
2060 croak ("aio_group->add called outside aio_block/callback context while IO::AIO::setsig is in use");
2061
2062 if (grp->int1 == 2)
2063 croak ("cannot add requests to IO::AIO::GRP after the group finished");
2064
2065 for (i = 1; i < items; ++i )
2066 {
2067 if (GIMME_V != G_VOID)
2068 XPUSHs (sv_2mortal (newSVsv (ST (i))));
2069
2070 req = SvAIO_REQ (ST (i));
2071
2072 if (req)
2073 {
2074 ++grp->size;
2075 req->grp = grp;
2076
2077 req->grp_prev = 0;
2078 req->grp_next = grp->grp_first;
2079
2080 if (grp->grp_first)
2081 grp->grp_first->grp_prev = req;
2082
2083 grp->grp_first = req;
2084 }
2085 }
2086 }
2087
2088 void
2089 cancel_subs (aio_req_ornot req)
2090 CODE:
2091 req_cancel_subs (req);
2092
2093 void
2094 result (aio_req grp, ...)
2095 CODE:
2096 {
2097 int i;
2098 AV *av;
2099
2100 grp->errorno = errno;
2101
2102 av = newAV ();
2103
2104 for (i = 1; i < items; ++i )
2105 av_push (av, newSVsv (ST (i)));
2106
2107 SvREFCNT_dec (grp->sv1);
2108 grp->sv1 = (SV *)av;
2109 }
2110
2111 void
2112 errno (aio_req grp, int errorno = errno)
2113 CODE:
2114 grp->errorno = errorno;
2115
2116 void
2117 limit (aio_req grp, int limit)
2118 CODE:
2119 grp->int2 = limit;
2120 aio_grp_feed (grp);
2121
2122 void
2123 feed (aio_req grp, SV *callback=&PL_sv_undef)
2124 CODE:
2125 {
2126 SvREFCNT_dec (grp->sv2);
2127 grp->sv2 = newSVsv (callback);
2128
2129 if (grp->int2 <= 0)
2130 grp->int2 = 2;
2131
2132 aio_grp_feed (grp);
2133 }
2134