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.16 by root, Sun Jul 31 18:20:07 2005 UTC vs.
Revision 1.154 by root, Wed Jul 15 01:39:15 2009 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines