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.112 by root, Tue Apr 1 19:40:47 2008 UTC vs.
Revision 1.205 by root, Sat Oct 1 10:53:47 2011 UTC

1#include "xthread.h" 1#include "libeio/xthread.h"
2 2
3#include <errno.h> 3#include <errno.h>
4 4
5#include "EXTERN.h" 5#include "EXTERN.h"
6#include "perl.h" 6#include "perl.h"
7#include "XSUB.h" 7#include "XSUB.h"
8
9#include "schmorp.h"
8 10
9#include <stddef.h> 11#include <stddef.h>
10#include <stdlib.h> 12#include <stdlib.h>
11#include <errno.h> 13#include <errno.h>
12#include <sys/types.h> 14#include <sys/types.h>
13#include <sys/stat.h> 15#include <sys/stat.h>
14#include <limits.h> 16#include <limits.h>
15#include <fcntl.h> 17#include <fcntl.h>
16#include <sched.h> 18#include <sched.h>
17 19
20#if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES
21# include <sys/mman.h>
22#endif
23
24/* perl namespace pollution */
25#undef VERSION
26
27/* perl stupidly overrides readdir and maybe others */
28/* with thread-unsafe versions, imagine that :( */
29#undef readdir
30#undef opendir
31#undef closedir
32
18#ifdef _WIN32 33#ifdef _WIN32
19 34
20# define SIGIO 0 35 // perl overrides all those nice libc functions
21 typedef Direntry_t X_DIRENT; 36
22#undef malloc 37 #undef malloc
23#undef free 38 #undef free
24
25// perl overrides all those nice win32 functions
26# undef open 39 #undef open
27# undef read 40 #undef read
28# undef write 41 #undef write
29# undef send 42 #undef send
30# undef recv 43 #undef recv
31# undef stat 44 #undef stat
45 #undef lstat
32# undef fstat 46 #undef fstat
33# define lstat stat
34# undef truncate 47 #undef truncate
35# undef ftruncate 48 #undef ftruncate
36# undef open 49 #undef open
50 #undef link
37# undef close 51 #undef close
38# undef unlink 52 #undef unlink
53 #undef mkdir
39# undef rmdir 54 #undef rmdir
40# undef rename 55 #undef rename
41# undef lseek 56 #undef lseek
42 57 #undef opendir
43# define chown(a,b,c) (errno = ENOSYS, -1) 58 #undef readdir
44# define fchown(a,b,c) (errno = ENOSYS, -1) 59 #undef closedir
45# define fchmod(a,b) (errno = ENOSYS, -1) 60 #undef chmod
46# define symlink(a,b) (errno = ENOSYS, -1) 61 #undef fchmod
47# define readlink(a,b,c) (errno = ENOSYS, -1) 62 #undef dup
48# define mknod(a,b,c) (errno = ENOSYS, -1) 63 #undef dup2
49# define truncate(a,b) (errno = ENOSYS, -1) 64 #undef abort
50# define ftruncate(fd,o) chsize ((fd), (o)) 65 #undef pipe
51# define fsync(fd) _commit (fd)
52# define opendir(fd) (errno = ENOSYS, 0)
53# define readdir(fd) (errno = ENOSYS, -1)
54# define closedir(fd) (errno = ENOSYS, -1)
55# define mkdir(a,b) mkdir (a)
56 66
57#else 67#else
58 68
59# include "autoconf/config.h"
60# include <sys/time.h> 69 #include <sys/time.h>
61# include <sys/select.h> 70 #include <sys/select.h>
62# include <unistd.h> 71 #include <unistd.h>
63# include <utime.h> 72 #include <utime.h>
64# include <signal.h> 73 #include <signal.h>
65 typedef struct dirent X_DIRENT;
66 74
67#endif
68
69#if HAVE_SENDFILE
70# if __linux
71# include <sys/sendfile.h>
72# elif __freebsd
73# include <sys/socket.h>
74# include <sys/uio.h>
75# elif __hpux
76# include <sys/socket.h>
77# elif __solaris /* not yet */
78# include <sys/sendfile.h>
79# else
80# error sendfile support requested but not available
81# endif 75#endif
82#endif
83 76
84/* number of seconds after which idle threads exit */ 77#define EIO_STRUCT_STAT Stat_t
85#define IDLE_TIMEOUT 10
86
87/* used for struct dirent, AIX doesn't provide it */
88#ifndef NAME_MAX
89# define NAME_MAX 4096
90#endif
91
92/* buffer size for various temporary buffers */
93#define AIO_BUFSIZE 65536
94 78
95/* use NV for 32 bit perls as it allows larger offsets */ 79/* use NV for 32 bit perls as it allows larger offsets */
96#if IVSIZE >= 8 80#if IVSIZE >= 8
81# define VAL64 IV
97# define SvVAL64 SvIV 82# define SvVAL64 SvIV
83# define newSVval64 newSViv
98#else 84#else
85# define VAL64 NV
99# define SvVAL64 SvNV 86# define SvVAL64 SvNV
87# define newSVval64 newSVnv
100#endif 88#endif
101 89
102static HV *stash; 90/*****************************************************************************/
103 91
104#define dBUF \ 92#if __GNUC__ >= 3
105 char *aio_buf; \ 93# define expect(expr,value) __builtin_expect ((expr),(value))
106 X_LOCK (wrklock); \ 94#else
107 self->dbuf = aio_buf = malloc (AIO_BUFSIZE); \ 95# define expect(expr,value) (expr)
108 X_UNLOCK (wrklock); \ 96#endif
109 if (!aio_buf) \ 97
110 return -1; 98#define expect_false(expr) expect ((expr) != 0, 0)
99#define expect_true(expr) expect ((expr) != 0, 1)
100
101/*****************************************************************************/
111 102
112typedef SV SV8; /* byte-sv, used for argument-checking */ 103typedef SV SV8; /* byte-sv, used for argument-checking */
104typedef int aio_rfd; /* read file desriptor */
105typedef int aio_wfd; /* write file descriptor */
106
107static HV *aio_stash, *aio_req_stash, *aio_grp_stash, *aio_wd_stash;
108
109#define EIO_REQ_MEMBERS \
110 SV *callback; \
111 SV *sv1, *sv2; \
112 SV *sv3, *sv4; \
113 STRLEN stroffset; \
114 SV *self;
115
116#define EIO_NO_WRAPPERS 1
117
118#include "libeio/config.h"
119#include "libeio/eio.h"
120
121static int req_invoke (eio_req *req);
122#define EIO_FINISH(req) req_invoke (req)
123static void req_destroy (eio_req *grp);
124#define EIO_DESTROY(req) req_destroy (req)
125
126#include "libeio/eio.c"
127
128/* Linux/others */
129#ifndef O_ASYNC
130# define O_ASYNC 0
131#endif
132#ifndef O_DIRECT
133# define O_DIRECT 0
134#endif
135#ifndef O_NOATIME
136# define O_NOATIME 0
137#endif
138
139/* POSIX */
140#ifndef O_CLOEXEC
141# define O_CLOEXEC 0
142#endif
143#ifndef O_NOFOLLOW
144# define O_NOFOLLOW 0
145#endif
146#ifndef O_NOCTTY
147# define O_NOCTTY 0
148#endif
149#ifndef O_NONBLOCK
150# define O_NONBLOCK 0
151#endif
152#ifndef O_EXEC
153# define O_EXEC 0
154#endif
155#ifndef O_SEARCH
156# define O_SEARCH 0
157#endif
158#ifndef O_DIRECTORY
159# define O_DIRECTORY 0
160#endif
161#ifndef O_DSYNC
162# define O_DSYNC 0
163#endif
164#ifndef O_RSYNC
165# define O_RSYNC 0
166#endif
167#ifndef O_SYNC
168# define O_SYNC 0
169#endif
170#ifndef O_TTY_INIT
171# define O_TTY_INIT 0
172#endif
173
174#ifndef POSIX_FADV_NORMAL
175# define POSIX_FADV_NORMAL 0
176#endif
177#ifndef POSIX_FADV_SEQUENTIAL
178# define POSIX_FADV_SEQUENTIAL 0
179#endif
180#ifndef POSIX_FADV_RANDOM
181# define POSIX_FADV_RANDOM 0
182#endif
183#ifndef POSIX_FADV_NOREUSE
184# define POSIX_FADV_NOREUSE 0
185#endif
186#ifndef POSIX_FADV_WILLNEED
187# define POSIX_FADV_WILLNEED 0
188#endif
189#ifndef POSIX_FADV_DONTNEED
190# define POSIX_FADV_DONTNEED 0
191#endif
192
193#if !HAVE_POSIX_FADVISE
194# define posix_fadvise(a,b,c,d) errno = ENOSYS /* also return ENOSYS */
195#endif
196
197#ifndef POSIX_MADV_NORMAL
198# define POSIX_MADV_NORMAL 0
199#endif
200#ifndef POSIX_MADV_SEQUENTIAL
201# define POSIX_MADV_SEQUENTIAL 0
202#endif
203#ifndef POSIX_MADV_RANDOM
204# define POSIX_MADV_RANDOM 0
205#endif
206#ifndef POSIX_MADV_WILLNEED
207# define POSIX_MADV_WILLNEED 0
208#endif
209#ifndef POSIX_MADV_DONTNEED
210# define POSIX_MADV_DONTNEED 0
211#endif
212
213#if !HAVE_POSIX_MADVISE
214# define posix_madvise(a,b,c) errno = ENOSYS /* also return ENOSYS */
215#endif
216
217#ifndef PROT_NONE
218# define PROT_NONE 0
219#endif
220#ifndef PROT_READ
221# define PROT_READ 0
222#endif
223#ifndef PROT_WRITE
224# define PROT_READ 0
225#endif
226#ifndef PROT_EXEC
227# define PROT_EXEC 0
228#endif
229
230#ifndef ST_RDONLY
231# define ST_RDONLY 0
232#endif
233#ifndef ST_NOSUID
234# define ST_NOSUID 0
235#endif
236#ifndef ST_NODEV
237# define ST_NODEV 0
238#endif
239#ifndef ST_NOEXEC
240# define ST_NOEXEC 0
241#endif
242#ifndef ST_SYNCHRONOUS
243# define ST_SYNCHRONOUS 0
244#endif
245#ifndef ST_MANDLOCK
246# define ST_MANDLOCK 0
247#endif
248#ifndef ST_WRITE
249# define ST_WRITE 0
250#endif
251#ifndef ST_APPEND
252# define ST_APPEND 0
253#endif
254#ifndef ST_IMMUTABLE
255# define ST_IMMUTABLE 0
256#endif
257#ifndef ST_NOATIME
258# define ST_NOATIME 0
259#endif
260#ifndef ST_NODIRATIME
261# define ST_NODIRATIME 0
262#endif
263#ifndef ST_RELATIME
264# define ST_RELATIME 0
265#endif
266
267#ifndef S_IFIFO
268# define S_IFIFO 0
269#endif
270#ifndef S_IFCHR
271# define S_IFCHR 0
272#endif
273#ifndef S_IFBLK
274# define S_IFBLK 0
275#endif
276#ifndef S_IFLNK
277# define S_IFLNK 0
278#endif
279#ifndef S_IFREG
280# define S_IFREG 0
281#endif
282#ifndef S_IFDIR
283# define S_IFDIR 0
284#endif
285#ifndef S_IFWHT
286# define S_IFWHT 0
287#endif
288#ifndef S_IFSOCK
289# define S_IFSOCK 0
290#endif
291
292#ifndef MAP_ANONYMOUS
293# ifdef MAP_ANON
294# define MAP_ANONYMOUS MAP_ANON
295# else
296# define MAP_ANONYMOUS MAP_FIXED /* and hope this fails */
297# endif
298#endif
299#ifndef MAP_HUGETLB
300# define MAP_HUGETLB 0
301#endif
302#ifndef MAP_LOCKED
303# define MAP_LOCKED 0
304#endif
305#ifndef MAP_NORESERVE
306# define MAP_NORESERVE 0
307#endif
308#ifndef MAP_POPULATE
309# define MAP_POPULATE 0
310#endif
311#ifndef MAP_NONBLOCK
312# define MAP_NONBLOCK 0
313#endif
314
315#ifndef makedev
316# define makedev(maj,min) (((maj) << 8) | (min))
317#endif
318#ifndef major
319# define major(dev) ((dev) >> 8)
320#endif
321#ifndef minor
322# define minor(dev) ((dev) & 0xff)
323#endif
324
325#ifndef PAGESIZE
326# define PAGESIZE sysconf (_SC_PAGESIZE)
327#endif
113 328
114enum { 329enum {
115 REQ_QUIT, 330 FLAG_SV2_RO_OFF = 0x40, /* data was set readonly */
116 REQ_OPEN, REQ_CLOSE,
117 REQ_READ, REQ_WRITE,
118 REQ_READAHEAD, REQ_SENDFILE,
119 REQ_STAT, REQ_LSTAT, REQ_FSTAT,
120 REQ_TRUNCATE, REQ_FTRUNCATE,
121 REQ_UTIME, REQ_FUTIME,
122 REQ_CHMOD, REQ_FCHMOD,
123 REQ_CHOWN, REQ_FCHOWN,
124 REQ_SYNC, REQ_FSYNC, REQ_FDATASYNC,
125 REQ_UNLINK, REQ_RMDIR, REQ_MKDIR, REQ_RENAME,
126 REQ_MKNOD, REQ_READDIR,
127 REQ_LINK, REQ_SYMLINK, REQ_READLINK,
128 REQ_GROUP, REQ_NOP,
129 REQ_BUSY,
130}; 331};
131 332
132#define AIO_REQ_KLASS "IO::AIO::REQ"
133#define AIO_GRP_KLASS "IO::AIO::GRP"
134
135typedef struct aio_cb
136{
137 struct aio_cb *volatile next;
138
139 SV *callback;
140 SV *sv1, *sv2;
141 void *ptr1, *ptr2;
142 off_t offs;
143 size_t size;
144 ssize_t result;
145 double nv1, nv2;
146
147 STRLEN stroffset;
148 int type;
149 int int1, int2, int3;
150 int errorno;
151 mode_t mode; /* open */
152
153 unsigned char flags;
154 unsigned char pri;
155
156 SV *self; /* the perl counterpart of this request, if any */
157 struct aio_cb *grp, *grp_prev, *grp_next, *grp_first;
158} aio_cb;
159
160enum {
161 FLAG_CANCELLED = 0x01, /* request was cancelled */
162 FLAG_SV2_RO_OFF = 0x40, /* data was set readonly */
163 FLAG_PTR2_FREE = 0x80, /* need to free(ptr2) */
164};
165
166typedef aio_cb *aio_req; 333typedef eio_req *aio_req;
167typedef aio_cb *aio_req_ornot; 334typedef eio_req *aio_req_ornot;
335typedef eio_wd aio_wd;
168 336
169enum { 337static SV *on_next_submit;
170 PRI_MIN = -4, 338static int next_pri = EIO_PRI_DEFAULT;
171 PRI_MAX = 4, 339static int max_outstanding;
172 340
173 DEFAULT_PRI = 0, 341static s_epipe respipe;
174 PRI_BIAS = -PRI_MIN,
175 NUM_PRI = PRI_MAX + PRI_BIAS + 1,
176};
177 342
178#define AIO_TICKS ((1000000 + 1023) >> 10)
179
180static unsigned int max_poll_time = 0;
181static unsigned int max_poll_reqs = 0;
182
183/* calculcate time difference in ~1/AIO_TICKS of a second */
184static int tvdiff (struct timeval *tv1, struct timeval *tv2)
185{
186 return (tv2->tv_sec - tv1->tv_sec ) * AIO_TICKS
187 + ((tv2->tv_usec - tv1->tv_usec) >> 10);
188}
189
190static thread_t main_tid;
191static int main_sig;
192static int block_sig_level;
193
194void block_sig (void)
195{
196 sigset_t ss;
197
198 if (block_sig_level++)
199 return;
200
201 if (!main_sig)
202 return;
203
204 sigemptyset (&ss);
205 sigaddset (&ss, main_sig);
206 pthread_sigmask (SIG_BLOCK, &ss, 0);
207}
208
209void unblock_sig (void)
210{
211 sigset_t ss;
212
213 if (--block_sig_level)
214 return;
215
216 if (!main_sig)
217 return;
218
219 sigemptyset (&ss);
220 sigaddset (&ss, main_sig);
221 pthread_sigmask (SIG_UNBLOCK, &ss, 0);
222}
223
224static int next_pri = DEFAULT_PRI + PRI_BIAS;
225
226static unsigned int started, idle, wanted;
227
228/* worker threads management */
229static mutex_t wrklock = X_MUTEX_INIT;
230
231typedef struct worker {
232 /* locked by wrklock */
233 struct worker *prev, *next;
234
235 thread_t tid;
236
237 /* locked by reslock, reqlock or wrklock */
238 aio_req req; /* currently processed request */
239 void *dbuf;
240 DIR *dirp;
241} worker;
242
243static worker wrk_first = { &wrk_first, &wrk_first, 0 };
244
245static void worker_clear (worker *wrk)
246{
247 if (wrk->dirp)
248 {
249 closedir (wrk->dirp);
250 wrk->dirp = 0;
251 }
252
253 if (wrk->dbuf)
254 {
255 free (wrk->dbuf);
256 wrk->dbuf = 0;
257 }
258}
259
260static void worker_free (worker *wrk)
261{
262 wrk->next->prev = wrk->prev;
263 wrk->prev->next = wrk->next;
264
265 free (wrk);
266}
267
268static volatile unsigned int nreqs, nready, npending;
269static volatile unsigned int max_idle = 4;
270static volatile unsigned int max_outstanding = 0xffffffff;
271static int respipe_osf [2], respipe [2] = { -1, -1 };
272
273static mutex_t reslock = X_MUTEX_INIT;
274static mutex_t reqlock = X_MUTEX_INIT;
275static cond_t reqwait = X_COND_INIT;
276
277#if WORDACCESS_UNSAFE
278
279static unsigned int get_nready (void)
280{
281 unsigned int retval;
282
283 X_LOCK (reqlock);
284 retval = nready;
285 X_UNLOCK (reqlock);
286
287 return retval;
288}
289
290static unsigned int get_npending (void)
291{
292 unsigned int retval;
293
294 X_LOCK (reslock);
295 retval = npending;
296 X_UNLOCK (reslock);
297
298 return retval;
299}
300
301static unsigned int get_nthreads (void)
302{
303 unsigned int retval;
304
305 X_LOCK (wrklock);
306 retval = started;
307 X_UNLOCK (wrklock);
308
309 return retval;
310}
311
312#else
313
314# define get_nready() nready
315# define get_npending() npending
316# define get_nthreads() started
317
318#endif
319
320/*
321 * a somewhat faster data structure might be nice, but
322 * with 8 priorities this actually needs <20 insns
323 * per shift, the most expensive operation.
324 */
325typedef struct {
326 aio_req qs[NUM_PRI], qe[NUM_PRI]; /* qstart, qend */
327 int size;
328} reqq;
329
330static reqq req_queue;
331static reqq res_queue;
332
333int reqq_push (reqq *q, aio_req req)
334{
335 int pri = req->pri;
336 req->next = 0;
337
338 if (q->qe[pri])
339 {
340 q->qe[pri]->next = req;
341 q->qe[pri] = req;
342 }
343 else
344 q->qe[pri] = q->qs[pri] = req;
345
346 return q->size++;
347}
348
349aio_req reqq_shift (reqq *q)
350{
351 int pri;
352
353 if (!q->size)
354 return 0;
355
356 --q->size;
357
358 for (pri = NUM_PRI; pri--; )
359 {
360 aio_req req = q->qs[pri];
361
362 if (req)
363 {
364 if (!(q->qs[pri] = req->next))
365 q->qe[pri] = 0;
366
367 return req;
368 }
369 }
370
371 abort ();
372}
373
374static int poll_cb (void);
375static int req_invoke (aio_req req);
376static void req_destroy (aio_req req); 343static void req_destroy (aio_req req);
377static void req_cancel (aio_req req); 344static void req_cancel (aio_req req);
378 345
346static void
347want_poll (void)
348{
349 /* write a dummy byte to the pipe so fh becomes ready */
350 s_epipe_signal (&respipe);
351}
352
353static void
354done_poll (void)
355{
356 /* read any signals sent by the worker threads */
357 s_epipe_drain (&respipe);
358}
359
379/* must be called at most once */ 360/* must be called at most once */
380static SV *req_sv (aio_req req, const char *klass) 361static SV *
362req_sv (aio_req req, HV *stash)
381{ 363{
382 if (!req->self) 364 if (!req->self)
383 { 365 {
384 req->self = (SV *)newHV (); 366 req->self = (SV *)newHV ();
385 sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0); 367 sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0);
386 } 368 }
387 369
388 return sv_2mortal (sv_bless (newRV_inc (req->self), gv_stashpv (klass, 1))); 370 return sv_2mortal (sv_bless (newRV_inc (req->self), stash));
389} 371}
390 372
391static aio_req SvAIO_REQ (SV *sv) 373static SV *
374newSVaio_wd (aio_wd wd)
375{
376 return sv_bless (newRV_noinc (newSViv ((IV)wd)), aio_wd_stash);
377}
378
379static aio_req
380SvAIO_REQ (SV *sv)
392{ 381{
393 MAGIC *mg; 382 MAGIC *mg;
394 383
395 if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv)) 384 if (!SvROK (sv)
385 /* for speed reasons, we do not verify that SvROK actually has a stash ptr */
386 || (SvSTASH (SvRV (sv)) != aio_grp_stash
387 && SvSTASH (SvRV (sv)) != aio_req_stash
388 && !sv_derived_from (sv, "IO::AIO::REQ")))
396 croak ("object of class " AIO_REQ_KLASS " expected"); 389 croak ("object of class IO::AIO::REQ expected");
397 390
398 mg = mg_find (SvRV (sv), PERL_MAGIC_ext); 391 mg = mg_find (SvRV (sv), PERL_MAGIC_ext);
399 392
400 return mg ? (aio_req)mg->mg_ptr : 0; 393 return mg ? (aio_req)mg->mg_ptr : 0;
401} 394}
402 395
396static aio_wd
397SvAIO_WD (SV *sv)
398{
399 if (!SvROK (sv)
400 || SvTYPE (SvRV (sv)) != SVt_PVMG
401 || SvSTASH (SvRV (sv)) != aio_wd_stash)
402 croak ("IO::AIO: expected a working directory object as returned by aio_wd");
403
404 return (aio_wd)(long)SvIVX (SvRV (sv));
405}
406
407static void
403static void aio_grp_feed (aio_req grp) 408aio_grp_feed (aio_req grp)
404{ 409{
405 block_sig (); 410 if (grp->sv2 && SvOK (grp->sv2))
406
407 while (grp->size < grp->int2 && !(grp->flags & FLAG_CANCELLED))
408 { 411 {
409 int old_len = grp->size;
410
411 if (grp->sv2 && SvOK (grp->sv2))
412 {
413 dSP; 412 dSP;
414 413
415 ENTER; 414 ENTER;
416 SAVETMPS; 415 SAVETMPS;
417 PUSHMARK (SP); 416 PUSHMARK (SP);
418 XPUSHs (req_sv (grp, AIO_GRP_KLASS)); 417 XPUSHs (req_sv (grp, aio_grp_stash));
419 PUTBACK; 418 PUTBACK;
420 call_sv (grp->sv2, G_VOID | G_EVAL | G_KEEPERR); 419 call_sv (grp->sv2, G_VOID | G_EVAL | G_KEEPERR);
421 SPAGAIN; 420 SPAGAIN;
422 FREETMPS; 421 FREETMPS;
423 LEAVE; 422 LEAVE;
424 }
425
426 /* stop if no progress has been made */
427 if (old_len == grp->size)
428 {
429 SvREFCNT_dec (grp->sv2);
430 grp->sv2 = 0;
431 break;
432 }
433 } 423 }
434
435 unblock_sig ();
436} 424}
437 425
438static void aio_grp_dec (aio_req grp) 426static void
427req_submit (eio_req *req)
439{ 428{
440 --grp->size; 429 eio_submit (req);
441 430
442 /* call feeder, if applicable */ 431 if (expect_false (on_next_submit))
443 aio_grp_feed (grp);
444
445 /* finish, if done */
446 if (!grp->size && grp->int1)
447 { 432 {
448 block_sig (); 433 dSP;
434 SV *cb = sv_2mortal (on_next_submit);
449 435
450 if (!req_invoke (grp)) 436 on_next_submit = 0;
451 {
452 req_destroy (grp);
453 unblock_sig ();
454 croak (0);
455 }
456 437
457 req_destroy (grp); 438 PUSHMARK (SP);
458 unblock_sig (); 439 PUTBACK;
440 call_sv (cb, G_DISCARD | G_EVAL);
459 } 441 }
460} 442}
461 443
444static int
462static int req_invoke (aio_req req) 445req_invoke (eio_req *req)
463{ 446{
464 dSP;
465
466 if (req->flags & FLAG_SV2_RO_OFF) 447 if (req->flags & FLAG_SV2_RO_OFF)
467 SvREADONLY_off (req->sv2); 448 SvREADONLY_off (req->sv2);
468 449
469 if (!(req->flags & FLAG_CANCELLED) && SvOK (req->callback)) 450 if (!EIO_CANCELLED (req) && req->callback)
470 { 451 {
452 dSP;
453 static SV *sv_result_cache; /* caches the result integer SV */
454 SV *sv_result;
455
471 ENTER; 456 ENTER;
472 SAVETMPS; 457 SAVETMPS;
473 PUSHMARK (SP); 458 PUSHMARK (SP);
474 EXTEND (SP, 1); 459 EXTEND (SP, 1);
475 460
461 /* do not recreate the result IV from scratch each time */
462 if (expect_true (sv_result_cache))
463 {
464 sv_result = sv_result_cache; sv_result_cache = 0;
465 SvIV_set (sv_result, req->result);
466 SvIOK_only (sv_result);
467 }
468 else
469 {
470 sv_result = newSViv (req->result);
471 SvREADONLY_on (sv_result);
472 }
473
476 switch (req->type) 474 switch (req->type)
477 { 475 {
476 case EIO_WD_OPEN:
477 PUSHs (req->result ? &PL_sv_undef : sv_2mortal (newSVaio_wd (req->wd)));
478 break;
479
478 case REQ_READDIR: 480 case EIO_READDIR:
479 { 481 {
480 SV *rv = &PL_sv_undef; 482 SV *rv = &PL_sv_undef;
481 483
482 if (req->result >= 0) 484 if (req->result >= 0)
483 { 485 {
484 int i; 486 int i;
485 char *buf = req->ptr2; 487 char *names = (char *)req->ptr2;
488 eio_dirent *ent = (eio_dirent *)req->ptr1; /* might be 0 */
486 AV *av = newAV (); 489 AV *av = newAV ();
487 490
488 av_extend (av, req->result - 1); 491 av_extend (av, req->result - 1);
489 492
490 for (i = 0; i < req->result; ++i) 493 for (i = 0; i < req->result; ++i)
491 { 494 {
492 SV *sv = newSVpv (buf, 0); 495 if (req->int1 & EIO_READDIR_DENTS)
496 {
497 SV *namesv = newSVpvn (names + ent->nameofs, ent->namelen);
493 498
499 if (req->int1 & EIO_READDIR_CUSTOM2)
500 {
501 static SV *sv_type [EIO_DT_MAX + 1]; /* type sv cache */
502 AV *avent = newAV ();
503
504 av_extend (avent, 2);
505
506 if (!sv_type [ent->type])
507 {
508 sv_type [ent->type] = newSViv (ent->type);
509 SvREADONLY_on (sv_type [ent->type]);
510 }
511
512 av_store (avent, 0, namesv);
513 av_store (avent, 1, SvREFCNT_inc (sv_type [ent->type]));
514 av_store (avent, 2, IVSIZE >= 8 ? newSVuv (ent->inode) : newSVnv (ent->inode));
515
516 av_store (av, i, newRV_noinc ((SV *)avent));
517 }
518 else
494 av_store (av, i, sv); 519 av_store (av, i, namesv);
520
521 ++ent;
522 }
523 else
524 {
525 SV *name = newSVpv (names, 0);
526 av_store (av, i, name);
495 buf += SvCUR (sv) + 1; 527 names += SvCUR (name) + 1;
528 }
496 } 529 }
497 530
498 rv = sv_2mortal (newRV_noinc ((SV *)av)); 531 rv = sv_2mortal (newRV_noinc ((SV *)av));
499 } 532 }
500 533
501 PUSHs (rv); 534 PUSHs (rv);
535
536 if (req->int1 & EIO_READDIR_CUSTOM1)
537 XPUSHs (sv_2mortal (newSViv (req->int1 & ~(EIO_READDIR_CUSTOM1 | EIO_READDIR_CUSTOM2))));
502 } 538 }
503 break; 539 break;
504 540
505 case REQ_OPEN: 541 case EIO_OPEN:
506 { 542 {
507 /* convert fd to fh */ 543 /* convert fd to fh */
508 SV *fh = &PL_sv_undef; 544 SV *fh = &PL_sv_undef;
509 545
510 if (req->result >= 0) 546 if (req->result >= 0)
512 GV *gv = (GV *)sv_newmortal (); 548 GV *gv = (GV *)sv_newmortal ();
513 int flags = req->int1 & (O_RDONLY | O_WRONLY | O_RDWR); 549 int flags = req->int1 & (O_RDONLY | O_WRONLY | O_RDWR);
514 char sym [64]; 550 char sym [64];
515 int symlen; 551 int symlen;
516 552
517 symlen = snprintf (sym, sizeof (sym), "fd#%d", req->result); 553 symlen = snprintf (sym, sizeof (sym), "fd#%d", (int)req->result);
518 gv_init (gv, stash, sym, symlen, 0); 554 gv_init (gv, aio_stash, sym, symlen, 0);
519 555
520 symlen = snprintf ( 556 symlen = snprintf (
521 sym, 557 sym,
522 sizeof (sym), 558 sizeof (sym),
523 "%s&=%d", 559 "%s&=%d",
524 flags == O_RDONLY ? "<" : flags == O_WRONLY ? ">" : "+<", 560 flags == O_RDONLY ? "<" : flags == O_WRONLY ? ">" : "+<",
525 req->result 561 (int)req->result
526 ); 562 );
527 563
528 if (do_open (gv, sym, symlen, 0, 0, 0, 0)) 564 if (do_open (gv, sym, symlen, 0, 0, 0, 0))
529 fh = (SV *)gv; 565 fh = (SV *)gv;
530 } 566 }
531 567
532 PUSHs (fh); 568 PUSHs (fh);
533 } 569 }
534 break; 570 break;
535 571
572 case EIO_STATVFS:
573 case EIO_FSTATVFS:
574 {
575 SV *rv = &PL_sv_undef;
576
577#ifndef _WIN32
578 if (req->result >= 0)
579 {
580 EIO_STRUCT_STATVFS *f = EIO_STATVFS_BUF (req);
581 HV *hv = newHV ();
582
583 rv = sv_2mortal (newRV_noinc ((SV *)hv));
584
585 hv_store (hv, "bsize" , sizeof ("bsize" ) - 1, newSVval64 (f->f_bsize ), 0);
586 hv_store (hv, "frsize" , sizeof ("frsize" ) - 1, newSVval64 (f->f_frsize ), 0);
587 hv_store (hv, "blocks" , sizeof ("blocks" ) - 1, newSVval64 (f->f_blocks ), 0);
588 hv_store (hv, "bfree" , sizeof ("bfree" ) - 1, newSVval64 (f->f_bfree ), 0);
589 hv_store (hv, "bavail" , sizeof ("bavail" ) - 1, newSVval64 (f->f_bavail ), 0);
590 hv_store (hv, "files" , sizeof ("files" ) - 1, newSVval64 (f->f_files ), 0);
591 hv_store (hv, "ffree" , sizeof ("ffree" ) - 1, newSVval64 (f->f_ffree ), 0);
592 hv_store (hv, "favail" , sizeof ("favail" ) - 1, newSVval64 (f->f_favail ), 0);
593 hv_store (hv, "fsid" , sizeof ("fsid" ) - 1, newSVval64 (f->f_fsid ), 0);
594 hv_store (hv, "flag" , sizeof ("flag" ) - 1, newSVval64 (f->f_flag ), 0);
595 hv_store (hv, "namemax", sizeof ("namemax") - 1, newSVval64 (f->f_namemax), 0);
596 }
597#endif
598
599 PUSHs (rv);
600 }
601
602 break;
603
536 case REQ_GROUP: 604 case EIO_GROUP:
537 req->int1 = 2; /* mark group as finished */ 605 req->int1 = 2; /* mark group as finished */
538 606
539 if (req->sv1) 607 if (req->sv1)
540 { 608 {
541 int i; 609 int i;
545 for (i = 0; i <= AvFILL (av); ++i) 613 for (i = 0; i <= AvFILL (av); ++i)
546 PUSHs (*av_fetch (av, i, 0)); 614 PUSHs (*av_fetch (av, i, 0));
547 } 615 }
548 break; 616 break;
549 617
550 case REQ_NOP: 618 case EIO_NOP:
551 case REQ_BUSY: 619 case EIO_BUSY:
552 break; 620 break;
553 621
554 case REQ_READLINK: 622 case EIO_READLINK:
623 case EIO_REALPATH:
555 if (req->result > 0) 624 if (req->result > 0)
556 { 625 PUSHs (sv_2mortal (newSVpvn (req->ptr2, req->result)));
557 SvCUR_set (req->sv2, req->result);
558 *SvEND (req->sv2) = 0;
559 PUSHs (req->sv2);
560 }
561 break; 626 break;
562 627
563 case REQ_STAT: 628 case EIO_STAT:
564 case REQ_LSTAT: 629 case EIO_LSTAT:
565 case REQ_FSTAT: 630 case EIO_FSTAT:
566 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; 631 PL_laststype = req->type == EIO_LSTAT ? OP_LSTAT : OP_STAT;
632
567 PL_laststatval = req->result; 633 if (!(PL_laststatval = req->result))
634 /* if compilation fails here then perl's Stat_t is not struct _stati64 */
568 PL_statcache = *(Stat_t *)(req->ptr2); 635 PL_statcache = *(EIO_STRUCT_STAT *)(req->ptr2);
569 PUSHs (sv_2mortal (newSViv (req->result))); 636
637 PUSHs (sv_result);
570 break; 638 break;
571 639
572 case REQ_READ: 640 case EIO_READ:
641 {
573 SvCUR_set (req->sv2, req->stroffset + (req->result > 0 ? req->result : 0)); 642 SvCUR_set (req->sv2, req->stroffset + (req->result > 0 ? req->result : 0));
574 *SvEND (req->sv2) = 0; 643 *SvEND (req->sv2) = 0;
575 PUSHs (sv_2mortal (newSViv (req->result))); 644 SvPOK_only (req->sv2);
645 SvSETMAGIC (req->sv2);
646 PUSHs (sv_result);
647 }
576 break; 648 break;
577 649
650 case EIO_DUP2: /* EIO_DUP2 actually means aio_close(), so fudge result value */
651 if (req->result > 0)
652 SvIV_set (sv_result, 0);
653 /* FALLTHROUGH */
654
578 default: 655 default:
579 PUSHs (sv_2mortal (newSViv (req->result))); 656 PUSHs (sv_result);
580 break; 657 break;
581 } 658 }
582 659
583 errno = req->errorno; 660 errno = req->errorno;
584 661
585 PUTBACK; 662 PUTBACK;
586 call_sv (req->callback, G_VOID | G_EVAL | G_DISCARD); 663 call_sv (req->callback, G_VOID | G_EVAL | G_DISCARD);
587 SPAGAIN; 664 SPAGAIN;
588 665
666 if (expect_false (SvREFCNT (sv_result) != 1 || sv_result_cache))
667 SvREFCNT_dec (sv_result);
668 else
669 sv_result_cache = sv_result;
670
589 FREETMPS; 671 FREETMPS;
590 LEAVE; 672 LEAVE;
591 673
592 PUTBACK; 674 PUTBACK;
593 } 675 }
594 676
595 if (req->grp)
596 {
597 aio_req grp = req->grp;
598
599 /* unlink request */
600 if (req->grp_next) req->grp_next->grp_prev = req->grp_prev;
601 if (req->grp_prev) req->grp_prev->grp_next = req->grp_next;
602
603 if (grp->grp_first == req)
604 grp->grp_first = req->grp_next;
605
606 aio_grp_dec (grp);
607 }
608
609 return !SvTRUE (ERRSV); 677 return !!SvTRUE (ERRSV);
610} 678}
611 679
680static void
612static void req_destroy (aio_req req) 681req_destroy (aio_req req)
613{ 682{
614 if (req->self) 683 if (req->self)
615 { 684 {
616 sv_unmagic (req->self, PERL_MAGIC_ext); 685 sv_unmagic (req->self, PERL_MAGIC_ext);
617 SvREFCNT_dec (req->self); 686 SvREFCNT_dec (req->self);
618 } 687 }
619 688
620 SvREFCNT_dec (req->sv1); 689 SvREFCNT_dec (req->sv1);
621 SvREFCNT_dec (req->sv2); 690 SvREFCNT_dec (req->sv2);
691 SvREFCNT_dec (req->sv3);
692 SvREFCNT_dec (req->sv4);
622 SvREFCNT_dec (req->callback); 693 SvREFCNT_dec (req->callback);
623 694
624 if (req->flags & FLAG_PTR2_FREE)
625 free (req->ptr2);
626
627 Safefree (req); 695 free (req);
628} 696}
629 697
698static void
630static void req_cancel_subs (aio_req grp) 699req_cancel_subs (aio_req grp)
631{ 700{
632 aio_req sub;
633
634 if (grp->type != REQ_GROUP) 701 if (grp->type != EIO_GROUP)
635 return; 702 return;
636 703
637 SvREFCNT_dec (grp->sv2); 704 SvREFCNT_dec (grp->sv2);
638 grp->sv2 = 0; 705 grp->sv2 = 0;
639 706
640 for (sub = grp->grp_first; sub; sub = sub->grp_next) 707 eio_grp_cancel (grp);
641 req_cancel (sub);
642} 708}
643 709
644static void req_cancel (aio_req req) 710static void ecb_cold
711create_respipe (void)
645{ 712{
646 req->flags |= FLAG_CANCELLED; 713 if (s_epipe_renew (&respipe))
647 714 croak ("IO::AIO: unable to initialize result pipe");
648 req_cancel_subs (req);
649} 715}
650
651#ifdef USE_SOCKETS_AS_HANDLES
652# define TO_SOCKET(x) (win32_get_osfhandle (x))
653#else
654# define TO_SOCKET(x) (x)
655#endif
656 716
657static void 717static void
658create_respipe (void) 718poll_wait (void)
659{ 719{
660 int old_readfd = respipe [0];
661
662 if (respipe [1] >= 0)
663 respipe_close (TO_SOCKET (respipe [1]));
664
665#ifdef _WIN32
666 if (PerlSock_socketpair (AF_UNIX, SOCK_STREAM, 0, respipe))
667#else
668 if (pipe (respipe))
669#endif
670 croak ("unable to initialize result pipe");
671
672 if (old_readfd >= 0)
673 {
674 if (dup2 (TO_SOCKET (respipe [0]), TO_SOCKET (old_readfd)) < 0)
675 croak ("unable to initialize result pipe(2)");
676
677 respipe_close (respipe [0]);
678 respipe [0] = old_readfd;
679 }
680
681#ifdef _WIN32
682 int arg = 1;
683 if (ioctlsocket (TO_SOCKET (respipe [0]), FIONBIO, &arg)
684 || ioctlsocket (TO_SOCKET (respipe [1]), FIONBIO, &arg))
685#else
686 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)
687 || fcntl (respipe [1], F_SETFL, O_NONBLOCK))
688#endif
689 croak ("unable to initialize result pipe(3)");
690
691 respipe_osf [0] = TO_SOCKET (respipe [0]);
692 respipe_osf [1] = TO_SOCKET (respipe [1]);
693}
694
695X_THREAD_PROC (aio_proc);
696
697static void start_thread (void)
698{
699 worker *wrk = calloc (1, sizeof (worker));
700
701 if (!wrk)
702 croak ("unable to allocate worker thread data");
703
704 X_LOCK (wrklock);
705
706 if (thread_create (&wrk->tid, aio_proc, (void *)wrk))
707 {
708 wrk->prev = &wrk_first;
709 wrk->next = wrk_first.next;
710 wrk_first.next->prev = wrk;
711 wrk_first.next = wrk;
712 ++started;
713 }
714 else
715 free (wrk);
716
717 X_UNLOCK (wrklock);
718}
719
720static void maybe_start_thread (void)
721{
722 if (get_nthreads () >= wanted)
723 return;
724
725 /* todo: maybe use idle here, but might be less exact */
726 if (0 <= (int)get_nthreads () + (int)get_npending () - (int)nreqs)
727 return;
728
729 start_thread ();
730}
731
732static void req_send (aio_req req)
733{
734 block_sig ();
735
736 ++nreqs;
737
738 X_LOCK (reqlock);
739 ++nready;
740 reqq_push (&req_queue, req);
741 X_COND_SIGNAL (reqwait);
742 X_UNLOCK (reqlock);
743
744 unblock_sig ();
745
746 maybe_start_thread ();
747}
748
749static void end_thread (void)
750{
751 aio_req req;
752
753 Newz (0, req, 1, aio_cb);
754
755 req->type = REQ_QUIT;
756 req->pri = PRI_MAX + PRI_BIAS;
757
758 X_LOCK (reqlock);
759 reqq_push (&req_queue, req);
760 X_COND_SIGNAL (reqwait);
761 X_UNLOCK (reqlock);
762
763 X_LOCK (wrklock);
764 --started;
765 X_UNLOCK (wrklock);
766}
767
768static void set_max_idle (int nthreads)
769{
770 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
771 max_idle = nthreads <= 0 ? 1 : nthreads;
772 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
773}
774
775static void min_parallel (int nthreads)
776{
777 if (wanted < nthreads)
778 wanted = nthreads;
779}
780
781static void max_parallel (int nthreads)
782{
783 if (wanted > nthreads)
784 wanted = nthreads;
785
786 while (started > wanted)
787 end_thread ();
788}
789
790static void poll_wait (void)
791{
792 fd_set rfd;
793
794 while (nreqs) 720 while (eio_nreqs ())
795 { 721 {
796 int size; 722 int size;
797 if (WORDACCESS_UNSAFE) X_LOCK (reslock); 723
724 X_LOCK (reslock);
798 size = res_queue.size; 725 size = res_queue.size;
799 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock); 726 X_UNLOCK (reslock);
800 727
801 if (size) 728 if (size)
802 return; 729 return;
803 730
804 maybe_start_thread (); 731 etp_maybe_start_thread ();
805 732
806 FD_ZERO (&rfd); 733 s_epipe_wait (&respipe);
807 FD_SET (respipe [0], &rfd);
808
809 PerlSock_select (respipe [0] + 1, &rfd, 0, 0, 0);
810 } 734 }
811} 735}
812 736
813static int poll_cb (void) 737static int
738poll_cb (void)
814{ 739{
815 dSP;
816 int count = 0;
817 int maxreqs = max_poll_reqs;
818 int do_croak = 0;
819 struct timeval tv_start, tv_now;
820 aio_req req;
821
822 if (max_poll_time)
823 gettimeofday (&tv_start, 0);
824
825 block_sig ();
826
827 for (;;) 740 for (;;)
828 { 741 {
829 for (;;) 742 int res = eio_poll ();
743
744 if (res > 0)
745 croak (0);
746
747 if (!max_outstanding || max_outstanding > eio_nreqs ())
748 return res;
749
750 poll_wait ();
751 }
752}
753
754static void ecb_cold
755reinit (void)
756{
757 create_respipe ();
758
759 if (eio_init (want_poll, done_poll) < 0)
760 croak ("IO::AIO: unable to initialise eio library");
761}
762
763/*****************************************************************************/
764
765#if !_POSIX_MAPPED_FILES
766# define mmap(addr,length,prot,flags,fd,offs) (errno = ENOSYS, -1)
767# define munmap(addr,length) (errno = ENOSYS, -1)
768#endif
769
770#if !_POSIX_MEMORY_PROTECTION
771# define mprotect(addr,len,prot) (errno = ENOSYS, -1)
772# define PROT_NONE 0
773# define PROT_WRITE 0
774# define MAP_PRIVATE 0
775# define MAP_SHARED 0
776# define MAP_FIXED 0
777#endif
778
779#define MMAP_MAGIC PERL_MAGIC_ext
780
781static int ecb_cold
782mmap_free (pTHX_ SV *sv, MAGIC *mg)
783{
784 int old_errno = errno;
785 munmap (mg->mg_ptr, (size_t)mg->mg_obj);
786 errno = old_errno;
787
788 mg->mg_obj = 0; /* just in case */
789
790 SvREADONLY_off (sv);
791
792 if (SvPVX (sv) != mg->mg_ptr)
793 croak ("ERROR: IO::AIO::mmap-mapped scalar changed location, detected");
794
795 SvCUR_set (sv, 0);
796 SvPVX (sv) = 0;
797 SvOK_off (sv);
798
799 return 0;
800}
801
802static MGVTBL mmap_vtbl = {
803 0, 0, 0, 0, mmap_free
804};
805
806/*****************************************************************************/
807
808static SV *
809get_cb (SV *cb_sv)
810{
811 SvGETMAGIC (cb_sv);
812 return SvOK (cb_sv) ? s_get_cv_croak (cb_sv) : 0;
813}
814
815static aio_req ecb_noinline
816dreq (SV *callback)
817{
818 SV *cb_cv;
819 aio_req req;
820 int req_pri = next_pri;
821 next_pri = EIO_PRI_DEFAULT;
822
823 cb_cv = get_cb (callback);
824
825 req = calloc (sizeof (*req), 1);
826 if (!req)
827 croak ("out of memory during eio_req allocation");
828
829 req->callback = SvREFCNT_inc (cb_cv);
830 req->pri = req_pri;
831
832 return req;
833}
834
835#define dREQ \
836 aio_req req = dreq (callback); \
837
838#define REQ_SEND \
839 PUTBACK; \
840 req_submit (req); \
841 SPAGAIN; \
842 \
843 if (GIMME_V != G_VOID) \
844 XPUSHs (req_sv (req, aio_req_stash));
845
846ecb_inline void
847req_set_path (aio_req req, SV *path, SV **wdsv, SV **pathsv, eio_wd *wd, void **ptr)
848{
849 if (expect_false (SvROK (path)))
850 {
851 SV *rv = SvRV (path);
852 SV *wdob;
853
854 if (SvTYPE (rv) == SVt_PVAV && AvFILLp (rv) == 1)
830 { 855 {
831 maybe_start_thread (); 856 path = AvARRAY (rv)[1];
857 wdob = AvARRAY (rv)[0];
832 858
833 X_LOCK (reslock); 859 if (SvOK (wdob))
834 req = reqq_shift (&res_queue);
835
836 if (req)
837 { 860 {
838 --npending; 861 *wd = SvAIO_WD (wdob);
839 862 *wdsv = SvREFCNT_inc_NN (SvRV (wdob));
840 if (!res_queue.size)
841 {
842 /* read any signals sent by the worker threads */
843 char buf [4];
844 while (respipe_read (respipe [0], buf, 4) == 4)
845 ;
846 }
847 }
848
849 X_UNLOCK (reslock);
850
851 if (!req)
852 break;
853
854 --nreqs;
855
856 if (req->type == REQ_GROUP && req->size)
857 {
858 req->int1 = 1; /* mark request as delayed */
859 continue;
860 } 863 }
861 else 864 else
862 { 865 *wd = EIO_INVALID_WD;
863 if (!req_invoke (req))
864 {
865 req_destroy (req);
866 unblock_sig ();
867 croak (0);
868 }
869
870 count++;
871 }
872
873 req_destroy (req);
874
875 if (maxreqs && !--maxreqs)
876 break;
877
878 if (max_poll_time)
879 {
880 gettimeofday (&tv_now, 0);
881
882 if (tvdiff (&tv_start, &tv_now) >= max_poll_time)
883 break;
884 }
885 } 866 }
867 else if (SvTYPE (rv) == SVt_PVMG && SvSTASH (rv) == aio_wd_stash)
868 {
869 *wd = SvIVX (rv);
870 *wdsv = SvREFCNT_inc_NN (rv);
871 *ptr = ".";
872 return; /* path set to "." */
873 }
874 else
875 croak ("IO::AIO: pathname arguments must be specified as a string, an IO::AIO::WD object or a [IO::AIO::WD, path] pair");
876 }
886 877
887 if (nreqs <= max_outstanding) 878 *pathsv = newSVsv (path);
879 *ptr = SvPVbyte_nolen (*pathsv);
880}
881
882static void ecb_noinline
883req_set_path1 (aio_req req, SV *path)
884{
885 req_set_path (req, path, &req->sv1, &req->sv3, &req->wd, &req->ptr1);
886}
887
888static void ecb_noinline
889req_set_fh_or_path (aio_req req, int type_path, int type_fh, SV *fh_or_path)
890{
891 SV *rv = SvROK (fh_or_path) ? SvRV (fh_or_path) : fh_or_path;
892
893 switch (SvTYPE (rv))
894 {
895 case SVt_PVIO:
896 case SVt_PVLV:
897 case SVt_PVGV:
898 req->type = type_fh;
899 req->sv1 = newSVsv (fh_or_path);
900 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
888 break; 901 break;
889 902
890 poll_wait (); 903 default:
891 904 req->type = type_path;
892 ++maxreqs; 905 req_set_path1 (req, fh_or_path);
906 break;
893 } 907 }
894
895 unblock_sig ();
896 return count;
897} 908}
898 909
899/*****************************************************************************/ 910XS(boot_IO__AIO) ecb_cold;
900/* work around various missing functions */
901 911
902#if !HAVE_PREADWRITE
903# define pread aio_pread
904# define pwrite aio_pwrite
905
906/*
907 * make our pread/pwrite safe against themselves, but not against
908 * normal read/write by using a mutex. slows down execution a lot,
909 * but that's your problem, not mine.
910 */
911static mutex_t preadwritelock = X_MUTEX_INIT;
912
913static ssize_t pread (int fd, void *buf, size_t count, off_t offset)
914{
915 ssize_t res;
916 off_t ooffset;
917
918 X_LOCK (preadwritelock);
919 ooffset = lseek (fd, 0, SEEK_CUR);
920 lseek (fd, offset, SEEK_SET);
921 res = read (fd, buf, count);
922 lseek (fd, ooffset, SEEK_SET);
923 X_UNLOCK (preadwritelock);
924
925 return res;
926}
927
928static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset)
929{
930 ssize_t res;
931 off_t ooffset;
932
933 X_LOCK (preadwritelock);
934 ooffset = lseek (fd, 0, SEEK_CUR);
935 lseek (fd, offset, SEEK_SET);
936 res = write (fd, buf, count);
937 lseek (fd, offset, SEEK_SET);
938 X_UNLOCK (preadwritelock);
939
940 return res;
941}
942#endif
943
944#ifndef HAVE_FUTIMES
945
946# define utimes(path,times) aio_utimes (path, times)
947# define futimes(fd,times) aio_futimes (fd, times)
948
949int aio_utimes (const char *filename, const struct timeval times[2])
950{
951 if (times)
952 {
953 struct utimbuf buf;
954
955 buf.actime = times[0].tv_sec;
956 buf.modtime = times[1].tv_sec;
957
958 return utime (filename, &buf);
959 }
960 else
961 return utime (filename, 0);
962}
963
964int aio_futimes (int fd, const struct timeval tv[2])
965{
966 errno = ENOSYS;
967 return -1;
968}
969
970#endif
971
972#if !HAVE_FDATASYNC
973# define fdatasync fsync
974#endif
975
976#if !HAVE_READAHEAD
977# define readahead(fd,offset,count) aio_readahead (fd, offset, count, self)
978
979static ssize_t aio_readahead (int fd, off_t offset, size_t count, worker *self)
980{
981 size_t todo = count;
982 dBUF;
983
984 while (todo > 0)
985 {
986 size_t len = todo < AIO_BUFSIZE ? todo : AIO_BUFSIZE;
987
988 pread (fd, aio_buf, len, offset);
989 offset += len;
990 todo -= len;
991 }
992
993 errno = 0;
994 return count;
995}
996
997#endif
998
999#if !HAVE_READDIR_R
1000# define readdir_r aio_readdir_r
1001
1002static mutex_t readdirlock = X_MUTEX_INIT;
1003
1004static int readdir_r (DIR *dirp, X_DIRENT *ent, X_DIRENT **res)
1005{
1006 X_DIRENT *e;
1007 int errorno;
1008
1009 X_LOCK (readdirlock);
1010
1011 e = readdir (dirp);
1012 errorno = errno;
1013
1014 if (e)
1015 {
1016 *res = ent;
1017 strcpy (ent->d_name, e->d_name);
1018 }
1019 else
1020 *res = 0;
1021
1022 X_UNLOCK (readdirlock);
1023
1024 errno = errorno;
1025 return e ? 0 : -1;
1026}
1027#endif
1028
1029/* sendfile always needs emulation */
1030static ssize_t sendfile_ (int ofd, int ifd, off_t offset, size_t count, worker *self)
1031{
1032 ssize_t res;
1033
1034 if (!count)
1035 return 0;
1036
1037#if HAVE_SENDFILE
1038# if __linux
1039 res = sendfile (ofd, ifd, &offset, count);
1040
1041# elif __freebsd
1042 /*
1043 * Of course, the freebsd sendfile is a dire hack with no thoughts
1044 * wasted on making it similar to other I/O functions.
1045 */
1046 {
1047 off_t sbytes;
1048 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0);
1049
1050 if (res < 0 && sbytes)
1051 /* maybe only on EAGAIN: as usual, the manpage leaves you guessing */
1052 res = sbytes;
1053 }
1054
1055# elif __hpux
1056 res = sendfile (ofd, ifd, offset, count, 0, 0);
1057
1058# elif __solaris
1059 {
1060 struct sendfilevec vec;
1061 size_t sbytes;
1062
1063 vec.sfv_fd = ifd;
1064 vec.sfv_flag = 0;
1065 vec.sfv_off = offset;
1066 vec.sfv_len = count;
1067
1068 res = sendfilev (ofd, &vec, 1, &sbytes);
1069
1070 if (res < 0 && sbytes)
1071 res = sbytes;
1072 }
1073
1074# endif
1075#else
1076 res = -1;
1077 errno = ENOSYS;
1078#endif
1079
1080 if (res < 0
1081 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK
1082#if __solaris
1083 || errno == EAFNOSUPPORT || errno == EPROTOTYPE
1084#endif
1085 )
1086 )
1087 {
1088 /* emulate sendfile. this is a major pain in the ass */
1089 dBUF;
1090
1091 res = 0;
1092
1093 while (count)
1094 {
1095 ssize_t cnt;
1096
1097 cnt = pread (ifd, aio_buf, count > AIO_BUFSIZE ? AIO_BUFSIZE : count, offset);
1098
1099 if (cnt <= 0)
1100 {
1101 if (cnt && !res) res = -1;
1102 break;
1103 }
1104
1105 cnt = write (ofd, aio_buf, cnt);
1106
1107 if (cnt <= 0)
1108 {
1109 if (cnt && !res) res = -1;
1110 break;
1111 }
1112
1113 offset += cnt;
1114 res += cnt;
1115 count -= cnt;
1116 }
1117 }
1118
1119 return res;
1120}
1121
1122/* read a full directory */
1123static void scandir_ (aio_req req, worker *self)
1124{
1125 DIR *dirp;
1126 union
1127 {
1128 X_DIRENT d;
1129 char b [offsetof (X_DIRENT, d_name) + NAME_MAX + 1];
1130 } *u;
1131 X_DIRENT *entp;
1132 char *name, *names;
1133 int memlen = 4096;
1134 int memofs = 0;
1135 int res = 0;
1136
1137 X_LOCK (wrklock);
1138 self->dirp = dirp = opendir (req->ptr1);
1139 self->dbuf = u = malloc (sizeof (*u));
1140 req->flags |= FLAG_PTR2_FREE;
1141 req->ptr2 = names = malloc (memlen);
1142 X_UNLOCK (wrklock);
1143
1144 if (dirp && u && names)
1145 for (;;)
1146 {
1147 errno = 0;
1148 readdir_r (dirp, &u->d, &entp);
1149
1150 if (!entp)
1151 break;
1152
1153 name = entp->d_name;
1154
1155 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2])))
1156 {
1157 int len = strlen (name) + 1;
1158
1159 res++;
1160
1161 while (memofs + len > memlen)
1162 {
1163 memlen *= 2;
1164 X_LOCK (wrklock);
1165 req->ptr2 = names = realloc (names, memlen);
1166 X_UNLOCK (wrklock);
1167
1168 if (!names)
1169 break;
1170 }
1171
1172 memcpy (names + memofs, name, len);
1173 memofs += len;
1174 }
1175 }
1176
1177 if (errno)
1178 res = -1;
1179
1180 req->result = res;
1181}
1182
1183/*****************************************************************************/
1184
1185X_THREAD_PROC (aio_proc)
1186{
1187 aio_req req;
1188 struct timespec ts;
1189 worker *self = (worker *)thr_arg;
1190
1191 /* try to distribute timeouts somewhat randomly */
1192 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL);
1193
1194 for (;;)
1195 {
1196 ts.tv_sec = time (0) + IDLE_TIMEOUT;
1197
1198 X_LOCK (reqlock);
1199
1200 for (;;)
1201 {
1202 self->req = req = reqq_shift (&req_queue);
1203
1204 if (req)
1205 break;
1206
1207 ++idle;
1208
1209 if (X_COND_TIMEDWAIT (reqwait, reqlock, ts)
1210 == ETIMEDOUT)
1211 {
1212 if (idle > max_idle)
1213 {
1214 --idle;
1215 X_UNLOCK (reqlock);
1216 X_LOCK (wrklock);
1217 --started;
1218 X_UNLOCK (wrklock);
1219 goto quit;
1220 }
1221
1222 /* we are allowed to idle, so do so without any timeout */
1223 X_COND_WAIT (reqwait, reqlock);
1224 ts.tv_sec = time (0) + IDLE_TIMEOUT;
1225 }
1226
1227 --idle;
1228 }
1229
1230 --nready;
1231
1232 X_UNLOCK (reqlock);
1233
1234 errno = 0; /* strictly unnecessary */
1235
1236 if (!(req->flags & FLAG_CANCELLED))
1237 switch (req->type)
1238 {
1239 case REQ_READ: req->result = req->offs >= 0
1240 ? pread (req->int1, req->ptr1, req->size, req->offs)
1241 : read (req->int1, req->ptr1, req->size); break;
1242 case REQ_WRITE: req->result = req->offs >= 0
1243 ? pwrite (req->int1, req->ptr1, req->size, req->offs)
1244 : write (req->int1, req->ptr1, req->size); break;
1245
1246 case REQ_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break;
1247 case REQ_SENDFILE: req->result = sendfile_ (req->int1, req->int2, req->offs, req->size, self); break;
1248
1249 case REQ_STAT: req->result = stat (req->ptr1, (Stat_t *)req->ptr2); break;
1250 case REQ_LSTAT: req->result = lstat (req->ptr1, (Stat_t *)req->ptr2); break;
1251 case REQ_FSTAT: req->result = fstat (req->int1, (Stat_t *)req->ptr2); break;
1252
1253 case REQ_CHOWN: req->result = chown (req->ptr1, req->int2, req->int3); break;
1254 case REQ_FCHOWN: req->result = fchown (req->int1, req->int2, req->int3); break;
1255 case REQ_CHMOD: req->result = chmod (req->ptr1, req->mode); break;
1256 case REQ_FCHMOD: req->result = fchmod (req->int1, req->mode); break;
1257 case REQ_TRUNCATE: req->result = truncate (req->ptr1, req->offs); break;
1258 case REQ_FTRUNCATE: req->result = ftruncate (req->int1, req->offs); break;
1259
1260 case REQ_OPEN: req->result = open (req->ptr1, req->int1, req->mode); break;
1261 case REQ_CLOSE: req->result = close (req->int1); break;
1262 case REQ_UNLINK: req->result = unlink (req->ptr1); break;
1263 case REQ_RMDIR: req->result = rmdir (req->ptr1); break;
1264 case REQ_MKDIR: req->result = mkdir (req->ptr1, req->mode); break;
1265 case REQ_RENAME: req->result = rename (req->ptr2, req->ptr1); break;
1266 case REQ_LINK: req->result = link (req->ptr2, req->ptr1); break;
1267 case REQ_SYMLINK: req->result = symlink (req->ptr2, req->ptr1); break;
1268 case REQ_MKNOD: req->result = mknod (req->ptr2, req->mode, (dev_t)req->offs); break;
1269 case REQ_READLINK: req->result = readlink (req->ptr2, req->ptr1, NAME_MAX); break;
1270
1271 case REQ_SYNC: req->result = 0; sync (); break;
1272 case REQ_FSYNC: req->result = fsync (req->int1); break;
1273 case REQ_FDATASYNC: req->result = fdatasync (req->int1); break;
1274
1275 case REQ_READDIR: scandir_ (req, self); break;
1276
1277 case REQ_BUSY:
1278#ifdef _WIN32
1279 Sleep (req->nv1 * 1000.);
1280#else
1281 {
1282 struct timeval tv;
1283
1284 tv.tv_sec = req->nv1;
1285 tv.tv_usec = (req->nv1 - tv.tv_sec) * 1000000.;
1286
1287 req->result = select (0, 0, 0, 0, &tv);
1288 }
1289#endif
1290 break;
1291
1292 case REQ_UTIME:
1293 case REQ_FUTIME:
1294 {
1295 struct timeval tv[2];
1296 struct timeval *times;
1297
1298 if (req->nv1 != -1. || req->nv2 != -1.)
1299 {
1300 tv[0].tv_sec = req->nv1;
1301 tv[0].tv_usec = (req->nv1 - tv[0].tv_sec) * 1000000.;
1302 tv[1].tv_sec = req->nv2;
1303 tv[1].tv_usec = (req->nv2 - tv[1].tv_sec) * 1000000.;
1304
1305 times = tv;
1306 }
1307 else
1308 times = 0;
1309
1310
1311 req->result = req->type == REQ_FUTIME
1312 ? futimes (req->int1, times)
1313 : utimes (req->ptr1, times);
1314 }
1315
1316 case REQ_GROUP:
1317 case REQ_NOP:
1318 break;
1319
1320 case REQ_QUIT:
1321 goto quit;
1322
1323 default:
1324 req->result = -1;
1325 break;
1326 }
1327
1328 req->errorno = errno;
1329
1330 X_LOCK (reslock);
1331
1332 ++npending;
1333
1334 if (!reqq_push (&res_queue, req))
1335 {
1336 /* write a dummy byte to the pipe so fh becomes ready */
1337 respipe_write (respipe_osf [1], (const void *)&respipe_osf, 1);
1338
1339 /* optionally signal the main thread asynchronously */
1340 if (main_sig)
1341 pthread_kill (main_tid, main_sig);
1342 }
1343
1344 self->req = 0;
1345 worker_clear (self);
1346
1347 X_UNLOCK (reslock);
1348 }
1349
1350quit:
1351 X_LOCK (wrklock);
1352 worker_free (self);
1353 X_UNLOCK (wrklock);
1354
1355 return 0;
1356}
1357
1358/*****************************************************************************/
1359
1360static void atfork_prepare (void)
1361{
1362 X_LOCK (wrklock);
1363 X_LOCK (reqlock);
1364 X_LOCK (reslock);
1365#if !HAVE_PREADWRITE
1366 X_LOCK (preadwritelock);
1367#endif
1368#if !HAVE_READDIR_R
1369 X_LOCK (readdirlock);
1370#endif
1371}
1372
1373static void atfork_parent (void)
1374{
1375#if !HAVE_READDIR_R
1376 X_UNLOCK (readdirlock);
1377#endif
1378#if !HAVE_PREADWRITE
1379 X_UNLOCK (preadwritelock);
1380#endif
1381 X_UNLOCK (reslock);
1382 X_UNLOCK (reqlock);
1383 X_UNLOCK (wrklock);
1384}
1385
1386static void atfork_child (void)
1387{
1388 aio_req prv;
1389
1390 while (prv = reqq_shift (&req_queue))
1391 req_destroy (prv);
1392
1393 while (prv = reqq_shift (&res_queue))
1394 req_destroy (prv);
1395
1396 while (wrk_first.next != &wrk_first)
1397 {
1398 worker *wrk = wrk_first.next;
1399
1400 if (wrk->req)
1401 req_destroy (wrk->req);
1402
1403 worker_clear (wrk);
1404 worker_free (wrk);
1405 }
1406
1407 started = 0;
1408 idle = 0;
1409 nreqs = 0;
1410 nready = 0;
1411 npending = 0;
1412
1413 create_respipe ();
1414
1415 atfork_parent ();
1416}
1417
1418#define dREQ \
1419 aio_req req; \
1420 int req_pri = next_pri; \
1421 next_pri = DEFAULT_PRI + PRI_BIAS; \
1422 \
1423 if (SvOK (callback) && !SvROK (callback)) \
1424 croak ("callback must be undef or of reference type"); \
1425 \
1426 Newz (0, req, 1, aio_cb); \
1427 if (!req) \
1428 croak ("out of memory during aio_req allocation"); \
1429 \
1430 req->callback = newSVsv (callback); \
1431 req->pri = req_pri
1432
1433#define REQ_SEND \
1434 req_send (req); \
1435 \
1436 if (GIMME_V != G_VOID) \
1437 XPUSHs (req_sv (req, AIO_REQ_KLASS));
1438
1439MODULE = IO::AIO PACKAGE = IO::AIO 912MODULE = IO::AIO PACKAGE = IO::AIO
1440 913
1441PROTOTYPES: ENABLE 914PROTOTYPES: ENABLE
1442 915
1443BOOT: 916BOOT:
1444{ 917{
918 static const struct {
919 const char *name;
920 IV iv;
921 } *civ, const_iv[] = {
922# define const_niv(name, value) { # name, (IV) value },
923# define const_iv(name) { # name, (IV) name },
924# define const_eio(name) { # name, (IV) EIO_ ## name },
925 const_iv (EXDEV)
926 const_iv (ENOSYS)
927 const_iv (O_RDONLY)
928 const_iv (O_WRONLY)
929 const_iv (O_RDWR)
930 const_iv (O_CREAT)
931 const_iv (O_TRUNC)
932 const_iv (O_EXCL)
933 const_iv (O_APPEND)
934
935 const_iv (O_ASYNC)
936 const_iv (O_DIRECT)
937 const_iv (O_NOATIME)
938
939 const_iv (O_CLOEXEC)
940 const_iv (O_NOCTTY)
941 const_iv (O_NOFOLLOW)
942 const_iv (O_NONBLOCK)
943 const_iv (O_EXEC)
944 const_iv (O_SEARCH)
945 const_iv (O_DIRECTORY)
946 const_iv (O_DSYNC)
947 const_iv (O_RSYNC)
948 const_iv (O_SYNC)
949 const_iv (O_TTY_INIT)
950
951 const_iv (S_IFIFO)
952 const_iv (S_IFCHR)
953 const_iv (S_IFBLK)
954 const_iv (S_IFLNK)
955 const_iv (S_IFREG)
956 const_iv (S_IFDIR)
957 const_iv (S_IFWHT)
958 const_iv (S_IFSOCK)
959 const_iv (S_IFMT)
960
961 const_niv (FADV_NORMAL , POSIX_FADV_NORMAL)
962 const_niv (FADV_SEQUENTIAL, POSIX_FADV_SEQUENTIAL)
963 const_niv (FADV_RANDOM , POSIX_FADV_RANDOM)
964 const_niv (FADV_NOREUSE , POSIX_FADV_NOREUSE)
965 const_niv (FADV_WILLNEED , POSIX_FADV_WILLNEED)
966 const_niv (FADV_DONTNEED , POSIX_FADV_DONTNEED)
967
968 const_niv (MADV_NORMAL , POSIX_MADV_NORMAL)
969 const_niv (MADV_SEQUENTIAL, POSIX_MADV_SEQUENTIAL)
970 const_niv (MADV_RANDOM , POSIX_MADV_RANDOM)
971 const_niv (MADV_WILLNEED , POSIX_MADV_WILLNEED)
972 const_niv (MADV_DONTNEED , POSIX_MADV_DONTNEED)
973
974 const_iv (ST_RDONLY)
975 const_iv (ST_NOSUID)
976 const_iv (ST_NODEV)
977 const_iv (ST_NOEXEC)
978 const_iv (ST_SYNCHRONOUS)
979 const_iv (ST_MANDLOCK)
980 const_iv (ST_WRITE)
981 const_iv (ST_APPEND)
982 const_iv (ST_IMMUTABLE)
983 const_iv (ST_NOATIME)
984 const_iv (ST_NODIRATIME)
985 const_iv (ST_RELATIME)
986
987 const_iv (PROT_NONE)
988 const_iv (PROT_EXEC)
989 const_iv (PROT_READ)
990 const_iv (PROT_WRITE)
991
992 /*const_iv (MAP_FIXED)*/
993 const_iv (MAP_PRIVATE)
994 const_iv (MAP_SHARED)
995 const_iv (MAP_ANONYMOUS)
996
997 /* linuxish */
998 const_iv (MAP_HUGETLB)
999 const_iv (MAP_LOCKED)
1000 const_iv (MAP_NORESERVE)
1001 const_iv (MAP_POPULATE)
1002 const_iv (MAP_NONBLOCK)
1003
1004 const_eio (MCL_FUTURE)
1005 const_eio (MCL_CURRENT)
1006
1007 const_eio (MS_ASYNC)
1008 const_eio (MS_INVALIDATE)
1009 const_eio (MS_SYNC)
1010
1011 const_eio (MT_MODIFY)
1012
1013 const_eio (SYNC_FILE_RANGE_WAIT_BEFORE)
1014 const_eio (SYNC_FILE_RANGE_WRITE)
1015 const_eio (SYNC_FILE_RANGE_WAIT_AFTER)
1016
1017 const_eio (FALLOC_FL_KEEP_SIZE)
1018
1019 const_eio (READDIR_DENTS)
1020 const_eio (READDIR_DIRS_FIRST)
1021 const_eio (READDIR_STAT_ORDER)
1022 const_eio (READDIR_FOUND_UNKNOWN)
1023
1024 const_eio (DT_UNKNOWN)
1025 const_eio (DT_FIFO)
1026 const_eio (DT_CHR)
1027 const_eio (DT_DIR)
1028 const_eio (DT_BLK)
1029 const_eio (DT_REG)
1030 const_eio (DT_LNK)
1031 const_eio (DT_SOCK)
1032 const_eio (DT_WHT)
1033 };
1034
1445 stash = gv_stashpv ("IO::AIO", 1); 1035 aio_stash = gv_stashpv ("IO::AIO" , 1);
1036 aio_req_stash = gv_stashpv ("IO::AIO::REQ", 1);
1037 aio_grp_stash = gv_stashpv ("IO::AIO::GRP", 1);
1038 aio_wd_stash = gv_stashpv ("IO::AIO::WD" , 1);
1446 1039
1447 newCONSTSUB (stash, "EXDEV", newSViv (EXDEV)); 1040 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--)
1448 newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY)); 1041 newCONSTSUB (aio_stash, (char *)civ[-1].name, newSViv (civ[-1].iv));
1449 newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY));
1450 newCONSTSUB (stash, "O_CREAT", newSViv (O_CREAT));
1451 newCONSTSUB (stash, "O_TRUNC", newSViv (O_TRUNC));
1452#ifdef _WIN32
1453 X_MUTEX_CHECK (wrklock);
1454 X_MUTEX_CHECK (reslock);
1455 X_MUTEX_CHECK (reqlock);
1456 X_MUTEX_CHECK (reqwait);
1457 X_MUTEX_CHECK (preadwritelock);
1458 X_MUTEX_CHECK (readdirlock);
1459 1042
1460 X_COND_CHECK (reqwait); 1043 newCONSTSUB (aio_stash, "PAGESIZE", newSViv (PAGESIZE));
1461#else
1462 newCONSTSUB (stash, "S_IFIFO", newSViv (S_IFIFO));
1463 newCONSTSUB (stash, "SIGIO", newSViv (SIGIO));
1464#endif
1465 1044
1466 create_respipe (); 1045 reinit ();
1467
1468 X_THREAD_ATFORK (atfork_prepare, atfork_parent, atfork_child);
1469} 1046}
1470 1047
1471void 1048void
1049reinit ()
1050 PROTOTYPE:
1051
1052void
1472max_poll_reqs (int nreqs) 1053max_poll_reqs (unsigned int nreqs)
1473 PROTOTYPE: $ 1054 PROTOTYPE: $
1474 CODE: 1055 CODE:
1475 max_poll_reqs = nreqs; 1056 eio_set_max_poll_reqs (nreqs);
1476 1057
1477void 1058void
1478max_poll_time (double nseconds) 1059max_poll_time (double nseconds)
1479 PROTOTYPE: $ 1060 PROTOTYPE: $
1480 CODE: 1061 CODE:
1481 max_poll_time = nseconds * AIO_TICKS; 1062 eio_set_max_poll_time (nseconds);
1482 1063
1483void 1064void
1484min_parallel (int nthreads) 1065min_parallel (unsigned int nthreads)
1485 PROTOTYPE: $
1486
1487void
1488max_parallel (int nthreads)
1489 PROTOTYPE: $
1490
1491void
1492max_idle (int nthreads)
1493 PROTOTYPE: $ 1066 PROTOTYPE: $
1494 CODE: 1067 CODE:
1495 set_max_idle (nthreads); 1068 eio_set_min_parallel (nthreads);
1496 1069
1497int 1070void
1498max_outstanding (int maxreqs) 1071max_parallel (unsigned int nthreads)
1499 PROTOTYPE: $ 1072 PROTOTYPE: $
1500 CODE: 1073 CODE:
1501 RETVAL = max_outstanding; 1074 eio_set_max_parallel (nthreads);
1075
1076void
1077max_idle (unsigned int nthreads)
1078 PROTOTYPE: $
1079 CODE:
1080 eio_set_max_idle (nthreads);
1081
1082void
1083idle_timeout (unsigned int seconds)
1084 PROTOTYPE: $
1085 CODE:
1086 eio_set_idle_timeout (seconds);
1087
1088void
1089max_outstanding (unsigned int maxreqs)
1090 PROTOTYPE: $
1091 CODE:
1502 max_outstanding = maxreqs; 1092 max_outstanding = maxreqs;
1503 OUTPUT: 1093
1504 RETVAL 1094void
1095aio_wd (SV8 *pathname, SV *callback=&PL_sv_undef)
1096 PPCODE:
1097{
1098 dREQ;
1099
1100 req->type = EIO_WD_OPEN;
1101 req_set_path1 (req, pathname);
1102
1103 REQ_SEND;
1104}
1505 1105
1506void 1106void
1507aio_open (SV8 *pathname, int flags, int mode, SV *callback=&PL_sv_undef) 1107aio_open (SV8 *pathname, int flags, int mode, SV *callback=&PL_sv_undef)
1508 PROTOTYPE: $$$;$
1509 PPCODE: 1108 PPCODE:
1510{ 1109{
1511 dREQ; 1110 dREQ;
1512 1111
1513 req->type = REQ_OPEN; 1112 req->type = EIO_OPEN;
1514 req->sv1 = newSVsv (pathname); 1113 req_set_path1 (req, pathname);
1515 req->ptr1 = SvPVbyte_nolen (req->sv1);
1516 req->int1 = flags; 1114 req->int1 = flags;
1517 req->mode = mode; 1115 req->int2 = mode;
1518 1116
1519 REQ_SEND; 1117 REQ_SEND;
1520} 1118}
1521 1119
1522void 1120void
1523aio_fsync (SV *fh, SV *callback=&PL_sv_undef) 1121aio_fsync (SV *fh, SV *callback=&PL_sv_undef)
1524 PROTOTYPE: $;$
1525 ALIAS: 1122 ALIAS:
1526 aio_fsync = REQ_FSYNC 1123 aio_fsync = EIO_FSYNC
1527 aio_fdatasync = REQ_FDATASYNC 1124 aio_fdatasync = EIO_FDATASYNC
1125 aio_syncfs = EIO_SYNCFS
1528 PPCODE: 1126 PPCODE:
1529{ 1127{
1128 int fd = s_fileno_croak (fh, 0);
1530 dREQ; 1129 dREQ;
1531 1130
1532 req->type = ix; 1131 req->type = ix;
1533 req->sv1 = newSVsv (fh); 1132 req->sv1 = newSVsv (fh);
1534 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh))); 1133 req->int1 = fd;
1535 1134
1536 REQ_SEND (req); 1135 REQ_SEND (req);
1537} 1136}
1538 1137
1539int
1540_dup (int fd)
1541 PROTOTYPE: $
1542 CODE:
1543 RETVAL = dup (fd);
1544 OUTPUT:
1545 RETVAL
1546
1547void 1138void
1548_aio_close (int fd, SV *callback=&PL_sv_undef) 1139aio_sync_file_range (SV *fh, off_t offset, size_t nbytes, UV flags, SV *callback=&PL_sv_undef)
1549 PROTOTYPE: $;$
1550 PPCODE: 1140 PPCODE:
1551{ 1141{
1142 int fd = s_fileno_croak (fh, 0);
1552 dREQ; 1143 dREQ;
1553 1144
1554 req->type = REQ_CLOSE; 1145 req->type = EIO_SYNC_FILE_RANGE;
1146 req->sv1 = newSVsv (fh);
1555 req->int1 = fd; 1147 req->int1 = fd;
1148 req->offs = offset;
1149 req->size = nbytes;
1150 req->int2 = flags;
1151
1152 REQ_SEND (req);
1153}
1154
1155void
1156aio_fallocate (SV *fh, int mode, off_t offset, size_t len, SV *callback=&PL_sv_undef)
1157 PPCODE:
1158{
1159 int fd = s_fileno_croak (fh, 0);
1160 dREQ;
1161
1162 req->type = EIO_FALLOCATE;
1163 req->sv1 = newSVsv (fh);
1164 req->int1 = fd;
1165 req->int2 = mode;
1166 req->offs = offset;
1167 req->size = len;
1168
1169 REQ_SEND (req);
1170}
1171
1172void
1173aio_close (SV *fh, SV *callback=&PL_sv_undef)
1174 PPCODE:
1175{
1176 static int close_fd = -1; /* dummy fd to close fds via dup2 */
1177 int fd = s_fileno_croak (fh, 0);
1178 dREQ;
1179
1180 if (expect_false (close_fd < 0))
1181 {
1182 int pipefd [2];
1183
1184 if (
1185#ifdef _WIN32
1186 _pipe (pipefd, 1, _O_BINARY) < 0
1187#else
1188 pipe (pipefd) < 0
1189 || fcntl (pipefd [0], F_SETFD, FD_CLOEXEC) < 0
1190#endif
1191 || close (pipefd [1]) < 0
1192 )
1193 abort (); /*D*/
1194
1195 close_fd = pipefd [0];
1196 }
1197
1198 req->type = EIO_DUP2;
1199 req->int1 = close_fd;
1200 req->sv2 = newSVsv (fh);
1201 req->int2 = fd;
1556 1202
1557 REQ_SEND (req); 1203 REQ_SEND (req);
1558} 1204}
1559 1205
1560void 1206void
1561aio_read (SV *fh, SV *offset, SV *length, SV8 *data, IV dataoffset, SV *callback=&PL_sv_undef) 1207aio_read (SV *fh, SV *offset, SV *length, SV8 *data, IV dataoffset, SV *callback=&PL_sv_undef)
1562 ALIAS: 1208 ALIAS:
1563 aio_read = REQ_READ 1209 aio_read = EIO_READ
1564 aio_write = REQ_WRITE 1210 aio_write = EIO_WRITE
1565 PROTOTYPE: $$$$$;$
1566 PPCODE: 1211 PPCODE:
1567{ 1212{
1568 STRLEN svlen; 1213 STRLEN svlen;
1214 int fd = s_fileno_croak (fh, ix == EIO_WRITE);
1569 char *svptr = SvPVbyte (data, svlen); 1215 char *svptr = SvPVbyte (data, svlen);
1570 UV len = SvUV (length); 1216 UV len = SvUV (length);
1571 1217
1572 SvUPGRADE (data, SVt_PV);
1573 SvPOK_on (data);
1574
1575 if (dataoffset < 0) 1218 if (dataoffset < 0)
1576 dataoffset += svlen; 1219 dataoffset += svlen;
1577 1220
1578 if (dataoffset < 0 || dataoffset > svlen) 1221 if (dataoffset < 0 || dataoffset > svlen)
1579 croak ("dataoffset outside of data scalar"); 1222 croak ("dataoffset outside of data scalar");
1580 1223
1581 if (ix == REQ_WRITE) 1224 if (ix == EIO_WRITE)
1582 { 1225 {
1583 /* write: check length and adjust. */ 1226 /* write: check length and adjust. */
1584 if (!SvOK (length) || len + dataoffset > svlen) 1227 if (!SvOK (length) || len + dataoffset > svlen)
1585 len = svlen - dataoffset; 1228 len = svlen - dataoffset;
1586 } 1229 }
1587 else 1230 else
1588 { 1231 {
1589 /* read: grow scalar as necessary */ 1232 /* read: check type and grow scalar as necessary */
1233 SvUPGRADE (data, SVt_PV);
1590 svptr = SvGROW (data, len + dataoffset + 1); 1234 svptr = SvGROW (data, len + dataoffset + 1);
1591 } 1235 }
1592 1236
1593 if (len < 0)
1594 croak ("length must not be negative");
1595
1596 { 1237 {
1597 dREQ; 1238 dREQ;
1598 1239
1599 req->type = ix; 1240 req->type = ix;
1600 req->sv1 = newSVsv (fh); 1241 req->sv1 = newSVsv (fh);
1601 req->int1 = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh)) 1242 req->int1 = fd;
1602 : IoOFP (sv_2io (fh)));
1603 req->offs = SvOK (offset) ? SvVAL64 (offset) : -1; 1243 req->offs = SvOK (offset) ? SvVAL64 (offset) : -1;
1604 req->size = len; 1244 req->size = len;
1605 req->sv2 = SvREFCNT_inc (data); 1245 req->sv2 = SvREFCNT_inc (data);
1606 req->ptr1 = (char *)svptr + dataoffset; 1246 req->ptr2 = (char *)svptr + dataoffset;
1607 req->stroffset = dataoffset; 1247 req->stroffset = dataoffset;
1608 1248
1609 if (!SvREADONLY (data)) 1249 if (!SvREADONLY (data))
1610 { 1250 {
1611 SvREADONLY_on (data); 1251 SvREADONLY_on (data);
1615 REQ_SEND; 1255 REQ_SEND;
1616 } 1256 }
1617} 1257}
1618 1258
1619void 1259void
1620aio_readlink (SV8 *path, SV *callback=&PL_sv_undef) 1260aio_readlink (SV8 *pathname, SV *callback=&PL_sv_undef)
1621 PROTOTYPE: $$;$ 1261 ALIAS:
1262 aio_readlink = EIO_READLINK
1263 aio_realpath = EIO_REALPATH
1622 PPCODE: 1264 PPCODE:
1623{ 1265{
1624 SV *data;
1625 dREQ; 1266 dREQ;
1626 1267
1627 data = newSV (NAME_MAX);
1628 SvPOK_on (data);
1629
1630 req->type = REQ_READLINK; 1268 req->type = ix;
1631 req->sv1 = newSVsv (path); 1269 req_set_path1 (req, pathname);
1632 req->ptr2 = SvPVbyte_nolen (req->sv1);
1633 req->sv2 = data;
1634 req->ptr1 = SvPVbyte_nolen (data);
1635 1270
1636 REQ_SEND; 1271 REQ_SEND;
1637} 1272}
1638 1273
1639void 1274void
1640aio_sendfile (SV *out_fh, SV *in_fh, SV *in_offset, UV length, SV *callback=&PL_sv_undef) 1275aio_sendfile (SV *out_fh, SV *in_fh, off_t in_offset, size_t length, SV *callback=&PL_sv_undef)
1641 PROTOTYPE: $$$$;$
1642 PPCODE: 1276 PPCODE:
1643{ 1277{
1278 int ifd = s_fileno_croak (in_fh , 0);
1279 int ofd = s_fileno_croak (out_fh, 1);
1644 dREQ; 1280 dREQ;
1645 1281
1646 req->type = REQ_SENDFILE; 1282 req->type = EIO_SENDFILE;
1647 req->sv1 = newSVsv (out_fh); 1283 req->sv1 = newSVsv (out_fh);
1648 req->int1 = PerlIO_fileno (IoIFP (sv_2io (out_fh))); 1284 req->int1 = ofd;
1649 req->sv2 = newSVsv (in_fh); 1285 req->sv2 = newSVsv (in_fh);
1650 req->int2 = PerlIO_fileno (IoIFP (sv_2io (in_fh))); 1286 req->int2 = ifd;
1651 req->offs = SvVAL64 (in_offset); 1287 req->offs = in_offset;
1652 req->size = length; 1288 req->size = length;
1653 1289
1654 REQ_SEND; 1290 REQ_SEND;
1655} 1291}
1656 1292
1657void 1293void
1658aio_readahead (SV *fh, SV *offset, IV length, SV *callback=&PL_sv_undef) 1294aio_readahead (SV *fh, off_t offset, size_t length, SV *callback=&PL_sv_undef)
1659 PROTOTYPE: $$$;$
1660 PPCODE: 1295 PPCODE:
1661{ 1296{
1297 int fd = s_fileno_croak (fh, 0);
1662 dREQ; 1298 dREQ;
1663 1299
1664 req->type = REQ_READAHEAD; 1300 req->type = EIO_READAHEAD;
1665 req->sv1 = newSVsv (fh); 1301 req->sv1 = newSVsv (fh);
1666 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh))); 1302 req->int1 = fd;
1667 req->offs = SvVAL64 (offset); 1303 req->offs = offset;
1668 req->size = length; 1304 req->size = length;
1669 1305
1670 REQ_SEND; 1306 REQ_SEND;
1671} 1307}
1672 1308
1673void 1309void
1674aio_stat (SV8 *fh_or_path, SV *callback=&PL_sv_undef) 1310aio_stat (SV8 *fh_or_path, SV *callback=&PL_sv_undef)
1675 ALIAS: 1311 ALIAS:
1676 aio_stat = REQ_STAT 1312 aio_stat = EIO_STAT
1677 aio_lstat = REQ_LSTAT 1313 aio_lstat = EIO_LSTAT
1314 aio_statvfs = EIO_STATVFS
1678 PPCODE: 1315 PPCODE:
1679{ 1316{
1680 dREQ; 1317 dREQ;
1681 1318
1682 req->ptr2 = malloc (sizeof (Stat_t)); 1319 req_set_fh_or_path (req, ix, ix == EIO_STATVFS ? EIO_FSTATVFS : EIO_FSTAT, fh_or_path);
1683 if (!req->ptr2)
1684 {
1685 req_destroy (req);
1686 croak ("out of memory during aio_stat statdata allocation");
1687 }
1688
1689 req->flags |= FLAG_PTR2_FREE;
1690 req->sv1 = newSVsv (fh_or_path);
1691
1692 if (SvPOK (fh_or_path))
1693 {
1694 req->type = ix;
1695 req->ptr1 = SvPVbyte_nolen (req->sv1);
1696 }
1697 else
1698 {
1699 req->type = REQ_FSTAT;
1700 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
1701 }
1702 1320
1703 REQ_SEND; 1321 REQ_SEND;
1704} 1322}
1323
1324UV
1325major (UV dev)
1326 ALIAS:
1327 minor = 1
1328 CODE:
1329 RETVAL = ix ? major (dev) : minor (dev);
1330 OUTPUT:
1331 RETVAL
1332
1333UV
1334makedev (UV maj, UV min)
1335 CODE:
1336 RETVAL = makedev (maj, min);
1337 OUTPUT:
1338 RETVAL
1705 1339
1706void 1340void
1707aio_utime (SV8 *fh_or_path, SV *atime, SV *mtime, SV *callback=&PL_sv_undef) 1341aio_utime (SV8 *fh_or_path, SV *atime, SV *mtime, SV *callback=&PL_sv_undef)
1708 PPCODE: 1342 PPCODE:
1709{ 1343{
1710 dREQ; 1344 dREQ;
1711 1345
1712 req->nv1 = SvOK (atime) ? SvNV (atime) : -1.; 1346 req->nv1 = SvOK (atime) ? SvNV (atime) : -1.;
1713 req->nv2 = SvOK (mtime) ? SvNV (mtime) : -1.; 1347 req->nv2 = SvOK (mtime) ? SvNV (mtime) : -1.;
1714 req->sv1 = newSVsv (fh_or_path); 1348 req_set_fh_or_path (req, EIO_UTIME, EIO_FUTIME, fh_or_path);
1715
1716 if (SvPOK (fh_or_path))
1717 {
1718 req->type = REQ_UTIME;
1719 req->ptr1 = SvPVbyte_nolen (req->sv1);
1720 }
1721 else
1722 {
1723 req->type = REQ_FUTIME;
1724 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
1725 }
1726 1349
1727 REQ_SEND; 1350 REQ_SEND;
1728} 1351}
1729 1352
1730void 1353void
1731aio_truncate (SV8 *fh_or_path, SV *offset, SV *callback=&PL_sv_undef) 1354aio_truncate (SV8 *fh_or_path, SV *offset, SV *callback=&PL_sv_undef)
1732 PPCODE: 1355 PPCODE:
1733{ 1356{
1734 dREQ; 1357 dREQ;
1735 1358
1736 req->sv1 = newSVsv (fh_or_path);
1737 req->offs = SvOK (offset) ? SvVAL64 (offset) : -1; 1359 req->offs = SvOK (offset) ? SvVAL64 (offset) : -1;
1738 1360 req_set_fh_or_path (req, EIO_TRUNCATE, EIO_FTRUNCATE, fh_or_path);
1739 if (SvPOK (fh_or_path))
1740 {
1741 req->type = REQ_TRUNCATE;
1742 req->ptr1 = SvPVbyte_nolen (req->sv1);
1743 }
1744 else
1745 {
1746 req->type = REQ_FTRUNCATE;
1747 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
1748 }
1749 1361
1750 REQ_SEND; 1362 REQ_SEND;
1751} 1363}
1752 1364
1753void 1365void
1754aio_chmod (SV8 *fh_or_path, int mode, SV *callback=&PL_sv_undef) 1366aio_chmod (SV8 *fh_or_path, int mode, SV *callback=&PL_sv_undef)
1755 PPCODE: 1367 PPCODE:
1756{ 1368{
1757 dREQ; 1369 dREQ;
1758 1370
1759 req->mode = mode; 1371 req->int2 = mode;
1760 req->sv1 = newSVsv (fh_or_path); 1372 req_set_fh_or_path (req, EIO_CHMOD, EIO_FCHMOD, fh_or_path);
1761
1762 if (SvPOK (fh_or_path))
1763 {
1764 req->type = REQ_CHMOD;
1765 req->ptr1 = SvPVbyte_nolen (req->sv1);
1766 }
1767 else
1768 {
1769 req->type = REQ_FCHMOD;
1770 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
1771 }
1772 1373
1773 REQ_SEND; 1374 REQ_SEND;
1774} 1375}
1775 1376
1776void 1377void
1779{ 1380{
1780 dREQ; 1381 dREQ;
1781 1382
1782 req->int2 = SvOK (uid) ? SvIV (uid) : -1; 1383 req->int2 = SvOK (uid) ? SvIV (uid) : -1;
1783 req->int3 = SvOK (gid) ? SvIV (gid) : -1; 1384 req->int3 = SvOK (gid) ? SvIV (gid) : -1;
1784 req->sv1 = newSVsv (fh_or_path); 1385 req_set_fh_or_path (req, EIO_CHOWN, EIO_FCHOWN, fh_or_path);
1785 1386
1786 if (SvPOK (fh_or_path)) 1387 REQ_SEND;
1787 { 1388}
1788 req->type = REQ_CHOWN; 1389
1789 req->ptr1 = SvPVbyte_nolen (req->sv1); 1390void
1790 } 1391aio_readdirx (SV8 *pathname, IV flags, SV *callback=&PL_sv_undef)
1791 else 1392 PPCODE:
1792 { 1393{
1793 req->type = REQ_FCHOWN; 1394 dREQ;
1794 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); 1395
1795 } 1396 req->type = EIO_READDIR;
1397 req->int1 = flags | EIO_READDIR_DENTS | EIO_READDIR_CUSTOM1;
1398
1399 if (flags & EIO_READDIR_DENTS)
1400 req->int1 |= EIO_READDIR_CUSTOM2;
1401
1402 req_set_path1 (req, pathname);
1403
1404 REQ_SEND;
1405}
1406
1407void
1408aio_mkdir (SV8 *pathname, int mode, SV *callback=&PL_sv_undef)
1409 PPCODE:
1410{
1411 dREQ;
1412
1413 req->type = EIO_MKDIR;
1414 req->int2 = mode;
1415 req_set_path1 (req, pathname);
1796 1416
1797 REQ_SEND; 1417 REQ_SEND;
1798} 1418}
1799 1419
1800void 1420void
1801aio_unlink (SV8 *pathname, SV *callback=&PL_sv_undef) 1421aio_unlink (SV8 *pathname, SV *callback=&PL_sv_undef)
1802 ALIAS: 1422 ALIAS:
1803 aio_unlink = REQ_UNLINK 1423 aio_unlink = EIO_UNLINK
1804 aio_rmdir = REQ_RMDIR 1424 aio_rmdir = EIO_RMDIR
1805 aio_readdir = REQ_READDIR 1425 aio_readdir = EIO_READDIR
1806 PPCODE: 1426 PPCODE:
1807{ 1427{
1808 dREQ; 1428 dREQ;
1809 1429
1810 req->type = ix; 1430 req->type = ix;
1811 req->sv1 = newSVsv (pathname); 1431 req_set_path1 (req, pathname);
1812 req->ptr1 = SvPVbyte_nolen (req->sv1);
1813 1432
1814 REQ_SEND; 1433 REQ_SEND;
1815} 1434}
1816 1435
1817void 1436void
1437aio_link (SV8 *oldpath, SV8 *newpath, SV *callback=&PL_sv_undef)
1438 ALIAS:
1439 aio_link = EIO_LINK
1440 aio_symlink = EIO_SYMLINK
1441 aio_rename = EIO_RENAME
1442 PPCODE:
1443{
1444 dREQ;
1445 eio_wd wd2 = 0;
1446
1447 req->type = ix;
1448 req_set_path1 (req, oldpath);
1449 req_set_path (req, newpath, &req->sv2, &req->sv4, &wd2, &req->ptr2);
1450 req->int3 = (long)wd2;
1451
1452 REQ_SEND;
1453}
1454
1455void
1818aio_mkdir (SV8 *pathname, int mode, SV *callback=&PL_sv_undef) 1456aio_mknod (SV8 *pathname, int mode, UV dev, SV *callback=&PL_sv_undef)
1819 PPCODE: 1457 PPCODE:
1820{ 1458{
1821 dREQ; 1459 dREQ;
1822 1460
1823 req->type = REQ_MKDIR; 1461 req->type = EIO_MKNOD;
1824 req->sv1 = newSVsv (pathname);
1825 req->ptr1 = SvPVbyte_nolen (req->sv1);
1826 req->mode = mode; 1462 req->int2 = (mode_t)mode;
1827 1463 req->offs = dev;
1828 REQ_SEND; 1464 req_set_path1 (req, pathname);
1829}
1830
1831void
1832aio_link (SV8 *oldpath, SV8 *newpath, SV *callback=&PL_sv_undef)
1833 ALIAS:
1834 aio_link = REQ_LINK
1835 aio_symlink = REQ_SYMLINK
1836 aio_rename = REQ_RENAME
1837 PPCODE:
1838{
1839 dREQ;
1840
1841 req->type = ix;
1842 req->sv2 = newSVsv (oldpath);
1843 req->ptr2 = SvPVbyte_nolen (req->sv2);
1844 req->sv1 = newSVsv (newpath);
1845 req->ptr1 = SvPVbyte_nolen (req->sv1);
1846 1465
1847 REQ_SEND; 1466 REQ_SEND;
1848} 1467}
1849 1468
1850void 1469void
1851aio_mknod (SV8 *pathname, int mode, UV dev, SV *callback=&PL_sv_undef) 1470aio_mtouch (SV8 *data, IV offset = 0, SV *length = &PL_sv_undef, int flags = 0, SV *callback=&PL_sv_undef)
1852 PPCODE: 1471 ALIAS:
1472 aio_mtouch = EIO_MTOUCH
1473 aio_msync = EIO_MSYNC
1474 PPCODE:
1853{ 1475{
1854 dREQ; 1476 STRLEN svlen;
1855 1477 char *svptr = SvPVbyte (data, svlen);
1478 UV len = SvUV (length);
1479
1480 if (offset < 0)
1481 offset += svlen;
1482
1483 if (offset < 0 || offset > svlen)
1484 croak ("offset outside of scalar");
1485
1486 if (!SvOK (length) || len + offset > svlen)
1487 len = svlen - offset;
1488
1489 {
1490 dREQ;
1491
1492 req->type = ix;
1493 req->sv2 = SvREFCNT_inc (data);
1494 req->ptr2 = (char *)svptr + offset;
1495 req->size = len;
1496 req->int1 = flags;
1497
1498 REQ_SEND;
1499 }
1500}
1501
1502void
1503aio_mlock (SV8 *data, IV offset = 0, SV *length = &PL_sv_undef, SV *callback=&PL_sv_undef)
1504 PPCODE:
1505{
1506 STRLEN svlen;
1507 char *svptr = SvPVbyte (data, svlen);
1508 UV len = SvUV (length);
1509
1510 if (offset < 0)
1511 offset += svlen;
1512
1513 if (offset < 0 || offset > svlen)
1514 croak ("offset outside of scalar");
1515
1516 if (!SvOK (length) || len + offset > svlen)
1517 len = svlen - offset;
1518
1519 {
1520 dREQ;
1521
1856 req->type = REQ_MKNOD; 1522 req->type = EIO_MLOCK;
1857 req->sv1 = newSVsv (pathname); 1523 req->sv2 = SvREFCNT_inc (data);
1858 req->ptr1 = SvPVbyte_nolen (req->sv1); 1524 req->ptr2 = (char *)svptr + offset;
1859 req->mode = (mode_t)mode;
1860 req->offs = dev; 1525 req->size = len;
1861 1526
1862 REQ_SEND; 1527 REQ_SEND;
1528 }
1529}
1530
1531void
1532aio_mlockall (IV flags, SV *callback=&PL_sv_undef)
1533 PPCODE:
1534{
1535 dREQ;
1536
1537 req->type = EIO_MLOCKALL;
1538 req->int1 = flags;
1539
1540 REQ_SEND;
1863} 1541}
1864 1542
1865void 1543void
1866aio_busy (double delay, SV *callback=&PL_sv_undef) 1544aio_busy (double delay, SV *callback=&PL_sv_undef)
1867 PPCODE: 1545 PPCODE:
1868{ 1546{
1869 dREQ; 1547 dREQ;
1870 1548
1871 req->type = REQ_BUSY; 1549 req->type = EIO_BUSY;
1872 req->nv1 = delay < 0. ? 0. : delay; 1550 req->nv1 = delay < 0. ? 0. : delay;
1873 1551
1874 REQ_SEND; 1552 REQ_SEND;
1875} 1553}
1876 1554
1877void 1555void
1878aio_group (SV *callback=&PL_sv_undef) 1556aio_group (SV *callback=&PL_sv_undef)
1879 PROTOTYPE: ;$
1880 PPCODE: 1557 PPCODE:
1881{ 1558{
1882 dREQ; 1559 dREQ;
1883 1560
1884 req->type = REQ_GROUP; 1561 req->type = EIO_GROUP;
1885 1562
1886 req_send (req); 1563 req_submit (req);
1887 XPUSHs (req_sv (req, AIO_GRP_KLASS)); 1564 XPUSHs (req_sv (req, aio_grp_stash));
1888} 1565}
1889 1566
1890void 1567void
1891aio_nop (SV *callback=&PL_sv_undef) 1568aio_nop (SV *callback=&PL_sv_undef)
1892 ALIAS: 1569 ALIAS:
1893 aio_nop = REQ_NOP 1570 aio_nop = EIO_NOP
1894 aio_sync = REQ_SYNC 1571 aio_sync = EIO_SYNC
1895 PPCODE: 1572 PPCODE:
1896{ 1573{
1897 dREQ; 1574 dREQ;
1898 1575
1899 req->type = ix; 1576 req->type = ix;
1901 REQ_SEND; 1578 REQ_SEND;
1902} 1579}
1903 1580
1904int 1581int
1905aioreq_pri (int pri = 0) 1582aioreq_pri (int pri = 0)
1906 PROTOTYPE: ;$
1907 CODE: 1583 CODE:
1908 RETVAL = next_pri - PRI_BIAS; 1584 RETVAL = next_pri;
1909 if (items > 0) 1585 if (items > 0)
1910 { 1586 {
1911 if (pri < PRI_MIN) pri = PRI_MIN; 1587 if (pri < EIO_PRI_MIN) pri = EIO_PRI_MIN;
1912 if (pri > PRI_MAX) pri = PRI_MAX; 1588 if (pri > EIO_PRI_MAX) pri = EIO_PRI_MAX;
1913 next_pri = pri + PRI_BIAS; 1589 next_pri = pri;
1914 } 1590 }
1915 OUTPUT: 1591 OUTPUT:
1916 RETVAL 1592 RETVAL
1917 1593
1918void 1594void
1919aioreq_nice (int nice = 0) 1595aioreq_nice (int nice = 0)
1920 CODE: 1596 CODE:
1921 nice = next_pri - nice; 1597 nice = next_pri - nice;
1922 if (nice < PRI_MIN) nice = PRI_MIN; 1598 if (nice < EIO_PRI_MIN) nice = EIO_PRI_MIN;
1923 if (nice > PRI_MAX) nice = PRI_MAX; 1599 if (nice > EIO_PRI_MAX) nice = EIO_PRI_MAX;
1924 next_pri = nice + PRI_BIAS; 1600 next_pri = nice;
1925 1601
1926void 1602void
1927flush () 1603flush ()
1928 PROTOTYPE:
1929 CODE: 1604 CODE:
1930 while (nreqs) 1605 while (eio_nreqs ())
1931 { 1606 {
1932 poll_wait (); 1607 poll_wait ();
1933 poll_cb (); 1608 poll_cb ();
1934 } 1609 }
1935 1610
1936int 1611int
1937poll() 1612poll ()
1938 PROTOTYPE:
1939 CODE: 1613 CODE:
1940 poll_wait (); 1614 poll_wait ();
1941 RETVAL = poll_cb (); 1615 RETVAL = poll_cb ();
1942 OUTPUT: 1616 OUTPUT:
1943 RETVAL 1617 RETVAL
1944 1618
1945int 1619int
1946poll_fileno() 1620poll_fileno ()
1947 PROTOTYPE:
1948 CODE: 1621 CODE:
1949 RETVAL = respipe [0]; 1622 RETVAL = s_epipe_fd (&respipe);
1950 OUTPUT: 1623 OUTPUT:
1951 RETVAL 1624 RETVAL
1952 1625
1953int 1626int
1954poll_cb(...) 1627poll_cb (...)
1955 PROTOTYPE: 1628 PROTOTYPE:
1956 CODE: 1629 CODE:
1957 RETVAL = poll_cb (); 1630 RETVAL = poll_cb ();
1958 OUTPUT: 1631 OUTPUT:
1959 RETVAL 1632 RETVAL
1960 1633
1961void 1634void
1962poll_wait() 1635poll_wait ()
1963 PROTOTYPE:
1964 CODE: 1636 CODE:
1965 poll_wait (); 1637 poll_wait ();
1966 1638
1967void
1968setsig (int signum = SIGIO)
1969 PROTOTYPE: ;$
1970 CODE:
1971{
1972 if (block_sig_level)
1973 croak ("cannot call IO::AIO::setsig from within aio_block/callback");
1974
1975 X_LOCK (reslock);
1976 main_tid = pthread_self ();
1977 main_sig = signum;
1978 X_UNLOCK (reslock);
1979
1980 if (main_sig && npending)
1981 pthread_kill (main_tid, main_sig);
1982}
1983
1984void
1985aio_block (SV *cb)
1986 PROTOTYPE: &
1987 PPCODE:
1988{
1989 int count;
1990
1991 block_sig ();
1992 PUSHMARK (SP);
1993 PUTBACK;
1994 count = call_sv (cb, GIMME_V | G_NOARGS | G_EVAL);
1995 unblock_sig ();
1996
1997 if (SvTRUE (ERRSV))
1998 croak (0);
1999
2000 XSRETURN (count);
2001}
2002
2003int 1639int
2004nreqs() 1640nreqs ()
2005 PROTOTYPE:
2006 CODE: 1641 CODE:
2007 RETVAL = nreqs; 1642 RETVAL = eio_nreqs ();
2008 OUTPUT: 1643 OUTPUT:
2009 RETVAL 1644 RETVAL
2010 1645
2011int 1646int
2012nready() 1647nready ()
2013 PROTOTYPE:
2014 CODE: 1648 CODE:
2015 RETVAL = get_nready (); 1649 RETVAL = eio_nready ();
2016 OUTPUT: 1650 OUTPUT:
2017 RETVAL 1651 RETVAL
2018 1652
2019int 1653int
2020npending() 1654npending ()
2021 PROTOTYPE:
2022 CODE: 1655 CODE:
2023 RETVAL = get_npending (); 1656 RETVAL = eio_npending ();
2024 OUTPUT: 1657 OUTPUT:
2025 RETVAL 1658 RETVAL
2026 1659
2027int 1660int
2028nthreads() 1661nthreads ()
2029 PROTOTYPE:
2030 CODE: 1662 CODE:
2031 if (WORDACCESS_UNSAFE) X_LOCK (wrklock); 1663 RETVAL = eio_nthreads ();
2032 RETVAL = started;
2033 if (WORDACCESS_UNSAFE) X_UNLOCK (wrklock);
2034 OUTPUT: 1664 OUTPUT:
2035 RETVAL 1665 RETVAL
2036 1666
1667int
1668fadvise (aio_rfd fh, off_t offset, off_t length, IV advice)
1669 CODE:
1670 RETVAL = posix_fadvise (fh, offset, length, advice);
1671 OUTPUT:
1672 RETVAL
1673
1674IV
1675sendfile (aio_wfd ofh, aio_rfd ifh, off_t offset, size_t count)
1676 CODE:
1677 RETVAL = eio_sendfile_sync (ofh, ifh, offset, count);
1678 OUTPUT:
1679 RETVAL
1680
1681void
1682mmap (SV *scalar, size_t length, int prot, int flags, SV *fh, off_t offset = 0)
1683 PPCODE:
1684 sv_unmagic (scalar, MMAP_MAGIC);
1685{
1686 int fd = SvOK (fh) ? s_fileno_croak (fh, flags & PROT_WRITE) : -1;
1687 void *addr = (void *)mmap (0, length, prot, flags, fd, offset);
1688 if (addr == (void *)-1)
1689 XSRETURN_NO;
1690
1691 sv_force_normal (scalar);
1692
1693 /* we store the length in mg_obj, as namlen is I32 :/ */
1694 sv_magicext (scalar, 0, MMAP_MAGIC, &mmap_vtbl, (char *)addr, 0)
1695 ->mg_obj = (SV *)length;
1696
1697 SvUPGRADE (scalar, SVt_PV); /* nop... */
1698
1699 if (!(prot & PROT_WRITE))
1700 SvREADONLY_on (scalar);
1701
1702 if (SvLEN (scalar))
1703 Safefree (SvPVX (scalar));
1704
1705 SvPVX (scalar) = (char *)addr;
1706 SvCUR_set (scalar, length);
1707 SvLEN_set (scalar, 0);
1708 SvPOK_only (scalar);
1709
1710 XSRETURN_YES;
1711}
1712
1713void
1714munmap (SV *scalar)
1715 CODE:
1716 sv_unmagic (scalar, MMAP_MAGIC);
1717
1718int
1719madvise (SV *scalar, off_t offset = 0, SV *length = &PL_sv_undef, IV advice_or_prot)
1720 ALIAS:
1721 mprotect = 1
1722 CODE:
1723{
1724 STRLEN svlen;
1725 void *addr = SvPVbyte (scalar, svlen);
1726 size_t len = SvUV (length);
1727
1728 if (offset < 0)
1729 offset += svlen;
1730
1731 if (offset < 0 || offset > svlen)
1732 croak ("offset outside of scalar");
1733
1734 if (!SvOK (length) || len + offset > svlen)
1735 len = svlen - offset;
1736
1737 addr = (void *)(((intptr_t)addr) + offset);
1738 eio_page_align (&addr, &len);
1739
1740 switch (ix)
1741 {
1742 case 0: RETVAL = posix_madvise (addr, len, advice_or_prot); break;
1743 case 1: RETVAL = mprotect (addr, len, advice_or_prot); break;
1744 }
1745}
1746 OUTPUT:
1747 RETVAL
1748
1749int
1750munlock (SV *scalar, off_t offset = 0, SV *length = &PL_sv_undef)
1751 CODE:
1752{
1753 STRLEN svlen;
1754 void *addr = SvPVbyte (scalar, svlen);
1755 size_t len = SvUV (length);
1756
1757 if (offset < 0)
1758 offset += svlen;
1759
1760 if (offset < 0 || offset > svlen)
1761 croak ("offset outside of scalar");
1762
1763 if (!SvOK (length) || len + offset > svlen)
1764 len = svlen - offset;
1765
1766 addr = (void *)(((intptr_t)addr) + offset);
1767 eio_page_align (&addr, &len);
1768#if _POSIX_MEMLOCK_RANGE
1769 RETVAL = munlock (addr, len);
1770#else
1771 RETVAL = ((errno = ENOSYS), -1);
1772#endif
1773}
1774 OUTPUT:
1775 RETVAL
1776
1777int
1778munlockall ()
1779 CODE:
1780#if _POSIX_MEMLOCK
1781 munlockall ();
1782#else
1783 RETVAL = -1;
1784 errno = ENOSYS;
1785#endif
1786 OUTPUT:
1787 RETVAL
1788
1789void _on_next_submit (SV *cb)
1790 CODE:
1791 SvREFCNT_dec (on_next_submit);
1792 on_next_submit = SvOK (cb) ? newSVsv (cb) : 0;
1793
2037PROTOTYPES: DISABLE 1794PROTOTYPES: DISABLE
1795
1796MODULE = IO::AIO PACKAGE = IO::AIO::WD
1797
1798BOOT:
1799{
1800 newCONSTSUB (aio_stash, "CWD" , newSVaio_wd (EIO_CWD ));
1801 newCONSTSUB (aio_stash, "INVALID_WD", newSVaio_wd (EIO_INVALID_WD));
1802}
1803
1804void
1805DESTROY (SV *self)
1806 CODE:
1807{
1808 aio_wd wd = SvAIO_WD (self);
1809#if HAVE_AT
1810 {
1811 SV *callback = &PL_sv_undef;
1812 dREQ; /* clobbers next_pri :/ */
1813 next_pri = req->pri; /* restore next_pri */
1814 req->pri = EIO_PRI_MAX; /* better use max. priority to conserve fds */
1815 req->type = EIO_WD_CLOSE;
1816 req->wd = wd;
1817 REQ_SEND;
1818 }
1819#else
1820 eio_wd_close_sync (wd);
1821#endif
1822}
2038 1823
2039MODULE = IO::AIO PACKAGE = IO::AIO::REQ 1824MODULE = IO::AIO PACKAGE = IO::AIO::REQ
2040 1825
2041void 1826void
2042cancel (aio_req_ornot req) 1827cancel (aio_req_ornot req)
2043 CODE: 1828 CODE:
2044 req_cancel (req); 1829 eio_cancel (req);
2045 1830
2046void 1831void
2047cb (aio_req_ornot req, SV *callback=&PL_sv_undef) 1832cb (aio_req_ornot req, SV *callback=&PL_sv_undef)
2048 CODE: 1833 PPCODE:
1834{
1835 if (GIMME_V != G_VOID)
1836 XPUSHs (req->callback ? sv_2mortal (newRV_inc (req->callback)) : &PL_sv_undef);
1837
1838 if (items > 1)
1839 {
1840 SV *cb_cv =get_cb (callback);
1841
2049 SvREFCNT_dec (req->callback); 1842 SvREFCNT_dec (req->callback);
2050 req->callback = newSVsv (callback); 1843 req->callback = SvREFCNT_inc (cb_cv);
1844 }
1845}
2051 1846
2052MODULE = IO::AIO PACKAGE = IO::AIO::GRP 1847MODULE = IO::AIO PACKAGE = IO::AIO::GRP
2053 1848
2054void 1849void
2055add (aio_req grp, ...) 1850add (aio_req grp, ...)
2056 PPCODE: 1851 PPCODE:
2057{ 1852{
2058 int i; 1853 int i;
2059 aio_req req;
2060
2061 if (main_sig && !block_sig_level)
2062 croak ("aio_group->add called outside aio_block/callback context while IO::AIO::setsig is in use");
2063 1854
2064 if (grp->int1 == 2) 1855 if (grp->int1 == 2)
2065 croak ("cannot add requests to IO::AIO::GRP after the group finished"); 1856 croak ("cannot add requests to IO::AIO::GRP after the group finished");
2066 1857
2067 for (i = 1; i < items; ++i ) 1858 for (i = 1; i < items; ++i )
2068 { 1859 {
1860 aio_req req;
1861
2069 if (GIMME_V != G_VOID) 1862 if (GIMME_V != G_VOID)
2070 XPUSHs (sv_2mortal (newSVsv (ST (i)))); 1863 XPUSHs (sv_2mortal (newSVsv (ST (i))));
2071 1864
2072 req = SvAIO_REQ (ST (i)); 1865 req = SvAIO_REQ (ST (i));
2073 1866
2074 if (req) 1867 if (req)
2075 { 1868 eio_grp_add (grp, req);
2076 ++grp->size;
2077 req->grp = grp;
2078
2079 req->grp_prev = 0;
2080 req->grp_next = grp->grp_first;
2081
2082 if (grp->grp_first)
2083 grp->grp_first->grp_prev = req;
2084
2085 grp->grp_first = req;
2086 }
2087 } 1869 }
2088} 1870}
2089 1871
2090void 1872void
2091cancel_subs (aio_req_ornot req) 1873cancel_subs (aio_req_ornot req)
2100 AV *av; 1882 AV *av;
2101 1883
2102 grp->errorno = errno; 1884 grp->errorno = errno;
2103 1885
2104 av = newAV (); 1886 av = newAV ();
1887 av_extend (av, items - 1);
2105 1888
2106 for (i = 1; i < items; ++i ) 1889 for (i = 1; i < items; ++i )
2107 av_push (av, newSVsv (ST (i))); 1890 av_push (av, newSVsv (ST (i)));
2108 1891
2109 SvREFCNT_dec (grp->sv1); 1892 SvREFCNT_dec (grp->sv1);
2116 grp->errorno = errorno; 1899 grp->errorno = errorno;
2117 1900
2118void 1901void
2119limit (aio_req grp, int limit) 1902limit (aio_req grp, int limit)
2120 CODE: 1903 CODE:
2121 grp->int2 = limit; 1904 eio_grp_limit (grp, limit);
2122 aio_grp_feed (grp);
2123 1905
2124void 1906void
2125feed (aio_req grp, SV *callback=&PL_sv_undef) 1907feed (aio_req grp, SV *callback=&PL_sv_undef)
2126 CODE: 1908 CODE:
2127{ 1909{
2128 SvREFCNT_dec (grp->sv2); 1910 SvREFCNT_dec (grp->sv2);
2129 grp->sv2 = newSVsv (callback); 1911 grp->sv2 = newSVsv (callback);
1912 grp->feed = aio_grp_feed;
2130 1913
2131 if (grp->int2 <= 0) 1914 if (grp->int2 <= 0)
2132 grp->int2 = 2; 1915 grp->int2 = 2;
2133 1916
2134 aio_grp_feed (grp); 1917 eio_grp_limit (grp, grp->int2);
2135} 1918}
2136 1919

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines