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.75 by root, Thu Oct 26 06:44:48 2006 UTC vs.
Revision 1.153 by root, Wed Jul 15 01:36:04 2009 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines