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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines