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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines