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.37 by root, Tue Aug 23 12:37:19 2005 UTC vs.
Revision 1.120 by root, Sun May 11 19:17:09 2008 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines