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.64 by root, Mon Oct 23 23:54:41 2006 UTC vs.
Revision 1.109 by root, Sat Oct 6 14:05:19 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines