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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines