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.70 by root, Tue Oct 24 15:15:56 2006 UTC vs.
Revision 1.86 by root, Mon Oct 30 23:30:00 2006 UTC

1/* solaris */ 1/* solaris */
2#define _POSIX_PTHREAD_SEMANTICS 1 2#define _POSIX_PTHREAD_SEMANTICS 1
3 3
4#if __linux 4#if __linux && !defined(_GNU_SOURCE)
5# define _GNU_SOURCE 5# define _GNU_SOURCE
6#endif 6#endif
7 7
8/* just in case */
8#define _REENTRANT 1 9#define _REENTRANT 1
9 10
10#include <errno.h> 11#include <errno.h>
11 12
12#include "EXTERN.h" 13#include "EXTERN.h"
42# else 43# else
43# error sendfile support requested but not available 44# error sendfile support requested but not available
44# endif 45# endif
45#endif 46#endif
46 47
48/* number of seconds after which idle threads exit */
49#define IDLE_TIMEOUT 10
50
47/* used for struct dirent, AIX doesn't provide it */ 51/* used for struct dirent, AIX doesn't provide it */
48#ifndef NAME_MAX 52#ifndef NAME_MAX
49# define NAME_MAX 4096 53# define NAME_MAX 4096
54#endif
55
56#ifndef PTHREAD_STACK_MIN
57/* care for broken platforms, e.g. windows */
58# define PTHREAD_STACK_MIN 16384
50#endif 59#endif
51 60
52#if __ia64 61#if __ia64
53# define STACKSIZE 65536 62# define STACKSIZE 65536
54#elif __i386 || __x86_64 /* 16k is unreasonably high :( */ 63#elif __i386 || __x86_64 /* 16k is unreasonably high :( */
55# define STACKSIZE PTHREAD_STACK_MIN 64# define STACKSIZE PTHREAD_STACK_MIN
56#else 65#else
57# define STACKSIZE 16384 66# define STACKSIZE 16384
58#endif 67#endif
59 68
69/* wether word reads are potentially non-atomic.
70 * this is conservatice, likely most arches this runs
71 * on have atomic word read/writes.
72 */
73#ifndef WORDACCESS_UNSAFE
74# if __i386 || __x86_64
75# define WORDACCESS_UNSAFE 0
76# else
77# define WORDACCESS_UNSAFE 1
78# endif
79#endif
80
60/* buffer size for various temporary buffers */ 81/* buffer size for various temporary buffers */
61#define AIO_BUFSIZE 65536 82#define AIO_BUFSIZE 65536
62 83
63#define dBUF \ 84#define dBUF \
85 char *aio_buf; \
86 LOCK (wrklock); \
64 char *aio_buf = malloc (AIO_BUFSIZE); \ 87 self->dbuf = aio_buf = malloc (AIO_BUFSIZE); \
88 UNLOCK (wrklock); \
65 if (!aio_buf) \ 89 if (!aio_buf) \
66 return -1; 90 return -1;
67
68#define fBUF free (aio_buf)
69 91
70enum { 92enum {
71 REQ_QUIT, 93 REQ_QUIT,
72 REQ_OPEN, REQ_CLOSE, 94 REQ_OPEN, REQ_CLOSE,
73 REQ_READ, REQ_WRITE, REQ_READAHEAD, 95 REQ_READ, REQ_WRITE, REQ_READAHEAD,
74 REQ_SENDFILE, 96 REQ_SENDFILE,
75 REQ_STAT, REQ_LSTAT, REQ_FSTAT, 97 REQ_STAT, REQ_LSTAT, REQ_FSTAT,
76 REQ_FSYNC, REQ_FDATASYNC, 98 REQ_FSYNC, REQ_FDATASYNC,
77 REQ_UNLINK, REQ_RMDIR, REQ_RENAME, 99 REQ_UNLINK, REQ_RMDIR, REQ_RENAME,
78 REQ_READDIR, 100 REQ_MKNOD, REQ_READDIR,
79 REQ_LINK, REQ_SYMLINK, 101 REQ_LINK, REQ_SYMLINK, REQ_READLINK,
80 REQ_GROUP, REQ_NOP, 102 REQ_GROUP, REQ_NOP,
81 REQ_BUSY, 103 REQ_BUSY,
82}; 104};
83 105
84#define AIO_REQ_KLASS "IO::AIO::REQ" 106#define AIO_REQ_KLASS "IO::AIO::REQ"
86 108
87typedef struct aio_cb 109typedef struct aio_cb
88{ 110{
89 struct aio_cb *volatile next; 111 struct aio_cb *volatile next;
90 112
91 SV *data, *callback; 113 SV *callback, *fh;
92 SV *fh, *fh2; 114 SV *sv1, *sv2;
93 void *dataptr, *data2ptr; 115 void *ptr1, *ptr2;
94 Stat_t *statdata; 116 Stat_t *statdata;
95 off_t offset; 117 off_t offs;
96 size_t length; 118 size_t size;
97 ssize_t result; 119 ssize_t result;
98 120
99 STRLEN dataoffset; 121 STRLEN stroffset;
100 int type; 122 int type;
101 int fd, fd2; 123 int int1, int2;
102 int errorno; 124 int errorno;
103 mode_t mode; /* open */ 125 mode_t mode; /* open */
104 126
105 unsigned char flags; 127 unsigned char flags;
106 unsigned char pri; 128 unsigned char pri;
108 SV *self; /* the perl counterpart of this request, if any */ 130 SV *self; /* the perl counterpart of this request, if any */
109 struct aio_cb *grp, *grp_prev, *grp_next, *grp_first; 131 struct aio_cb *grp, *grp_prev, *grp_next, *grp_first;
110} aio_cb; 132} aio_cb;
111 133
112enum { 134enum {
113 FLAG_CANCELLED = 0x01, 135 FLAG_CANCELLED = 0x01,
136 FLAG_DATA_RO_OFF = 0x80, /* data was set readonly */
114}; 137};
115 138
116typedef aio_cb *aio_req; 139typedef aio_cb *aio_req;
117typedef aio_cb *aio_req_ornot; 140typedef aio_cb *aio_req_ornot;
118 141
123 DEFAULT_PRI = 0, 146 DEFAULT_PRI = 0,
124 PRI_BIAS = -PRI_MIN, 147 PRI_BIAS = -PRI_MIN,
125 NUM_PRI = PRI_MAX + PRI_BIAS + 1, 148 NUM_PRI = PRI_MAX + PRI_BIAS + 1,
126}; 149};
127 150
151#define AIO_TICKS ((1000000 + 1023) >> 10)
152
153static unsigned int max_poll_time = 0;
154static unsigned int max_poll_reqs = 0;
155
156/* calculcate time difference in ~1/AIO_TICKS of a second */
157static int tvdiff (struct timeval *tv1, struct timeval *tv2)
158{
159 return (tv2->tv_sec - tv1->tv_sec ) * AIO_TICKS
160 + ((tv2->tv_usec - tv1->tv_usec) >> 10);
161}
162
128static int next_pri = DEFAULT_PRI + PRI_BIAS; 163static int next_pri = DEFAULT_PRI + PRI_BIAS;
129 164
130static int started, wanted; 165static unsigned int started, idle, wanted;
131static volatile int nreqs;
132static int max_outstanding = 1<<30;
133static int respipe [2];
134 166
135#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) 167#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP)
136# define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP 168# define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
137#else 169#else
138# define AIO_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER 170# define AIO_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
139#endif 171#endif
140 172
173#define LOCK(mutex) pthread_mutex_lock (&(mutex))
174#define UNLOCK(mutex) pthread_mutex_unlock (&(mutex))
175
176/* worker threads management */
177static pthread_mutex_t wrklock = AIO_MUTEX_INIT;
178
179typedef struct worker {
180 /* locked by wrklock */
181 struct worker *prev, *next;
182
183 pthread_t tid;
184
185 /* locked by reslock, reqlock or wrklock */
186 aio_req req; /* currently processed request */
187 void *dbuf;
188 DIR *dirp;
189} worker;
190
191static worker wrk_first = { &wrk_first, &wrk_first, 0 };
192
193static void worker_clear (worker *wrk)
194{
195 if (wrk->dirp)
196 {
197 closedir (wrk->dirp);
198 wrk->dirp = 0;
199 }
200
201 if (wrk->dbuf)
202 {
203 free (wrk->dbuf);
204 wrk->dbuf = 0;
205 }
206}
207
208static void worker_free (worker *wrk)
209{
210 wrk->next->prev = wrk->prev;
211 wrk->prev->next = wrk->next;
212
213 free (wrk);
214}
215
216static volatile unsigned int nreqs, nready, npending;
217static volatile unsigned int max_idle = 4;
218static volatile unsigned int max_outstanding = 0xffffffff;
219static int respipe [2];
220
141static pthread_mutex_t reslock = AIO_MUTEX_INIT; 221static pthread_mutex_t reslock = AIO_MUTEX_INIT;
142static pthread_mutex_t reqlock = AIO_MUTEX_INIT; 222static pthread_mutex_t reqlock = AIO_MUTEX_INIT;
143static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; 223static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER;
224
225#if WORDACCESS_UNSAFE
226
227static unsigned int get_nready ()
228{
229 unsigned int retval;
230
231 LOCK (reqlock);
232 retval = nready;
233 UNLOCK (reqlock);
234
235 return retval;
236}
237
238static unsigned int get_npending ()
239{
240 unsigned int retval;
241
242 LOCK (reslock);
243 retval = npending;
244 UNLOCK (reslock);
245
246 return retval;
247}
248
249static unsigned int get_nthreads ()
250{
251 unsigned int retval;
252
253 LOCK (wrklock);
254 retval = started;
255 UNLOCK (wrklock);
256
257 return retval;
258}
259
260#else
261
262# define get_nready() nready
263# define get_npending() npending
264# define get_nthreads() started
265
266#endif
144 267
145/* 268/*
146 * a somewhat faster data structure might be nice, but 269 * a somewhat faster data structure might be nice, but
147 * with 8 priorities this actually needs <20 insns 270 * with 8 priorities this actually needs <20 insns
148 * per shift, the most expensive operation. 271 * per shift, the most expensive operation.
194 } 317 }
195 318
196 abort (); 319 abort ();
197} 320}
198 321
322static int poll_cb ();
199static void req_invoke (aio_req req); 323static void req_invoke (aio_req req);
200static void req_free (aio_req req); 324static void req_free (aio_req req);
325static void req_cancel (aio_req req);
201 326
202/* must be called at most once */ 327/* must be called at most once */
203static SV *req_sv (aio_req req, const char *klass) 328static SV *req_sv (aio_req req, const char *klass)
204{ 329{
205 if (!req->self) 330 if (!req->self)
223 return mg ? (aio_req)mg->mg_ptr : 0; 348 return mg ? (aio_req)mg->mg_ptr : 0;
224} 349}
225 350
226static void aio_grp_feed (aio_req grp) 351static void aio_grp_feed (aio_req grp)
227{ 352{
228 while (grp->length < grp->fd2 && !(grp->flags & FLAG_CANCELLED)) 353 while (grp->size < grp->int2 && !(grp->flags & FLAG_CANCELLED))
229 { 354 {
230 int old_len = grp->length; 355 int old_len = grp->size;
231 356
232 if (grp->fh2 && SvOK (grp->fh2)) 357 if (grp->sv2 && SvOK (grp->sv2))
233 { 358 {
234 dSP; 359 dSP;
235 360
236 ENTER; 361 ENTER;
237 SAVETMPS; 362 SAVETMPS;
238 PUSHMARK (SP); 363 PUSHMARK (SP);
239 XPUSHs (req_sv (grp, AIO_GRP_KLASS)); 364 XPUSHs (req_sv (grp, AIO_GRP_KLASS));
240 PUTBACK; 365 PUTBACK;
241 call_sv (grp->fh2, G_VOID | G_EVAL | G_KEEPERR); 366 call_sv (grp->sv2, G_VOID | G_EVAL | G_KEEPERR);
242 SPAGAIN; 367 SPAGAIN;
243 FREETMPS; 368 FREETMPS;
244 LEAVE; 369 LEAVE;
245 } 370 }
246 371
247 /* stop if no progress has been made */ 372 /* stop if no progress has been made */
248 if (old_len == grp->length) 373 if (old_len == grp->size)
249 { 374 {
250 SvREFCNT_dec (grp->fh2); 375 SvREFCNT_dec (grp->sv2);
251 grp->fh2 = 0; 376 grp->sv2 = 0;
252 break; 377 break;
253 } 378 }
254 } 379 }
255} 380}
256 381
257static void aio_grp_dec (aio_req grp) 382static void aio_grp_dec (aio_req grp)
258{ 383{
259 --grp->length; 384 --grp->size;
260 385
261 /* call feeder, if applicable */ 386 /* call feeder, if applicable */
262 aio_grp_feed (grp); 387 aio_grp_feed (grp);
263 388
264 /* finish, if done */ 389 /* finish, if done */
265 if (!grp->length && grp->fd) 390 if (!grp->size && grp->int1)
266 { 391 {
267 req_invoke (grp); 392 req_invoke (grp);
268 req_free (grp); 393 req_free (grp);
269 } 394 }
270} 395}
271 396
272static void poll_wait ()
273{
274 fd_set rfd;
275
276 while (nreqs)
277 {
278 int size;
279#if !(__i386 || __x86_64) /* safe without sempahore on this archs */
280 pthread_mutex_lock (&reslock);
281#endif
282 size = res_queue.size;
283#if !(__i386 || __x86_64) /* safe without sempahore on this archs */
284 pthread_mutex_unlock (&reslock);
285#endif
286
287 if (size)
288 return;
289
290 FD_ZERO(&rfd);
291 FD_SET(respipe [0], &rfd);
292
293 select (respipe [0] + 1, &rfd, 0, 0, 0);
294 }
295}
296
297static void req_invoke (aio_req req) 397static void req_invoke (aio_req req)
298{ 398{
299 dSP; 399 dSP;
300 400
401 if (req->flags & FLAG_DATA_RO_OFF)
402 SvREADONLY_off (req->sv1);
403
404 if (req->statdata)
405 {
406 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
407 PL_laststatval = req->result;
408 PL_statcache = *(req->statdata);
409 }
410
301 if (!(req->flags & FLAG_CANCELLED) && SvOK (req->callback)) 411 if (!(req->flags & FLAG_CANCELLED) && SvOK (req->callback))
302 { 412 {
303 errno = req->errorno;
304
305 ENTER; 413 ENTER;
306 SAVETMPS; 414 SAVETMPS;
307 PUSHMARK (SP); 415 PUSHMARK (SP);
308 EXTEND (SP, 1); 416 EXTEND (SP, 1);
309 417
313 { 421 {
314 SV *rv = &PL_sv_undef; 422 SV *rv = &PL_sv_undef;
315 423
316 if (req->result >= 0) 424 if (req->result >= 0)
317 { 425 {
426 int i;
318 char *buf = req->data2ptr; 427 char *buf = req->ptr2;
319 AV *av = newAV (); 428 AV *av = newAV ();
320 429
321 while (req->result) 430 av_extend (av, req->result - 1);
431
432 for (i = 0; i < req->result; ++i)
322 { 433 {
323 SV *sv = newSVpv (buf, 0); 434 SV *sv = newSVpv (buf, 0);
324 435
325 av_push (av, sv); 436 av_store (av, i, sv);
326 buf += SvCUR (sv) + 1; 437 buf += SvCUR (sv) + 1;
327 req->result--;
328 } 438 }
329 439
330 rv = sv_2mortal (newRV_noinc ((SV *)av)); 440 rv = sv_2mortal (newRV_noinc ((SV *)av));
331 } 441 }
332 442
350 XPUSHs (sv_2mortal (fh)); 460 XPUSHs (sv_2mortal (fh));
351 } 461 }
352 break; 462 break;
353 463
354 case REQ_GROUP: 464 case REQ_GROUP:
355 req->fd = 2; /* mark group as finished */ 465 req->int1 = 2; /* mark group as finished */
356 466
357 if (req->data) 467 if (req->sv1)
358 { 468 {
359 int i; 469 int i;
360 AV *av = (AV *)req->data; 470 AV *av = (AV *)req->sv1;
361 471
362 EXTEND (SP, AvFILL (av) + 1); 472 EXTEND (SP, AvFILL (av) + 1);
363 for (i = 0; i <= AvFILL (av); ++i) 473 for (i = 0; i <= AvFILL (av); ++i)
364 PUSHs (*av_fetch (av, i, 0)); 474 PUSHs (*av_fetch (av, i, 0));
365 } 475 }
367 477
368 case REQ_NOP: 478 case REQ_NOP:
369 case REQ_BUSY: 479 case REQ_BUSY:
370 break; 480 break;
371 481
482 case REQ_READLINK:
483 if (req->result > 0)
484 {
485 SvCUR_set (req->sv1, req->result);
486 *SvEND (req->sv1) = 0;
487 PUSHs (req->sv1);
488 }
489 break;
490
491 case REQ_READ:
492 SvCUR_set (req->sv1, req->stroffset + (req->result > 0 ? req->result : 0));
493 *SvEND (req->sv1) = 0;
494 /* fall through */
372 default: 495 default:
373 PUSHs (sv_2mortal (newSViv (req->result))); 496 PUSHs (sv_2mortal (newSViv (req->result)));
374 break; 497 break;
375 } 498 }
376 499
500 errno = req->errorno;
377 501
378 PUTBACK; 502 PUTBACK;
379 call_sv (req->callback, G_VOID | G_EVAL); 503 call_sv (req->callback, G_VOID | G_EVAL);
380 SPAGAIN; 504 SPAGAIN;
381 505
410 { 534 {
411 sv_unmagic (req->self, PERL_MAGIC_ext); 535 sv_unmagic (req->self, PERL_MAGIC_ext);
412 SvREFCNT_dec (req->self); 536 SvREFCNT_dec (req->self);
413 } 537 }
414 538
415 SvREFCNT_dec (req->data);
416 SvREFCNT_dec (req->fh); 539 SvREFCNT_dec (req->fh);
540 SvREFCNT_dec (req->sv1);
417 SvREFCNT_dec (req->fh2); 541 SvREFCNT_dec (req->sv2);
418 SvREFCNT_dec (req->callback); 542 SvREFCNT_dec (req->callback);
419 Safefree (req->statdata); 543 Safefree (req->statdata);
420 544
421 if (req->type == REQ_READDIR && req->result >= 0) 545 if (req->type == REQ_READDIR)
422 free (req->data2ptr); 546 free (req->ptr2);
423 547
424 Safefree (req); 548 Safefree (req);
425} 549}
426 550
551static void req_cancel_subs (aio_req grp)
552{
553 aio_req sub;
554
555 if (grp->type != REQ_GROUP)
556 return;
557
558 SvREFCNT_dec (grp->sv2);
559 grp->sv2 = 0;
560
561 for (sub = grp->grp_first; sub; sub = sub->grp_next)
562 req_cancel (sub);
563}
564
427static void req_cancel (aio_req req) 565static void req_cancel (aio_req req)
428{ 566{
429 req->flags |= FLAG_CANCELLED; 567 req->flags |= FLAG_CANCELLED;
430 568
431 if (req->type == REQ_GROUP) 569 req_cancel_subs (req);
432 {
433 aio_req sub;
434
435 for (sub = req->grp_first; sub; sub = sub->grp_next)
436 req_cancel (sub);
437 }
438}
439
440static int poll_cb ()
441{
442 dSP;
443 int count = 0;
444 int do_croak = 0;
445 aio_req req;
446
447 for (;;)
448 {
449 pthread_mutex_lock (&reslock);
450 req = reqq_shift (&res_queue);
451
452 if (req)
453 {
454 if (!res_queue.size)
455 {
456 /* read any signals sent by the worker threads */
457 char buf [32];
458 while (read (respipe [0], buf, 32) == 32)
459 ;
460 }
461 }
462
463 pthread_mutex_unlock (&reslock);
464
465 if (!req)
466 break;
467
468 --nreqs;
469
470 if (req->type == REQ_QUIT)
471 started--;
472 else if (req->type == REQ_GROUP && req->length)
473 {
474 req->fd = 1; /* mark request as delayed */
475 continue;
476 }
477 else
478 {
479 if (req->type == REQ_READ)
480 SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0));
481
482 if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE))
483 SvREADONLY_off (req->data);
484
485 if (req->statdata)
486 {
487 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
488 PL_laststatval = req->result;
489 PL_statcache = *(req->statdata);
490 }
491
492 req_invoke (req);
493
494 count++;
495 }
496
497 req_free (req);
498 }
499
500 return count;
501} 570}
502 571
503static void *aio_proc(void *arg); 572static void *aio_proc(void *arg);
504 573
505static void start_thread (void) 574static void start_thread (void)
506{ 575{
507 sigset_t fullsigset, oldsigset; 576 sigset_t fullsigset, oldsigset;
508 pthread_t tid;
509 pthread_attr_t attr; 577 pthread_attr_t attr;
578
579 worker *wrk = calloc (1, sizeof (worker));
580
581 if (!wrk)
582 croak ("unable to allocate worker thread data");
510 583
511 pthread_attr_init (&attr); 584 pthread_attr_init (&attr);
512 pthread_attr_setstacksize (&attr, STACKSIZE); 585 pthread_attr_setstacksize (&attr, STACKSIZE);
513 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); 586 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
587#ifdef PTHREAD_SCOPE_PROCESS
588 pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS);
589#endif
514 590
515 sigfillset (&fullsigset); 591 sigfillset (&fullsigset);
592
593 LOCK (wrklock);
516 sigprocmask (SIG_SETMASK, &fullsigset, &oldsigset); 594 sigprocmask (SIG_SETMASK, &fullsigset, &oldsigset);
517 595
518 if (pthread_create (&tid, &attr, aio_proc, 0) == 0) 596 if (pthread_create (&wrk->tid, &attr, aio_proc, (void *)wrk) == 0)
597 {
598 wrk->prev = &wrk_first;
599 wrk->next = wrk_first.next;
600 wrk_first.next->prev = wrk;
601 wrk_first.next = wrk;
519 started++; 602 ++started;
603 }
604 else
605 free (wrk);
520 606
521 sigprocmask (SIG_SETMASK, &oldsigset, 0); 607 sigprocmask (SIG_SETMASK, &oldsigset, 0);
608 UNLOCK (wrklock);
609}
610
611static void maybe_start_thread ()
612{
613 if (get_nthreads () >= wanted)
614 return;
615
616 /* todo: maybe use idle here, but might be less exact */
617 if (0 <= (int)get_nthreads () + (int)get_npending () - (int)nreqs)
618 return;
619
620 start_thread ();
522} 621}
523 622
524static void req_send (aio_req req) 623static void req_send (aio_req req)
525{ 624{
526 while (started < wanted && nreqs >= started)
527 start_thread ();
528
529 ++nreqs; 625 ++nreqs;
530 626
531 pthread_mutex_lock (&reqlock); 627 LOCK (reqlock);
628 ++nready;
532 reqq_push (&req_queue, req); 629 reqq_push (&req_queue, req);
533 pthread_cond_signal (&reqwait); 630 pthread_cond_signal (&reqwait);
534 pthread_mutex_unlock (&reqlock); 631 UNLOCK (reqlock);
535 632
536 if (nreqs > max_outstanding) 633 maybe_start_thread ();
537 for (;;)
538 {
539 poll_cb ();
540
541 if (nreqs <= max_outstanding)
542 break;
543
544 poll_wait ();
545 }
546} 634}
547 635
548static void end_thread (void) 636static void end_thread (void)
549{ 637{
550 aio_req req; 638 aio_req req;
552 Newz (0, req, 1, aio_cb); 640 Newz (0, req, 1, aio_cb);
553 641
554 req->type = REQ_QUIT; 642 req->type = REQ_QUIT;
555 req->pri = PRI_MAX + PRI_BIAS; 643 req->pri = PRI_MAX + PRI_BIAS;
556 644
557 req_send (req); 645 LOCK (reqlock);
646 reqq_push (&req_queue, req);
647 pthread_cond_signal (&reqwait);
648 UNLOCK (reqlock);
649
650 LOCK (wrklock);
651 --started;
652 UNLOCK (wrklock);
653}
654
655static void set_max_idle (int nthreads)
656{
657 if (WORDACCESS_UNSAFE) LOCK (reqlock);
658 max_idle = nthreads <= 0 ? 1 : nthreads;
659 if (WORDACCESS_UNSAFE) UNLOCK (reqlock);
558} 660}
559 661
560static void min_parallel (int nthreads) 662static void min_parallel (int nthreads)
561{ 663{
562 if (wanted < nthreads) 664 if (wanted < nthreads)
563 wanted = nthreads; 665 wanted = nthreads;
564} 666}
565 667
566static void max_parallel (int nthreads) 668static void max_parallel (int nthreads)
567{ 669{
568 int cur = started;
569
570 if (wanted > nthreads) 670 if (wanted > nthreads)
571 wanted = nthreads; 671 wanted = nthreads;
572 672
573 while (cur > wanted)
574 {
575 end_thread ();
576 cur--;
577 }
578
579 while (started > wanted) 673 while (started > wanted)
674 end_thread ();
675}
676
677static void poll_wait ()
678{
679 fd_set rfd;
680
681 while (nreqs)
682 {
683 int size;
684 if (WORDACCESS_UNSAFE) LOCK (reslock);
685 size = res_queue.size;
686 if (WORDACCESS_UNSAFE) UNLOCK (reslock);
687
688 if (size)
689 return;
690
691 maybe_start_thread ();
692
693 FD_ZERO(&rfd);
694 FD_SET(respipe [0], &rfd);
695
696 select (respipe [0] + 1, &rfd, 0, 0, 0);
580 { 697 }
698}
699
700static int poll_cb ()
701{
702 dSP;
703 int count = 0;
704 int maxreqs = max_poll_reqs;
705 int do_croak = 0;
706 struct timeval tv_start, tv_now;
707 aio_req req;
708
709 if (max_poll_time)
710 gettimeofday (&tv_start, 0);
711
712 for (;;)
713 {
714 for (;;)
715 {
716 maybe_start_thread ();
717
718 LOCK (reslock);
719 req = reqq_shift (&res_queue);
720
721 if (req)
722 {
723 --npending;
724
725 if (!res_queue.size)
726 {
727 /* read any signals sent by the worker threads */
728 char buf [32];
729 while (read (respipe [0], buf, 32) == 32)
730 ;
731 }
732 }
733
734 UNLOCK (reslock);
735
736 if (!req)
737 break;
738
739 --nreqs;
740
741 if (req->type == REQ_GROUP && req->size)
742 {
743 req->int1 = 1; /* mark request as delayed */
744 continue;
745 }
746 else
747 {
748 req_invoke (req);
749
750 count++;
751 }
752
753 req_free (req);
754
755 if (maxreqs && !--maxreqs)
756 break;
757
758 if (max_poll_time)
759 {
760 gettimeofday (&tv_now, 0);
761
762 if (tvdiff (&tv_start, &tv_now) >= max_poll_time)
763 break;
764 }
765 }
766
767 if (nreqs <= max_outstanding)
768 break;
769
581 poll_wait (); 770 poll_wait ();
582 poll_cb (); 771
772 ++maxreqs;
583 } 773 }
774
775 return count;
584} 776}
585 777
586static void create_pipe () 778static void create_pipe ()
587{ 779{
588 if (pipe (respipe)) 780 if (pipe (respipe))
612static ssize_t pread (int fd, void *buf, size_t count, off_t offset) 804static ssize_t pread (int fd, void *buf, size_t count, off_t offset)
613{ 805{
614 ssize_t res; 806 ssize_t res;
615 off_t ooffset; 807 off_t ooffset;
616 808
617 pthread_mutex_lock (&preadwritelock); 809 LOCK (preadwritelock);
618 ooffset = lseek (fd, 0, SEEK_CUR); 810 ooffset = lseek (fd, 0, SEEK_CUR);
619 lseek (fd, offset, SEEK_SET); 811 lseek (fd, offset, SEEK_SET);
620 res = read (fd, buf, count); 812 res = read (fd, buf, count);
621 lseek (fd, ooffset, SEEK_SET); 813 lseek (fd, ooffset, SEEK_SET);
622 pthread_mutex_unlock (&preadwritelock); 814 UNLOCK (preadwritelock);
623 815
624 return res; 816 return res;
625} 817}
626 818
627static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset) 819static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset)
628{ 820{
629 ssize_t res; 821 ssize_t res;
630 off_t ooffset; 822 off_t ooffset;
631 823
632 pthread_mutex_lock (&preadwritelock); 824 LOCK (preadwritelock);
633 ooffset = lseek (fd, 0, SEEK_CUR); 825 ooffset = lseek (fd, 0, SEEK_CUR);
634 lseek (fd, offset, SEEK_SET); 826 lseek (fd, offset, SEEK_SET);
635 res = write (fd, buf, count); 827 res = write (fd, buf, count);
636 lseek (fd, offset, SEEK_SET); 828 lseek (fd, offset, SEEK_SET);
637 pthread_mutex_unlock (&preadwritelock); 829 UNLOCK (preadwritelock);
638 830
639 return res; 831 return res;
640} 832}
641#endif 833#endif
642 834
643#if !HAVE_FDATASYNC 835#if !HAVE_FDATASYNC
644# define fdatasync fsync 836# define fdatasync fsync
645#endif 837#endif
646 838
647#if !HAVE_READAHEAD 839#if !HAVE_READAHEAD
648# define readahead aio_readahead 840# define readahead(fd,offset,count) aio_readahead (fd, offset, count, self)
649 841
650static ssize_t readahead (int fd, off_t offset, size_t count) 842static ssize_t aio_readahead (int fd, off_t offset, size_t count, worker *self)
651{ 843{
652 dBUF; 844 dBUF;
653 845
654 while (count > 0) 846 while (count > 0)
655 { 847 {
658 pread (fd, aio_buf, len, offset); 850 pread (fd, aio_buf, len, offset);
659 offset += len; 851 offset += len;
660 count -= len; 852 count -= len;
661 } 853 }
662 854
663 fBUF;
664
665 errno = 0; 855 errno = 0;
666} 856}
857
667#endif 858#endif
668 859
669#if !HAVE_READDIR_R 860#if !HAVE_READDIR_R
670# define readdir_r aio_readdir_r 861# define readdir_r aio_readdir_r
671 862
674static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) 865static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res)
675{ 866{
676 struct dirent *e; 867 struct dirent *e;
677 int errorno; 868 int errorno;
678 869
679 pthread_mutex_lock (&readdirlock); 870 LOCK (readdirlock);
680 871
681 e = readdir (dirp); 872 e = readdir (dirp);
682 errorno = errno; 873 errorno = errno;
683 874
684 if (e) 875 if (e)
687 strcpy (ent->d_name, e->d_name); 878 strcpy (ent->d_name, e->d_name);
688 } 879 }
689 else 880 else
690 *res = 0; 881 *res = 0;
691 882
692 pthread_mutex_unlock (&readdirlock); 883 UNLOCK (readdirlock);
693 884
694 errno = errorno; 885 errno = errorno;
695 return e ? 0 : -1; 886 return e ? 0 : -1;
696} 887}
697#endif 888#endif
698 889
699/* sendfile always needs emulation */ 890/* sendfile always needs emulation */
700static ssize_t sendfile_ (int ofd, int ifd, off_t offset, size_t count) 891static ssize_t sendfile_ (int ofd, int ifd, off_t offset, size_t count, worker *self)
701{ 892{
702 ssize_t res; 893 ssize_t res;
703 894
704 if (!count) 895 if (!count)
705 return 0; 896 return 0;
716 { 907 {
717 off_t sbytes; 908 off_t sbytes;
718 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); 909 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0);
719 910
720 if (res < 0 && sbytes) 911 if (res < 0 && sbytes)
721 /* maybe only on EAGAIN only: as usual, the manpage leaves you guessing */ 912 /* maybe only on EAGAIN: as usual, the manpage leaves you guessing */
722 res = sbytes; 913 res = sbytes;
723 } 914 }
724 915
725# elif __hpux 916# elif __hpux
726 res = sendfile (ofd, ifd, offset, count, 0, 0); 917 res = sendfile (ofd, ifd, offset, count, 0, 0);
782 973
783 offset += cnt; 974 offset += cnt;
784 res += cnt; 975 res += cnt;
785 count -= cnt; 976 count -= cnt;
786 } 977 }
787
788 fBUF;
789 } 978 }
790 979
791 return res; 980 return res;
792} 981}
793 982
794/* read a full directory */ 983/* read a full directory */
795static int scandir_ (const char *path, void **namesp) 984static void scandir_ (aio_req req, worker *self)
796{ 985{
797 DIR *dirp; 986 DIR *dirp;
798 union 987 union
799 { 988 {
800 struct dirent d; 989 struct dirent d;
805 int memlen = 4096; 994 int memlen = 4096;
806 int memofs = 0; 995 int memofs = 0;
807 int res = 0; 996 int res = 0;
808 int errorno; 997 int errorno;
809 998
810 dirp = opendir (path); 999 LOCK (wrklock);
811 if (!dirp) 1000 self->dirp = dirp = opendir (req->ptr1);
812 return -1;
813
814 u = malloc (sizeof (*u)); 1001 self->dbuf = u = malloc (sizeof (*u));
815 names = malloc (memlen); 1002 req->ptr2 = names = malloc (memlen);
1003 UNLOCK (wrklock);
816 1004
817 if (u && names) 1005 if (dirp && u && names)
818 for (;;) 1006 for (;;)
819 { 1007 {
820 errno = 0; 1008 errno = 0;
821 readdir_r (dirp, &u->d, &entp); 1009 readdir_r (dirp, &u->d, &entp);
822 1010
832 res++; 1020 res++;
833 1021
834 while (memofs + len > memlen) 1022 while (memofs + len > memlen)
835 { 1023 {
836 memlen *= 2; 1024 memlen *= 2;
1025 LOCK (wrklock);
837 names = realloc (names, memlen); 1026 req->ptr2 = names = realloc (names, memlen);
1027 UNLOCK (wrklock);
1028
838 if (!names) 1029 if (!names)
839 break; 1030 break;
840 } 1031 }
841 1032
842 memcpy (names + memofs, name, len); 1033 memcpy (names + memofs, name, len);
843 memofs += len; 1034 memofs += len;
844 } 1035 }
845 } 1036 }
846 1037
847 errorno = errno;
848 free (u);
849 closedir (dirp);
850
851 if (errorno) 1038 if (errno)
852 {
853 free (names);
854 errno = errorno;
855 res = -1; 1039 res = -1;
856 } 1040
857 1041 req->result = res;
858 *namesp = (void *)names;
859 return res;
860} 1042}
861 1043
862/*****************************************************************************/ 1044/*****************************************************************************/
863 1045
864static void *aio_proc (void *thr_arg) 1046static void *aio_proc (void *thr_arg)
865{ 1047{
866 aio_req req; 1048 aio_req req;
867 int type; 1049 struct timespec ts;
1050 worker *self = (worker *)thr_arg;
868 1051
869 do 1052 /* try to distribute timeouts somewhat evenly */
1053 ts.tv_nsec = (((unsigned long)self + (unsigned long)ts.tv_sec) & 1023UL)
1054 * (1000000000UL / 1024UL);
1055
1056 for (;;)
870 { 1057 {
871 pthread_mutex_lock (&reqlock); 1058 ts.tv_sec = time (0) + IDLE_TIMEOUT;
1059
1060 LOCK (reqlock);
872 1061
873 for (;;) 1062 for (;;)
874 { 1063 {
875 req = reqq_shift (&req_queue); 1064 self->req = req = reqq_shift (&req_queue);
876 1065
877 if (req) 1066 if (req)
878 break; 1067 break;
879 1068
1069 ++idle;
1070
1071 if (pthread_cond_timedwait (&reqwait, &reqlock, &ts)
1072 == ETIMEDOUT)
1073 {
1074 if (idle > max_idle)
1075 {
1076 --idle;
1077 UNLOCK (reqlock);
1078 LOCK (wrklock);
1079 --started;
1080 UNLOCK (wrklock);
1081 goto quit;
1082 }
1083
1084 /* we are allowed to idle, so do so without any timeout */
880 pthread_cond_wait (&reqwait, &reqlock); 1085 pthread_cond_wait (&reqwait, &reqlock);
1086 ts.tv_sec = time (0) + IDLE_TIMEOUT;
1087 }
1088
1089 --idle;
881 } 1090 }
882 1091
883 pthread_mutex_unlock (&reqlock); 1092 --nready;
1093
1094 UNLOCK (reqlock);
884 1095
885 errno = 0; /* strictly unnecessary */ 1096 errno = 0; /* strictly unnecessary */
886 type = req->type; /* remember type for QUIT check */
887 1097
888 if (!(req->flags & FLAG_CANCELLED)) 1098 if (!(req->flags & FLAG_CANCELLED))
889 switch (type) 1099 switch (req->type)
890 { 1100 {
891 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; 1101 case REQ_READ: req->result = pread (req->int1, req->ptr1, req->size, req->offs); break;
892 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; 1102 case REQ_WRITE: req->result = pwrite (req->int1, req->ptr1, req->size, req->offs); break;
893 1103
894 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; 1104 case REQ_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break;
895 case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length); break; 1105 case REQ_SENDFILE: req->result = sendfile_ (req->int1, req->int2, req->offs, req->size, self); break;
896 1106
897 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; 1107 case REQ_STAT: req->result = stat (req->ptr1, req->statdata); break;
898 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; 1108 case REQ_LSTAT: req->result = lstat (req->ptr1, req->statdata); break;
899 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; 1109 case REQ_FSTAT: req->result = fstat (req->int1, req->statdata); break;
900 1110
901 case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; 1111 case REQ_OPEN: req->result = open (req->ptr1, req->int1, req->mode); break;
902 case REQ_CLOSE: req->result = close (req->fd); break; 1112 case REQ_CLOSE: req->result = close (req->int1); break;
903 case REQ_UNLINK: req->result = unlink (req->dataptr); break; 1113 case REQ_UNLINK: req->result = unlink (req->ptr1); break;
904 case REQ_RMDIR: req->result = rmdir (req->dataptr); break; 1114 case REQ_RMDIR: req->result = rmdir (req->ptr1); break;
905 case REQ_RENAME: req->result = rename (req->data2ptr, req->dataptr); break; 1115 case REQ_RENAME: req->result = rename (req->ptr2, req->ptr1); break;
906 case REQ_LINK: req->result = link (req->data2ptr, req->dataptr); break; 1116 case REQ_LINK: req->result = link (req->ptr2, req->ptr1); break;
907 case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; 1117 case REQ_SYMLINK: req->result = symlink (req->ptr2, req->ptr1); break;
1118 case REQ_MKNOD: req->result = mknod (req->ptr2, req->mode, (dev_t)req->offs); break;
1119 case REQ_READLINK: req->result = readlink (req->ptr2, req->ptr1, NAME_MAX); break;
908 1120
909 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; 1121 case REQ_FDATASYNC: req->result = fdatasync (req->int1); break;
910 case REQ_FSYNC: req->result = fsync (req->fd); break; 1122 case REQ_FSYNC: req->result = fsync (req->int1); break;
911 case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break; 1123 case REQ_READDIR: scandir_ (req, self); break;
912 1124
913 case REQ_BUSY: 1125 case REQ_BUSY:
914 { 1126 {
915 struct timeval tv; 1127 struct timeval tv;
916 1128
917 tv.tv_sec = req->fd; 1129 tv.tv_sec = req->int1;
918 tv.tv_usec = req->fd2; 1130 tv.tv_usec = req->int2;
919 1131
920 req->result = select (0, 0, 0, 0, &tv); 1132 req->result = select (0, 0, 0, 0, &tv);
921 } 1133 }
922 1134
923 case REQ_GROUP: 1135 case REQ_GROUP:
924 case REQ_NOP: 1136 case REQ_NOP:
1137 break;
1138
925 case REQ_QUIT: 1139 case REQ_QUIT:
926 break; 1140 goto quit;
927 1141
928 default: 1142 default:
929 req->result = ENOSYS; 1143 req->result = ENOSYS;
930 break; 1144 break;
931 } 1145 }
932 1146
933 req->errorno = errno; 1147 req->errorno = errno;
934 1148
935 pthread_mutex_lock (&reslock); 1149 LOCK (reslock);
1150
1151 ++npending;
936 1152
937 if (!reqq_push (&res_queue, req)) 1153 if (!reqq_push (&res_queue, req))
938 /* write a dummy byte to the pipe so fh becomes ready */ 1154 /* write a dummy byte to the pipe so fh becomes ready */
939 write (respipe [1], &respipe, 1); 1155 write (respipe [1], &respipe, 1);
940 1156
941 pthread_mutex_unlock (&reslock); 1157 self->req = 0;
1158 worker_clear (self);
1159
1160 UNLOCK (reslock);
942 } 1161 }
943 while (type != REQ_QUIT); 1162
1163quit:
1164 LOCK (wrklock);
1165 worker_free (self);
1166 UNLOCK (wrklock);
944 1167
945 return 0; 1168 return 0;
946} 1169}
947 1170
948/*****************************************************************************/ 1171/*****************************************************************************/
949 1172
950static void atfork_prepare (void) 1173static void atfork_prepare (void)
951{ 1174{
952 pthread_mutex_lock (&reqlock); 1175 LOCK (wrklock);
953 pthread_mutex_lock (&reslock); 1176 LOCK (reqlock);
1177 LOCK (reslock);
954#if !HAVE_PREADWRITE 1178#if !HAVE_PREADWRITE
955 pthread_mutex_lock (&preadwritelock); 1179 LOCK (preadwritelock);
956#endif 1180#endif
957#if !HAVE_READDIR_R 1181#if !HAVE_READDIR_R
958 pthread_mutex_lock (&readdirlock); 1182 LOCK (readdirlock);
959#endif 1183#endif
960} 1184}
961 1185
962static void atfork_parent (void) 1186static void atfork_parent (void)
963{ 1187{
964#if !HAVE_READDIR_R 1188#if !HAVE_READDIR_R
965 pthread_mutex_unlock (&readdirlock); 1189 UNLOCK (readdirlock);
966#endif 1190#endif
967#if !HAVE_PREADWRITE 1191#if !HAVE_PREADWRITE
968 pthread_mutex_unlock (&preadwritelock); 1192 UNLOCK (preadwritelock);
969#endif 1193#endif
970 pthread_mutex_unlock (&reslock); 1194 UNLOCK (reslock);
971 pthread_mutex_unlock (&reqlock); 1195 UNLOCK (reqlock);
1196 UNLOCK (wrklock);
972} 1197}
973 1198
974static void atfork_child (void) 1199static void atfork_child (void)
975{ 1200{
976 aio_req prv; 1201 aio_req prv;
977
978 started = 0;
979 1202
980 while (prv = reqq_shift (&req_queue)) 1203 while (prv = reqq_shift (&req_queue))
981 req_free (prv); 1204 req_free (prv);
982 1205
983 while (prv = reqq_shift (&res_queue)) 1206 while (prv = reqq_shift (&res_queue))
984 req_free (prv); 1207 req_free (prv);
985 1208
1209 while (wrk_first.next != &wrk_first)
1210 {
1211 worker *wrk = wrk_first.next;
1212
1213 if (wrk->req)
1214 req_free (wrk->req);
1215
1216 worker_clear (wrk);
1217 worker_free (wrk);
1218 }
1219
1220 started = 0;
1221 idle = 0;
1222 nreqs = 0;
1223 nready = 0;
1224 npending = 0;
1225
986 close (respipe [0]); 1226 close (respipe [0]);
987 close (respipe [1]); 1227 close (respipe [1]);
988 create_pipe (); 1228 create_pipe ();
989 1229
990 atfork_parent (); 1230 atfork_parent ();
1016PROTOTYPES: ENABLE 1256PROTOTYPES: ENABLE
1017 1257
1018BOOT: 1258BOOT:
1019{ 1259{
1020 HV *stash = gv_stashpv ("IO::AIO", 1); 1260 HV *stash = gv_stashpv ("IO::AIO", 1);
1261
1021 newCONSTSUB (stash, "EXDEV", newSViv (EXDEV)); 1262 newCONSTSUB (stash, "EXDEV", newSViv (EXDEV));
1022 newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY)); 1263 newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY));
1023 newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY)); 1264 newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY));
1265 newCONSTSUB (stash, "O_CREAT", newSViv (O_CREAT));
1266 newCONSTSUB (stash, "O_TRUNC", newSViv (O_TRUNC));
1267 newCONSTSUB (stash, "S_IFIFO", newSViv (S_IFIFO));
1024 1268
1025 create_pipe (); 1269 create_pipe ();
1026 pthread_atfork (atfork_prepare, atfork_parent, atfork_child); 1270 pthread_atfork (atfork_prepare, atfork_parent, atfork_child);
1027} 1271}
1028 1272
1029void 1273void
1030min_parallel (nthreads) 1274max_poll_reqs (int nreqs)
1031 int nthreads
1032 PROTOTYPE: $ 1275 PROTOTYPE: $
1276 CODE:
1277 max_poll_reqs = nreqs;
1033 1278
1034void 1279void
1035max_parallel (nthreads) 1280max_poll_time (double nseconds)
1036 int nthreads
1037 PROTOTYPE: $ 1281 PROTOTYPE: $
1282 CODE:
1283 max_poll_time = nseconds * AIO_TICKS;
1284
1285void
1286min_parallel (int nthreads)
1287 PROTOTYPE: $
1288
1289void
1290max_parallel (int nthreads)
1291 PROTOTYPE: $
1292
1293void
1294max_idle (int nthreads)
1295 PROTOTYPE: $
1296 CODE:
1297 set_max_idle (nthreads);
1038 1298
1039int 1299int
1040max_outstanding (nreqs) 1300max_outstanding (int maxreqs)
1041 int nreqs 1301 PROTOTYPE: $
1042 PROTOTYPE: $
1043 CODE: 1302 CODE:
1044 RETVAL = max_outstanding; 1303 RETVAL = max_outstanding;
1045 max_outstanding = nreqs; 1304 max_outstanding = maxreqs;
1305 OUTPUT:
1306 RETVAL
1046 1307
1047void 1308void
1048aio_open (pathname,flags,mode,callback=&PL_sv_undef) 1309aio_open (pathname,flags,mode,callback=&PL_sv_undef)
1049 SV * pathname 1310 SV * pathname
1050 int flags 1311 int flags
1054 PPCODE: 1315 PPCODE:
1055{ 1316{
1056 dREQ; 1317 dREQ;
1057 1318
1058 req->type = REQ_OPEN; 1319 req->type = REQ_OPEN;
1059 req->data = newSVsv (pathname); 1320 req->sv1 = newSVsv (pathname);
1060 req->dataptr = SvPVbyte_nolen (req->data); 1321 req->ptr1 = SvPVbyte_nolen (pathname);
1061 req->fd = flags; 1322 req->int1 = flags;
1062 req->mode = mode; 1323 req->mode = mode;
1063 1324
1064 REQ_SEND; 1325 REQ_SEND;
1065} 1326}
1066 1327
1076 PPCODE: 1337 PPCODE:
1077{ 1338{
1078 dREQ; 1339 dREQ;
1079 1340
1080 req->type = ix; 1341 req->type = ix;
1081 req->fh = newSVsv (fh); 1342 req->fh = newSVsv (fh);
1082 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); 1343 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh)));
1083 1344
1084 REQ_SEND (req); 1345 REQ_SEND (req);
1085} 1346}
1086 1347
1087void 1348void
1096 aio_read = REQ_READ 1357 aio_read = REQ_READ
1097 aio_write = REQ_WRITE 1358 aio_write = REQ_WRITE
1098 PROTOTYPE: $$$$$;$ 1359 PROTOTYPE: $$$$$;$
1099 PPCODE: 1360 PPCODE:
1100{ 1361{
1101 aio_req req;
1102 STRLEN svlen; 1362 STRLEN svlen;
1103 char *svptr = SvPVbyte (data, svlen); 1363 char *svptr = SvPVbyte (data, svlen);
1104 1364
1105 SvUPGRADE (data, SVt_PV); 1365 SvUPGRADE (data, SVt_PV);
1106 SvPOK_on (data); 1366 SvPOK_on (data);
1128 1388
1129 { 1389 {
1130 dREQ; 1390 dREQ;
1131 1391
1132 req->type = ix; 1392 req->type = ix;
1133 req->fh = newSVsv (fh); 1393 req->fh = newSVsv (fh);
1134 req->fd = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh)) 1394 req->int1 = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh))
1135 : IoOFP (sv_2io (fh))); 1395 : IoOFP (sv_2io (fh)));
1136 req->offset = offset; 1396 req->offs = offset;
1137 req->length = length; 1397 req->size = length;
1138 req->data = SvREFCNT_inc (data); 1398 req->sv1 = SvREFCNT_inc (data);
1139 req->dataptr = (char *)svptr + dataoffset; 1399 req->ptr1 = (char *)svptr + dataoffset;
1400 req->stroffset = dataoffset;
1140 1401
1141 if (!SvREADONLY (data)) 1402 if (!SvREADONLY (data))
1142 { 1403 {
1143 SvREADONLY_on (data); 1404 SvREADONLY_on (data);
1144 req->data2ptr = (void *)data; 1405 req->flags |= FLAG_DATA_RO_OFF;
1145 } 1406 }
1146 1407
1147 REQ_SEND; 1408 REQ_SEND;
1148 } 1409 }
1410}
1411
1412void
1413aio_readlink (path,callback=&PL_sv_undef)
1414 SV * path
1415 SV * callback
1416 PROTOTYPE: $$;$
1417 PPCODE:
1418{
1419 SV *data;
1420 dREQ;
1421
1422 data = newSV (NAME_MAX);
1423 SvPOK_on (data);
1424
1425 req->type = REQ_READLINK;
1426 req->fh = newSVsv (path);
1427 req->ptr2 = SvPVbyte_nolen (path);
1428 req->sv1 = data;
1429 req->ptr1 = SvPVbyte_nolen (data);
1430
1431 REQ_SEND;
1149} 1432}
1150 1433
1151void 1434void
1152aio_sendfile (out_fh,in_fh,in_offset,length,callback=&PL_sv_undef) 1435aio_sendfile (out_fh,in_fh,in_offset,length,callback=&PL_sv_undef)
1153 SV * out_fh 1436 SV * out_fh
1159 PPCODE: 1442 PPCODE:
1160{ 1443{
1161 dREQ; 1444 dREQ;
1162 1445
1163 req->type = REQ_SENDFILE; 1446 req->type = REQ_SENDFILE;
1164 req->fh = newSVsv (out_fh); 1447 req->fh = newSVsv (out_fh);
1165 req->fd = PerlIO_fileno (IoIFP (sv_2io (out_fh))); 1448 req->int1 = PerlIO_fileno (IoIFP (sv_2io (out_fh)));
1166 req->fh2 = newSVsv (in_fh); 1449 req->sv2 = newSVsv (in_fh);
1167 req->fd2 = PerlIO_fileno (IoIFP (sv_2io (in_fh))); 1450 req->int2 = PerlIO_fileno (IoIFP (sv_2io (in_fh)));
1168 req->offset = in_offset; 1451 req->offs = in_offset;
1169 req->length = length; 1452 req->size = length;
1170 1453
1171 REQ_SEND; 1454 REQ_SEND;
1172} 1455}
1173 1456
1174void 1457void
1181 PPCODE: 1464 PPCODE:
1182{ 1465{
1183 dREQ; 1466 dREQ;
1184 1467
1185 req->type = REQ_READAHEAD; 1468 req->type = REQ_READAHEAD;
1186 req->fh = newSVsv (fh); 1469 req->fh = newSVsv (fh);
1187 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); 1470 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh)));
1188 req->offset = offset; 1471 req->offs = offset;
1189 req->length = length; 1472 req->size = length;
1190 1473
1191 REQ_SEND; 1474 REQ_SEND;
1192} 1475}
1193 1476
1194void 1477void
1210 } 1493 }
1211 1494
1212 if (SvPOK (fh_or_path)) 1495 if (SvPOK (fh_or_path))
1213 { 1496 {
1214 req->type = ix; 1497 req->type = ix;
1215 req->data = newSVsv (fh_or_path); 1498 req->sv1 = newSVsv (fh_or_path);
1216 req->dataptr = SvPVbyte_nolen (req->data); 1499 req->ptr1 = SvPVbyte_nolen (fh_or_path);
1217 } 1500 }
1218 else 1501 else
1219 { 1502 {
1220 req->type = REQ_FSTAT; 1503 req->type = REQ_FSTAT;
1221 req->fh = newSVsv (fh_or_path); 1504 req->fh = newSVsv (fh_or_path);
1222 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); 1505 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
1223 } 1506 }
1224 1507
1225 REQ_SEND; 1508 REQ_SEND;
1226} 1509}
1227 1510
1228void 1511void
1229aio_unlink (pathname,callback=&PL_sv_undef) 1512aio_unlink (pathname,callback=&PL_sv_undef)
1230 SV * pathname 1513 SV * pathname
1231 SV * callback 1514 SV * callback
1232 ALIAS: 1515 ALIAS:
1233 aio_unlink = REQ_UNLINK 1516 aio_unlink = REQ_UNLINK
1234 aio_rmdir = REQ_RMDIR 1517 aio_rmdir = REQ_RMDIR
1235 aio_readdir = REQ_READDIR 1518 aio_readdir = REQ_READDIR
1236 PPCODE: 1519 PPCODE:
1237{ 1520{
1238 dREQ; 1521 dREQ;
1239 1522
1240 req->type = ix; 1523 req->type = ix;
1241 req->data = newSVsv (pathname); 1524 req->sv1 = newSVsv (pathname);
1242 req->dataptr = SvPVbyte_nolen (req->data); 1525 req->ptr1 = SvPVbyte_nolen (pathname);
1243 1526
1244 REQ_SEND; 1527 REQ_SEND;
1245} 1528}
1246 1529
1247void 1530void
1248aio_link (oldpath,newpath,callback=&PL_sv_undef) 1531aio_link (oldpath,newpath,callback=&PL_sv_undef)
1249 SV * oldpath 1532 SV * oldpath
1250 SV * newpath 1533 SV * newpath
1251 SV * callback 1534 SV * callback
1252 ALIAS: 1535 ALIAS:
1253 aio_link = REQ_LINK 1536 aio_link = REQ_LINK
1254 aio_symlink = REQ_SYMLINK 1537 aio_symlink = REQ_SYMLINK
1255 aio_rename = REQ_RENAME 1538 aio_rename = REQ_RENAME
1256 PPCODE: 1539 PPCODE:
1257{ 1540{
1258 dREQ; 1541 dREQ;
1259 1542
1260 req->type = ix; 1543 req->type = ix;
1261 req->fh = newSVsv (oldpath); 1544 req->fh = newSVsv (oldpath);
1262 req->data2ptr = SvPVbyte_nolen (req->fh); 1545 req->ptr2 = SvPVbyte_nolen (req->fh);
1263 req->data = newSVsv (newpath); 1546 req->sv1 = newSVsv (newpath);
1264 req->dataptr = SvPVbyte_nolen (req->data); 1547 req->ptr1 = SvPVbyte_nolen (newpath);
1265 1548
1266 REQ_SEND; 1549 REQ_SEND;
1267} 1550}
1268 1551
1269void 1552void
1553aio_mknod (pathname,mode,dev,callback=&PL_sv_undef)
1554 SV * pathname
1555 SV * callback
1556 UV mode
1557 UV dev
1558 PPCODE:
1559{
1560 dREQ;
1561
1562 req->type = REQ_MKNOD;
1563 req->sv1 = newSVsv (pathname);
1564 req->ptr1 = SvPVbyte_nolen (pathname);
1565 req->mode = (mode_t)mode;
1566 req->offs = dev;
1567
1568 REQ_SEND;
1569}
1570
1571void
1270aio_busy (delay,callback=&PL_sv_undef) 1572aio_busy (delay,callback=&PL_sv_undef)
1271 double delay 1573 double delay
1272 SV * callback 1574 SV * callback
1273 PPCODE: 1575 PPCODE:
1274{ 1576{
1275 dREQ; 1577 dREQ;
1276 1578
1277 req->type = REQ_BUSY; 1579 req->type = REQ_BUSY;
1278 req->fd = delay < 0. ? 0 : delay; 1580 req->int1 = delay < 0. ? 0 : delay;
1279 req->fd2 = delay < 0. ? 0 : 1000. * (delay - req->fd); 1581 req->int2 = delay < 0. ? 0 : 1000. * (delay - req->int1);
1280 1582
1281 REQ_SEND; 1583 REQ_SEND;
1282} 1584}
1283 1585
1284void 1586void
1288 PPCODE: 1590 PPCODE:
1289{ 1591{
1290 dREQ; 1592 dREQ;
1291 1593
1292 req->type = REQ_GROUP; 1594 req->type = REQ_GROUP;
1595
1293 req_send (req); 1596 req_send (req);
1294
1295 XPUSHs (req_sv (req, AIO_GRP_KLASS)); 1597 XPUSHs (req_sv (req, AIO_GRP_KLASS));
1296} 1598}
1297 1599
1298void 1600void
1299aio_nop (callback=&PL_sv_undef) 1601aio_nop (callback=&PL_sv_undef)
1300 SV * callback 1602 SV * callback
1301 PPCODE: 1603 PPCODE:
1302{ 1604{
1303 dREQ; 1605 dREQ;
1304 1606
1305 req->type = REQ_NOP; 1607 req->type = REQ_NOP;
1306 1608
1307 REQ_SEND; 1609 REQ_SEND;
1308} 1610}
1309 1611
1310void 1612int
1311aioreq_pri (int pri = DEFAULT_PRI) 1613aioreq_pri (int pri = 0)
1312 CODE: 1614 PROTOTYPE: ;$
1615 CODE:
1616 RETVAL = next_pri - PRI_BIAS;
1617 if (items > 0)
1618 {
1313 if (pri < PRI_MIN) pri = PRI_MIN; 1619 if (pri < PRI_MIN) pri = PRI_MIN;
1314 if (pri > PRI_MAX) pri = PRI_MAX; 1620 if (pri > PRI_MAX) pri = PRI_MAX;
1315 next_pri = pri + PRI_BIAS; 1621 next_pri = pri + PRI_BIAS;
1622 }
1623 OUTPUT:
1624 RETVAL
1316 1625
1317void 1626void
1318aioreq_nice (int nice = 0) 1627aioreq_nice (int nice = 0)
1319 CODE: 1628 CODE:
1320 nice = next_pri - nice; 1629 nice = next_pri - nice;
1321 if (nice < PRI_MIN) nice = PRI_MIN; 1630 if (nice < PRI_MIN) nice = PRI_MIN;
1322 if (nice > PRI_MAX) nice = PRI_MAX; 1631 if (nice > PRI_MAX) nice = PRI_MAX;
1323 next_pri = nice + PRI_BIAS; 1632 next_pri = nice + PRI_BIAS;
1324 1633
1325void 1634void
1326flush () 1635flush ()
1327 PROTOTYPE: 1636 PROTOTYPE:
1328 CODE: 1637 CODE:
1329 while (nreqs) 1638 while (nreqs)
1330 { 1639 {
1331 poll_wait (); 1640 poll_wait ();
1332 poll_cb (); 1641 poll_cb (0);
1333 } 1642 }
1334 1643
1335void 1644void
1336poll() 1645poll()
1337 PROTOTYPE: 1646 PROTOTYPE:
1338 CODE: 1647 CODE:
1339 if (nreqs) 1648 if (nreqs)
1340 { 1649 {
1341 poll_wait (); 1650 poll_wait ();
1342 poll_cb (); 1651 poll_cb (0);
1343 } 1652 }
1344 1653
1345int 1654int
1346poll_fileno() 1655poll_fileno()
1347 PROTOTYPE: 1656 PROTOTYPE:
1371 CODE: 1680 CODE:
1372 RETVAL = nreqs; 1681 RETVAL = nreqs;
1373 OUTPUT: 1682 OUTPUT:
1374 RETVAL 1683 RETVAL
1375 1684
1685int
1686nready()
1687 PROTOTYPE:
1688 CODE:
1689 RETVAL = get_nready ();
1690 OUTPUT:
1691 RETVAL
1692
1693int
1694npending()
1695 PROTOTYPE:
1696 CODE:
1697 RETVAL = get_npending ();
1698 OUTPUT:
1699 RETVAL
1700
1701int
1702nthreads()
1703 PROTOTYPE:
1704 CODE:
1705 if (WORDACCESS_UNSAFE) LOCK (wrklock);
1706 RETVAL = started;
1707 if (WORDACCESS_UNSAFE) UNLOCK (wrklock);
1708 OUTPUT:
1709 RETVAL
1710
1376PROTOTYPES: DISABLE 1711PROTOTYPES: DISABLE
1377 1712
1378MODULE = IO::AIO PACKAGE = IO::AIO::REQ 1713MODULE = IO::AIO PACKAGE = IO::AIO::REQ
1379 1714
1380void 1715void
1395 PPCODE: 1730 PPCODE:
1396{ 1731{
1397 int i; 1732 int i;
1398 aio_req req; 1733 aio_req req;
1399 1734
1400 if (grp->fd == 2) 1735 if (grp->int1 == 2)
1401 croak ("cannot add requests to IO::AIO::GRP after the group finished"); 1736 croak ("cannot add requests to IO::AIO::GRP after the group finished");
1402 1737
1403 for (i = 1; i < items; ++i ) 1738 for (i = 1; i < items; ++i )
1404 { 1739 {
1405 if (GIMME_V != G_VOID) 1740 if (GIMME_V != G_VOID)
1407 1742
1408 req = SvAIO_REQ (ST (i)); 1743 req = SvAIO_REQ (ST (i));
1409 1744
1410 if (req) 1745 if (req)
1411 { 1746 {
1412 ++grp->length; 1747 ++grp->size;
1413 req->grp = grp; 1748 req->grp = grp;
1414 1749
1415 req->grp_prev = 0; 1750 req->grp_prev = 0;
1416 req->grp_next = grp->grp_first; 1751 req->grp_next = grp->grp_first;
1417 1752
1422 } 1757 }
1423 } 1758 }
1424} 1759}
1425 1760
1426void 1761void
1762cancel_subs (aio_req_ornot req)
1763 CODE:
1764 req_cancel_subs (req);
1765
1766void
1427result (aio_req grp, ...) 1767result (aio_req grp, ...)
1428 CODE: 1768 CODE:
1429{ 1769{
1430 int i; 1770 int i;
1771 AV *av;
1772
1773 grp->errorno = errno;
1774
1431 AV *av = newAV (); 1775 av = newAV ();
1432 1776
1433 for (i = 1; i < items; ++i ) 1777 for (i = 1; i < items; ++i )
1434 av_push (av, newSVsv (ST (i))); 1778 av_push (av, newSVsv (ST (i)));
1435 1779
1436 SvREFCNT_dec (grp->data); 1780 SvREFCNT_dec (grp->sv1);
1437 grp->data = (SV *)av; 1781 grp->sv1 = (SV *)av;
1438} 1782}
1783
1784void
1785errno (aio_req grp, int errorno = errno)
1786 CODE:
1787 grp->errorno = errorno;
1439 1788
1440void 1789void
1441limit (aio_req grp, int limit) 1790limit (aio_req grp, int limit)
1442 CODE: 1791 CODE:
1443 grp->fd2 = limit; 1792 grp->int2 = limit;
1444 aio_grp_feed (grp); 1793 aio_grp_feed (grp);
1445 1794
1446void 1795void
1447feed (aio_req grp, SV *callback=&PL_sv_undef) 1796feed (aio_req grp, SV *callback=&PL_sv_undef)
1448 CODE: 1797 CODE:
1449{ 1798{
1450 SvREFCNT_dec (grp->fh2); 1799 SvREFCNT_dec (grp->sv2);
1451 grp->fh2 = newSVsv (callback); 1800 grp->sv2 = newSVsv (callback);
1452 1801
1453 if (grp->fd2 <= 0) 1802 if (grp->int2 <= 0)
1454 grp->fd2 = 2; 1803 grp->int2 = 2;
1455 1804
1456 aio_grp_feed (grp); 1805 aio_grp_feed (grp);
1457} 1806}
1458 1807

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines