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