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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines