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