ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/IO-AIO/AIO.xs
Revision: 1.104
Committed: Sun Jul 8 11:05:36 2007 UTC (16 years, 10 months ago) by root
Branch: MAIN
Changes since 1.103: +39 -12 lines
Log Message:
*** empty log message ***

File Contents

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