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.63 by root, Mon Oct 23 23:48:31 2006 UTC vs.
Revision 1.130 by root, Thu Oct 2 11:35:03 2008 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines