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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines