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.84 by root, Sat Oct 28 01:24:19 2006 UTC vs.
Revision 1.165 by root, Sun May 9 20:42:55 2010 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines