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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines