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.36 by root, Tue Aug 23 01:18:04 2005 UTC vs.
Revision 1.119 by root, Sun May 11 19:11:05 2008 UTC

1#define _REENTRANT 1 1#include "libeio/xthread.h"
2
2#include <errno.h> 3#include <errno.h>
3 4
4#include "EXTERN.h" 5#include "EXTERN.h"
5#include "perl.h" 6#include "perl.h"
6#include "XSUB.h" 7#include "XSUB.h"
7 8
8#include "autoconf/config.h"
9
10#include <pthread.h> 9#include <stddef.h>
11 10#include <stdlib.h>
11#include <errno.h>
12#include <sys/types.h> 12#include <sys/types.h>
13#include <sys/stat.h> 13#include <sys/stat.h>
14
15#include <unistd.h> 14#include <limits.h>
16#include <fcntl.h> 15#include <fcntl.h>
17#include <signal.h>
18#include <sched.h> 16#include <sched.h>
19 17
20#if HAVE_SENDFILE 18/* perl namespace pollution */
21# if __linux 19#undef VERSION
22# include <sys/sendfile.h> 20
23# elif __freebsd 21#ifdef _WIN32
24# include <sys/socket.h> 22
25# include <sys/uio.h> 23# define EIO_STRUCT_DIRENT Direntry_t
26# elif __hpux 24# undef malloc
27# include <sys/socket.h> 25# undef free
28# elif __solaris /* not yet */ 26
29# include <sys/sendfile.h> 27// perl overrides all those nice win32 functions
28# undef open
29# undef read
30# undef write
31# undef send
32# undef recv
33# undef stat
34# undef fstat
35# define lstat stat
36# undef truncate
37# undef ftruncate
38# undef open
39# undef close
40# undef unlink
41# undef rmdir
42# undef rename
43# undef lseek
44
45# define chown(a,b,c) (errno = ENOSYS, -1)
46# define fchown(a,b,c) (errno = ENOSYS, -1)
47# define fchmod(a,b) (errno = ENOSYS, -1)
48# define symlink(a,b) (errno = ENOSYS, -1)
49# define readlink(a,b,c) (errno = ENOSYS, -1)
50# define mknod(a,b,c) (errno = ENOSYS, -1)
51# define truncate(a,b) (errno = ENOSYS, -1)
52# define ftruncate(fd,o) chsize ((fd), (o))
53# define fsync(fd) _commit (fd)
54# define opendir(fd) (errno = ENOSYS, 0)
55# define readdir(fd) (errno = ENOSYS, -1)
56# define closedir(fd) (errno = ENOSYS, -1)
57# define mkdir(a,b) mkdir (a)
58
30# else 59#else
31# error sendfile support requested but not available 60
32# endif 61# include <sys/time.h>
62# include <sys/select.h>
63# include <unistd.h>
64# include <utime.h>
65# include <signal.h>
66# define EIO_STRUCT_DIRENT struct dirent
67
33#endif 68#endif
34 69
35#if __ia64 70#define EIO_STRUCT_STAT Stat_t
36# define STACKSIZE 65536 71
72/* use NV for 32 bit perls as it allows larger offsets */
73#if IVSIZE >= 8
74# define SvVAL64 SvIV
37#else 75#else
38# define STACKSIZE 4096 76# define SvVAL64 SvNV
39#endif 77#endif
40 78
79static HV *stash;
80typedef SV SV8; /* byte-sv, used for argument-checking */
81
82#define AIO_REQ_KLASS "IO::AIO::REQ"
83#define AIO_GRP_KLASS "IO::AIO::GRP"
84
85#define EIO_COMMON \
86 SV *callback; \
87 SV *sv1, *sv2; \
88 STRLEN stroffset; \
89 SV *self
90
91#include "libeio/eio.h"
92
93static int req_invoke (eio_req *req);
94#define EIO_FINISH(req) req_invoke (req)
95static void aio_grp_feed (eio_req *grp);
96#define EIO_FEED(req) aio_grp_feed (req)
97static void req_destroy (eio_req *grp);
98#define EIO_DESTROY(req) req_destroy (req)
99
41enum { 100enum {
42 REQ_QUIT, 101 FLAG_SV2_RO_OFF = 0x40, /* data was set readonly */
43 REQ_OPEN, REQ_CLOSE,
44 REQ_READ, REQ_WRITE, REQ_READAHEAD,
45 REQ_SENDFILE,
46 REQ_STAT, REQ_LSTAT, REQ_FSTAT,
47 REQ_FSYNC, REQ_FDATASYNC,
48 REQ_UNLINK, REQ_RMDIR,
49 REQ_SYMLINK,
50}; 102};
51 103
52typedef struct aio_cb { 104#include "libeio/eio.c"
53 struct aio_cb *volatile next;
54 105
55 int type;
56
57 int fd, fd2;
58 off_t offset;
59 size_t length;
60 ssize_t result;
61 mode_t mode; /* open */
62 int errorno;
63 SV *data, *callback;
64 SV *fh, *fh2;
65 void *dataptr, *data2ptr;
66 STRLEN dataoffset;
67
68 Stat_t *statdata;
69} aio_cb;
70
71typedef aio_cb *aio_req; 106typedef eio_req *aio_req;
107typedef eio_req *aio_req_ornot;
72 108
73static int started, wanted; 109static SV *on_next_submit;
74static volatile int nreqs; 110static int next_pri = EIO_DEFAULT_PRI + EIO_PRI_BIAS;
75static int max_outstanding = 1<<30; 111static int max_outstanding;
76static int respipe [2];
77 112
78static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; 113static int respipe_osf [2], respipe [2] = { -1, -1 };
79static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER;
80static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER;
81 114
82static volatile aio_req reqs, reqe; /* queue start, queue end */ 115static void req_destroy (aio_req req);
83static volatile aio_req ress, rese; /* queue start, queue end */
84
85static void free_req (aio_req req) 116static void req_cancel (aio_req req);
86{
87 if (req->data)
88 SvREFCNT_dec (req->data);
89 117
118static void want_poll (void)
119{
120 /* write a dummy byte to the pipe so fh becomes ready */
121 respipe_write (respipe_osf [1], (const void *)&respipe_osf, 1);
122}
123
124static void done_poll (void)
125{
126 /* read any signals sent by the worker threads */
127 char buf [4];
128 while (respipe_read (respipe [0], buf, 4) == 4)
129 ;
130}
131
132/* must be called at most once */
133static SV *req_sv (aio_req req, const char *klass)
134{
90 if (req->fh) 135 if (!req->self)
91 SvREFCNT_dec (req->fh);
92
93 if (req->fh2)
94 SvREFCNT_dec (req->fh2);
95
96 if (req->statdata)
97 Safefree (req->statdata);
98
99 if (req->callback)
100 SvREFCNT_dec (req->callback);
101
102 Safefree (req);
103}
104
105static void
106poll_wait ()
107{
108 if (nreqs && !ress)
109 { 136 {
110 fd_set rfd; 137 req->self = (SV *)newHV ();
111 FD_ZERO(&rfd); 138 sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0);
112 FD_SET(respipe [0], &rfd);
113
114 select (respipe [0] + 1, &rfd, 0, 0, 0);
115 } 139 }
116}
117 140
118static int 141 return sv_2mortal (sv_bless (newRV_inc (req->self), gv_stashpv (klass, 1)));
119poll_cb () 142}
143
144static aio_req SvAIO_REQ (SV *sv)
145{
146 MAGIC *mg;
147
148 if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv))
149 croak ("object of class " AIO_REQ_KLASS " expected");
150
151 mg = mg_find (SvRV (sv), PERL_MAGIC_ext);
152
153 return mg ? (aio_req)mg->mg_ptr : 0;
154}
155
156static void aio_grp_feed (aio_req grp)
157{
158 if (grp->sv2 && SvOK (grp->sv2))
159 {
160 dSP;
161
162 ENTER;
163 SAVETMPS;
164 PUSHMARK (SP);
165 XPUSHs (req_sv (grp, AIO_GRP_KLASS));
166 PUTBACK;
167 call_sv (grp->sv2, G_VOID | G_EVAL | G_KEEPERR);
168 SPAGAIN;
169 FREETMPS;
170 LEAVE;
171 }
172}
173
174static void req_submit (eio_req *req)
175{
176 eio_submit (req);
177
178 if (on_next_submit)
179 {
180 dSP;
181 SV *cb = sv_2mortal (on_next_submit);
182
183 on_next_submit = 0;
184
185 PUSHMARK (SP);
186 PUTBACK;
187 call_sv (cb, G_DISCARD | G_EVAL);
188 }
189}
190
191static int req_invoke (eio_req *req)
120{ 192{
121 dSP; 193 dSP;
122 int count = 0;
123 int do_croak = 0;
124 aio_req req;
125 194
126 for (;;) 195 if (req->flags & FLAG_SV2_RO_OFF)
196 SvREADONLY_off (req->sv2);
197
198 if (!EIO_CANCELLED (req) && SvOK (req->callback))
127 { 199 {
128 pthread_mutex_lock (&reslock); 200 ENTER;
129 req = ress; 201 SAVETMPS;
202 PUSHMARK (SP);
203 EXTEND (SP, 1);
130 204
131 if (req) 205 switch (req->type)
132 { 206 {
133 ress = req->next; 207 case EIO_READDIR:
134
135 if (!ress)
136 { 208 {
137 /* read any signals sent by the worker threads */ 209 SV *rv = &PL_sv_undef;
138 char buf [32]; 210
139 while (read (respipe [0], buf, 32) == 32) 211 if (req->result >= 0)
140 ; 212 {
213 int i;
214 char *buf = req->ptr2;
215 AV *av = newAV ();
141 216
142 rese = 0; 217 av_extend (av, req->result - 1);
218
219 for (i = 0; i < req->result; ++i)
220 {
221 SV *sv = newSVpv (buf, 0);
222
223 av_store (av, i, sv);
224 buf += SvCUR (sv) + 1;
225 }
226
227 rv = sv_2mortal (newRV_noinc ((SV *)av));
228 }
229
230 PUSHs (rv);
143 } 231 }
144 }
145
146 pthread_mutex_unlock (&reslock);
147
148 if (!req)
149 break; 232 break;
150 233
151 nreqs--; 234 case EIO_OPEN:
152
153 if (req->type == REQ_QUIT)
154 started--;
155 else
156 {
157 int errorno = errno;
158 errno = req->errorno;
159
160 if (req->type == REQ_READ)
161 SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0));
162
163 if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE))
164 SvREADONLY_off (req->data);
165
166 if (req->statdata)
167 {
168 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
169 PL_laststatval = req->result;
170 PL_statcache = *(req->statdata);
171 }
172
173 ENTER;
174 PUSHMARK (SP);
175 XPUSHs (sv_2mortal (newSViv (req->result)));
176
177 if (req->type == REQ_OPEN)
178 { 235 {
179 /* convert fd to fh */ 236 /* convert fd to fh */
180 SV *fh; 237 SV *fh = &PL_sv_undef;
181 238
182 PUTBACK; 239 if (req->result >= 0)
183 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); 240 {
184 SPAGAIN; 241 GV *gv = (GV *)sv_newmortal ();
242 int flags = req->int1 & (O_RDONLY | O_WRONLY | O_RDWR);
243 char sym [64];
244 int symlen;
245
246 symlen = snprintf (sym, sizeof (sym), "fd#%d", req->result);
247 gv_init (gv, stash, sym, symlen, 0);
185 248
186 fh = SvREFCNT_inc (POPs); 249 symlen = snprintf (
250 sym,
251 sizeof (sym),
252 "%s&=%d",
253 flags == O_RDONLY ? "<" : flags == O_WRONLY ? ">" : "+<",
254 req->result
255 );
187 256
257 if (do_open (gv, sym, symlen, 0, 0, 0, 0))
258 fh = (SV *)gv;
259 }
260
188 PUSHMARK (SP); 261 PUSHs (fh);
189 XPUSHs (sv_2mortal (fh));
190 } 262 }
191
192 if (SvOK (req->callback))
193 {
194 PUTBACK;
195 call_sv (req->callback, G_VOID | G_EVAL);
196 SPAGAIN;
197
198 if (SvTRUE (ERRSV))
199 {
200 free_req (req);
201 croak (0);
202 }
203 }
204
205 LEAVE;
206
207 errno = errorno;
208 count++;
209 }
210
211 free_req (req);
212 }
213
214 return count;
215}
216
217static void *aio_proc(void *arg);
218
219static void
220start_thread (void)
221{
222 sigset_t fullsigset, oldsigset;
223 pthread_t tid;
224 pthread_attr_t attr;
225
226 pthread_attr_init (&attr);
227 pthread_attr_setstacksize (&attr, STACKSIZE);
228 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
229
230 sigfillset (&fullsigset);
231 sigprocmask (SIG_SETMASK, &fullsigset, &oldsigset);
232
233 if (pthread_create (&tid, &attr, aio_proc, 0) == 0)
234 started++;
235
236 sigprocmask (SIG_SETMASK, &oldsigset, 0);
237}
238
239static void
240send_req (aio_req req)
241{
242 while (started < wanted && nreqs >= started)
243 start_thread ();
244
245 nreqs++;
246
247 pthread_mutex_lock (&reqlock);
248
249 req->next = 0;
250
251 if (reqe)
252 {
253 reqe->next = req;
254 reqe = req;
255 }
256 else
257 reqe = reqs = req;
258
259 pthread_cond_signal (&reqwait);
260 pthread_mutex_unlock (&reqlock);
261
262 if (nreqs > max_outstanding)
263 for (;;)
264 {
265 poll_cb ();
266
267 if (nreqs <= max_outstanding)
268 break;
269
270 poll_wait ();
271 }
272}
273
274static void
275end_thread (void)
276{
277 aio_req req;
278 Newz (0, req, 1, aio_cb);
279 req->type = REQ_QUIT;
280
281 send_req (req);
282}
283
284static void min_parallel (int nthreads)
285{
286 if (wanted < nthreads)
287 wanted = nthreads;
288}
289
290static void max_parallel (int nthreads)
291{
292 int cur = started;
293
294 if (wanted > nthreads)
295 wanted = nthreads;
296
297 while (cur > wanted)
298 {
299 end_thread ();
300 cur--;
301 }
302
303 while (started > wanted)
304 {
305 poll_wait ();
306 poll_cb ();
307 }
308}
309
310static void create_pipe ()
311{
312 if (pipe (respipe))
313 croak ("unable to initialize result pipe");
314
315 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK))
316 croak ("cannot set result pipe to nonblocking mode");
317
318 if (fcntl (respipe [1], F_SETFL, O_NONBLOCK))
319 croak ("cannot set result pipe to nonblocking mode");
320}
321
322static void atfork_prepare (void)
323{
324 pthread_mutex_lock (&reqlock);
325 pthread_mutex_lock (&reslock);
326}
327
328static void atfork_parent (void)
329{
330 pthread_mutex_unlock (&reslock);
331 pthread_mutex_unlock (&reqlock);
332}
333
334static void atfork_child (void)
335{
336 aio_req prv;
337
338 started = 0;
339
340 while (reqs)
341 {
342 prv = reqs;
343 reqs = prv->next;
344 free_req (prv);
345 }
346
347 reqs = reqe = 0;
348
349 while (ress)
350 {
351 prv = ress;
352 ress = prv->next;
353 free_req (prv);
354 }
355
356 ress = rese = 0;
357
358 close (respipe [0]);
359 close (respipe [1]);
360 create_pipe ();
361
362 atfork_parent ();
363}
364
365/*****************************************************************************/
366/* work around various missing functions */
367
368#if !HAVE_PREADWRITE
369# define pread aio_pread
370# define pwrite aio_pwrite
371
372/*
373 * make our pread/pwrite safe against themselves, but not against
374 * normal read/write by using a mutex. slows down execution a lot,
375 * but that's your problem, not mine.
376 */
377static pthread_mutex_t iolock = PTHREAD_MUTEX_INITIALIZER;
378
379static ssize_t
380pread (int fd, void *buf, size_t count, off_t offset)
381{
382 ssize_t res;
383 off_t ooffset;
384
385 pthread_mutex_lock (&iolock);
386 ooffset = lseek (fd, 0, SEEK_CUR);
387 lseek (fd, offset, SEEK_SET);
388 res = read (fd, buf, count);
389 lseek (fd, ooffset, SEEK_SET);
390 pthread_mutex_unlock (&iolock);
391
392 return res;
393}
394
395static ssize_t
396pwrite (int fd, void *buf, size_t count, off_t offset)
397{
398 ssize_t res;
399 off_t ooffset;
400
401 pthread_mutex_lock (&iolock);
402 ooffset = lseek (fd, 0, SEEK_CUR);
403 lseek (fd, offset, SEEK_SET);
404 res = write (fd, buf, count);
405 lseek (fd, offset, SEEK_SET);
406 pthread_mutex_unlock (&iolock);
407
408 return res;
409}
410#endif
411
412#if !HAVE_FDATASYNC
413# define fdatasync fsync
414#endif
415
416#if !HAVE_READAHEAD
417# define readahead aio_readahead
418
419static char readahead_buf[4096];
420
421static ssize_t
422readahead (int fd, off_t offset, size_t count)
423{
424 while (count > 0)
425 {
426 size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf);
427
428 pread (fd, readahead_buf, len, offset);
429 offset += len;
430 count -= len;
431 }
432
433 errno = 0;
434}
435#endif
436
437/* sendfile always needs emulation */
438static ssize_t
439sendfile_ (int ofd, int ifd, off_t offset, size_t count)
440{
441 ssize_t res;
442
443 if (!count)
444 return 0;
445
446#if HAVE_SENDFILE
447# if __linux
448 res = sendfile (ofd, ifd, &offset, count);
449
450# elif __freebsd
451 /*
452 * Of course, the freebsd sendfile is a dire hack with no thoughts
453 * wasted on making it similar to other I/O functions.
454 */
455 {
456 off_t sbytes;
457 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0);
458
459 if (res < 0 && sbytes)
460 /* maybe only on EAGAIN only: as usual, the manpage leaves you guessing */
461 res = sbytes;
462 }
463
464# elif __hpux
465 res = sendfile (ofd, ifd, offset, count, 0, 0);
466
467# elif __solaris
468 {
469 struct sendfilevec vec;
470 size_t sbytes;
471
472 vec.sfv_fd = ifd;
473 vec.sfv_flag = 0;
474 vec.sfv_off = offset;
475 vec.sfv_len = count;
476
477 res = sendfilev (ofd, &vec, 1, &sbytes);
478
479 if (res < 0 && sbytes)
480 res = sbytes;
481 }
482
483# else
484 res = -1;
485 errno = ENOSYS;
486# endif
487#endif
488
489 if (res < 0
490 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK
491#if __solaris
492 || errno == EAFNOSUPPORT || errno == EPROTOTYPE
493#endif
494 )
495 )
496 {
497 /* emulate sendfile. this is a major pain in the ass */
498 char *buf = malloc (4096);
499 res = 0;
500
501 for (;;)
502 {
503 ssize_t cnt;
504
505 cnt = pread (ifd, buf, 4096, offset);
506
507 if (cnt <= 0)
508 {
509 if (cnt && !res) res = -1;
510 break;
511 }
512
513 cnt = write (ofd, buf, cnt);
514
515 if (cnt <= 0)
516 {
517 if (cnt && !res) res = -1;
518 break;
519 }
520
521 offset += cnt;
522 res += cnt;
523 }
524
525 {
526 int errorno = errno;
527 free (buf);
528 errno = errorno;
529 }
530 }
531
532 return res;
533}
534
535/*****************************************************************************/
536
537static void *
538aio_proc (void *thr_arg)
539{
540 aio_req req;
541 int type;
542
543 do
544 {
545 pthread_mutex_lock (&reqlock);
546
547 for (;;)
548 {
549 req = reqs;
550
551 if (reqs)
552 {
553 reqs = reqs->next;
554 if (!reqs) reqe = 0;
555 }
556
557 if (req)
558 break; 263 break;
559 264
560 pthread_cond_wait (&reqwait, &reqlock); 265 case EIO_GROUP:
561 } 266 req->int1 = 2; /* mark group as finished */
562 267
563 pthread_mutex_unlock (&reqlock); 268 if (req->sv1)
564
565 errno = 0; /* strictly unnecessary */
566
567 type = req->type;
568
569 switch (type)
570 { 269 {
571 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; 270 int i;
572 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; 271 AV *av = (AV *)req->sv1;
573 272
574 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; 273 EXTEND (SP, AvFILL (av) + 1);
575 case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length); break; 274 for (i = 0; i <= AvFILL (av); ++i)
576 275 PUSHs (*av_fetch (av, i, 0));
577 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; 276 }
578 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break;
579 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break;
580
581 case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break;
582 case REQ_CLOSE: req->result = close (req->fd); break;
583 case REQ_UNLINK: req->result = unlink (req->dataptr); break;
584 case REQ_RMDIR: req->result = rmdir (req->dataptr); break;
585 case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break;
586
587 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break;
588 case REQ_FSYNC: req->result = fsync (req->fd); break;
589
590 case REQ_QUIT:
591 break; 277 break;
592 278
279 case EIO_NOP:
280 case EIO_BUSY:
281 break;
282
283 case EIO_READLINK:
284 if (req->result > 0)
285 PUSHs (sv_2mortal (newSVpvn (req->ptr2, req->result)));
286 break;
287
288 case EIO_STAT:
289 case EIO_LSTAT:
290 case EIO_FSTAT:
291 PL_laststype = req->type == EIO_LSTAT ? OP_LSTAT : OP_STAT;
292 PL_laststatval = req->result;
293 PL_statcache = *(EIO_STRUCT_STAT *)(req->ptr2);
294 PUSHs (sv_2mortal (newSViv (req->result)));
295 break;
296
297 case EIO_READ:
298 SvCUR_set (req->sv2, req->stroffset + (req->result > 0 ? req->result : 0));
299 *SvEND (req->sv2) = 0;
300 PUSHs (sv_2mortal (newSViv (req->result)));
301 break;
302
303 case EIO_DUP2:
304 if (req->result > 0)
305 req->result = 0;
306 /* FALLTHROUGH */
307
593 default: 308 default:
594 req->result = ENOSYS; 309 PUSHs (sv_2mortal (newSViv (req->result)));
595 break; 310 break;
596 } 311 }
597 312
598 req->errorno = errno; 313 errno = req->errorno;
599 314
600 pthread_mutex_lock (&reslock); 315 PUTBACK;
316 call_sv (req->callback, G_VOID | G_EVAL | G_DISCARD);
317 SPAGAIN;
601 318
602 req->next = 0; 319 FREETMPS;
320 LEAVE;
603 321
604 if (rese) 322 PUTBACK;
605 {
606 rese->next = req;
607 rese = req;
608 }
609 else
610 {
611 rese = ress = req;
612
613 /* write a dummy byte to the pipe so fh becomes ready */
614 write (respipe [1], &respipe, 1);
615 }
616
617 pthread_mutex_unlock (&reslock);
618 } 323 }
619 while (type != REQ_QUIT);
620 324
325 return !!SvTRUE (ERRSV);
326}
327
328static void req_destroy (aio_req req)
329{
330 if (req->self)
331 {
332 sv_unmagic (req->self, PERL_MAGIC_ext);
333 SvREFCNT_dec (req->self);
334 }
335
336 SvREFCNT_dec (req->sv1);
337 SvREFCNT_dec (req->sv2);
338 SvREFCNT_dec (req->callback);
339
340 Safefree (req);
341}
342
343static void req_cancel_subs (aio_req grp)
344{
345 aio_req sub;
346
347 if (grp->type != EIO_GROUP)
348 return;
349
350 SvREFCNT_dec (grp->sv2);
351 grp->sv2 = 0;
352
353 eio_grp_cancel (grp);
354}
355
356#ifdef USE_SOCKETS_AS_HANDLES
357# define TO_SOCKET(x) (win32_get_osfhandle (x))
358#else
359# define TO_SOCKET(x) (x)
360#endif
361
362static void
363create_respipe (void)
364{
365 int old_readfd = respipe [0];
366
367 if (respipe [1] >= 0)
368 respipe_close (TO_SOCKET (respipe [1]));
369
370#ifdef _WIN32
371 if (PerlSock_socketpair (AF_UNIX, SOCK_STREAM, 0, respipe))
372#else
373 if (pipe (respipe))
374#endif
375 croak ("unable to initialize result pipe");
376
377 if (old_readfd >= 0)
378 {
379 if (dup2 (TO_SOCKET (respipe [0]), TO_SOCKET (old_readfd)) < 0)
380 croak ("unable to initialize result pipe(2)");
381
382 respipe_close (respipe [0]);
383 respipe [0] = old_readfd;
384 }
385
386#ifdef _WIN32
387 int arg = 1;
388 if (ioctlsocket (TO_SOCKET (respipe [0]), FIONBIO, &arg)
389 || ioctlsocket (TO_SOCKET (respipe [1]), FIONBIO, &arg))
390#else
391 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)
392 || fcntl (respipe [1], F_SETFL, O_NONBLOCK))
393#endif
394 croak ("unable to initialize result pipe(3)");
395
396 respipe_osf [0] = TO_SOCKET (respipe [0]);
397 respipe_osf [1] = TO_SOCKET (respipe [1]);
398}
399
400static void poll_wait (void)
401{
402 fd_set rfd;
403
404 while (eio_nreqs ())
405 {
406 int size;
407
408 X_LOCK (reslock);
409 size = res_queue.size;
410 X_UNLOCK (reslock);
411
412 if (size)
413 return;
414
415 maybe_start_thread ();
416
417 FD_ZERO (&rfd);
418 FD_SET (respipe [0], &rfd);
419
420 PerlSock_select (respipe [0] + 1, &rfd, 0, 0, 0);
421 }
422}
423
424static int poll_cb (void)
425{
426 int res;
427
428 do
429 {
430 res = eio_poll ();
431
432 if (res > 0)
433 croak (0);
434 }
435 while (max_outstanding && max_outstanding <= eio_nreqs ());
436
621 return 0; 437 return res;
438}
439
440static void atfork_child (void)
441{
442 create_respipe ();
622} 443}
623 444
624#define dREQ \ 445#define dREQ \
625 aio_req req; \ 446 aio_req req; \
447 int req_pri = next_pri; \
448 next_pri = EIO_DEFAULT_PRI + EIO_PRI_BIAS; \
626 \ 449 \
627 if (SvOK (callback) && !SvROK (callback)) \ 450 if (SvOK (callback) && !SvROK (callback)) \
628 croak ("clalback must be undef or of reference type"); \ 451 croak ("callback must be undef or of reference type"); \
629 \ 452 \
630 Newz (0, req, 1, aio_cb); \ 453 Newz (0, req, 1, eio_req); \
631 if (!req) \ 454 if (!req) \
632 croak ("out of memory during aio_req allocation"); \ 455 croak ("out of memory during eio_req allocation"); \
633 \ 456 \
634 req->callback = newSVsv (callback); 457 req->callback = newSVsv (callback); \
458 req->pri = req_pri
459
460#define REQ_SEND \
461 req_submit (req); \
462 \
463 if (GIMME_V != G_VOID) \
464 XPUSHs (req_sv (req, AIO_REQ_KLASS));
635 465
636MODULE = IO::AIO PACKAGE = IO::AIO 466MODULE = IO::AIO PACKAGE = IO::AIO
637 467
638PROTOTYPES: ENABLE 468PROTOTYPES: ENABLE
639 469
640BOOT: 470BOOT:
641{ 471{
642 create_pipe (); 472 stash = gv_stashpv ("IO::AIO", 1);
643 pthread_atfork (atfork_prepare, atfork_parent, atfork_child);
644}
645 473
474 newCONSTSUB (stash, "EXDEV", newSViv (EXDEV));
475 newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY));
476 newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY));
477 newCONSTSUB (stash, "O_CREAT", newSViv (O_CREAT));
478 newCONSTSUB (stash, "O_TRUNC", newSViv (O_TRUNC));
479#ifdef _WIN32
480 X_MUTEX_CHECK (wrklock);
481 X_MUTEX_CHECK (reslock);
482 X_MUTEX_CHECK (reqlock);
483 X_MUTEX_CHECK (reqwait);
484 X_MUTEX_CHECK (preadwritelock);
485 X_MUTEX_CHECK (readdirlock);
486
487 X_COND_CHECK (reqwait);
488#else
489 newCONSTSUB (stash, "S_IFIFO", newSViv (S_IFIFO));
490#endif
491
492 create_respipe ();
493
494 if (eio_init (want_poll, done_poll) < 0)
495 croak ("IO::AIO: unable to initialise eio library");
496
497 /* atfork child called in fifo order, so before eio's handler */
498 X_THREAD_ATFORK (0, 0, atfork_child);
499}
500
646void 501void
647min_parallel(nthreads) 502max_poll_reqs (int nreqs)
648 int nthreads
649 PROTOTYPE: $ 503 PROTOTYPE: $
504 CODE:
505 eio_set_max_poll_reqs (nreqs);
650 506
651void 507void
652max_parallel(nthreads) 508max_poll_time (double nseconds)
653 int nthreads
654 PROTOTYPE: $ 509 PROTOTYPE: $
655
656int
657max_outstanding(nreqs)
658 int nreqs
659 PROTOTYPE: $
660 CODE: 510 CODE:
661 RETVAL = max_outstanding; 511 eio_set_max_poll_time (nseconds);
512
513void
514min_parallel (int nthreads)
515 PROTOTYPE: $
516 CODE:
517 eio_set_min_parallel (nthreads);
518
519void
520max_parallel (int nthreads)
521 PROTOTYPE: $
522 CODE:
523 eio_set_max_parallel (nthreads);
524
525void
526max_idle (int nthreads)
527 PROTOTYPE: $
528 CODE:
529 eio_set_max_idle (nthreads);
530
531void
532max_outstanding (int maxreqs)
533 PROTOTYPE: $
534 CODE:
662 max_outstanding = nreqs; 535 max_outstanding = maxreqs;
663 536
664void 537void
665aio_open(pathname,flags,mode,callback=&PL_sv_undef) 538aio_open (SV8 *pathname, int flags, int mode, SV *callback=&PL_sv_undef)
666 SV * pathname
667 int flags
668 int mode
669 SV * callback
670 PROTOTYPE: $$$;$ 539 PROTOTYPE: $$$;$
671 CODE: 540 PPCODE:
672{ 541{
673 dREQ; 542 dREQ;
674 543
675 req->type = REQ_OPEN; 544 req->type = EIO_OPEN;
676 req->data = newSVsv (pathname); 545 req->sv1 = newSVsv (pathname);
677 req->dataptr = SvPVbyte_nolen (req->data); 546 req->ptr1 = SvPVbyte_nolen (req->sv1);
678 req->fd = flags; 547 req->int1 = flags;
679 req->mode = mode; 548 req->int2 = mode;
680 549
681 send_req (req); 550 REQ_SEND;
682} 551}
683 552
684void 553void
685aio_close(fh,callback=&PL_sv_undef) 554aio_fsync (SV *fh, SV *callback=&PL_sv_undef)
686 SV * fh
687 SV * callback
688 PROTOTYPE: $;$ 555 PROTOTYPE: $;$
689 ALIAS: 556 ALIAS:
690 aio_close = REQ_CLOSE
691 aio_fsync = REQ_FSYNC 557 aio_fsync = EIO_FSYNC
692 aio_fdatasync = REQ_FDATASYNC 558 aio_fdatasync = EIO_FDATASYNC
693 CODE: 559 PPCODE:
694{ 560{
695 dREQ; 561 dREQ;
696 562
697 req->type = ix; 563 req->type = ix;
698 req->fh = newSVsv (fh); 564 req->sv1 = newSVsv (fh);
699 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); 565 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh)));
700 566
701 send_req (req); 567 REQ_SEND (req);
702} 568}
703 569
704void 570void
571aio_close (SV *fh, SV *callback=&PL_sv_undef)
572 PROTOTYPE: $;$
573 PPCODE:
574{
575 static int close_pipe = -1; /* dummy fd to close fds via dup2 */
576 dREQ;
577
578 if (close_pipe < 0)
579 {
580 int pipefd [2];
581
582 if (pipe (pipefd) < 0
583 || close (pipefd [1]) < 0
584 || fcntl (pipefd [0], F_SETFD, FD_CLOEXEC) < 0)
585 abort (); /*D*/
586
587 close_pipe = pipefd [0];
588 }
589
590 req->type = EIO_DUP2;
591 req->int1 = close_pipe;
592 req->sv2 = newSVsv (fh);
593 req->int2 = PerlIO_fileno (IoIFP (sv_2io (fh)));
594
595 REQ_SEND (req);
596}
597
598void
705aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) 599aio_read (SV *fh, SV *offset, SV *length, SV8 *data, IV dataoffset, SV *callback=&PL_sv_undef)
706 SV * fh
707 UV offset
708 UV length
709 SV * data
710 UV dataoffset
711 SV * callback
712 ALIAS: 600 ALIAS:
713 aio_read = REQ_READ 601 aio_read = EIO_READ
714 aio_write = REQ_WRITE 602 aio_write = EIO_WRITE
715 PROTOTYPE: $$$$$;$ 603 PROTOTYPE: $$$$$;$
716 CODE: 604 PPCODE:
717{ 605{
718 aio_req req;
719 STRLEN svlen; 606 STRLEN svlen;
720 char *svptr = SvPVbyte (data, svlen); 607 char *svptr = SvPVbyte (data, svlen);
608 UV len = SvUV (length);
721 609
722 SvUPGRADE (data, SVt_PV); 610 SvUPGRADE (data, SVt_PV);
723 SvPOK_on (data); 611 SvPOK_on (data);
724 612
725 if (dataoffset < 0) 613 if (dataoffset < 0)
726 dataoffset += svlen; 614 dataoffset += svlen;
727 615
728 if (dataoffset < 0 || dataoffset > svlen) 616 if (dataoffset < 0 || dataoffset > svlen)
729 croak ("data offset outside of string"); 617 croak ("dataoffset outside of data scalar");
730 618
731 if (ix == REQ_WRITE) 619 if (ix == EIO_WRITE)
732 { 620 {
733 /* write: check length and adjust. */ 621 /* write: check length and adjust. */
734 if (length < 0 || length + dataoffset > svlen) 622 if (!SvOK (length) || len + dataoffset > svlen)
735 length = svlen - dataoffset; 623 len = svlen - dataoffset;
736 } 624 }
737 else 625 else
738 { 626 {
739 /* read: grow scalar as necessary */ 627 /* read: grow scalar as necessary */
740 svptr = SvGROW (data, length + dataoffset); 628 svptr = SvGROW (data, len + dataoffset + 1);
741 } 629 }
742 630
743 if (length < 0) 631 if (len < 0)
744 croak ("length must not be negative"); 632 croak ("length must not be negative");
745 633
746 { 634 {
747 dREQ; 635 dREQ;
748 636
749 req->type = ix; 637 req->type = ix;
750 req->fh = newSVsv (fh); 638 req->sv1 = newSVsv (fh);
751 req->fd = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh)) 639 req->int1 = PerlIO_fileno (ix == EIO_READ ? IoIFP (sv_2io (fh))
752 : IoOFP (sv_2io (fh))); 640 : IoOFP (sv_2io (fh)));
641 req->offs = SvOK (offset) ? SvVAL64 (offset) : -1;
753 req->offset = offset; 642 req->size = len;
754 req->length = length;
755 req->data = SvREFCNT_inc (data); 643 req->sv2 = SvREFCNT_inc (data);
756 req->dataptr = (char *)svptr + dataoffset; 644 req->ptr2 = (char *)svptr + dataoffset;
645 req->stroffset = dataoffset;
757 646
758 if (!SvREADONLY (data)) 647 if (!SvREADONLY (data))
759 { 648 {
760 SvREADONLY_on (data); 649 SvREADONLY_on (data);
761 req->data2ptr = (void *)data; 650 req->flags |= FLAG_SV2_RO_OFF;
762 } 651 }
763 652
764 send_req (req); 653 REQ_SEND;
765 } 654 }
766} 655}
767 656
768void 657void
658aio_readlink (SV8 *path, SV *callback=&PL_sv_undef)
659 PROTOTYPE: $$;$
660 PPCODE:
661{
662 SV *data;
663 dREQ;
664
665 req->type = EIO_READLINK;
666 req->sv1 = newSVsv (path);
667 req->ptr1 = SvPVbyte_nolen (req->sv1);
668
669 REQ_SEND;
670}
671
672void
769aio_sendfile(out_fh,in_fh,in_offset,length,callback=&PL_sv_undef) 673aio_sendfile (SV *out_fh, SV *in_fh, SV *in_offset, UV length, SV *callback=&PL_sv_undef)
770 SV * out_fh
771 SV * in_fh
772 UV in_offset
773 UV length
774 SV * callback
775 PROTOTYPE: $$$$;$ 674 PROTOTYPE: $$$$;$
776 CODE: 675 PPCODE:
777{ 676{
778 dREQ; 677 dREQ;
779 678
780 req->type = REQ_SENDFILE; 679 req->type = EIO_SENDFILE;
781 req->fh = newSVsv (out_fh); 680 req->sv1 = newSVsv (out_fh);
782 req->fd = PerlIO_fileno (IoIFP (sv_2io (out_fh))); 681 req->int1 = PerlIO_fileno (IoIFP (sv_2io (out_fh)));
783 req->fh2 = newSVsv (in_fh); 682 req->sv2 = newSVsv (in_fh);
784 req->fd2 = PerlIO_fileno (IoIFP (sv_2io (in_fh))); 683 req->int2 = PerlIO_fileno (IoIFP (sv_2io (in_fh)));
785 req->offset = in_offset; 684 req->offs = SvVAL64 (in_offset);
786 req->length = length; 685 req->size = length;
787 686
788 send_req (req); 687 REQ_SEND;
789} 688}
790 689
791void 690void
792aio_readahead(fh,offset,length,callback=&PL_sv_undef) 691aio_readahead (SV *fh, SV *offset, IV length, SV *callback=&PL_sv_undef)
793 SV * fh
794 UV offset
795 IV length
796 SV * callback
797 PROTOTYPE: $$$;$ 692 PROTOTYPE: $$$;$
798 CODE: 693 PPCODE:
799{ 694{
800 dREQ; 695 dREQ;
801 696
802 req->type = REQ_READAHEAD; 697 req->type = EIO_READAHEAD;
803 req->fh = newSVsv (fh); 698 req->sv1 = newSVsv (fh);
804 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); 699 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh)));
805 req->offset = offset; 700 req->offs = SvVAL64 (offset);
806 req->length = length; 701 req->size = length;
807 702
808 send_req (req); 703 REQ_SEND;
809} 704}
810 705
811void 706void
812aio_stat(fh_or_path,callback=&PL_sv_undef) 707aio_stat (SV8 *fh_or_path, SV *callback=&PL_sv_undef)
813 SV * fh_or_path
814 SV * callback
815 ALIAS: 708 ALIAS:
816 aio_stat = REQ_STAT 709 aio_stat = EIO_STAT
817 aio_lstat = REQ_LSTAT 710 aio_lstat = EIO_LSTAT
818 CODE: 711 PPCODE:
819{ 712{
820 dREQ; 713 dREQ;
821 714
822 New (0, req->statdata, 1, Stat_t); 715 req->sv1 = newSVsv (fh_or_path);
823 if (!req->statdata)
824 {
825 free_req (req);
826 croak ("out of memory during aio_req->statdata allocation");
827 }
828 716
829 if (SvPOK (fh_or_path)) 717 if (SvPOK (req->sv1))
830 { 718 {
831 req->type = ix; 719 req->type = ix;
832 req->data = newSVsv (fh_or_path);
833 req->dataptr = SvPVbyte_nolen (req->data); 720 req->ptr1 = SvPVbyte_nolen (req->sv1);
834 } 721 }
835 else 722 else
836 { 723 {
837 req->type = REQ_FSTAT; 724 req->type = EIO_FSTAT;
838 req->fh = newSVsv (fh_or_path);
839 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); 725 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
726 }
727
728 REQ_SEND;
729}
730
731void
732aio_utime (SV8 *fh_or_path, SV *atime, SV *mtime, SV *callback=&PL_sv_undef)
733 PPCODE:
734{
735 dREQ;
736
737 req->nv1 = SvOK (atime) ? SvNV (atime) : -1.;
738 req->nv2 = SvOK (mtime) ? SvNV (mtime) : -1.;
739 req->sv1 = newSVsv (fh_or_path);
740
741 if (SvPOK (req->sv1))
840 } 742 {
743 req->type = EIO_UTIME;
744 req->ptr1 = SvPVbyte_nolen (req->sv1);
745 }
746 else
747 {
748 req->type = EIO_FUTIME;
749 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
750 }
841 751
842 send_req (req); 752 REQ_SEND;
843} 753}
844 754
845void 755void
756aio_truncate (SV8 *fh_or_path, SV *offset, SV *callback=&PL_sv_undef)
757 PPCODE:
758{
759 dREQ;
760
761 req->sv1 = newSVsv (fh_or_path);
762 req->offs = SvOK (offset) ? SvVAL64 (offset) : -1;
763
764 if (SvPOK (req->sv1))
765 {
766 req->type = EIO_TRUNCATE;
767 req->ptr1 = SvPVbyte_nolen (req->sv1);
768 }
769 else
770 {
771 req->type = EIO_FTRUNCATE;
772 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
773 }
774
775 REQ_SEND;
776}
777
778void
779aio_chmod (SV8 *fh_or_path, int mode, SV *callback=&PL_sv_undef)
780 ALIAS:
781 aio_chmod = EIO_CHMOD
782 aio_mkdir = EIO_MKDIR
783 PPCODE:
784{
785 dREQ;
786
787 req->int2 = mode;
788 req->sv1 = newSVsv (fh_or_path);
789
790 if (SvPOK (req->sv1))
791 {
792 req->type = EIO_FCHMOD;
793 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
794 }
795 else
796 {
797 req->type = ix;
798 req->ptr1 = SvPVbyte_nolen (req->sv1);
799 }
800
801 REQ_SEND;
802}
803
804void
805aio_chown (SV8 *fh_or_path, SV *uid, SV *gid, SV *callback=&PL_sv_undef)
806 PPCODE:
807{
808 dREQ;
809
810 req->int2 = SvOK (uid) ? SvIV (uid) : -1;
811 req->int3 = SvOK (gid) ? SvIV (gid) : -1;
812 req->sv1 = newSVsv (fh_or_path);
813
814 if (SvPOK (req->sv1))
815 {
816 req->type = EIO_CHOWN;
817 req->ptr1 = SvPVbyte_nolen (req->sv1);
818 }
819 else
820 {
821 req->type = EIO_FCHOWN;
822 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
823 }
824
825 REQ_SEND;
826}
827
828void
846aio_unlink(pathname,callback=&PL_sv_undef) 829aio_unlink (SV8 *pathname, SV *callback=&PL_sv_undef)
847 SV * pathname
848 SV * callback
849 ALIAS: 830 ALIAS:
850 aio_unlink = REQ_UNLINK 831 aio_unlink = EIO_UNLINK
851 aio_rmdir = REQ_RMDIR 832 aio_rmdir = EIO_RMDIR
833 aio_readdir = EIO_READDIR
852 CODE: 834 PPCODE:
853{ 835{
854 dREQ; 836 dREQ;
855 837
856 req->type = ix; 838 req->type = ix;
857 req->data = newSVsv (pathname); 839 req->sv1 = newSVsv (pathname);
858 req->dataptr = SvPVbyte_nolen (req->data); 840 req->ptr1 = SvPVbyte_nolen (req->sv1);
841
842 REQ_SEND;
843}
844
845void
846aio_link (SV8 *oldpath, SV8 *newpath, SV *callback=&PL_sv_undef)
847 ALIAS:
848 aio_link = EIO_LINK
849 aio_symlink = EIO_SYMLINK
850 aio_rename = EIO_RENAME
851 PPCODE:
852{
853 dREQ;
859 854
860 send_req (req); 855 req->type = ix;
861} 856 req->sv1 = newSVsv (oldpath);
862 857 req->ptr1 = SvPVbyte_nolen (req->sv1);
863void 858 req->sv2 = newSVsv (newpath);
864aio_symlink(oldpath,newpath,callback=&PL_sv_undef) 859 req->ptr2 = SvPVbyte_nolen (req->sv2);
865 SV * oldpath
866 SV * newpath
867 SV * callback
868 CODE:
869{
870 dREQ;
871 860
872 req->type = REQ_SYMLINK; 861 REQ_SEND;
873 req->fh = newSVsv (oldpath); 862}
874 req->data2ptr = SvPVbyte_nolen (req->fh); 863
875 req->data = newSVsv (newpath); 864void
876 req->dataptr = SvPVbyte_nolen (req->data); 865aio_mknod (SV8 *pathname, int mode, UV dev, SV *callback=&PL_sv_undef)
866 PPCODE:
867{
868 dREQ;
877 869
878 send_req (req); 870 req->type = EIO_MKNOD;
871 req->sv1 = newSVsv (pathname);
872 req->ptr1 = SvPVbyte_nolen (req->sv1);
873 req->int2 = (mode_t)mode;
874 req->offs = dev;
875
876 REQ_SEND;
879} 877}
880 878
881void 879void
880aio_busy (double delay, SV *callback=&PL_sv_undef)
881 PPCODE:
882{
883 dREQ;
884
885 req->type = EIO_BUSY;
886 req->nv1 = delay < 0. ? 0. : delay;
887
888 REQ_SEND;
889}
890
891void
892aio_group (SV *callback=&PL_sv_undef)
893 PROTOTYPE: ;$
894 PPCODE:
895{
896 dREQ;
897
898 req->type = EIO_GROUP;
899
900 eio_submit (req);
901 XPUSHs (req_sv (req, AIO_GRP_KLASS));
902}
903
904void
905aio_nop (SV *callback=&PL_sv_undef)
906 ALIAS:
907 aio_nop = EIO_NOP
908 aio_sync = EIO_SYNC
909 PPCODE:
910{
911 dREQ;
912
913 req->type = ix;
914
915 REQ_SEND;
916}
917
918int
919aioreq_pri (int pri = 0)
920 PROTOTYPE: ;$
921 CODE:
922 RETVAL = next_pri - EIO_PRI_BIAS;
923 if (items > 0)
924 {
925 if (pri < EIO_PRI_MIN) pri = EIO_PRI_MIN;
926 if (pri > EIO_PRI_MAX) pri = EIO_PRI_MAX;
927 next_pri = pri + EIO_PRI_BIAS;
928 }
929 OUTPUT:
930 RETVAL
931
932void
933aioreq_nice (int nice = 0)
934 CODE:
935 nice = next_pri - nice;
936 if (nice < EIO_PRI_MIN) nice = EIO_PRI_MIN;
937 if (nice > EIO_PRI_MAX) nice = EIO_PRI_MAX;
938 next_pri = nice + EIO_PRI_BIAS;
939
940void
882flush() 941flush ()
883 PROTOTYPE: 942 PROTOTYPE:
884 CODE: 943 CODE:
885 while (nreqs) 944 while (eio_nreqs ())
886 { 945 {
887 poll_wait (); 946 poll_wait ();
888 poll_cb (); 947 poll_cb ();
889 } 948 }
890 949
891void 950int
892poll() 951poll()
893 PROTOTYPE: 952 PROTOTYPE:
894 CODE: 953 CODE:
895 if (nreqs)
896 {
897 poll_wait (); 954 poll_wait ();
898 poll_cb (); 955 RETVAL = poll_cb ();
899 } 956 OUTPUT:
957 RETVAL
900 958
901int 959int
902poll_fileno() 960poll_fileno()
903 PROTOTYPE: 961 PROTOTYPE:
904 CODE: 962 CODE:
916 974
917void 975void
918poll_wait() 976poll_wait()
919 PROTOTYPE: 977 PROTOTYPE:
920 CODE: 978 CODE:
921 if (nreqs)
922 poll_wait (); 979 poll_wait ();
923 980
924int 981int
925nreqs() 982nreqs()
926 PROTOTYPE: 983 PROTOTYPE:
927 CODE: 984 CODE:
928 RETVAL = nreqs; 985 RETVAL = eio_nreqs ();
929 OUTPUT: 986 OUTPUT:
930 RETVAL 987 RETVAL
931 988
989int
990nready()
991 PROTOTYPE:
992 CODE:
993 RETVAL = eio_nready ();
994 OUTPUT:
995 RETVAL
996
997int
998npending()
999 PROTOTYPE:
1000 CODE:
1001 RETVAL = eio_npending ();
1002 OUTPUT:
1003 RETVAL
1004
1005int
1006nthreads()
1007 PROTOTYPE:
1008 CODE:
1009 if (WORDACCESS_UNSAFE) X_LOCK (wrklock);
1010 RETVAL = started;
1011 if (WORDACCESS_UNSAFE) X_UNLOCK (wrklock);
1012 OUTPUT:
1013 RETVAL
1014
1015void _on_next_submit (SV *cb)
1016 CODE:
1017 SvREFCNT_dec (on_next_submit);
1018 on_next_submit = SvOK (cb) ? newSVsv (cb) : 0;
1019
1020PROTOTYPES: DISABLE
1021
1022MODULE = IO::AIO PACKAGE = IO::AIO::REQ
1023
1024void
1025cancel (aio_req_ornot req)
1026 CODE:
1027 eio_cancel (req);
1028
1029void
1030cb (aio_req_ornot req, SV *callback=&PL_sv_undef)
1031 CODE:
1032 SvREFCNT_dec (req->callback);
1033 req->callback = newSVsv (callback);
1034
1035MODULE = IO::AIO PACKAGE = IO::AIO::GRP
1036
1037void
1038add (aio_req grp, ...)
1039 PPCODE:
1040{
1041 int i;
1042
1043 if (grp->int1 == 2)
1044 croak ("cannot add requests to IO::AIO::GRP after the group finished");
1045
1046 for (i = 1; i < items; ++i )
1047 {
1048 aio_req req;
1049
1050 if (GIMME_V != G_VOID)
1051 XPUSHs (sv_2mortal (newSVsv (ST (i))));
1052
1053 req = SvAIO_REQ (ST (i));
1054
1055 if (req)
1056 eio_grp_add (grp, req);
1057 }
1058}
1059
1060void
1061cancel_subs (aio_req_ornot req)
1062 CODE:
1063 req_cancel_subs (req);
1064
1065void
1066result (aio_req grp, ...)
1067 CODE:
1068{
1069 int i;
1070 AV *av;
1071
1072 grp->errorno = errno;
1073
1074 av = newAV ();
1075
1076 for (i = 1; i < items; ++i )
1077 av_push (av, newSVsv (ST (i)));
1078
1079 SvREFCNT_dec (grp->sv1);
1080 grp->sv1 = (SV *)av;
1081}
1082
1083void
1084errno (aio_req grp, int errorno = errno)
1085 CODE:
1086 grp->errorno = errorno;
1087
1088void
1089limit (aio_req grp, int limit)
1090 CODE:
1091 eio_grp_limit (grp, limit);
1092
1093void
1094feed (aio_req grp, SV *callback=&PL_sv_undef)
1095 CODE:
1096{
1097 SvREFCNT_dec (grp->sv2);
1098 grp->sv2 = newSVsv (callback);
1099
1100 if (grp->int2 <= 0)
1101 grp->int2 = 2;
1102
1103 eio_grp_limit (grp, grp->int2);
1104}
1105

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines