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.109 by root, Sat Oct 6 14:05:19 2007 UTC vs.
Revision 1.151 by root, Tue Jul 14 00:13:36 2009 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines