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.114 by root, Sat Apr 26 12:00:23 2008 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines