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

Comparing IO-AIO/AIO.xs (file contents):
Revision 1.32 by root, Mon Aug 22 23:20:37 2005 UTC vs.
Revision 1.103 by root, Sun Jul 8 09:09:34 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines