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.63 by root, Mon Oct 23 23:48:31 2006 UTC vs.
Revision 1.102 by root, Sun Jun 3 09:44:17 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines