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.77 by root, Thu Oct 26 13:25:40 2006 UTC vs.
Revision 1.146 by root, Fri Jun 12 17:07:00 2009 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines