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.70 by root, Tue Oct 24 15:15:56 2006 UTC vs.
Revision 1.103 by root, Sun Jul 8 09:09:34 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines