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.1 by root, Sun Jul 10 17:07:44 2005 UTC vs.
Revision 1.152 by root, Tue Jul 14 00:32:27 2009 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines