1 | #define PERL_NO_GET_CONTEXT |
1 | /* solaris */ |
|
|
2 | #define _POSIX_PTHREAD_SEMANTICS 1 |
|
|
3 | |
|
|
4 | #if __linux |
|
|
5 | # define _GNU_SOURCE |
|
|
6 | #endif |
|
|
7 | |
|
|
8 | #define _REENTRANT 1 |
|
|
9 | |
|
|
10 | #include <errno.h> |
2 | |
11 | |
3 | #include "EXTERN.h" |
12 | #include "EXTERN.h" |
4 | #include "perl.h" |
13 | #include "perl.h" |
5 | #include "XSUB.h" |
14 | #include "XSUB.h" |
6 | |
15 | |
|
|
16 | #include "autoconf/config.h" |
|
|
17 | |
|
|
18 | #include <pthread.h> |
|
|
19 | |
|
|
20 | #include <stddef.h> |
|
|
21 | #include <errno.h> |
|
|
22 | #include <sys/time.h> |
|
|
23 | #include <sys/select.h> |
7 | #include <sys/types.h> |
24 | #include <sys/types.h> |
8 | #include <sys/stat.h> |
25 | #include <sys/stat.h> |
|
|
26 | #include <limits.h> |
9 | #include <unistd.h> |
27 | #include <unistd.h> |
10 | #include <fcntl.h> |
28 | #include <fcntl.h> |
11 | #include <signal.h> |
29 | #include <signal.h> |
12 | #include <sched.h> |
30 | #include <sched.h> |
13 | #include <endian.h> |
|
|
14 | |
31 | |
15 | #include <pthread.h> |
32 | #if HAVE_SENDFILE |
|
|
33 | # if __linux |
|
|
34 | # include <sys/sendfile.h> |
|
|
35 | # elif __freebsd |
16 | #include <sys/syscall.h> |
36 | # include <sys/socket.h> |
|
|
37 | # include <sys/uio.h> |
|
|
38 | # elif __hpux |
|
|
39 | # include <sys/socket.h> |
|
|
40 | # elif __solaris /* not yet */ |
|
|
41 | # include <sys/sendfile.h> |
|
|
42 | # else |
|
|
43 | # error sendfile support requested but not available |
|
|
44 | # endif |
|
|
45 | #endif |
17 | |
46 | |
18 | typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
47 | /* used for struct dirent, AIX doesn't provide it */ |
19 | typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
48 | #ifndef NAME_MAX |
20 | typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
49 | # define NAME_MAX 4096 |
|
|
50 | #endif |
21 | |
51 | |
22 | #if __ia64 |
52 | #if __ia64 |
23 | # define STACKSIZE 65536 |
53 | # define STACKSIZE 65536 |
|
|
54 | #elif __i386 || __x86_64 /* 16k is unreasonably high :( */ |
|
|
55 | # define STACKSIZE PTHREAD_STACK_MIN |
24 | #else |
56 | #else |
25 | # define STACKSIZE 4096 |
57 | # define STACKSIZE 16384 |
26 | #endif |
58 | #endif |
|
|
59 | |
|
|
60 | /* buffer size for various temporary buffers */ |
|
|
61 | #define AIO_BUFSIZE 65536 |
|
|
62 | |
|
|
63 | #define dBUF \ |
|
|
64 | char *aio_buf; \ |
|
|
65 | LOCK (wrklock); \ |
|
|
66 | self->dbuf = aio_buf = malloc (AIO_BUFSIZE); \ |
|
|
67 | UNLOCK (wrklock); \ |
|
|
68 | if (!aio_buf) \ |
|
|
69 | return -1; |
27 | |
70 | |
28 | enum { |
71 | enum { |
29 | REQ_QUIT, |
72 | REQ_QUIT, |
30 | REQ_OPEN, REQ_CLOSE, |
73 | REQ_OPEN, REQ_CLOSE, |
31 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
74 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
|
|
75 | REQ_SENDFILE, |
32 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_UNLINK, |
76 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
33 | REQ_FSYNC, REQ_FDATASYNC, |
77 | REQ_FSYNC, REQ_FDATASYNC, |
|
|
78 | REQ_UNLINK, REQ_RMDIR, REQ_RENAME, |
|
|
79 | REQ_READDIR, |
|
|
80 | REQ_LINK, REQ_SYMLINK, |
|
|
81 | REQ_GROUP, REQ_NOP, |
|
|
82 | REQ_BUSY, |
34 | }; |
83 | }; |
35 | |
84 | |
|
|
85 | #define AIO_REQ_KLASS "IO::AIO::REQ" |
|
|
86 | #define AIO_GRP_KLASS "IO::AIO::GRP" |
|
|
87 | |
36 | typedef struct aio_cb { |
88 | typedef struct aio_cb |
|
|
89 | { |
37 | struct aio_cb *volatile next; |
90 | struct aio_cb *volatile next; |
38 | |
91 | |
39 | int type; |
92 | SV *data, *callback; |
40 | |
93 | SV *fh, *fh2; |
41 | int fd; |
94 | void *dataptr, *data2ptr; |
|
|
95 | Stat_t *statdata; |
42 | off_t offset; |
96 | off_t offset; |
43 | size_t length; |
97 | size_t length; |
44 | ssize_t result; |
98 | ssize_t result; |
|
|
99 | |
|
|
100 | STRLEN dataoffset; |
|
|
101 | int type; |
|
|
102 | int fd, fd2; |
|
|
103 | int errorno; |
45 | mode_t mode; /* open */ |
104 | mode_t mode; /* open */ |
46 | int errorno; |
|
|
47 | SV *data, *callback; |
|
|
48 | void *dataptr; |
|
|
49 | STRLEN dataoffset; |
|
|
50 | |
105 | |
51 | Stat_t *statdata; |
106 | unsigned char flags; |
|
|
107 | unsigned char pri; |
|
|
108 | |
|
|
109 | SV *self; /* the perl counterpart of this request, if any */ |
|
|
110 | struct aio_cb *grp, *grp_prev, *grp_next, *grp_first; |
52 | } aio_cb; |
111 | } aio_cb; |
53 | |
112 | |
|
|
113 | enum { |
|
|
114 | FLAG_CANCELLED = 0x01, |
|
|
115 | }; |
|
|
116 | |
54 | typedef aio_cb *aio_req; |
117 | typedef aio_cb *aio_req; |
|
|
118 | typedef aio_cb *aio_req_ornot; |
55 | |
119 | |
|
|
120 | enum { |
|
|
121 | PRI_MIN = -4, |
|
|
122 | PRI_MAX = 4, |
|
|
123 | |
|
|
124 | DEFAULT_PRI = 0, |
|
|
125 | PRI_BIAS = -PRI_MIN, |
|
|
126 | NUM_PRI = PRI_MAX + PRI_BIAS + 1, |
|
|
127 | }; |
|
|
128 | |
|
|
129 | static int next_pri = DEFAULT_PRI + PRI_BIAS; |
|
|
130 | |
56 | static int started; |
131 | static int started, wanted; |
57 | static int nreqs; |
132 | static volatile int nreqs; |
58 | static int max_outstanding = 1<<30; |
133 | static int max_outstanding = 1<<30; |
59 | static int respipe [2]; |
134 | static int respipe [2]; |
60 | |
135 | |
|
|
136 | #if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) |
|
|
137 | # define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP |
|
|
138 | #else |
|
|
139 | # define AIO_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER |
|
|
140 | #endif |
|
|
141 | |
|
|
142 | #define LOCK(mutex) pthread_mutex_lock (&(mutex)) |
|
|
143 | #define UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) |
|
|
144 | |
|
|
145 | /* worker threasd management */ |
|
|
146 | static pthread_mutex_t wrklock = AIO_MUTEX_INIT; |
|
|
147 | |
|
|
148 | typedef struct worker { |
|
|
149 | /* locked by wrklock */ |
|
|
150 | struct worker *prev, *next; |
|
|
151 | |
|
|
152 | pthread_t tid; |
|
|
153 | |
|
|
154 | /* locked by reslock, reqlock or wrklock */ |
|
|
155 | aio_req req; /* currently processed request */ |
|
|
156 | void *dbuf; |
|
|
157 | DIR *dirp; |
|
|
158 | } worker; |
|
|
159 | |
|
|
160 | static worker wrk_first = { &wrk_first, &wrk_first, 0 }; |
|
|
161 | |
|
|
162 | static void worker_clear (worker *wrk) |
|
|
163 | { |
|
|
164 | if (wrk->dirp) |
|
|
165 | { |
|
|
166 | closedir (wrk->dirp); |
|
|
167 | wrk->dirp = 0; |
|
|
168 | } |
|
|
169 | |
|
|
170 | if (wrk->dbuf) |
|
|
171 | { |
|
|
172 | free (wrk->dbuf); |
|
|
173 | wrk->dbuf = 0; |
|
|
174 | } |
|
|
175 | } |
|
|
176 | |
|
|
177 | static void worker_free (worker *wrk) |
|
|
178 | { |
|
|
179 | wrk->next->prev = wrk->prev; |
|
|
180 | wrk->prev->next = wrk->next; |
|
|
181 | |
|
|
182 | free (wrk); |
|
|
183 | } |
|
|
184 | |
61 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
185 | static pthread_mutex_t reslock = AIO_MUTEX_INIT; |
62 | static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; |
186 | static pthread_mutex_t reqlock = AIO_MUTEX_INIT; |
63 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
187 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
64 | |
188 | |
65 | static volatile aio_req reqs, reqe; /* queue start, queue end */ |
189 | /* |
66 | static volatile aio_req ress, rese; /* queue start, queue end */ |
190 | * a somewhat faster data structure might be nice, but |
|
|
191 | * with 8 priorities this actually needs <20 insns |
|
|
192 | * per shift, the most expensive operation. |
|
|
193 | */ |
|
|
194 | typedef struct { |
|
|
195 | aio_req qs[NUM_PRI], qe[NUM_PRI]; /* qstart, qend */ |
|
|
196 | int size; |
|
|
197 | } reqq; |
67 | |
198 | |
68 | static void |
199 | static reqq req_queue; |
69 | poll_wait () |
200 | static reqq res_queue; |
|
|
201 | |
|
|
202 | int reqq_push (reqq *q, aio_req req) |
70 | { |
203 | { |
71 | if (!nreqs) |
204 | int pri = req->pri; |
|
|
205 | req->next = 0; |
|
|
206 | |
|
|
207 | if (q->qe[pri]) |
|
|
208 | { |
|
|
209 | q->qe[pri]->next = req; |
|
|
210 | q->qe[pri] = req; |
|
|
211 | } |
|
|
212 | else |
|
|
213 | q->qe[pri] = q->qs[pri] = req; |
|
|
214 | |
|
|
215 | return q->size++; |
|
|
216 | } |
|
|
217 | |
|
|
218 | aio_req reqq_shift (reqq *q) |
|
|
219 | { |
|
|
220 | int pri; |
|
|
221 | |
|
|
222 | if (!q->size) |
72 | return; |
223 | return 0; |
73 | |
224 | |
|
|
225 | --q->size; |
|
|
226 | |
|
|
227 | for (pri = NUM_PRI; pri--; ) |
|
|
228 | { |
|
|
229 | aio_req req = q->qs[pri]; |
|
|
230 | |
|
|
231 | if (req) |
|
|
232 | { |
|
|
233 | if (!(q->qs[pri] = req->next)) |
|
|
234 | q->qe[pri] = 0; |
|
|
235 | |
|
|
236 | return req; |
|
|
237 | } |
|
|
238 | } |
|
|
239 | |
|
|
240 | abort (); |
|
|
241 | } |
|
|
242 | |
|
|
243 | static void req_invoke (aio_req req); |
|
|
244 | static void req_free (aio_req req); |
|
|
245 | |
|
|
246 | /* must be called at most once */ |
|
|
247 | static SV *req_sv (aio_req req, const char *klass) |
|
|
248 | { |
|
|
249 | if (!req->self) |
|
|
250 | { |
|
|
251 | req->self = (SV *)newHV (); |
|
|
252 | sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0); |
|
|
253 | } |
|
|
254 | |
|
|
255 | return sv_2mortal (sv_bless (newRV_inc (req->self), gv_stashpv (klass, 1))); |
|
|
256 | } |
|
|
257 | |
|
|
258 | static aio_req SvAIO_REQ (SV *sv) |
|
|
259 | { |
|
|
260 | MAGIC *mg; |
|
|
261 | |
|
|
262 | if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv)) |
|
|
263 | croak ("object of class " AIO_REQ_KLASS " expected"); |
|
|
264 | |
|
|
265 | mg = mg_find (SvRV (sv), PERL_MAGIC_ext); |
|
|
266 | |
|
|
267 | return mg ? (aio_req)mg->mg_ptr : 0; |
|
|
268 | } |
|
|
269 | |
|
|
270 | static void aio_grp_feed (aio_req grp) |
|
|
271 | { |
|
|
272 | while (grp->length < grp->fd2 && !(grp->flags & FLAG_CANCELLED)) |
|
|
273 | { |
|
|
274 | int old_len = grp->length; |
|
|
275 | |
|
|
276 | if (grp->fh2 && SvOK (grp->fh2)) |
|
|
277 | { |
|
|
278 | dSP; |
|
|
279 | |
|
|
280 | ENTER; |
|
|
281 | SAVETMPS; |
|
|
282 | PUSHMARK (SP); |
|
|
283 | XPUSHs (req_sv (grp, AIO_GRP_KLASS)); |
|
|
284 | PUTBACK; |
|
|
285 | call_sv (grp->fh2, G_VOID | G_EVAL | G_KEEPERR); |
|
|
286 | SPAGAIN; |
|
|
287 | FREETMPS; |
|
|
288 | LEAVE; |
|
|
289 | } |
|
|
290 | |
|
|
291 | /* stop if no progress has been made */ |
|
|
292 | if (old_len == grp->length) |
|
|
293 | { |
|
|
294 | SvREFCNT_dec (grp->fh2); |
|
|
295 | grp->fh2 = 0; |
|
|
296 | break; |
|
|
297 | } |
|
|
298 | } |
|
|
299 | } |
|
|
300 | |
|
|
301 | static void aio_grp_dec (aio_req grp) |
|
|
302 | { |
|
|
303 | --grp->length; |
|
|
304 | |
|
|
305 | /* call feeder, if applicable */ |
|
|
306 | aio_grp_feed (grp); |
|
|
307 | |
|
|
308 | /* finish, if done */ |
|
|
309 | if (!grp->length && grp->fd) |
|
|
310 | { |
|
|
311 | req_invoke (grp); |
|
|
312 | req_free (grp); |
|
|
313 | } |
|
|
314 | } |
|
|
315 | |
|
|
316 | static void poll_wait () |
|
|
317 | { |
74 | fd_set rfd; |
318 | fd_set rfd; |
|
|
319 | |
|
|
320 | while (nreqs) |
|
|
321 | { |
|
|
322 | int size; |
|
|
323 | #if !(__i386 || __x86_64) /* safe without sempahore on these archs */ |
|
|
324 | LOCK (reslock); |
|
|
325 | #endif |
|
|
326 | size = res_queue.size; |
|
|
327 | #if !(__i386 || __x86_64) /* safe without sempahore on these archs */ |
|
|
328 | UNLOCK (reslock); |
|
|
329 | #endif |
|
|
330 | |
|
|
331 | if (size) |
|
|
332 | return; |
|
|
333 | |
75 | FD_ZERO(&rfd); |
334 | FD_ZERO(&rfd); |
76 | FD_SET(respipe [0], &rfd); |
335 | FD_SET(respipe [0], &rfd); |
77 | |
336 | |
78 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
337 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
|
|
338 | } |
79 | } |
339 | } |
80 | |
340 | |
81 | static int |
341 | static void req_invoke (aio_req req) |
82 | poll_cb (pTHX) |
342 | { |
|
|
343 | dSP; |
|
|
344 | |
|
|
345 | if (!(req->flags & FLAG_CANCELLED) && SvOK (req->callback)) |
|
|
346 | { |
|
|
347 | errno = req->errorno; |
|
|
348 | |
|
|
349 | ENTER; |
|
|
350 | SAVETMPS; |
|
|
351 | PUSHMARK (SP); |
|
|
352 | EXTEND (SP, 1); |
|
|
353 | |
|
|
354 | switch (req->type) |
|
|
355 | { |
|
|
356 | case REQ_READDIR: |
|
|
357 | { |
|
|
358 | SV *rv = &PL_sv_undef; |
|
|
359 | |
|
|
360 | if (req->result >= 0) |
|
|
361 | { |
|
|
362 | int i; |
|
|
363 | char *buf = req->data2ptr; |
|
|
364 | AV *av = newAV (); |
|
|
365 | |
|
|
366 | av_extend (av, req->result - 1); |
|
|
367 | |
|
|
368 | for (i = 0; i < req->result; ++i) |
|
|
369 | { |
|
|
370 | SV *sv = newSVpv (buf, 0); |
|
|
371 | |
|
|
372 | av_store (av, i, sv); |
|
|
373 | buf += SvCUR (sv) + 1; |
|
|
374 | } |
|
|
375 | |
|
|
376 | rv = sv_2mortal (newRV_noinc ((SV *)av)); |
|
|
377 | } |
|
|
378 | |
|
|
379 | PUSHs (rv); |
|
|
380 | } |
|
|
381 | break; |
|
|
382 | |
|
|
383 | case REQ_OPEN: |
|
|
384 | { |
|
|
385 | /* convert fd to fh */ |
|
|
386 | SV *fh; |
|
|
387 | |
|
|
388 | PUSHs (sv_2mortal (newSViv (req->result))); |
|
|
389 | PUTBACK; |
|
|
390 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
|
|
391 | SPAGAIN; |
|
|
392 | |
|
|
393 | fh = SvREFCNT_inc (POPs); |
|
|
394 | |
|
|
395 | PUSHMARK (SP); |
|
|
396 | XPUSHs (sv_2mortal (fh)); |
|
|
397 | } |
|
|
398 | break; |
|
|
399 | |
|
|
400 | case REQ_GROUP: |
|
|
401 | req->fd = 2; /* mark group as finished */ |
|
|
402 | |
|
|
403 | if (req->data) |
|
|
404 | { |
|
|
405 | int i; |
|
|
406 | AV *av = (AV *)req->data; |
|
|
407 | |
|
|
408 | EXTEND (SP, AvFILL (av) + 1); |
|
|
409 | for (i = 0; i <= AvFILL (av); ++i) |
|
|
410 | PUSHs (*av_fetch (av, i, 0)); |
|
|
411 | } |
|
|
412 | break; |
|
|
413 | |
|
|
414 | case REQ_NOP: |
|
|
415 | case REQ_BUSY: |
|
|
416 | break; |
|
|
417 | |
|
|
418 | default: |
|
|
419 | PUSHs (sv_2mortal (newSViv (req->result))); |
|
|
420 | break; |
|
|
421 | } |
|
|
422 | |
|
|
423 | |
|
|
424 | PUTBACK; |
|
|
425 | call_sv (req->callback, G_VOID | G_EVAL); |
|
|
426 | SPAGAIN; |
|
|
427 | |
|
|
428 | FREETMPS; |
|
|
429 | LEAVE; |
|
|
430 | } |
|
|
431 | |
|
|
432 | if (req->grp) |
|
|
433 | { |
|
|
434 | aio_req grp = req->grp; |
|
|
435 | |
|
|
436 | /* unlink request */ |
|
|
437 | if (req->grp_next) req->grp_next->grp_prev = req->grp_prev; |
|
|
438 | if (req->grp_prev) req->grp_prev->grp_next = req->grp_next; |
|
|
439 | |
|
|
440 | if (grp->grp_first == req) |
|
|
441 | grp->grp_first = req->grp_next; |
|
|
442 | |
|
|
443 | aio_grp_dec (grp); |
|
|
444 | } |
|
|
445 | |
|
|
446 | if (SvTRUE (ERRSV)) |
|
|
447 | { |
|
|
448 | req_free (req); |
|
|
449 | croak (0); |
|
|
450 | } |
|
|
451 | } |
|
|
452 | |
|
|
453 | static void req_free (aio_req req) |
|
|
454 | { |
|
|
455 | if (req->self) |
|
|
456 | { |
|
|
457 | sv_unmagic (req->self, PERL_MAGIC_ext); |
|
|
458 | SvREFCNT_dec (req->self); |
|
|
459 | } |
|
|
460 | |
|
|
461 | SvREFCNT_dec (req->data); |
|
|
462 | SvREFCNT_dec (req->fh); |
|
|
463 | SvREFCNT_dec (req->fh2); |
|
|
464 | SvREFCNT_dec (req->callback); |
|
|
465 | Safefree (req->statdata); |
|
|
466 | |
|
|
467 | if (req->type == REQ_READDIR) |
|
|
468 | free (req->data2ptr); |
|
|
469 | |
|
|
470 | Safefree (req); |
|
|
471 | } |
|
|
472 | |
|
|
473 | static void req_cancel (aio_req req) |
|
|
474 | { |
|
|
475 | req->flags |= FLAG_CANCELLED; |
|
|
476 | |
|
|
477 | if (req->type == REQ_GROUP) |
|
|
478 | { |
|
|
479 | aio_req sub; |
|
|
480 | |
|
|
481 | for (sub = req->grp_first; sub; sub = sub->grp_next) |
|
|
482 | req_cancel (sub); |
|
|
483 | } |
|
|
484 | } |
|
|
485 | |
|
|
486 | static int poll_cb () |
83 | { |
487 | { |
84 | dSP; |
488 | dSP; |
85 | int count = 0; |
489 | int count = 0; |
|
|
490 | int do_croak = 0; |
86 | aio_req req; |
491 | aio_req req; |
87 | |
|
|
88 | { |
|
|
89 | /* read and signals sent by the worker threads */ |
|
|
90 | char buf [32]; |
|
|
91 | while (read (respipe [0], buf, 32) > 0) |
|
|
92 | ; |
|
|
93 | } |
|
|
94 | |
492 | |
95 | for (;;) |
493 | for (;;) |
96 | { |
494 | { |
97 | pthread_mutex_lock (&reslock); |
495 | LOCK (reslock); |
|
|
496 | req = reqq_shift (&res_queue); |
98 | |
497 | |
99 | req = ress; |
|
|
100 | |
|
|
101 | if (ress) |
498 | if (req) |
102 | { |
499 | { |
103 | ress = ress->next; |
500 | if (!res_queue.size) |
104 | if (!ress) rese = 0; |
501 | { |
|
|
502 | /* read any signals sent by the worker threads */ |
|
|
503 | char buf [32]; |
|
|
504 | while (read (respipe [0], buf, 32) == 32) |
|
|
505 | ; |
|
|
506 | } |
105 | } |
507 | } |
106 | |
508 | |
107 | pthread_mutex_unlock (&reslock); |
509 | UNLOCK (reslock); |
108 | |
510 | |
109 | if (!req) |
511 | if (!req) |
110 | break; |
512 | break; |
111 | |
513 | |
112 | nreqs--; |
514 | --nreqs; |
113 | |
515 | |
114 | if (req->type == REQ_QUIT) |
516 | if (req->type == REQ_QUIT) |
115 | started--; |
517 | started--; |
|
|
518 | else if (req->type == REQ_GROUP && req->length) |
|
|
519 | { |
|
|
520 | req->fd = 1; /* mark request as delayed */ |
|
|
521 | continue; |
|
|
522 | } |
116 | else |
523 | else |
117 | { |
524 | { |
118 | int errorno = errno; |
|
|
119 | errno = req->errorno; |
|
|
120 | |
|
|
121 | if (req->type == REQ_READ) |
525 | if (req->type == REQ_READ) |
122 | SvCUR_set (req->data, req->dataoffset |
526 | SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); |
123 | + req->result > 0 ? req->result : 0); |
|
|
124 | |
527 | |
|
|
528 | if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE)) |
|
|
529 | SvREADONLY_off (req->data); |
|
|
530 | |
125 | if (req->data) |
531 | if (req->statdata) |
126 | SvREFCNT_dec (req->data); |
|
|
127 | |
|
|
128 | if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) |
|
|
129 | { |
532 | { |
130 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
533 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
131 | PL_laststatval = req->result; |
534 | PL_laststatval = req->result; |
132 | PL_statcache = *(req->statdata); |
535 | PL_statcache = *(req->statdata); |
133 | |
|
|
134 | Safefree (req->statdata); |
|
|
135 | } |
536 | } |
136 | |
537 | |
137 | PUSHMARK (SP); |
538 | req_invoke (req); |
138 | XPUSHs (sv_2mortal (newSViv (req->result))); |
|
|
139 | |
539 | |
140 | if (req->type == REQ_OPEN) |
|
|
141 | { |
|
|
142 | /* convert fd to fh */ |
|
|
143 | SV *fh; |
|
|
144 | |
|
|
145 | PUTBACK; |
|
|
146 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
|
|
147 | SPAGAIN; |
|
|
148 | |
|
|
149 | fh = POPs; |
|
|
150 | |
|
|
151 | PUSHMARK (SP); |
|
|
152 | XPUSHs (fh); |
|
|
153 | } |
|
|
154 | |
|
|
155 | PUTBACK; |
|
|
156 | call_sv (req->callback, G_VOID | G_EVAL); |
|
|
157 | SPAGAIN; |
|
|
158 | |
|
|
159 | if (req->callback) |
|
|
160 | SvREFCNT_dec (req->callback); |
|
|
161 | |
|
|
162 | errno = errorno; |
|
|
163 | count++; |
540 | count++; |
164 | } |
541 | } |
165 | |
542 | |
166 | Safefree (req); |
543 | req_free (req); |
167 | } |
544 | } |
168 | |
545 | |
169 | return count; |
546 | return count; |
170 | } |
547 | } |
171 | |
548 | |
172 | static void *aio_proc(void *arg); |
549 | static void *aio_proc(void *arg); |
173 | |
550 | |
174 | static void |
|
|
175 | start_thread (void) |
551 | static void start_thread (void) |
176 | { |
552 | { |
|
|
553 | worker *wrk = calloc (1, sizeof (worker)); |
|
|
554 | |
|
|
555 | if (!wrk) |
|
|
556 | croak ("unable to allocate worker thread data"); |
|
|
557 | |
177 | sigset_t fullsigset, oldsigset; |
558 | sigset_t fullsigset, oldsigset; |
178 | pthread_t tid; |
|
|
179 | pthread_attr_t attr; |
559 | pthread_attr_t attr; |
180 | |
560 | |
181 | pthread_attr_init (&attr); |
561 | pthread_attr_init (&attr); |
182 | pthread_attr_setstacksize (&attr, STACKSIZE); |
562 | pthread_attr_setstacksize (&attr, STACKSIZE); |
183 | pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); |
563 | pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); |
184 | |
564 | |
185 | sigfillset (&fullsigset); |
565 | sigfillset (&fullsigset); |
|
|
566 | |
|
|
567 | LOCK (wrklock); |
186 | sigprocmask (SIG_SETMASK, &fullsigset, &oldsigset); |
568 | sigprocmask (SIG_SETMASK, &fullsigset, &oldsigset); |
187 | |
569 | |
188 | if (pthread_create (&tid, &attr, aio_proc, 0) == 0) |
570 | if (pthread_create (&wrk->tid, &attr, aio_proc, (void *)wrk) == 0) |
|
|
571 | { |
|
|
572 | wrk->prev = &wrk_first; |
|
|
573 | wrk->next = wrk_first.next; |
|
|
574 | wrk_first.next->prev = wrk; |
|
|
575 | wrk_first.next = wrk; |
189 | started++; |
576 | started++; |
|
|
577 | } |
|
|
578 | else |
|
|
579 | free (wrk); |
190 | |
580 | |
191 | sigprocmask (SIG_SETMASK, &oldsigset, 0); |
581 | sigprocmask (SIG_SETMASK, &oldsigset, 0); |
|
|
582 | UNLOCK (wrklock); |
192 | } |
583 | } |
193 | |
584 | |
194 | static void |
585 | static void req_send (aio_req req) |
195 | send_req (aio_req req) |
|
|
196 | { |
586 | { |
|
|
587 | while (started < wanted && nreqs >= started) |
|
|
588 | start_thread (); |
|
|
589 | |
197 | nreqs++; |
590 | ++nreqs; |
198 | |
591 | |
199 | pthread_mutex_lock (&reqlock); |
592 | LOCK (reqlock); |
200 | |
593 | reqq_push (&req_queue, req); |
201 | req->next = 0; |
|
|
202 | |
|
|
203 | if (reqe) |
|
|
204 | { |
|
|
205 | reqe->next = req; |
|
|
206 | reqe = req; |
|
|
207 | } |
|
|
208 | else |
|
|
209 | reqe = reqs = req; |
|
|
210 | |
|
|
211 | pthread_cond_signal (&reqwait); |
594 | pthread_cond_signal (&reqwait); |
212 | pthread_mutex_unlock (&reqlock); |
595 | UNLOCK (reqlock); |
213 | |
596 | |
214 | while (nreqs > max_outstanding) |
597 | if (nreqs > max_outstanding) |
|
|
598 | for (;;) |
|
|
599 | { |
|
|
600 | poll_cb (); |
|
|
601 | |
|
|
602 | if (nreqs <= max_outstanding) |
|
|
603 | break; |
|
|
604 | |
|
|
605 | poll_wait (); |
|
|
606 | } |
|
|
607 | } |
|
|
608 | |
|
|
609 | static void end_thread (void) |
|
|
610 | { |
|
|
611 | aio_req req; |
|
|
612 | |
|
|
613 | Newz (0, req, 1, aio_cb); |
|
|
614 | |
|
|
615 | req->type = REQ_QUIT; |
|
|
616 | req->pri = PRI_MAX + PRI_BIAS; |
|
|
617 | |
|
|
618 | req_send (req); |
|
|
619 | } |
|
|
620 | |
|
|
621 | static void min_parallel (int nthreads) |
|
|
622 | { |
|
|
623 | if (wanted < nthreads) |
|
|
624 | wanted = nthreads; |
|
|
625 | } |
|
|
626 | |
|
|
627 | static void max_parallel (int nthreads) |
|
|
628 | { |
|
|
629 | int cur = started; |
|
|
630 | |
|
|
631 | if (wanted > nthreads) |
|
|
632 | wanted = nthreads; |
|
|
633 | |
|
|
634 | while (cur > wanted) |
|
|
635 | { |
|
|
636 | end_thread (); |
|
|
637 | cur--; |
|
|
638 | } |
|
|
639 | |
|
|
640 | while (started > wanted) |
215 | { |
641 | { |
216 | poll_wait (); |
642 | poll_wait (); |
217 | poll_cb (); |
643 | poll_cb (); |
218 | } |
644 | } |
219 | } |
645 | } |
220 | |
646 | |
221 | static void |
647 | static void create_pipe () |
222 | end_thread (void) |
|
|
223 | { |
648 | { |
224 | aio_req req; |
649 | if (pipe (respipe)) |
225 | New (0, req, 1, aio_cb); |
650 | croak ("unable to initialize result pipe"); |
226 | req->type = REQ_QUIT; |
|
|
227 | |
651 | |
228 | send_req (req); |
652 | if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)) |
229 | } |
653 | croak ("cannot set result pipe to nonblocking mode"); |
230 | |
654 | |
231 | static void |
655 | if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) |
232 | read_write (pTHX_ |
656 | croak ("cannot set result pipe to nonblocking mode"); |
233 | int dowrite, int fd, off_t offset, size_t length, |
657 | } |
234 | SV *data, STRLEN dataoffset, SV *callback) |
|
|
235 | { |
|
|
236 | aio_req req; |
|
|
237 | STRLEN svlen; |
|
|
238 | char *svptr = SvPV (data, svlen); |
|
|
239 | |
658 | |
240 | SvUPGRADE (data, SVt_PV); |
659 | /*****************************************************************************/ |
241 | SvPOK_on (data); |
660 | /* work around various missing functions */ |
242 | |
661 | |
243 | if (dataoffset < 0) |
662 | #if !HAVE_PREADWRITE |
|
|
663 | # define pread aio_pread |
|
|
664 | # define pwrite aio_pwrite |
|
|
665 | |
|
|
666 | /* |
|
|
667 | * make our pread/pwrite safe against themselves, but not against |
|
|
668 | * normal read/write by using a mutex. slows down execution a lot, |
|
|
669 | * but that's your problem, not mine. |
|
|
670 | */ |
|
|
671 | static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER; |
|
|
672 | |
|
|
673 | static ssize_t pread (int fd, void *buf, size_t count, off_t offset) |
|
|
674 | { |
|
|
675 | ssize_t res; |
|
|
676 | off_t ooffset; |
|
|
677 | |
|
|
678 | LOCK (preadwritelock); |
|
|
679 | ooffset = lseek (fd, 0, SEEK_CUR); |
|
|
680 | lseek (fd, offset, SEEK_SET); |
|
|
681 | res = read (fd, buf, count); |
|
|
682 | lseek (fd, ooffset, SEEK_SET); |
|
|
683 | UNLOCK (preadwritelock); |
|
|
684 | |
|
|
685 | return res; |
|
|
686 | } |
|
|
687 | |
|
|
688 | static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset) |
|
|
689 | { |
|
|
690 | ssize_t res; |
|
|
691 | off_t ooffset; |
|
|
692 | |
|
|
693 | LOCK (preadwritelock); |
|
|
694 | ooffset = lseek (fd, 0, SEEK_CUR); |
|
|
695 | lseek (fd, offset, SEEK_SET); |
|
|
696 | res = write (fd, buf, count); |
|
|
697 | lseek (fd, offset, SEEK_SET); |
|
|
698 | UNLOCK (preadwritelock); |
|
|
699 | |
|
|
700 | return res; |
|
|
701 | } |
|
|
702 | #endif |
|
|
703 | |
|
|
704 | #if !HAVE_FDATASYNC |
|
|
705 | # define fdatasync fsync |
|
|
706 | #endif |
|
|
707 | |
|
|
708 | #if !HAVE_READAHEAD |
|
|
709 | # define readahead aio_readahead |
|
|
710 | |
|
|
711 | static ssize_t readahead (int fd, off_t offset, size_t count) |
|
|
712 | { |
|
|
713 | dBUF; |
|
|
714 | |
|
|
715 | while (count > 0) |
|
|
716 | { |
|
|
717 | size_t len = count < AIO_BUFSIZE ? count : AIO_BUFSIZE; |
|
|
718 | |
|
|
719 | pread (fd, aio_buf, len, offset); |
244 | dataoffset += svlen; |
720 | offset += len; |
245 | |
721 | count -= len; |
246 | if (dataoffset < 0 || dataoffset > svlen) |
|
|
247 | croak ("data offset outside of string"); |
|
|
248 | |
|
|
249 | if (dowrite) |
|
|
250 | { |
722 | } |
251 | /* write: check length and adjust. */ |
723 | |
252 | if (length < 0 || length + dataoffset > svlen) |
724 | errno = 0; |
253 | length = svlen - dataoffset; |
725 | } |
|
|
726 | #endif |
|
|
727 | |
|
|
728 | #if !HAVE_READDIR_R |
|
|
729 | # define readdir_r aio_readdir_r |
|
|
730 | |
|
|
731 | static pthread_mutex_t readdirlock = PTHREAD_MUTEX_INITIALIZER; |
|
|
732 | |
|
|
733 | static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) |
|
|
734 | { |
|
|
735 | struct dirent *e; |
|
|
736 | int errorno; |
|
|
737 | |
|
|
738 | LOCK (readdirlock); |
|
|
739 | |
|
|
740 | e = readdir (dirp); |
|
|
741 | errorno = errno; |
|
|
742 | |
|
|
743 | if (e) |
|
|
744 | { |
|
|
745 | *res = ent; |
|
|
746 | strcpy (ent->d_name, e->d_name); |
254 | } |
747 | } |
255 | else |
748 | else |
|
|
749 | *res = 0; |
|
|
750 | |
|
|
751 | UNLOCK (readdirlock); |
|
|
752 | |
|
|
753 | errno = errorno; |
|
|
754 | return e ? 0 : -1; |
|
|
755 | } |
|
|
756 | #endif |
|
|
757 | |
|
|
758 | /* sendfile always needs emulation */ |
|
|
759 | static ssize_t sendfile_ (int ofd, int ifd, off_t offset, size_t count, worker *self) |
|
|
760 | { |
|
|
761 | ssize_t res; |
|
|
762 | |
|
|
763 | if (!count) |
|
|
764 | return 0; |
|
|
765 | |
|
|
766 | #if HAVE_SENDFILE |
|
|
767 | # if __linux |
|
|
768 | res = sendfile (ofd, ifd, &offset, count); |
|
|
769 | |
|
|
770 | # elif __freebsd |
|
|
771 | /* |
|
|
772 | * Of course, the freebsd sendfile is a dire hack with no thoughts |
|
|
773 | * wasted on making it similar to other I/O functions. |
|
|
774 | */ |
|
|
775 | { |
|
|
776 | off_t sbytes; |
|
|
777 | res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); |
|
|
778 | |
|
|
779 | if (res < 0 && sbytes) |
|
|
780 | /* maybe only on EAGAIN only: as usual, the manpage leaves you guessing */ |
|
|
781 | res = sbytes; |
|
|
782 | } |
|
|
783 | |
|
|
784 | # elif __hpux |
|
|
785 | res = sendfile (ofd, ifd, offset, count, 0, 0); |
|
|
786 | |
|
|
787 | # elif __solaris |
|
|
788 | { |
|
|
789 | struct sendfilevec vec; |
|
|
790 | size_t sbytes; |
|
|
791 | |
|
|
792 | vec.sfv_fd = ifd; |
|
|
793 | vec.sfv_flag = 0; |
|
|
794 | vec.sfv_off = offset; |
|
|
795 | vec.sfv_len = count; |
|
|
796 | |
|
|
797 | res = sendfilev (ofd, &vec, 1, &sbytes); |
|
|
798 | |
|
|
799 | if (res < 0 && sbytes) |
|
|
800 | res = sbytes; |
|
|
801 | } |
|
|
802 | |
|
|
803 | # endif |
|
|
804 | #else |
|
|
805 | res = -1; |
|
|
806 | errno = ENOSYS; |
|
|
807 | #endif |
|
|
808 | |
|
|
809 | if (res < 0 |
|
|
810 | && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK |
|
|
811 | #if __solaris |
|
|
812 | || errno == EAFNOSUPPORT || errno == EPROTOTYPE |
|
|
813 | #endif |
|
|
814 | ) |
|
|
815 | ) |
|
|
816 | { |
|
|
817 | /* emulate sendfile. this is a major pain in the ass */ |
|
|
818 | dBUF; |
|
|
819 | |
|
|
820 | res = 0; |
|
|
821 | |
|
|
822 | while (count) |
|
|
823 | { |
|
|
824 | ssize_t cnt; |
|
|
825 | |
|
|
826 | cnt = pread (ifd, aio_buf, count > AIO_BUFSIZE ? AIO_BUFSIZE : count, offset); |
|
|
827 | |
|
|
828 | if (cnt <= 0) |
|
|
829 | { |
|
|
830 | if (cnt && !res) res = -1; |
|
|
831 | break; |
|
|
832 | } |
|
|
833 | |
|
|
834 | cnt = write (ofd, aio_buf, cnt); |
|
|
835 | |
|
|
836 | if (cnt <= 0) |
|
|
837 | { |
|
|
838 | if (cnt && !res) res = -1; |
|
|
839 | break; |
|
|
840 | } |
|
|
841 | |
|
|
842 | offset += cnt; |
|
|
843 | res += cnt; |
|
|
844 | count -= cnt; |
|
|
845 | } |
|
|
846 | } |
|
|
847 | |
|
|
848 | return res; |
|
|
849 | } |
|
|
850 | |
|
|
851 | /* read a full directory */ |
|
|
852 | static void scandir_ (aio_req req, worker *self) |
|
|
853 | { |
|
|
854 | DIR *dirp; |
|
|
855 | union |
|
|
856 | { |
|
|
857 | struct dirent d; |
|
|
858 | char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; |
|
|
859 | } *u; |
|
|
860 | struct dirent *entp; |
|
|
861 | char *name, *names; |
|
|
862 | int memlen = 4096; |
|
|
863 | int memofs = 0; |
|
|
864 | int res = 0; |
|
|
865 | int errorno; |
|
|
866 | |
|
|
867 | LOCK (wrklock); |
|
|
868 | self->dirp = dirp = opendir (req->dataptr); |
|
|
869 | self->dbuf = u = malloc (sizeof (*u)); |
|
|
870 | UNLOCK (wrklock); |
|
|
871 | |
|
|
872 | req->data2ptr = names = malloc (memlen); |
|
|
873 | |
|
|
874 | if (dirp && u && names) |
|
|
875 | for (;;) |
256 | { |
876 | { |
257 | /* read: grow scalar as necessary */ |
877 | errno = 0; |
258 | svptr = SvGROW (data, length + dataoffset); |
878 | readdir_r (dirp, &u->d, &entp); |
|
|
879 | |
|
|
880 | if (!entp) |
|
|
881 | break; |
|
|
882 | |
|
|
883 | name = entp->d_name; |
|
|
884 | |
|
|
885 | if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) |
|
|
886 | { |
|
|
887 | int len = strlen (name) + 1; |
|
|
888 | |
|
|
889 | res++; |
|
|
890 | |
|
|
891 | while (memofs + len > memlen) |
|
|
892 | { |
|
|
893 | memlen *= 2; |
|
|
894 | LOCK (wrklock); |
|
|
895 | req->data2ptr = names = realloc (names, memlen); |
|
|
896 | UNLOCK (wrklock); |
|
|
897 | |
|
|
898 | if (!names) |
|
|
899 | break; |
|
|
900 | } |
|
|
901 | |
|
|
902 | memcpy (names + memofs, name, len); |
|
|
903 | memofs += len; |
|
|
904 | } |
259 | } |
905 | } |
260 | |
906 | |
261 | if (length < 0) |
907 | if (errno) |
262 | croak ("length must not be negative"); |
908 | res = -1; |
263 | |
909 | |
264 | Newz (0, req, 1, aio_cb); |
910 | req->result = res; |
265 | |
|
|
266 | if (!req) |
|
|
267 | croak ("out of memory during aio_req allocation"); |
|
|
268 | |
|
|
269 | req->type = dowrite ? REQ_WRITE : REQ_READ; |
|
|
270 | req->fd = fd; |
|
|
271 | req->offset = offset; |
|
|
272 | req->length = length; |
|
|
273 | req->data = SvREFCNT_inc (data); |
|
|
274 | req->dataptr = (char *)svptr + dataoffset; |
|
|
275 | req->callback = SvREFCNT_inc (callback); |
|
|
276 | |
|
|
277 | send_req (req); |
|
|
278 | } |
911 | } |
279 | |
912 | |
280 | static void * |
913 | /*****************************************************************************/ |
|
|
914 | |
281 | aio_proc (void *thr_arg) |
915 | static void *aio_proc (void *thr_arg) |
282 | { |
916 | { |
283 | aio_req req; |
917 | aio_req req; |
284 | int type; |
918 | int type; |
|
|
919 | worker *self = (worker *)thr_arg; |
285 | |
920 | |
286 | do |
921 | do |
287 | { |
922 | { |
288 | pthread_mutex_lock (&reqlock); |
923 | LOCK (reqlock); |
289 | |
924 | |
290 | for (;;) |
925 | for (;;) |
291 | { |
926 | { |
292 | req = reqs; |
927 | self->req = req = reqq_shift (&req_queue); |
293 | |
|
|
294 | if (reqs) |
|
|
295 | { |
|
|
296 | reqs = reqs->next; |
|
|
297 | if (!reqs) reqe = 0; |
|
|
298 | } |
|
|
299 | |
928 | |
300 | if (req) |
929 | if (req) |
301 | break; |
930 | break; |
302 | |
931 | |
303 | pthread_cond_wait (&reqwait, &reqlock); |
932 | pthread_cond_wait (&reqwait, &reqlock); |
304 | } |
933 | } |
305 | |
934 | |
306 | pthread_mutex_unlock (&reqlock); |
935 | UNLOCK (reqlock); |
307 | |
936 | |
308 | errno = 0; /* strictly unnecessary */ |
937 | errno = 0; /* strictly unnecessary */ |
|
|
938 | type = req->type; /* remember type for QUIT check */ |
309 | |
939 | |
310 | type = req->type; |
940 | if (!(req->flags & FLAG_CANCELLED)) |
311 | |
|
|
312 | switch (type) |
941 | switch (type) |
313 | { |
942 | { |
314 | case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, req->offset); break; |
943 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
315 | case REQ_WRITE: req->result = pwrite64 (req->fd, req->dataptr, req->length, req->offset); break; |
944 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
316 | #if SYS_readahead |
945 | |
317 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
946 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
318 | #else |
947 | case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length, self); break; |
319 | case REQ_READAHEAD: req->result = -1; errno = ENOSYS; break; |
|
|
320 | #endif |
|
|
321 | |
948 | |
322 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
949 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
323 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
950 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
324 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
951 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
325 | |
952 | |
326 | case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
953 | case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
327 | case REQ_CLOSE: req->result = close (req->fd); break; |
954 | case REQ_CLOSE: req->result = close (req->fd); break; |
328 | case REQ_UNLINK: req->result = unlink (req->dataptr); break; |
955 | case REQ_UNLINK: req->result = unlink (req->dataptr); break; |
329 | |
|
|
330 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
956 | case REQ_RMDIR: req->result = rmdir (req->dataptr); break; |
|
|
957 | case REQ_RENAME: req->result = rename (req->data2ptr, req->dataptr); break; |
|
|
958 | case REQ_LINK: req->result = link (req->data2ptr, req->dataptr); break; |
|
|
959 | case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; |
|
|
960 | |
331 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
961 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
|
|
962 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
|
|
963 | case REQ_READDIR: scandir_ (req, self); break; |
332 | |
964 | |
|
|
965 | case REQ_BUSY: |
|
|
966 | { |
|
|
967 | struct timeval tv; |
|
|
968 | |
|
|
969 | tv.tv_sec = req->fd; |
|
|
970 | tv.tv_usec = req->fd2; |
|
|
971 | |
|
|
972 | req->result = select (0, 0, 0, 0, &tv); |
|
|
973 | } |
|
|
974 | |
|
|
975 | case REQ_GROUP: |
|
|
976 | case REQ_NOP: |
333 | case REQ_QUIT: |
977 | case REQ_QUIT: |
334 | break; |
978 | break; |
335 | |
979 | |
336 | default: |
980 | default: |
337 | req->result = ENOSYS; |
981 | req->result = ENOSYS; |
338 | break; |
982 | break; |
339 | } |
983 | } |
340 | |
984 | |
341 | req->errorno = errno; |
985 | req->errorno = errno; |
342 | |
986 | |
343 | pthread_mutex_lock (&reslock); |
987 | LOCK (reslock); |
344 | |
988 | |
345 | req->next = 0; |
989 | if (!reqq_push (&res_queue, req)) |
346 | |
|
|
347 | if (rese) |
|
|
348 | { |
|
|
349 | rese->next = req; |
|
|
350 | rese = req; |
|
|
351 | } |
|
|
352 | else |
|
|
353 | { |
|
|
354 | rese = ress = req; |
|
|
355 | |
|
|
356 | /* write a dummy byte to the pipe so fh becomes ready */ |
990 | /* write a dummy byte to the pipe so fh becomes ready */ |
357 | write (respipe [1], &respipe, 1); |
991 | write (respipe [1], &respipe, 1); |
358 | } |
|
|
359 | |
992 | |
360 | pthread_mutex_unlock (&reslock); |
993 | self->req = 0; |
|
|
994 | worker_clear (self); |
|
|
995 | |
|
|
996 | UNLOCK (reslock); |
361 | } |
997 | } |
362 | while (type != REQ_QUIT); |
998 | while (type != REQ_QUIT); |
363 | |
999 | |
|
|
1000 | LOCK (wrklock); |
|
|
1001 | worker_free (self); |
|
|
1002 | UNLOCK (wrklock); |
|
|
1003 | |
364 | return 0; |
1004 | return 0; |
365 | } |
1005 | } |
366 | |
1006 | |
|
|
1007 | /*****************************************************************************/ |
|
|
1008 | |
|
|
1009 | static void atfork_prepare (void) |
|
|
1010 | { |
|
|
1011 | LOCK (wrklock); |
|
|
1012 | LOCK (reqlock); |
|
|
1013 | LOCK (reslock); |
|
|
1014 | #if !HAVE_PREADWRITE |
|
|
1015 | LOCK (preadwritelock); |
|
|
1016 | #endif |
|
|
1017 | #if !HAVE_READDIR_R |
|
|
1018 | LOCK (readdirlock); |
|
|
1019 | #endif |
|
|
1020 | } |
|
|
1021 | |
|
|
1022 | static void atfork_parent (void) |
|
|
1023 | { |
|
|
1024 | #if !HAVE_READDIR_R |
|
|
1025 | UNLOCK (readdirlock); |
|
|
1026 | #endif |
|
|
1027 | #if !HAVE_PREADWRITE |
|
|
1028 | UNLOCK (preadwritelock); |
|
|
1029 | #endif |
|
|
1030 | UNLOCK (reslock); |
|
|
1031 | UNLOCK (reqlock); |
|
|
1032 | UNLOCK (wrklock); |
|
|
1033 | } |
|
|
1034 | |
|
|
1035 | static void atfork_child (void) |
|
|
1036 | { |
|
|
1037 | aio_req prv; |
|
|
1038 | |
|
|
1039 | while (prv = reqq_shift (&req_queue)) |
|
|
1040 | req_free (prv); |
|
|
1041 | |
|
|
1042 | while (prv = reqq_shift (&res_queue)) |
|
|
1043 | req_free (prv); |
|
|
1044 | |
|
|
1045 | while (wrk_first.next != &wrk_first) |
|
|
1046 | { |
|
|
1047 | worker *wrk = wrk_first.next; |
|
|
1048 | |
|
|
1049 | if (wrk->req) |
|
|
1050 | req_free (wrk->req); |
|
|
1051 | |
|
|
1052 | worker_clear (wrk); |
|
|
1053 | worker_free (wrk); |
|
|
1054 | } |
|
|
1055 | |
|
|
1056 | started = 0; |
|
|
1057 | nreqs = 0; |
|
|
1058 | |
|
|
1059 | close (respipe [0]); |
|
|
1060 | close (respipe [1]); |
|
|
1061 | create_pipe (); |
|
|
1062 | |
|
|
1063 | atfork_parent (); |
|
|
1064 | } |
|
|
1065 | |
|
|
1066 | #define dREQ \ |
|
|
1067 | aio_req req; \ |
|
|
1068 | int req_pri = next_pri; \ |
|
|
1069 | next_pri = DEFAULT_PRI + PRI_BIAS; \ |
|
|
1070 | \ |
|
|
1071 | if (SvOK (callback) && !SvROK (callback)) \ |
|
|
1072 | croak ("callback must be undef or of reference type"); \ |
|
|
1073 | \ |
|
|
1074 | Newz (0, req, 1, aio_cb); \ |
|
|
1075 | if (!req) \ |
|
|
1076 | croak ("out of memory during aio_req allocation"); \ |
|
|
1077 | \ |
|
|
1078 | req->callback = newSVsv (callback); \ |
|
|
1079 | req->pri = req_pri |
|
|
1080 | |
|
|
1081 | #define REQ_SEND \ |
|
|
1082 | req_send (req); \ |
|
|
1083 | \ |
|
|
1084 | if (GIMME_V != G_VOID) \ |
|
|
1085 | XPUSHs (req_sv (req, AIO_REQ_KLASS)); |
|
|
1086 | |
367 | MODULE = IO::AIO PACKAGE = IO::AIO |
1087 | MODULE = IO::AIO PACKAGE = IO::AIO |
368 | |
1088 | |
|
|
1089 | PROTOTYPES: ENABLE |
|
|
1090 | |
369 | BOOT: |
1091 | BOOT: |
370 | { |
1092 | { |
371 | if (pipe (respipe)) |
1093 | HV *stash = gv_stashpv ("IO::AIO", 1); |
372 | croak ("unable to initialize result pipe"); |
1094 | newCONSTSUB (stash, "EXDEV", newSViv (EXDEV)); |
|
|
1095 | newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY)); |
|
|
1096 | newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY)); |
373 | |
1097 | |
374 | if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)) |
1098 | create_pipe (); |
375 | croak ("cannot set result pipe to nonblocking mode"); |
1099 | pthread_atfork (atfork_prepare, atfork_parent, atfork_child); |
376 | |
|
|
377 | if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) |
|
|
378 | croak ("cannot set result pipe to nonblocking mode"); |
|
|
379 | } |
1100 | } |
380 | |
1101 | |
381 | void |
1102 | void |
382 | min_parallel(nthreads) |
1103 | min_parallel (nthreads) |
383 | int nthreads |
1104 | int nthreads |
384 | PROTOTYPE: $ |
1105 | PROTOTYPE: $ |
385 | CODE: |
|
|
386 | while (nthreads > started) |
|
|
387 | start_thread (); |
|
|
388 | |
1106 | |
389 | void |
1107 | void |
390 | max_parallel(nthreads) |
1108 | max_parallel (nthreads) |
391 | int nthreads |
1109 | int nthreads |
392 | PROTOTYPE: $ |
1110 | PROTOTYPE: $ |
393 | CODE: |
|
|
394 | { |
|
|
395 | int cur = started; |
|
|
396 | while (cur > nthreads) |
|
|
397 | { |
|
|
398 | end_thread (); |
|
|
399 | cur--; |
|
|
400 | } |
|
|
401 | |
|
|
402 | while (started > nthreads) |
|
|
403 | { |
|
|
404 | poll_wait (); |
|
|
405 | poll_cb (aTHX); |
|
|
406 | } |
|
|
407 | } |
|
|
408 | |
1111 | |
409 | int |
1112 | int |
410 | max_outstanding(nreqs) |
1113 | max_outstanding (nreqs) |
411 | int nreqs |
1114 | int nreqs |
412 | PROTOTYPE: $ |
1115 | PROTOTYPE: $ |
413 | CODE: |
1116 | CODE: |
414 | RETVAL = max_outstanding; |
1117 | RETVAL = max_outstanding; |
415 | max_outstanding = nreqs; |
1118 | max_outstanding = nreqs; |
416 | |
1119 | |
417 | void |
1120 | void |
418 | aio_open(pathname,flags,mode,callback) |
1121 | aio_open (pathname,flags,mode,callback=&PL_sv_undef) |
419 | SV * pathname |
1122 | SV * pathname |
420 | int flags |
1123 | int flags |
421 | int mode |
1124 | int mode |
422 | SV * callback |
1125 | SV * callback |
423 | PROTOTYPE: $$$$ |
1126 | PROTOTYPE: $$$;$ |
424 | CODE: |
1127 | PPCODE: |
425 | { |
1128 | { |
426 | aio_req req; |
1129 | dREQ; |
427 | |
|
|
428 | Newz (0, req, 1, aio_cb); |
|
|
429 | |
|
|
430 | if (!req) |
|
|
431 | croak ("out of memory during aio_req allocation"); |
|
|
432 | |
1130 | |
433 | req->type = REQ_OPEN; |
1131 | req->type = REQ_OPEN; |
434 | req->data = newSVsv (pathname); |
1132 | req->data = newSVsv (pathname); |
435 | req->dataptr = SvPV_nolen (req->data); |
1133 | req->dataptr = SvPVbyte_nolen (req->data); |
436 | req->fd = flags; |
1134 | req->fd = flags; |
437 | req->mode = mode; |
1135 | req->mode = mode; |
438 | req->callback = SvREFCNT_inc (callback); |
|
|
439 | |
1136 | |
440 | send_req (req); |
1137 | REQ_SEND; |
441 | } |
1138 | } |
442 | |
1139 | |
443 | void |
1140 | void |
444 | aio_close(fh,callback) |
1141 | aio_close (fh,callback=&PL_sv_undef) |
445 | InputStream fh |
1142 | SV * fh |
446 | SV * callback |
1143 | SV * callback |
447 | PROTOTYPE: $$ |
1144 | PROTOTYPE: $;$ |
448 | ALIAS: |
1145 | ALIAS: |
449 | aio_close = REQ_CLOSE |
1146 | aio_close = REQ_CLOSE |
450 | aio_fsync = REQ_FSYNC |
1147 | aio_fsync = REQ_FSYNC |
451 | aio_fdatasync = REQ_FDATASYNC |
1148 | aio_fdatasync = REQ_FDATASYNC |
452 | CODE: |
1149 | PPCODE: |
|
|
1150 | { |
|
|
1151 | dREQ; |
|
|
1152 | |
|
|
1153 | req->type = ix; |
|
|
1154 | req->fh = newSVsv (fh); |
|
|
1155 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
|
|
1156 | |
|
|
1157 | REQ_SEND (req); |
|
|
1158 | } |
|
|
1159 | |
|
|
1160 | void |
|
|
1161 | aio_read (fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
|
|
1162 | SV * fh |
|
|
1163 | UV offset |
|
|
1164 | UV length |
|
|
1165 | SV * data |
|
|
1166 | UV dataoffset |
|
|
1167 | SV * callback |
|
|
1168 | ALIAS: |
|
|
1169 | aio_read = REQ_READ |
|
|
1170 | aio_write = REQ_WRITE |
|
|
1171 | PROTOTYPE: $$$$$;$ |
|
|
1172 | PPCODE: |
453 | { |
1173 | { |
454 | aio_req req; |
1174 | aio_req req; |
|
|
1175 | STRLEN svlen; |
|
|
1176 | char *svptr = SvPVbyte (data, svlen); |
455 | |
1177 | |
456 | Newz (0, req, 1, aio_cb); |
1178 | SvUPGRADE (data, SVt_PV); |
|
|
1179 | SvPOK_on (data); |
457 | |
1180 | |
458 | if (!req) |
1181 | if (dataoffset < 0) |
459 | croak ("out of memory during aio_req allocation"); |
1182 | dataoffset += svlen; |
460 | |
1183 | |
461 | req->type = ix; |
1184 | if (dataoffset < 0 || dataoffset > svlen) |
462 | req->fd = PerlIO_fileno (fh); |
1185 | croak ("data offset outside of string"); |
463 | req->callback = SvREFCNT_inc (callback); |
|
|
464 | |
1186 | |
465 | send_req (req); |
1187 | if (ix == REQ_WRITE) |
466 | } |
1188 | { |
467 | |
1189 | /* write: check length and adjust. */ |
468 | void |
1190 | if (length < 0 || length + dataoffset > svlen) |
469 | aio_read(fh,offset,length,data,dataoffset,callback) |
1191 | length = svlen - dataoffset; |
470 | InputStream fh |
1192 | } |
471 | UV offset |
1193 | else |
472 | IV length |
1194 | { |
473 | SV * data |
1195 | /* read: grow scalar as necessary */ |
474 | IV dataoffset |
1196 | svptr = SvGROW (data, length + dataoffset); |
475 | SV * callback |
1197 | } |
476 | PROTOTYPE: $$$$$$ |
|
|
477 | CODE: |
|
|
478 | read_write (aTHX_ 0, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
|
|
479 | |
|
|
480 | void |
|
|
481 | aio_write(fh,offset,length,data,dataoffset,callback) |
|
|
482 | OutputStream fh |
|
|
483 | UV offset |
|
|
484 | IV length |
|
|
485 | SV * data |
|
|
486 | IV dataoffset |
|
|
487 | SV * callback |
|
|
488 | PROTOTYPE: $$$$$$ |
|
|
489 | CODE: |
|
|
490 | read_write (aTHX_ 1, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
|
|
491 | |
|
|
492 | void |
|
|
493 | aio_readahead(fh,offset,length,callback) |
|
|
494 | InputStream fh |
|
|
495 | UV offset |
|
|
496 | IV length |
|
|
497 | SV * callback |
|
|
498 | PROTOTYPE: $$$$ |
|
|
499 | CODE: |
|
|
500 | { |
|
|
501 | aio_req req; |
|
|
502 | |
1198 | |
503 | if (length < 0) |
1199 | if (length < 0) |
504 | croak ("length must not be negative"); |
1200 | croak ("length must not be negative"); |
505 | |
1201 | |
506 | Newz (0, req, 1, aio_cb); |
1202 | { |
|
|
1203 | dREQ; |
507 | |
1204 | |
508 | if (!req) |
1205 | req->type = ix; |
509 | croak ("out of memory during aio_req allocation"); |
1206 | req->fh = newSVsv (fh); |
|
|
1207 | req->fd = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh)) |
|
|
1208 | : IoOFP (sv_2io (fh))); |
|
|
1209 | req->offset = offset; |
|
|
1210 | req->length = length; |
|
|
1211 | req->data = SvREFCNT_inc (data); |
|
|
1212 | req->dataptr = (char *)svptr + dataoffset; |
|
|
1213 | |
|
|
1214 | if (!SvREADONLY (data)) |
|
|
1215 | { |
|
|
1216 | SvREADONLY_on (data); |
|
|
1217 | req->data2ptr = (void *)data; |
|
|
1218 | } |
|
|
1219 | |
|
|
1220 | REQ_SEND; |
|
|
1221 | } |
|
|
1222 | } |
|
|
1223 | |
|
|
1224 | void |
|
|
1225 | aio_sendfile (out_fh,in_fh,in_offset,length,callback=&PL_sv_undef) |
|
|
1226 | SV * out_fh |
|
|
1227 | SV * in_fh |
|
|
1228 | UV in_offset |
|
|
1229 | UV length |
|
|
1230 | SV * callback |
|
|
1231 | PROTOTYPE: $$$$;$ |
|
|
1232 | PPCODE: |
|
|
1233 | { |
|
|
1234 | dREQ; |
|
|
1235 | |
|
|
1236 | req->type = REQ_SENDFILE; |
|
|
1237 | req->fh = newSVsv (out_fh); |
|
|
1238 | req->fd = PerlIO_fileno (IoIFP (sv_2io (out_fh))); |
|
|
1239 | req->fh2 = newSVsv (in_fh); |
|
|
1240 | req->fd2 = PerlIO_fileno (IoIFP (sv_2io (in_fh))); |
|
|
1241 | req->offset = in_offset; |
|
|
1242 | req->length = length; |
|
|
1243 | |
|
|
1244 | REQ_SEND; |
|
|
1245 | } |
|
|
1246 | |
|
|
1247 | void |
|
|
1248 | aio_readahead (fh,offset,length,callback=&PL_sv_undef) |
|
|
1249 | SV * fh |
|
|
1250 | UV offset |
|
|
1251 | IV length |
|
|
1252 | SV * callback |
|
|
1253 | PROTOTYPE: $$$;$ |
|
|
1254 | PPCODE: |
|
|
1255 | { |
|
|
1256 | dREQ; |
510 | |
1257 | |
511 | req->type = REQ_READAHEAD; |
1258 | req->type = REQ_READAHEAD; |
|
|
1259 | req->fh = newSVsv (fh); |
512 | req->fd = PerlIO_fileno (fh); |
1260 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
513 | req->offset = offset; |
1261 | req->offset = offset; |
514 | req->length = length; |
1262 | req->length = length; |
515 | req->callback = SvREFCNT_inc (callback); |
|
|
516 | |
1263 | |
517 | send_req (req); |
1264 | REQ_SEND; |
518 | } |
1265 | } |
519 | |
1266 | |
520 | void |
1267 | void |
521 | aio_stat(fh_or_path,callback) |
1268 | aio_stat (fh_or_path,callback=&PL_sv_undef) |
522 | SV * fh_or_path |
1269 | SV * fh_or_path |
523 | SV * callback |
1270 | SV * callback |
524 | PROTOTYPE: $$ |
|
|
525 | ALIAS: |
1271 | ALIAS: |
|
|
1272 | aio_stat = REQ_STAT |
526 | aio_lstat = 1 |
1273 | aio_lstat = REQ_LSTAT |
527 | CODE: |
1274 | PPCODE: |
528 | { |
1275 | { |
529 | aio_req req; |
1276 | dREQ; |
530 | |
|
|
531 | Newz (0, req, 1, aio_cb); |
|
|
532 | |
|
|
533 | if (!req) |
|
|
534 | croak ("out of memory during aio_req allocation"); |
|
|
535 | |
1277 | |
536 | New (0, req->statdata, 1, Stat_t); |
1278 | New (0, req->statdata, 1, Stat_t); |
537 | |
|
|
538 | if (!req->statdata) |
1279 | if (!req->statdata) |
|
|
1280 | { |
|
|
1281 | req_free (req); |
539 | croak ("out of memory during aio_req->statdata allocation"); |
1282 | croak ("out of memory during aio_req->statdata allocation"); |
|
|
1283 | } |
540 | |
1284 | |
541 | if (SvPOK (fh_or_path)) |
1285 | if (SvPOK (fh_or_path)) |
542 | { |
1286 | { |
543 | req->type = ix ? REQ_LSTAT : REQ_STAT; |
1287 | req->type = ix; |
544 | req->data = newSVsv (fh_or_path); |
1288 | req->data = newSVsv (fh_or_path); |
545 | req->dataptr = SvPV_nolen (req->data); |
1289 | req->dataptr = SvPVbyte_nolen (req->data); |
546 | } |
1290 | } |
547 | else |
1291 | else |
548 | { |
1292 | { |
549 | req->type = REQ_FSTAT; |
1293 | req->type = REQ_FSTAT; |
|
|
1294 | req->fh = newSVsv (fh_or_path); |
550 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
1295 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
551 | } |
1296 | } |
552 | |
1297 | |
553 | req->callback = SvREFCNT_inc (callback); |
1298 | REQ_SEND; |
554 | |
|
|
555 | send_req (req); |
|
|
556 | } |
1299 | } |
557 | |
1300 | |
558 | void |
1301 | void |
559 | aio_unlink(pathname,callback) |
1302 | aio_unlink (pathname,callback=&PL_sv_undef) |
560 | SV * pathname |
1303 | SV * pathname |
561 | SV * callback |
1304 | SV * callback |
|
|
1305 | ALIAS: |
|
|
1306 | aio_unlink = REQ_UNLINK |
|
|
1307 | aio_rmdir = REQ_RMDIR |
|
|
1308 | aio_readdir = REQ_READDIR |
|
|
1309 | PPCODE: |
|
|
1310 | { |
|
|
1311 | dREQ; |
|
|
1312 | |
|
|
1313 | req->type = ix; |
|
|
1314 | req->data = newSVsv (pathname); |
|
|
1315 | req->dataptr = SvPVbyte_nolen (req->data); |
|
|
1316 | |
|
|
1317 | REQ_SEND; |
|
|
1318 | } |
|
|
1319 | |
|
|
1320 | void |
|
|
1321 | aio_link (oldpath,newpath,callback=&PL_sv_undef) |
|
|
1322 | SV * oldpath |
|
|
1323 | SV * newpath |
|
|
1324 | SV * callback |
|
|
1325 | ALIAS: |
|
|
1326 | aio_link = REQ_LINK |
|
|
1327 | aio_symlink = REQ_SYMLINK |
|
|
1328 | aio_rename = REQ_RENAME |
|
|
1329 | PPCODE: |
|
|
1330 | { |
|
|
1331 | dREQ; |
|
|
1332 | |
|
|
1333 | req->type = ix; |
|
|
1334 | req->fh = newSVsv (oldpath); |
|
|
1335 | req->data2ptr = SvPVbyte_nolen (req->fh); |
|
|
1336 | req->data = newSVsv (newpath); |
|
|
1337 | req->dataptr = SvPVbyte_nolen (req->data); |
|
|
1338 | |
|
|
1339 | REQ_SEND; |
|
|
1340 | } |
|
|
1341 | |
|
|
1342 | void |
|
|
1343 | aio_busy (delay,callback=&PL_sv_undef) |
|
|
1344 | double delay |
|
|
1345 | SV * callback |
|
|
1346 | PPCODE: |
|
|
1347 | { |
|
|
1348 | dREQ; |
|
|
1349 | |
|
|
1350 | req->type = REQ_BUSY; |
|
|
1351 | req->fd = delay < 0. ? 0 : delay; |
|
|
1352 | req->fd2 = delay < 0. ? 0 : 1000. * (delay - req->fd); |
|
|
1353 | |
|
|
1354 | REQ_SEND; |
|
|
1355 | } |
|
|
1356 | |
|
|
1357 | void |
|
|
1358 | aio_group (callback=&PL_sv_undef) |
|
|
1359 | SV * callback |
562 | PROTOTYPE: $$ |
1360 | PROTOTYPE: ;$ |
|
|
1361 | PPCODE: |
|
|
1362 | { |
|
|
1363 | dREQ; |
|
|
1364 | |
|
|
1365 | req->type = REQ_GROUP; |
|
|
1366 | req_send (req); |
|
|
1367 | |
|
|
1368 | XPUSHs (req_sv (req, AIO_GRP_KLASS)); |
|
|
1369 | } |
|
|
1370 | |
|
|
1371 | void |
|
|
1372 | aio_nop (callback=&PL_sv_undef) |
|
|
1373 | SV * callback |
|
|
1374 | PPCODE: |
|
|
1375 | { |
|
|
1376 | dREQ; |
|
|
1377 | |
|
|
1378 | req->type = REQ_NOP; |
|
|
1379 | |
|
|
1380 | REQ_SEND; |
|
|
1381 | } |
|
|
1382 | |
|
|
1383 | void |
|
|
1384 | aioreq_pri (int pri = DEFAULT_PRI) |
|
|
1385 | CODE: |
|
|
1386 | if (pri < PRI_MIN) pri = PRI_MIN; |
|
|
1387 | if (pri > PRI_MAX) pri = PRI_MAX; |
|
|
1388 | next_pri = pri + PRI_BIAS; |
|
|
1389 | |
|
|
1390 | void |
|
|
1391 | aioreq_nice (int nice = 0) |
|
|
1392 | CODE: |
|
|
1393 | nice = next_pri - nice; |
|
|
1394 | if (nice < PRI_MIN) nice = PRI_MIN; |
|
|
1395 | if (nice > PRI_MAX) nice = PRI_MAX; |
|
|
1396 | next_pri = nice + PRI_BIAS; |
|
|
1397 | |
|
|
1398 | void |
|
|
1399 | flush () |
|
|
1400 | PROTOTYPE: |
563 | CODE: |
1401 | CODE: |
564 | { |
1402 | while (nreqs) |
565 | aio_req req; |
1403 | { |
566 | |
1404 | poll_wait (); |
567 | Newz (0, req, 1, aio_cb); |
1405 | poll_cb (); |
568 | |
1406 | } |
569 | if (!req) |
1407 | |
570 | croak ("out of memory during aio_req allocation"); |
1408 | void |
571 | |
1409 | poll() |
572 | req->type = REQ_UNLINK; |
1410 | PROTOTYPE: |
573 | req->data = newSVsv (pathname); |
1411 | CODE: |
574 | req->dataptr = SvPV_nolen (req->data); |
1412 | if (nreqs) |
575 | req->callback = SvREFCNT_inc (callback); |
1413 | { |
576 | |
1414 | poll_wait (); |
577 | send_req (req); |
1415 | poll_cb (); |
578 | } |
1416 | } |
579 | |
1417 | |
580 | int |
1418 | int |
581 | poll_fileno() |
1419 | poll_fileno() |
582 | PROTOTYPE: |
1420 | PROTOTYPE: |
583 | CODE: |
1421 | CODE: |
… | |
… | |
587 | |
1425 | |
588 | int |
1426 | int |
589 | poll_cb(...) |
1427 | poll_cb(...) |
590 | PROTOTYPE: |
1428 | PROTOTYPE: |
591 | CODE: |
1429 | CODE: |
592 | RETVAL = poll_cb (aTHX); |
1430 | RETVAL = poll_cb (); |
593 | OUTPUT: |
1431 | OUTPUT: |
594 | RETVAL |
1432 | RETVAL |
595 | |
1433 | |
596 | void |
1434 | void |
597 | poll_wait() |
1435 | poll_wait() |
… | |
… | |
606 | CODE: |
1444 | CODE: |
607 | RETVAL = nreqs; |
1445 | RETVAL = nreqs; |
608 | OUTPUT: |
1446 | OUTPUT: |
609 | RETVAL |
1447 | RETVAL |
610 | |
1448 | |
|
|
1449 | PROTOTYPES: DISABLE |
|
|
1450 | |
|
|
1451 | MODULE = IO::AIO PACKAGE = IO::AIO::REQ |
|
|
1452 | |
|
|
1453 | void |
|
|
1454 | cancel (aio_req_ornot req) |
|
|
1455 | CODE: |
|
|
1456 | req_cancel (req); |
|
|
1457 | |
|
|
1458 | void |
|
|
1459 | cb (aio_req_ornot req, SV *callback=&PL_sv_undef) |
|
|
1460 | CODE: |
|
|
1461 | SvREFCNT_dec (req->callback); |
|
|
1462 | req->callback = newSVsv (callback); |
|
|
1463 | |
|
|
1464 | MODULE = IO::AIO PACKAGE = IO::AIO::GRP |
|
|
1465 | |
|
|
1466 | void |
|
|
1467 | add (aio_req grp, ...) |
|
|
1468 | PPCODE: |
|
|
1469 | { |
|
|
1470 | int i; |
|
|
1471 | aio_req req; |
|
|
1472 | |
|
|
1473 | if (grp->fd == 2) |
|
|
1474 | croak ("cannot add requests to IO::AIO::GRP after the group finished"); |
|
|
1475 | |
|
|
1476 | for (i = 1; i < items; ++i ) |
|
|
1477 | { |
|
|
1478 | if (GIMME_V != G_VOID) |
|
|
1479 | XPUSHs (sv_2mortal (newSVsv (ST (i)))); |
|
|
1480 | |
|
|
1481 | req = SvAIO_REQ (ST (i)); |
|
|
1482 | |
|
|
1483 | if (req) |
|
|
1484 | { |
|
|
1485 | ++grp->length; |
|
|
1486 | req->grp = grp; |
|
|
1487 | |
|
|
1488 | req->grp_prev = 0; |
|
|
1489 | req->grp_next = grp->grp_first; |
|
|
1490 | |
|
|
1491 | if (grp->grp_first) |
|
|
1492 | grp->grp_first->grp_prev = req; |
|
|
1493 | |
|
|
1494 | grp->grp_first = req; |
|
|
1495 | } |
|
|
1496 | } |
|
|
1497 | } |
|
|
1498 | |
|
|
1499 | void |
|
|
1500 | result (aio_req grp, ...) |
|
|
1501 | CODE: |
|
|
1502 | { |
|
|
1503 | int i; |
|
|
1504 | AV *av = newAV (); |
|
|
1505 | |
|
|
1506 | for (i = 1; i < items; ++i ) |
|
|
1507 | av_push (av, newSVsv (ST (i))); |
|
|
1508 | |
|
|
1509 | SvREFCNT_dec (grp->data); |
|
|
1510 | grp->data = (SV *)av; |
|
|
1511 | } |
|
|
1512 | |
|
|
1513 | void |
|
|
1514 | limit (aio_req grp, int limit) |
|
|
1515 | CODE: |
|
|
1516 | grp->fd2 = limit; |
|
|
1517 | aio_grp_feed (grp); |
|
|
1518 | |
|
|
1519 | void |
|
|
1520 | feed (aio_req grp, SV *callback=&PL_sv_undef) |
|
|
1521 | CODE: |
|
|
1522 | { |
|
|
1523 | SvREFCNT_dec (grp->fh2); |
|
|
1524 | grp->fh2 = newSVsv (callback); |
|
|
1525 | |
|
|
1526 | if (grp->fd2 <= 0) |
|
|
1527 | grp->fd2 = 2; |
|
|
1528 | |
|
|
1529 | aio_grp_feed (grp); |
|
|
1530 | } |
|
|
1531 | |