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.49 by root, Sun Oct 22 13:33:28 2006 UTC vs.
Revision 1.84 by root, Sat Oct 28 01:24:19 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines