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.18 by root, Sun Jul 31 19:00:31 2005 UTC vs.
Revision 1.75 by root, Thu Oct 26 06:44:48 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines