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.37 by root, Tue Aug 23 12:37:19 2005 UTC vs.
Revision 1.70 by root, Tue Oct 24 15:15:56 2006 UTC

1/* solaris */
2#define _POSIX_PTHREAD_SEMANTICS 1
3
4#if __linux
5# define _GNU_SOURCE
6#endif
7
1#define _REENTRANT 1 8#define _REENTRANT 1
9
2#include <errno.h> 10#include <errno.h>
3 11
4#include "EXTERN.h" 12#include "EXTERN.h"
5#include "perl.h" 13#include "perl.h"
6#include "XSUB.h" 14#include "XSUB.h"
8#include "autoconf/config.h" 16#include "autoconf/config.h"
9 17
10#include <pthread.h> 18#include <pthread.h>
11 19
12#include <stddef.h> 20#include <stddef.h>
21#include <errno.h>
22#include <sys/time.h>
23#include <sys/select.h>
13#include <sys/types.h> 24#include <sys/types.h>
14#include <sys/stat.h> 25#include <sys/stat.h>
15#include <limits.h> 26#include <limits.h>
16#include <unistd.h> 27#include <unistd.h>
17#include <fcntl.h> 28#include <fcntl.h>
31# else 42# else
32# error sendfile support requested but not available 43# error sendfile support requested but not available
33# endif 44# endif
34#endif 45#endif
35 46
47/* used for struct dirent, AIX doesn't provide it */
48#ifndef NAME_MAX
49# define NAME_MAX 4096
50#endif
51
36#if __ia64 52#if __ia64
37# define STACKSIZE 65536 53# define STACKSIZE 65536
54#elif __i386 || __x86_64 /* 16k is unreasonably high :( */
55# define STACKSIZE PTHREAD_STACK_MIN
38#else 56#else
39# define STACKSIZE 8192 57# define STACKSIZE 16384
40#endif 58#endif
59
60/* buffer size for various temporary buffers */
61#define AIO_BUFSIZE 65536
62
63#define dBUF \
64 char *aio_buf = malloc (AIO_BUFSIZE); \
65 if (!aio_buf) \
66 return -1;
67
68#define fBUF free (aio_buf)
41 69
42enum { 70enum {
43 REQ_QUIT, 71 REQ_QUIT,
44 REQ_OPEN, REQ_CLOSE, 72 REQ_OPEN, REQ_CLOSE,
45 REQ_READ, REQ_WRITE, REQ_READAHEAD, 73 REQ_READ, REQ_WRITE, REQ_READAHEAD,
46 REQ_SENDFILE, 74 REQ_SENDFILE,
47 REQ_STAT, REQ_LSTAT, REQ_FSTAT, 75 REQ_STAT, REQ_LSTAT, REQ_FSTAT,
48 REQ_FSYNC, REQ_FDATASYNC, 76 REQ_FSYNC, REQ_FDATASYNC,
49 REQ_UNLINK, REQ_RMDIR, 77 REQ_UNLINK, REQ_RMDIR, REQ_RENAME,
50 REQ_READDIR, 78 REQ_READDIR,
51 REQ_SYMLINK, 79 REQ_LINK, REQ_SYMLINK,
80 REQ_GROUP, REQ_NOP,
81 REQ_BUSY,
52}; 82};
53 83
84#define AIO_REQ_KLASS "IO::AIO::REQ"
85#define AIO_GRP_KLASS "IO::AIO::GRP"
86
54typedef struct aio_cb { 87typedef struct aio_cb
88{
55 struct aio_cb *volatile next; 89 struct aio_cb *volatile next;
56 90
57 int type; 91 SV *data, *callback;
58 92 SV *fh, *fh2;
59 /* should receive a cleanup, with unions */ 93 void *dataptr, *data2ptr;
60 int fd, fd2; 94 Stat_t *statdata;
61 off_t offset; 95 off_t offset;
62 size_t length; 96 size_t length;
63 ssize_t result; 97 ssize_t result;
98
99 STRLEN dataoffset;
100 int type;
101 int fd, fd2;
102 int errorno;
64 mode_t mode; /* open */ 103 mode_t mode; /* open */
65 int errorno;
66 SV *data, *callback;
67 SV *fh, *fh2;
68 void *dataptr, *data2ptr;
69 STRLEN dataoffset;
70 104
71 Stat_t *statdata; 105 unsigned char flags;
106 unsigned char pri;
107
108 SV *self; /* the perl counterpart of this request, if any */
109 struct aio_cb *grp, *grp_prev, *grp_next, *grp_first;
72} aio_cb; 110} aio_cb;
73 111
112enum {
113 FLAG_CANCELLED = 0x01,
114};
115
74typedef aio_cb *aio_req; 116typedef aio_cb *aio_req;
117typedef aio_cb *aio_req_ornot;
118
119enum {
120 PRI_MIN = -4,
121 PRI_MAX = 4,
122
123 DEFAULT_PRI = 0,
124 PRI_BIAS = -PRI_MIN,
125 NUM_PRI = PRI_MAX + PRI_BIAS + 1,
126};
127
128static int next_pri = DEFAULT_PRI + PRI_BIAS;
75 129
76static int started, wanted; 130static int started, wanted;
77static volatile int nreqs; 131static volatile int nreqs;
78static int max_outstanding = 1<<30; 132static int max_outstanding = 1<<30;
79static int respipe [2]; 133static int respipe [2];
80 134
135#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP)
136# define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
137#else
138# define AIO_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
139#endif
140
81static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; 141static pthread_mutex_t reslock = AIO_MUTEX_INIT;
82static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; 142static pthread_mutex_t reqlock = AIO_MUTEX_INIT;
83static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; 143static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER;
84 144
85static volatile aio_req reqs, reqe; /* queue start, queue end */ 145/*
86static volatile aio_req ress, rese; /* queue start, queue end */ 146 * a somewhat faster data structure might be nice, but
147 * with 8 priorities this actually needs <20 insns
148 * per shift, the most expensive operation.
149 */
150typedef struct {
151 aio_req qs[NUM_PRI], qe[NUM_PRI]; /* qstart, qend */
152 int size;
153} reqq;
87 154
155static reqq req_queue;
156static reqq res_queue;
157
158int reqq_push (reqq *q, aio_req req)
159{
160 int pri = req->pri;
161 req->next = 0;
162
163 if (q->qe[pri])
164 {
165 q->qe[pri]->next = req;
166 q->qe[pri] = req;
167 }
168 else
169 q->qe[pri] = q->qs[pri] = req;
170
171 return q->size++;
172}
173
174aio_req reqq_shift (reqq *q)
175{
176 int pri;
177
178 if (!q->size)
179 return 0;
180
181 --q->size;
182
183 for (pri = NUM_PRI; pri--; )
184 {
185 aio_req req = q->qs[pri];
186
187 if (req)
188 {
189 if (!(q->qs[pri] = req->next))
190 q->qe[pri] = 0;
191
192 return req;
193 }
194 }
195
196 abort ();
197}
198
199static void req_invoke (aio_req req);
200static void req_free (aio_req req);
201
202/* must be called at most once */
203static SV *req_sv (aio_req req, const char *klass)
204{
205 if (!req->self)
206 {
207 req->self = (SV *)newHV ();
208 sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0);
209 }
210
211 return sv_2mortal (sv_bless (newRV_inc (req->self), gv_stashpv (klass, 1)));
212}
213
214static aio_req SvAIO_REQ (SV *sv)
215{
216 MAGIC *mg;
217
218 if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv))
219 croak ("object of class " AIO_REQ_KLASS " expected");
220
221 mg = mg_find (SvRV (sv), PERL_MAGIC_ext);
222
223 return mg ? (aio_req)mg->mg_ptr : 0;
224}
225
226static void aio_grp_feed (aio_req grp)
227{
228 while (grp->length < grp->fd2 && !(grp->flags & FLAG_CANCELLED))
229 {
230 int old_len = grp->length;
231
232 if (grp->fh2 && SvOK (grp->fh2))
233 {
234 dSP;
235
236 ENTER;
237 SAVETMPS;
238 PUSHMARK (SP);
239 XPUSHs (req_sv (grp, AIO_GRP_KLASS));
240 PUTBACK;
241 call_sv (grp->fh2, G_VOID | G_EVAL | G_KEEPERR);
242 SPAGAIN;
243 FREETMPS;
244 LEAVE;
245 }
246
247 /* stop if no progress has been made */
248 if (old_len == grp->length)
249 {
250 SvREFCNT_dec (grp->fh2);
251 grp->fh2 = 0;
252 break;
253 }
254 }
255}
256
257static void aio_grp_dec (aio_req grp)
258{
259 --grp->length;
260
261 /* call feeder, if applicable */
262 aio_grp_feed (grp);
263
264 /* finish, if done */
265 if (!grp->length && grp->fd)
266 {
267 req_invoke (grp);
268 req_free (grp);
269 }
270}
271
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)
298{
299 dSP;
300
301 if (!(req->flags & FLAG_CANCELLED) && SvOK (req->callback))
302 {
303 errno = req->errorno;
304
305 ENTER;
306 SAVETMPS;
307 PUSHMARK (SP);
308 EXTEND (SP, 1);
309
310 switch (req->type)
311 {
312 case REQ_READDIR:
313 {
314 SV *rv = &PL_sv_undef;
315
316 if (req->result >= 0)
317 {
318 char *buf = req->data2ptr;
319 AV *av = newAV ();
320
321 while (req->result)
322 {
323 SV *sv = newSVpv (buf, 0);
324
325 av_push (av, sv);
326 buf += SvCUR (sv) + 1;
327 req->result--;
328 }
329
330 rv = sv_2mortal (newRV_noinc ((SV *)av));
331 }
332
333 PUSHs (rv);
334 }
335 break;
336
337 case REQ_OPEN:
338 {
339 /* convert fd to fh */
340 SV *fh;
341
342 PUSHs (sv_2mortal (newSViv (req->result)));
343 PUTBACK;
344 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL);
345 SPAGAIN;
346
347 fh = SvREFCNT_inc (POPs);
348
349 PUSHMARK (SP);
350 XPUSHs (sv_2mortal (fh));
351 }
352 break;
353
354 case REQ_GROUP:
355 req->fd = 2; /* mark group as finished */
356
357 if (req->data)
358 {
359 int i;
360 AV *av = (AV *)req->data;
361
362 EXTEND (SP, AvFILL (av) + 1);
363 for (i = 0; i <= AvFILL (av); ++i)
364 PUSHs (*av_fetch (av, i, 0));
365 }
366 break;
367
368 case REQ_NOP:
369 case REQ_BUSY:
370 break;
371
372 default:
373 PUSHs (sv_2mortal (newSViv (req->result)));
374 break;
375 }
376
377
378 PUTBACK;
379 call_sv (req->callback, G_VOID | G_EVAL);
380 SPAGAIN;
381
382 FREETMPS;
383 LEAVE;
384 }
385
386 if (req->grp)
387 {
388 aio_req grp = req->grp;
389
390 /* unlink request */
391 if (req->grp_next) req->grp_next->grp_prev = req->grp_prev;
392 if (req->grp_prev) req->grp_prev->grp_next = req->grp_next;
393
394 if (grp->grp_first == req)
395 grp->grp_first = req->grp_next;
396
397 aio_grp_dec (grp);
398 }
399
400 if (SvTRUE (ERRSV))
401 {
402 req_free (req);
403 croak (0);
404 }
405}
406
88static void free_req (aio_req req) 407static void req_free (aio_req req)
89{ 408{
90 if (req->data) 409 if (req->self)
410 {
411 sv_unmagic (req->self, PERL_MAGIC_ext);
412 SvREFCNT_dec (req->self);
413 }
414
91 SvREFCNT_dec (req->data); 415 SvREFCNT_dec (req->data);
92
93 if (req->fh)
94 SvREFCNT_dec (req->fh); 416 SvREFCNT_dec (req->fh);
95
96 if (req->fh2)
97 SvREFCNT_dec (req->fh2); 417 SvREFCNT_dec (req->fh2);
98
99 if (req->statdata)
100 Safefree (req->statdata);
101
102 if (req->callback)
103 SvREFCNT_dec (req->callback); 418 SvREFCNT_dec (req->callback);
419 Safefree (req->statdata);
104 420
105 if (req->type == REQ_READDIR && req->result >= 0) 421 if (req->type == REQ_READDIR && req->result >= 0)
106 free (req->data2ptr); 422 free (req->data2ptr);
107 423
108 Safefree (req); 424 Safefree (req);
109} 425}
110 426
111static void 427static void req_cancel (aio_req req)
112poll_wait ()
113{ 428{
114 if (nreqs && !ress) 429 req->flags |= FLAG_CANCELLED;
115 {
116 fd_set rfd;
117 FD_ZERO(&rfd);
118 FD_SET(respipe [0], &rfd);
119 430
120 select (respipe [0] + 1, &rfd, 0, 0, 0); 431 if (req->type == REQ_GROUP)
121 } 432 {
122} 433 aio_req sub;
123 434
124static int 435 for (sub = req->grp_first; sub; sub = sub->grp_next)
125poll_cb () 436 req_cancel (sub);
437 }
438}
439
440static int poll_cb ()
126{ 441{
127 dSP; 442 dSP;
128 int count = 0; 443 int count = 0;
129 int do_croak = 0; 444 int do_croak = 0;
130 aio_req req; 445 aio_req req;
131 446
132 for (;;) 447 for (;;)
133 { 448 {
134 pthread_mutex_lock (&reslock); 449 pthread_mutex_lock (&reslock);
135 req = ress; 450 req = reqq_shift (&res_queue);
136 451
137 if (req) 452 if (req)
138 { 453 {
139 ress = req->next;
140
141 if (!ress) 454 if (!res_queue.size)
142 { 455 {
143 /* read any signals sent by the worker threads */ 456 /* read any signals sent by the worker threads */
144 char buf [32]; 457 char buf [32];
145 while (read (respipe [0], buf, 32) == 32) 458 while (read (respipe [0], buf, 32) == 32)
146 ; 459 ;
147
148 rese = 0;
149 } 460 }
150 } 461 }
151 462
152 pthread_mutex_unlock (&reslock); 463 pthread_mutex_unlock (&reslock);
153 464
154 if (!req) 465 if (!req)
155 break; 466 break;
156 467
157 nreqs--; 468 --nreqs;
158 469
159 if (req->type == REQ_QUIT) 470 if (req->type == REQ_QUIT)
160 started--; 471 started--;
472 else if (req->type == REQ_GROUP && req->length)
473 {
474 req->fd = 1; /* mark request as delayed */
475 continue;
476 }
161 else 477 else
162 { 478 {
163 int errorno = errno;
164 errno = req->errorno;
165
166 if (req->type == REQ_READ) 479 if (req->type == REQ_READ)
167 SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); 480 SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0));
168 481
169 if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE)) 482 if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE))
170 SvREADONLY_off (req->data); 483 SvREADONLY_off (req->data);
174 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; 487 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
175 PL_laststatval = req->result; 488 PL_laststatval = req->result;
176 PL_statcache = *(req->statdata); 489 PL_statcache = *(req->statdata);
177 } 490 }
178 491
179 ENTER; 492 req_invoke (req);
180 PUSHMARK (SP);
181 493
182 if (req->type == REQ_READDIR)
183 {
184 SV *rv = &PL_sv_undef;
185
186 if (req->result >= 0)
187 {
188 char *buf = req->data2ptr;
189 AV *av = newAV ();
190
191 while (req->result)
192 {
193 SV *sv = newSVpv (buf, 0);
194
195 av_push (av, sv);
196 buf += SvCUR (sv) + 1;
197 req->result--;
198 }
199
200 rv = sv_2mortal (newRV_noinc ((SV *)av));
201 }
202
203 XPUSHs (rv);
204 }
205 else
206 {
207 XPUSHs (sv_2mortal (newSViv (req->result)));
208
209 if (req->type == REQ_OPEN)
210 {
211 /* convert fd to fh */
212 SV *fh;
213
214 PUTBACK;
215 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL);
216 SPAGAIN;
217
218 fh = SvREFCNT_inc (POPs);
219
220 PUSHMARK (SP);
221 XPUSHs (sv_2mortal (fh));
222 }
223 }
224
225 if (SvOK (req->callback))
226 {
227 PUTBACK;
228 call_sv (req->callback, G_VOID | G_EVAL);
229 SPAGAIN;
230
231 if (SvTRUE (ERRSV))
232 {
233 free_req (req);
234 croak (0);
235 }
236 }
237
238 LEAVE;
239
240 errno = errorno;
241 count++; 494 count++;
242 } 495 }
243 496
244 free_req (req); 497 req_free (req);
245 } 498 }
246 499
247 return count; 500 return count;
248} 501}
249 502
250static void *aio_proc(void *arg); 503static void *aio_proc(void *arg);
251 504
252static void
253start_thread (void) 505static void start_thread (void)
254{ 506{
255 sigset_t fullsigset, oldsigset; 507 sigset_t fullsigset, oldsigset;
256 pthread_t tid; 508 pthread_t tid;
257 pthread_attr_t attr; 509 pthread_attr_t attr;
258 510
267 started++; 519 started++;
268 520
269 sigprocmask (SIG_SETMASK, &oldsigset, 0); 521 sigprocmask (SIG_SETMASK, &oldsigset, 0);
270} 522}
271 523
272static void 524static void req_send (aio_req req)
273send_req (aio_req req)
274{ 525{
275 while (started < wanted && nreqs >= started) 526 while (started < wanted && nreqs >= started)
276 start_thread (); 527 start_thread ();
277 528
278 nreqs++; 529 ++nreqs;
279 530
280 pthread_mutex_lock (&reqlock); 531 pthread_mutex_lock (&reqlock);
281 532 reqq_push (&req_queue, req);
282 req->next = 0;
283
284 if (reqe)
285 {
286 reqe->next = req;
287 reqe = req;
288 }
289 else
290 reqe = reqs = req;
291
292 pthread_cond_signal (&reqwait); 533 pthread_cond_signal (&reqwait);
293 pthread_mutex_unlock (&reqlock); 534 pthread_mutex_unlock (&reqlock);
294 535
295 if (nreqs > max_outstanding) 536 if (nreqs > max_outstanding)
296 for (;;) 537 for (;;)
302 543
303 poll_wait (); 544 poll_wait ();
304 } 545 }
305} 546}
306 547
307static void 548static void end_thread (void)
308end_thread (void)
309{ 549{
310 aio_req req; 550 aio_req req;
551
311 Newz (0, req, 1, aio_cb); 552 Newz (0, req, 1, aio_cb);
553
312 req->type = REQ_QUIT; 554 req->type = REQ_QUIT;
555 req->pri = PRI_MAX + PRI_BIAS;
313 556
314 send_req (req); 557 req_send (req);
315} 558}
316 559
317static void min_parallel (int nthreads) 560static void min_parallel (int nthreads)
318{ 561{
319 if (wanted < nthreads) 562 if (wanted < nthreads)
364 * normal read/write by using a mutex. slows down execution a lot, 607 * normal read/write by using a mutex. slows down execution a lot,
365 * but that's your problem, not mine. 608 * but that's your problem, not mine.
366 */ 609 */
367static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER; 610static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER;
368 611
369static ssize_t 612static ssize_t pread (int fd, void *buf, size_t count, off_t offset)
370pread (int fd, void *buf, size_t count, off_t offset)
371{ 613{
372 ssize_t res; 614 ssize_t res;
373 off_t ooffset; 615 off_t ooffset;
374 616
375 pthread_mutex_lock (&preadwritelock); 617 pthread_mutex_lock (&preadwritelock);
380 pthread_mutex_unlock (&preadwritelock); 622 pthread_mutex_unlock (&preadwritelock);
381 623
382 return res; 624 return res;
383} 625}
384 626
385static ssize_t
386pwrite (int fd, void *buf, size_t count, off_t offset) 627static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset)
387{ 628{
388 ssize_t res; 629 ssize_t res;
389 off_t ooffset; 630 off_t ooffset;
390 631
391 pthread_mutex_lock (&preadwritelock); 632 pthread_mutex_lock (&preadwritelock);
404#endif 645#endif
405 646
406#if !HAVE_READAHEAD 647#if !HAVE_READAHEAD
407# define readahead aio_readahead 648# define readahead aio_readahead
408 649
409static ssize_t
410readahead (int fd, off_t offset, size_t count) 650static ssize_t readahead (int fd, off_t offset, size_t count)
411{ 651{
412 char readahead_buf[4096]; 652 dBUF;
413 653
414 while (count > 0) 654 while (count > 0)
415 { 655 {
416 size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); 656 size_t len = count < AIO_BUFSIZE ? count : AIO_BUFSIZE;
417 657
418 pread (fd, readahead_buf, len, offset); 658 pread (fd, aio_buf, len, offset);
419 offset += len; 659 offset += len;
420 count -= len; 660 count -= len;
421 } 661 }
422 662
663 fBUF;
664
423 errno = 0; 665 errno = 0;
424} 666}
425#endif 667#endif
426 668
427#if !HAVE_READDIR_R 669#if !HAVE_READDIR_R
428# define readdir_r aio_readdir_r 670# define readdir_r aio_readdir_r
429 671
430static pthread_mutex_t readdirlock = PTHREAD_MUTEX_INITIALIZER; 672static pthread_mutex_t readdirlock = PTHREAD_MUTEX_INITIALIZER;
431 673
432static int
433readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) 674static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res)
434{ 675{
435 struct dirent *e; 676 struct dirent *e;
436 int errorno; 677 int errorno;
437 678
438 pthread_mutex_lock (&readdirlock); 679 pthread_mutex_lock (&readdirlock);
454 return e ? 0 : -1; 695 return e ? 0 : -1;
455} 696}
456#endif 697#endif
457 698
458/* sendfile always needs emulation */ 699/* sendfile always needs emulation */
459static ssize_t
460sendfile_ (int ofd, int ifd, off_t offset, size_t count) 700static ssize_t sendfile_ (int ofd, int ifd, off_t offset, size_t count)
461{ 701{
462 ssize_t res; 702 ssize_t res;
463 703
464 if (!count) 704 if (!count)
465 return 0; 705 return 0;
499 739
500 if (res < 0 && sbytes) 740 if (res < 0 && sbytes)
501 res = sbytes; 741 res = sbytes;
502 } 742 }
503 743
744# endif
504# else 745#else
505 res = -1; 746 res = -1;
506 errno = ENOSYS; 747 errno = ENOSYS;
507# endif
508#endif 748#endif
509 749
510 if (res < 0 750 if (res < 0
511 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK 751 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK
512#if __solaris 752#if __solaris
514#endif 754#endif
515 ) 755 )
516 ) 756 )
517 { 757 {
518 /* emulate sendfile. this is a major pain in the ass */ 758 /* emulate sendfile. this is a major pain in the ass */
519 char buf[4096]; 759 dBUF;
760
520 res = 0; 761 res = 0;
521 762
522 for (;;) 763 while (count)
523 { 764 {
524 ssize_t cnt; 765 ssize_t cnt;
525 766
526 cnt = pread (ifd, buf, 4096, offset); 767 cnt = pread (ifd, aio_buf, count > AIO_BUFSIZE ? AIO_BUFSIZE : count, offset);
527 768
528 if (cnt <= 0) 769 if (cnt <= 0)
529 { 770 {
530 if (cnt && !res) res = -1; 771 if (cnt && !res) res = -1;
531 break; 772 break;
532 } 773 }
533 774
534 cnt = write (ofd, buf, cnt); 775 cnt = write (ofd, aio_buf, cnt);
535 776
536 if (cnt <= 0) 777 if (cnt <= 0)
537 { 778 {
538 if (cnt && !res) res = -1; 779 if (cnt && !res) res = -1;
539 break; 780 break;
540 } 781 }
541 782
542 offset += cnt; 783 offset += cnt;
543 res += cnt; 784 res += cnt;
785 count -= cnt;
544 } 786 }
787
788 fBUF;
545 } 789 }
546 790
547 return res; 791 return res;
548} 792}
549 793
550/* read a full directory */ 794/* read a full directory */
551static int
552scandir_ (const char *path, void **namesp) 795static int scandir_ (const char *path, void **namesp)
553{ 796{
554 DIR *dirp = opendir (path); 797 DIR *dirp;
555 union 798 union
556 { 799 {
557 struct dirent d; 800 struct dirent d;
558 char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; 801 char b [offsetof (struct dirent, d_name) + NAME_MAX + 1];
559 } u; 802 } *u;
560 struct dirent *entp; 803 struct dirent *entp;
561 char *name, *names; 804 char *name, *names;
562 int memlen = 4096; 805 int memlen = 4096;
563 int memofs = 0; 806 int memofs = 0;
564 int res = 0; 807 int res = 0;
565 int errorno; 808 int errorno;
566 809
810 dirp = opendir (path);
567 if (!dirp) 811 if (!dirp)
568 return -1; 812 return -1;
569 813
814 u = malloc (sizeof (*u));
570 names = malloc (memlen); 815 names = malloc (memlen);
571 816
817 if (u && names)
572 for (;;) 818 for (;;)
573 { 819 {
820 errno = 0;
574 errno = 0, readdir_r (dirp, &u.d, &entp); 821 readdir_r (dirp, &u->d, &entp);
575 822
576 if (!entp) 823 if (!entp)
577 break; 824 break;
578 825
579 name = entp->d_name; 826 name = entp->d_name;
580 827
581 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) 828 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2])))
582 { 829 {
583 int len = strlen (name) + 1; 830 int len = strlen (name) + 1;
584 831
585 res++; 832 res++;
586 833
587 while (memofs + len > memlen) 834 while (memofs + len > memlen)
588 { 835 {
589 memlen *= 2; 836 memlen *= 2;
590 names = realloc (names, memlen); 837 names = realloc (names, memlen);
591 if (!names) 838 if (!names)
592 break; 839 break;
593 } 840 }
594 841
595 memcpy (names + memofs, name, len); 842 memcpy (names + memofs, name, len);
596 memofs += len; 843 memofs += len;
597 } 844 }
598 } 845 }
599 846
600 errorno = errno; 847 errorno = errno;
848 free (u);
601 closedir (dirp); 849 closedir (dirp);
602 850
603 if (errorno) 851 if (errorno)
604 { 852 {
605 free (names); 853 free (names);
611 return res; 859 return res;
612} 860}
613 861
614/*****************************************************************************/ 862/*****************************************************************************/
615 863
616static void *
617aio_proc (void *thr_arg) 864static void *aio_proc (void *thr_arg)
618{ 865{
619 aio_req req; 866 aio_req req;
620 int type; 867 int type;
621 868
622 do 869 do
623 { 870 {
624 pthread_mutex_lock (&reqlock); 871 pthread_mutex_lock (&reqlock);
625 872
626 for (;;) 873 for (;;)
627 { 874 {
628 req = reqs; 875 req = reqq_shift (&req_queue);
629
630 if (reqs)
631 {
632 reqs = reqs->next;
633 if (!reqs) reqe = 0;
634 }
635 876
636 if (req) 877 if (req)
637 break; 878 break;
638 879
639 pthread_cond_wait (&reqwait, &reqlock); 880 pthread_cond_wait (&reqwait, &reqlock);
640 } 881 }
641 882
642 pthread_mutex_unlock (&reqlock); 883 pthread_mutex_unlock (&reqlock);
643 884
644 errno = 0; /* strictly unnecessary */ 885 errno = 0; /* strictly unnecessary */
886 type = req->type; /* remember type for QUIT check */
645 887
646 type = req->type; 888 if (!(req->flags & FLAG_CANCELLED))
647
648 switch (type) 889 switch (type)
649 { 890 {
650 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; 891 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break;
651 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; 892 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break;
652 893
653 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; 894 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break;
654 case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length); break; 895 case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length); break;
655 896
656 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; 897 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break;
657 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; 898 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break;
658 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; 899 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break;
659 900
660 case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; 901 case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break;
661 case REQ_CLOSE: req->result = close (req->fd); break; 902 case REQ_CLOSE: req->result = close (req->fd); break;
662 case REQ_UNLINK: req->result = unlink (req->dataptr); break; 903 case REQ_UNLINK: req->result = unlink (req->dataptr); break;
663 case REQ_RMDIR: req->result = rmdir (req->dataptr); break; 904 case REQ_RMDIR: req->result = rmdir (req->dataptr); break;
905 case REQ_RENAME: req->result = rename (req->data2ptr, req->dataptr); break;
906 case REQ_LINK: req->result = link (req->data2ptr, req->dataptr); break;
664 case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; 907 case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break;
665 908
666 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; 909 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break;
667 case REQ_FSYNC: req->result = fsync (req->fd); break; 910 case REQ_FSYNC: req->result = fsync (req->fd); break;
668 case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break; 911 case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break;
669 912
913 case REQ_BUSY:
914 {
915 struct timeval tv;
916
917 tv.tv_sec = req->fd;
918 tv.tv_usec = req->fd2;
919
920 req->result = select (0, 0, 0, 0, &tv);
921 }
922
923 case REQ_GROUP:
924 case REQ_NOP:
670 case REQ_QUIT: 925 case REQ_QUIT:
671 break; 926 break;
672 927
673 default: 928 default:
674 req->result = ENOSYS; 929 req->result = ENOSYS;
675 break; 930 break;
676 } 931 }
677 932
678 req->errorno = errno; 933 req->errorno = errno;
679 934
680 pthread_mutex_lock (&reslock); 935 pthread_mutex_lock (&reslock);
681 936
682 req->next = 0; 937 if (!reqq_push (&res_queue, req))
683
684 if (rese)
685 {
686 rese->next = req;
687 rese = req;
688 }
689 else
690 {
691 rese = ress = req;
692
693 /* write a dummy byte to the pipe so fh becomes ready */ 938 /* write a dummy byte to the pipe so fh becomes ready */
694 write (respipe [1], &respipe, 1); 939 write (respipe [1], &respipe, 1);
695 }
696 940
697 pthread_mutex_unlock (&reslock); 941 pthread_mutex_unlock (&reslock);
698 } 942 }
699 while (type != REQ_QUIT); 943 while (type != REQ_QUIT);
700 944
731{ 975{
732 aio_req prv; 976 aio_req prv;
733 977
734 started = 0; 978 started = 0;
735 979
736 while (reqs) 980 while (prv = reqq_shift (&req_queue))
737 {
738 prv = reqs;
739 reqs = prv->next;
740 free_req (prv); 981 req_free (prv);
741 }
742 982
743 reqs = reqe = 0; 983 while (prv = reqq_shift (&res_queue))
744
745 while (ress)
746 {
747 prv = ress;
748 ress = prv->next;
749 free_req (prv); 984 req_free (prv);
750 } 985
751
752 ress = rese = 0;
753
754 close (respipe [0]); 986 close (respipe [0]);
755 close (respipe [1]); 987 close (respipe [1]);
756 create_pipe (); 988 create_pipe ();
757 989
758 atfork_parent (); 990 atfork_parent ();
759} 991}
760 992
761#define dREQ \ 993#define dREQ \
762 aio_req req; \ 994 aio_req req; \
995 int req_pri = next_pri; \
996 next_pri = DEFAULT_PRI + PRI_BIAS; \
763 \ 997 \
764 if (SvOK (callback) && !SvROK (callback)) \ 998 if (SvOK (callback) && !SvROK (callback)) \
765 croak ("clalback must be undef or of reference type"); \ 999 croak ("callback must be undef or of reference type"); \
766 \ 1000 \
767 Newz (0, req, 1, aio_cb); \ 1001 Newz (0, req, 1, aio_cb); \
768 if (!req) \ 1002 if (!req) \
769 croak ("out of memory during aio_req allocation"); \ 1003 croak ("out of memory during aio_req allocation"); \
770 \ 1004 \
771 req->callback = newSVsv (callback); 1005 req->callback = newSVsv (callback); \
1006 req->pri = req_pri
1007
1008#define REQ_SEND \
1009 req_send (req); \
1010 \
1011 if (GIMME_V != G_VOID) \
1012 XPUSHs (req_sv (req, AIO_REQ_KLASS));
772 1013
773MODULE = IO::AIO PACKAGE = IO::AIO 1014MODULE = IO::AIO PACKAGE = IO::AIO
774 1015
775PROTOTYPES: ENABLE 1016PROTOTYPES: ENABLE
776 1017
777BOOT: 1018BOOT:
778{ 1019{
1020 HV *stash = gv_stashpv ("IO::AIO", 1);
1021 newCONSTSUB (stash, "EXDEV", newSViv (EXDEV));
1022 newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY));
1023 newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY));
1024
779 create_pipe (); 1025 create_pipe ();
780 pthread_atfork (atfork_prepare, atfork_parent, atfork_child); 1026 pthread_atfork (atfork_prepare, atfork_parent, atfork_child);
781} 1027}
782 1028
783void 1029void
784min_parallel(nthreads) 1030min_parallel (nthreads)
785 int nthreads 1031 int nthreads
786 PROTOTYPE: $ 1032 PROTOTYPE: $
787 1033
788void 1034void
789max_parallel(nthreads) 1035max_parallel (nthreads)
790 int nthreads 1036 int nthreads
791 PROTOTYPE: $ 1037 PROTOTYPE: $
792 1038
793int 1039int
794max_outstanding(nreqs) 1040max_outstanding (nreqs)
795 int nreqs 1041 int nreqs
796 PROTOTYPE: $ 1042 PROTOTYPE: $
797 CODE: 1043 CODE:
798 RETVAL = max_outstanding; 1044 RETVAL = max_outstanding;
799 max_outstanding = nreqs; 1045 max_outstanding = nreqs;
800 1046
801void 1047void
802aio_open(pathname,flags,mode,callback=&PL_sv_undef) 1048aio_open (pathname,flags,mode,callback=&PL_sv_undef)
803 SV * pathname 1049 SV * pathname
804 int flags 1050 int flags
805 int mode 1051 int mode
806 SV * callback 1052 SV * callback
807 PROTOTYPE: $$$;$ 1053 PROTOTYPE: $$$;$
808 CODE: 1054 PPCODE:
809{ 1055{
810 dREQ; 1056 dREQ;
811 1057
812 req->type = REQ_OPEN; 1058 req->type = REQ_OPEN;
813 req->data = newSVsv (pathname); 1059 req->data = newSVsv (pathname);
814 req->dataptr = SvPVbyte_nolen (req->data); 1060 req->dataptr = SvPVbyte_nolen (req->data);
815 req->fd = flags; 1061 req->fd = flags;
816 req->mode = mode; 1062 req->mode = mode;
817 1063
818 send_req (req); 1064 REQ_SEND;
819} 1065}
820 1066
821void 1067void
822aio_close(fh,callback=&PL_sv_undef) 1068aio_close (fh,callback=&PL_sv_undef)
823 SV * fh 1069 SV * fh
824 SV * callback 1070 SV * callback
825 PROTOTYPE: $;$ 1071 PROTOTYPE: $;$
826 ALIAS: 1072 ALIAS:
827 aio_close = REQ_CLOSE 1073 aio_close = REQ_CLOSE
828 aio_fsync = REQ_FSYNC 1074 aio_fsync = REQ_FSYNC
829 aio_fdatasync = REQ_FDATASYNC 1075 aio_fdatasync = REQ_FDATASYNC
830 CODE: 1076 PPCODE:
831{ 1077{
832 dREQ; 1078 dREQ;
833 1079
834 req->type = ix; 1080 req->type = ix;
835 req->fh = newSVsv (fh); 1081 req->fh = newSVsv (fh);
836 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); 1082 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh)));
837 1083
838 send_req (req); 1084 REQ_SEND (req);
839} 1085}
840 1086
841void 1087void
842aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) 1088aio_read (fh,offset,length,data,dataoffset,callback=&PL_sv_undef)
843 SV * fh 1089 SV * fh
844 UV offset 1090 UV offset
845 UV length 1091 UV length
846 SV * data 1092 SV * data
847 UV dataoffset 1093 UV dataoffset
848 SV * callback 1094 SV * callback
849 ALIAS: 1095 ALIAS:
850 aio_read = REQ_READ 1096 aio_read = REQ_READ
851 aio_write = REQ_WRITE 1097 aio_write = REQ_WRITE
852 PROTOTYPE: $$$$$;$ 1098 PROTOTYPE: $$$$$;$
853 CODE: 1099 PPCODE:
854{ 1100{
855 aio_req req; 1101 aio_req req;
856 STRLEN svlen; 1102 STRLEN svlen;
857 char *svptr = SvPVbyte (data, svlen); 1103 char *svptr = SvPVbyte (data, svlen);
858 1104
896 { 1142 {
897 SvREADONLY_on (data); 1143 SvREADONLY_on (data);
898 req->data2ptr = (void *)data; 1144 req->data2ptr = (void *)data;
899 } 1145 }
900 1146
901 send_req (req); 1147 REQ_SEND;
902 } 1148 }
903} 1149}
904 1150
905void 1151void
906aio_sendfile(out_fh,in_fh,in_offset,length,callback=&PL_sv_undef) 1152aio_sendfile (out_fh,in_fh,in_offset,length,callback=&PL_sv_undef)
907 SV * out_fh 1153 SV * out_fh
908 SV * in_fh 1154 SV * in_fh
909 UV in_offset 1155 UV in_offset
910 UV length 1156 UV length
911 SV * callback 1157 SV * callback
912 PROTOTYPE: $$$$;$ 1158 PROTOTYPE: $$$$;$
913 CODE: 1159 PPCODE:
914{ 1160{
915 dREQ; 1161 dREQ;
916 1162
917 req->type = REQ_SENDFILE; 1163 req->type = REQ_SENDFILE;
918 req->fh = newSVsv (out_fh); 1164 req->fh = newSVsv (out_fh);
920 req->fh2 = newSVsv (in_fh); 1166 req->fh2 = newSVsv (in_fh);
921 req->fd2 = PerlIO_fileno (IoIFP (sv_2io (in_fh))); 1167 req->fd2 = PerlIO_fileno (IoIFP (sv_2io (in_fh)));
922 req->offset = in_offset; 1168 req->offset = in_offset;
923 req->length = length; 1169 req->length = length;
924 1170
925 send_req (req); 1171 REQ_SEND;
926} 1172}
927 1173
928void 1174void
929aio_readahead(fh,offset,length,callback=&PL_sv_undef) 1175aio_readahead (fh,offset,length,callback=&PL_sv_undef)
930 SV * fh 1176 SV * fh
931 UV offset 1177 UV offset
932 IV length 1178 IV length
933 SV * callback 1179 SV * callback
934 PROTOTYPE: $$$;$ 1180 PROTOTYPE: $$$;$
935 CODE: 1181 PPCODE:
936{ 1182{
937 dREQ; 1183 dREQ;
938 1184
939 req->type = REQ_READAHEAD; 1185 req->type = REQ_READAHEAD;
940 req->fh = newSVsv (fh); 1186 req->fh = newSVsv (fh);
941 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); 1187 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh)));
942 req->offset = offset; 1188 req->offset = offset;
943 req->length = length; 1189 req->length = length;
944 1190
945 send_req (req); 1191 REQ_SEND;
946} 1192}
947 1193
948void 1194void
949aio_stat(fh_or_path,callback=&PL_sv_undef) 1195aio_stat (fh_or_path,callback=&PL_sv_undef)
950 SV * fh_or_path 1196 SV * fh_or_path
951 SV * callback 1197 SV * callback
952 ALIAS: 1198 ALIAS:
953 aio_stat = REQ_STAT 1199 aio_stat = REQ_STAT
954 aio_lstat = REQ_LSTAT 1200 aio_lstat = REQ_LSTAT
955 CODE: 1201 PPCODE:
956{ 1202{
957 dREQ; 1203 dREQ;
958 1204
959 New (0, req->statdata, 1, Stat_t); 1205 New (0, req->statdata, 1, Stat_t);
960 if (!req->statdata) 1206 if (!req->statdata)
961 { 1207 {
962 free_req (req); 1208 req_free (req);
963 croak ("out of memory during aio_req->statdata allocation"); 1209 croak ("out of memory during aio_req->statdata allocation");
964 } 1210 }
965 1211
966 if (SvPOK (fh_or_path)) 1212 if (SvPOK (fh_or_path))
967 { 1213 {
974 req->type = REQ_FSTAT; 1220 req->type = REQ_FSTAT;
975 req->fh = newSVsv (fh_or_path); 1221 req->fh = newSVsv (fh_or_path);
976 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); 1222 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
977 } 1223 }
978 1224
979 send_req (req); 1225 REQ_SEND;
980} 1226}
981 1227
982void 1228void
983aio_unlink(pathname,callback=&PL_sv_undef) 1229aio_unlink (pathname,callback=&PL_sv_undef)
984 SV * pathname 1230 SV * pathname
985 SV * callback 1231 SV * callback
986 ALIAS: 1232 ALIAS:
987 aio_unlink = REQ_UNLINK 1233 aio_unlink = REQ_UNLINK
988 aio_rmdir = REQ_RMDIR 1234 aio_rmdir = REQ_RMDIR
1235 aio_readdir = REQ_READDIR
989 CODE: 1236 PPCODE:
990{ 1237{
991 dREQ; 1238 dREQ;
992 1239
993 req->type = ix; 1240 req->type = ix;
994 req->data = newSVsv (pathname); 1241 req->data = newSVsv (pathname);
995 req->dataptr = SvPVbyte_nolen (req->data); 1242 req->dataptr = SvPVbyte_nolen (req->data);
996 1243
997 send_req (req); 1244 REQ_SEND;
998} 1245}
999 1246
1000void 1247void
1001aio_symlink(oldpath,newpath,callback=&PL_sv_undef) 1248aio_link (oldpath,newpath,callback=&PL_sv_undef)
1002 SV * oldpath 1249 SV * oldpath
1003 SV * newpath 1250 SV * newpath
1004 SV * callback 1251 SV * callback
1252 ALIAS:
1253 aio_link = REQ_LINK
1254 aio_symlink = REQ_SYMLINK
1255 aio_rename = REQ_RENAME
1005 CODE: 1256 PPCODE:
1006{ 1257{
1007 dREQ; 1258 dREQ;
1008 1259
1009 req->type = REQ_SYMLINK; 1260 req->type = ix;
1010 req->fh = newSVsv (oldpath); 1261 req->fh = newSVsv (oldpath);
1011 req->data2ptr = SvPVbyte_nolen (req->fh); 1262 req->data2ptr = SvPVbyte_nolen (req->fh);
1012 req->data = newSVsv (newpath); 1263 req->data = newSVsv (newpath);
1013 req->dataptr = SvPVbyte_nolen (req->data); 1264 req->dataptr = SvPVbyte_nolen (req->data);
1014 1265
1015 send_req (req); 1266 REQ_SEND;
1016} 1267}
1017 1268
1018void 1269void
1019aio_readdir(pathname,callback=&PL_sv_undef) 1270aio_busy (delay,callback=&PL_sv_undef)
1020 SV * pathname 1271 double delay
1021 SV * callback 1272 SV * callback
1022 CODE: 1273 PPCODE:
1023{ 1274{
1024 dREQ; 1275 dREQ;
1025 1276
1277 req->type = REQ_BUSY;
1278 req->fd = delay < 0. ? 0 : delay;
1279 req->fd2 = delay < 0. ? 0 : 1000. * (delay - req->fd);
1280
1281 REQ_SEND;
1282}
1283
1284void
1285aio_group (callback=&PL_sv_undef)
1286 SV * callback
1287 PROTOTYPE: ;$
1288 PPCODE:
1289{
1290 dREQ;
1291
1026 req->type = REQ_READDIR; 1292 req->type = REQ_GROUP;
1027 req->data = newSVsv (pathname); 1293 req_send (req);
1028 req->dataptr = SvPVbyte_nolen (req->data);
1029
1030 send_req (req);
1031}
1032 1294
1295 XPUSHs (req_sv (req, AIO_GRP_KLASS));
1296}
1297
1033void 1298void
1299aio_nop (callback=&PL_sv_undef)
1300 SV * callback
1301 PPCODE:
1302{
1303 dREQ;
1304
1305 req->type = REQ_NOP;
1306
1307 REQ_SEND;
1308}
1309
1310void
1311aioreq_pri (int pri = DEFAULT_PRI)
1312 CODE:
1313 if (pri < PRI_MIN) pri = PRI_MIN;
1314 if (pri > PRI_MAX) pri = PRI_MAX;
1315 next_pri = pri + PRI_BIAS;
1316
1317void
1318aioreq_nice (int nice = 0)
1319 CODE:
1320 nice = next_pri - nice;
1321 if (nice < PRI_MIN) nice = PRI_MIN;
1322 if (nice > PRI_MAX) nice = PRI_MAX;
1323 next_pri = nice + PRI_BIAS;
1324
1325void
1034flush() 1326flush ()
1035 PROTOTYPE: 1327 PROTOTYPE:
1036 CODE: 1328 CODE:
1037 while (nreqs) 1329 while (nreqs)
1038 { 1330 {
1039 poll_wait (); 1331 poll_wait ();
1079 CODE: 1371 CODE:
1080 RETVAL = nreqs; 1372 RETVAL = nreqs;
1081 OUTPUT: 1373 OUTPUT:
1082 RETVAL 1374 RETVAL
1083 1375
1376PROTOTYPES: DISABLE
1377
1378MODULE = IO::AIO PACKAGE = IO::AIO::REQ
1379
1380void
1381cancel (aio_req_ornot req)
1382 CODE:
1383 req_cancel (req);
1384
1385void
1386cb (aio_req_ornot req, SV *callback=&PL_sv_undef)
1387 CODE:
1388 SvREFCNT_dec (req->callback);
1389 req->callback = newSVsv (callback);
1390
1391MODULE = IO::AIO PACKAGE = IO::AIO::GRP
1392
1393void
1394add (aio_req grp, ...)
1395 PPCODE:
1396{
1397 int i;
1398 aio_req req;
1399
1400 if (grp->fd == 2)
1401 croak ("cannot add requests to IO::AIO::GRP after the group finished");
1402
1403 for (i = 1; i < items; ++i )
1404 {
1405 if (GIMME_V != G_VOID)
1406 XPUSHs (sv_2mortal (newSVsv (ST (i))));
1407
1408 req = SvAIO_REQ (ST (i));
1409
1410 if (req)
1411 {
1412 ++grp->length;
1413 req->grp = grp;
1414
1415 req->grp_prev = 0;
1416 req->grp_next = grp->grp_first;
1417
1418 if (grp->grp_first)
1419 grp->grp_first->grp_prev = req;
1420
1421 grp->grp_first = req;
1422 }
1423 }
1424}
1425
1426void
1427result (aio_req grp, ...)
1428 CODE:
1429{
1430 int i;
1431 AV *av = newAV ();
1432
1433 for (i = 1; i < items; ++i )
1434 av_push (av, newSVsv (ST (i)));
1435
1436 SvREFCNT_dec (grp->data);
1437 grp->data = (SV *)av;
1438}
1439
1440void
1441limit (aio_req grp, int limit)
1442 CODE:
1443 grp->fd2 = limit;
1444 aio_grp_feed (grp);
1445
1446void
1447feed (aio_req grp, SV *callback=&PL_sv_undef)
1448 CODE:
1449{
1450 SvREFCNT_dec (grp->fh2);
1451 grp->fh2 = newSVsv (callback);
1452
1453 if (grp->fd2 <= 0)
1454 grp->fd2 = 2;
1455
1456 aio_grp_feed (grp);
1457}
1458

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines