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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines