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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines