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.57 by root, Mon Oct 23 18:38:15 2006 UTC vs.
Revision 1.112 by root, Tue Apr 1 19:40:47 2008 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines