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.49 by root, Sun Oct 22 13:33:28 2006 UTC vs.
Revision 1.104 by root, Sun Jul 8 11:05:36 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines