|
|
1 | /* solaris */ |
|
|
2 | #define _POSIX_PTHREAD_SEMANTICS 1 |
|
|
3 | |
|
|
4 | #if __linux && !defined(_GNU_SOURCE) |
|
|
5 | # define _GNU_SOURCE |
|
|
6 | #endif |
|
|
7 | |
|
|
8 | /* just in case */ |
1 | #define _REENTRANT 1 |
9 | #define _REENTRANT 1 |
|
|
10 | |
2 | #include <errno.h> |
11 | #include <errno.h> |
3 | |
12 | |
4 | #include "EXTERN.h" |
13 | #include "EXTERN.h" |
5 | #include "perl.h" |
14 | #include "perl.h" |
6 | #include "XSUB.h" |
15 | #include "XSUB.h" |
… | |
… | |
39 | /* used for struct dirent, AIX doesn't provide it */ |
48 | /* used for struct dirent, AIX doesn't provide it */ |
40 | #ifndef NAME_MAX |
49 | #ifndef NAME_MAX |
41 | # define NAME_MAX 4096 |
50 | # define NAME_MAX 4096 |
42 | #endif |
51 | #endif |
43 | |
52 | |
|
|
53 | #ifndef PTHREAD_STACK_MIN |
|
|
54 | /* care for broken platforms, e.g. windows */ |
|
|
55 | # define PTHREAD_STACK_MIN 16384 |
|
|
56 | #endif |
|
|
57 | |
44 | #if __ia64 |
58 | #if __ia64 |
45 | # define STACKSIZE 65536 |
59 | # define STACKSIZE 65536 |
|
|
60 | #elif __i386 || __x86_64 /* 16k is unreasonably high :( */ |
|
|
61 | # define STACKSIZE PTHREAD_STACK_MIN |
46 | #else |
62 | #else |
47 | # define STACKSIZE 8192 |
63 | # define STACKSIZE 16384 |
48 | #endif |
64 | #endif |
|
|
65 | |
|
|
66 | /* buffer size for various temporary buffers */ |
|
|
67 | #define AIO_BUFSIZE 65536 |
|
|
68 | |
|
|
69 | #define dBUF \ |
|
|
70 | char *aio_buf; \ |
|
|
71 | LOCK (wrklock); \ |
|
|
72 | self->dbuf = aio_buf = malloc (AIO_BUFSIZE); \ |
|
|
73 | UNLOCK (wrklock); \ |
|
|
74 | if (!aio_buf) \ |
|
|
75 | return -1; |
49 | |
76 | |
50 | enum { |
77 | enum { |
51 | REQ_QUIT, |
78 | REQ_QUIT, |
52 | REQ_OPEN, REQ_CLOSE, |
79 | REQ_OPEN, REQ_CLOSE, |
53 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
80 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
… | |
… | |
56 | REQ_FSYNC, REQ_FDATASYNC, |
83 | REQ_FSYNC, REQ_FDATASYNC, |
57 | REQ_UNLINK, REQ_RMDIR, REQ_RENAME, |
84 | REQ_UNLINK, REQ_RMDIR, REQ_RENAME, |
58 | REQ_READDIR, |
85 | REQ_READDIR, |
59 | REQ_LINK, REQ_SYMLINK, |
86 | REQ_LINK, REQ_SYMLINK, |
60 | REQ_GROUP, REQ_NOP, |
87 | REQ_GROUP, REQ_NOP, |
61 | REQ_SLEEP, |
88 | REQ_BUSY, |
62 | }; |
89 | }; |
63 | |
90 | |
64 | #define AIO_REQ_KLASS "IO::AIO::REQ" |
91 | #define AIO_REQ_KLASS "IO::AIO::REQ" |
65 | #define AIO_GRP_KLASS "IO::AIO::GRP" |
92 | #define AIO_GRP_KLASS "IO::AIO::GRP" |
66 | |
93 | |
… | |
… | |
100 | PRI_MIN = -4, |
127 | PRI_MIN = -4, |
101 | PRI_MAX = 4, |
128 | PRI_MAX = 4, |
102 | |
129 | |
103 | DEFAULT_PRI = 0, |
130 | DEFAULT_PRI = 0, |
104 | PRI_BIAS = -PRI_MIN, |
131 | PRI_BIAS = -PRI_MIN, |
|
|
132 | NUM_PRI = PRI_MAX + PRI_BIAS + 1, |
105 | }; |
133 | }; |
106 | |
134 | |
107 | static int next_pri = DEFAULT_PRI + PRI_BIAS; |
135 | static int next_pri = DEFAULT_PRI + PRI_BIAS; |
108 | |
136 | |
109 | static int started, wanted; |
137 | static unsigned int started, wanted; |
110 | static volatile int nreqs; |
138 | static volatile unsigned int nreqs; |
111 | static int max_outstanding = 1<<30; |
139 | static volatile unsigned int max_outstanding = 0xffffffff; |
112 | static int respipe [2]; |
140 | static int respipe [2]; |
113 | |
141 | |
|
|
142 | #if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) |
|
|
143 | # define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP |
|
|
144 | #else |
|
|
145 | # define AIO_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER |
|
|
146 | #endif |
|
|
147 | |
|
|
148 | #define LOCK(mutex) pthread_mutex_lock (&(mutex)) |
|
|
149 | #define UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) |
|
|
150 | |
|
|
151 | /* worker threads management */ |
|
|
152 | static pthread_mutex_t wrklock = AIO_MUTEX_INIT; |
|
|
153 | |
|
|
154 | typedef struct worker { |
|
|
155 | /* locked by wrklock */ |
|
|
156 | struct worker *prev, *next; |
|
|
157 | |
|
|
158 | pthread_t tid; |
|
|
159 | |
|
|
160 | /* locked by reslock, reqlock or wrklock */ |
|
|
161 | aio_req req; /* currently processed request */ |
|
|
162 | void *dbuf; |
|
|
163 | DIR *dirp; |
|
|
164 | } worker; |
|
|
165 | |
|
|
166 | static worker wrk_first = { &wrk_first, &wrk_first, 0 }; |
|
|
167 | |
|
|
168 | static void worker_clear (worker *wrk) |
|
|
169 | { |
|
|
170 | if (wrk->dirp) |
|
|
171 | { |
|
|
172 | closedir (wrk->dirp); |
|
|
173 | wrk->dirp = 0; |
|
|
174 | } |
|
|
175 | |
|
|
176 | if (wrk->dbuf) |
|
|
177 | { |
|
|
178 | free (wrk->dbuf); |
|
|
179 | wrk->dbuf = 0; |
|
|
180 | } |
|
|
181 | } |
|
|
182 | |
|
|
183 | static void worker_free (worker *wrk) |
|
|
184 | { |
|
|
185 | wrk->next->prev = wrk->prev; |
|
|
186 | wrk->prev->next = wrk->next; |
|
|
187 | |
|
|
188 | free (wrk); |
|
|
189 | } |
|
|
190 | |
114 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
191 | static pthread_mutex_t reslock = AIO_MUTEX_INIT; |
115 | static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; |
192 | static pthread_mutex_t reqlock = AIO_MUTEX_INIT; |
116 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
193 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
117 | |
194 | |
118 | static volatile aio_req reqs, reqe; /* queue start, queue end */ |
195 | /* |
119 | static volatile aio_req ress, rese; /* queue start, queue end */ |
196 | * a somewhat faster data structure might be nice, but |
|
|
197 | * with 8 priorities this actually needs <20 insns |
|
|
198 | * per shift, the most expensive operation. |
|
|
199 | */ |
|
|
200 | typedef struct { |
|
|
201 | aio_req qs[NUM_PRI], qe[NUM_PRI]; /* qstart, qend */ |
|
|
202 | int size; |
|
|
203 | } reqq; |
120 | |
204 | |
|
|
205 | static reqq req_queue; |
|
|
206 | static reqq res_queue; |
|
|
207 | |
|
|
208 | int reqq_push (reqq *q, aio_req req) |
|
|
209 | { |
|
|
210 | int pri = req->pri; |
|
|
211 | req->next = 0; |
|
|
212 | |
|
|
213 | if (q->qe[pri]) |
|
|
214 | { |
|
|
215 | q->qe[pri]->next = req; |
|
|
216 | q->qe[pri] = req; |
|
|
217 | } |
|
|
218 | else |
|
|
219 | q->qe[pri] = q->qs[pri] = req; |
|
|
220 | |
|
|
221 | return q->size++; |
|
|
222 | } |
|
|
223 | |
|
|
224 | aio_req reqq_shift (reqq *q) |
|
|
225 | { |
|
|
226 | int pri; |
|
|
227 | |
|
|
228 | if (!q->size) |
|
|
229 | return 0; |
|
|
230 | |
|
|
231 | --q->size; |
|
|
232 | |
|
|
233 | for (pri = NUM_PRI; pri--; ) |
|
|
234 | { |
|
|
235 | aio_req req = q->qs[pri]; |
|
|
236 | |
|
|
237 | if (req) |
|
|
238 | { |
|
|
239 | if (!(q->qs[pri] = req->next)) |
|
|
240 | q->qe[pri] = 0; |
|
|
241 | |
|
|
242 | return req; |
|
|
243 | } |
|
|
244 | } |
|
|
245 | |
|
|
246 | abort (); |
|
|
247 | } |
|
|
248 | |
|
|
249 | static int poll_cb (int max); |
121 | static void req_invoke (aio_req req); |
250 | static void req_invoke (aio_req req); |
122 | static void req_free (aio_req req); |
251 | static void req_free (aio_req req); |
|
|
252 | static void req_cancel (aio_req req); |
123 | |
253 | |
124 | /* must be called at most once */ |
254 | /* must be called at most once */ |
125 | static SV *req_sv (aio_req req, const char *klass) |
255 | static SV *req_sv (aio_req req, const char *klass) |
126 | { |
256 | { |
127 | if (!req->self) |
257 | if (!req->self) |
… | |
… | |
158 | ENTER; |
288 | ENTER; |
159 | SAVETMPS; |
289 | SAVETMPS; |
160 | PUSHMARK (SP); |
290 | PUSHMARK (SP); |
161 | XPUSHs (req_sv (grp, AIO_GRP_KLASS)); |
291 | XPUSHs (req_sv (grp, AIO_GRP_KLASS)); |
162 | PUTBACK; |
292 | PUTBACK; |
163 | call_sv (grp->fh2, G_VOID | G_EVAL); |
293 | call_sv (grp->fh2, G_VOID | G_EVAL | G_KEEPERR); |
164 | SPAGAIN; |
294 | SPAGAIN; |
165 | FREETMPS; |
295 | FREETMPS; |
166 | LEAVE; |
296 | LEAVE; |
167 | } |
297 | } |
168 | |
298 | |
… | |
… | |
195 | { |
325 | { |
196 | fd_set rfd; |
326 | fd_set rfd; |
197 | |
327 | |
198 | while (nreqs) |
328 | while (nreqs) |
199 | { |
329 | { |
200 | aio_req req; |
330 | int size; |
201 | pthread_mutex_lock (&reslock); |
331 | #if !(__i386 || __x86_64) /* safe without sempahore on these archs */ |
202 | req = ress; |
332 | LOCK (reslock); |
203 | pthread_mutex_unlock (&reslock); |
333 | #endif |
|
|
334 | size = res_queue.size; |
|
|
335 | #if !(__i386 || __x86_64) /* safe without sempahore on these archs */ |
|
|
336 | UNLOCK (reslock); |
|
|
337 | #endif |
204 | |
338 | |
205 | if (req) |
339 | if (size) |
206 | return; |
340 | return; |
207 | |
341 | |
208 | FD_ZERO(&rfd); |
342 | FD_ZERO(&rfd); |
209 | FD_SET(respipe [0], &rfd); |
343 | FD_SET(respipe [0], &rfd); |
210 | |
344 | |
… | |
… | |
213 | } |
347 | } |
214 | |
348 | |
215 | static void req_invoke (aio_req req) |
349 | static void req_invoke (aio_req req) |
216 | { |
350 | { |
217 | dSP; |
351 | dSP; |
218 | int errorno = errno; |
|
|
219 | |
352 | |
220 | if (req->flags & FLAG_CANCELLED || !SvOK (req->callback)) |
353 | if (!(req->flags & FLAG_CANCELLED) && SvOK (req->callback)) |
221 | return; |
354 | { |
222 | |
|
|
223 | errno = req->errorno; |
355 | errno = req->errorno; |
224 | |
356 | |
225 | ENTER; |
357 | ENTER; |
226 | SAVETMPS; |
358 | SAVETMPS; |
227 | PUSHMARK (SP); |
359 | PUSHMARK (SP); |
228 | EXTEND (SP, 1); |
360 | EXTEND (SP, 1); |
229 | |
361 | |
230 | switch (req->type) |
362 | switch (req->type) |
231 | { |
|
|
232 | case REQ_READDIR: |
|
|
233 | { |
363 | { |
234 | SV *rv = &PL_sv_undef; |
364 | case REQ_READDIR: |
235 | |
|
|
236 | if (req->result >= 0) |
|
|
237 | { |
365 | { |
238 | char *buf = req->data2ptr; |
366 | SV *rv = &PL_sv_undef; |
239 | AV *av = newAV (); |
|
|
240 | |
367 | |
241 | while (req->result) |
368 | if (req->result >= 0) |
242 | { |
369 | { |
|
|
370 | int i; |
|
|
371 | char *buf = req->data2ptr; |
|
|
372 | AV *av = newAV (); |
|
|
373 | |
|
|
374 | av_extend (av, req->result - 1); |
|
|
375 | |
|
|
376 | for (i = 0; i < req->result; ++i) |
|
|
377 | { |
243 | SV *sv = newSVpv (buf, 0); |
378 | SV *sv = newSVpv (buf, 0); |
244 | |
379 | |
245 | av_push (av, sv); |
380 | av_store (av, i, sv); |
246 | buf += SvCUR (sv) + 1; |
381 | buf += SvCUR (sv) + 1; |
247 | req->result--; |
382 | } |
|
|
383 | |
|
|
384 | rv = sv_2mortal (newRV_noinc ((SV *)av)); |
248 | } |
385 | } |
249 | |
386 | |
250 | rv = sv_2mortal (newRV_noinc ((SV *)av)); |
387 | PUSHs (rv); |
251 | } |
388 | } |
|
|
389 | break; |
252 | |
390 | |
253 | PUSHs (rv); |
391 | case REQ_OPEN: |
|
|
392 | { |
|
|
393 | /* convert fd to fh */ |
|
|
394 | SV *fh; |
|
|
395 | |
|
|
396 | PUSHs (sv_2mortal (newSViv (req->result))); |
|
|
397 | PUTBACK; |
|
|
398 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
|
|
399 | SPAGAIN; |
|
|
400 | |
|
|
401 | fh = SvREFCNT_inc (POPs); |
|
|
402 | |
|
|
403 | PUSHMARK (SP); |
|
|
404 | XPUSHs (sv_2mortal (fh)); |
|
|
405 | } |
|
|
406 | break; |
|
|
407 | |
|
|
408 | case REQ_GROUP: |
|
|
409 | req->fd = 2; /* mark group as finished */ |
|
|
410 | |
|
|
411 | if (req->data) |
|
|
412 | { |
|
|
413 | int i; |
|
|
414 | AV *av = (AV *)req->data; |
|
|
415 | |
|
|
416 | EXTEND (SP, AvFILL (av) + 1); |
|
|
417 | for (i = 0; i <= AvFILL (av); ++i) |
|
|
418 | PUSHs (*av_fetch (av, i, 0)); |
|
|
419 | } |
|
|
420 | break; |
|
|
421 | |
|
|
422 | case REQ_NOP: |
|
|
423 | case REQ_BUSY: |
|
|
424 | break; |
|
|
425 | |
|
|
426 | default: |
|
|
427 | PUSHs (sv_2mortal (newSViv (req->result))); |
|
|
428 | break; |
254 | } |
429 | } |
255 | break; |
|
|
256 | |
430 | |
257 | case REQ_OPEN: |
|
|
258 | { |
|
|
259 | /* convert fd to fh */ |
|
|
260 | SV *fh; |
|
|
261 | |
431 | |
262 | PUSHs (sv_2mortal (newSViv (req->result))); |
|
|
263 | PUTBACK; |
432 | PUTBACK; |
264 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
|
|
265 | SPAGAIN; |
|
|
266 | |
|
|
267 | fh = SvREFCNT_inc (POPs); |
|
|
268 | |
|
|
269 | PUSHMARK (SP); |
|
|
270 | XPUSHs (sv_2mortal (fh)); |
|
|
271 | } |
|
|
272 | break; |
|
|
273 | |
|
|
274 | case REQ_GROUP: |
|
|
275 | req->fd = 2; /* mark group as finished */ |
|
|
276 | |
|
|
277 | if (req->data) |
|
|
278 | { |
|
|
279 | int i; |
|
|
280 | AV *av = (AV *)req->data; |
|
|
281 | |
|
|
282 | EXTEND (SP, AvFILL (av) + 1); |
|
|
283 | for (i = 0; i <= AvFILL (av); ++i) |
|
|
284 | PUSHs (*av_fetch (av, i, 0)); |
|
|
285 | } |
|
|
286 | break; |
|
|
287 | |
|
|
288 | case REQ_NOP: |
|
|
289 | case REQ_SLEEP: |
|
|
290 | break; |
|
|
291 | |
|
|
292 | default: |
|
|
293 | PUSHs (sv_2mortal (newSViv (req->result))); |
|
|
294 | break; |
|
|
295 | } |
|
|
296 | |
|
|
297 | |
|
|
298 | PUTBACK; |
|
|
299 | call_sv (req->callback, G_VOID | G_EVAL); |
433 | call_sv (req->callback, G_VOID | G_EVAL); |
300 | SPAGAIN; |
434 | SPAGAIN; |
301 | |
435 | |
302 | FREETMPS; |
436 | FREETMPS; |
303 | LEAVE; |
437 | LEAVE; |
304 | |
|
|
305 | errno = errorno; |
|
|
306 | |
|
|
307 | if (SvTRUE (ERRSV)) |
|
|
308 | { |
438 | } |
309 | req_free (req); |
|
|
310 | croak (0); |
|
|
311 | } |
|
|
312 | } |
|
|
313 | |
439 | |
314 | static void req_free (aio_req req) |
|
|
315 | { |
|
|
316 | if (req->grp) |
440 | if (req->grp) |
317 | { |
441 | { |
318 | aio_req grp = req->grp; |
442 | aio_req grp = req->grp; |
319 | |
443 | |
320 | /* unlink request */ |
444 | /* unlink request */ |
… | |
… | |
325 | grp->grp_first = req->grp_next; |
449 | grp->grp_first = req->grp_next; |
326 | |
450 | |
327 | aio_grp_dec (grp); |
451 | aio_grp_dec (grp); |
328 | } |
452 | } |
329 | |
453 | |
|
|
454 | if (SvTRUE (ERRSV)) |
|
|
455 | { |
|
|
456 | req_free (req); |
|
|
457 | croak (0); |
|
|
458 | } |
|
|
459 | } |
|
|
460 | |
|
|
461 | static void req_free (aio_req req) |
|
|
462 | { |
330 | if (req->self) |
463 | if (req->self) |
331 | { |
464 | { |
332 | sv_unmagic (req->self, PERL_MAGIC_ext); |
465 | sv_unmagic (req->self, PERL_MAGIC_ext); |
333 | SvREFCNT_dec (req->self); |
466 | SvREFCNT_dec (req->self); |
334 | } |
467 | } |
… | |
… | |
337 | SvREFCNT_dec (req->fh); |
470 | SvREFCNT_dec (req->fh); |
338 | SvREFCNT_dec (req->fh2); |
471 | SvREFCNT_dec (req->fh2); |
339 | SvREFCNT_dec (req->callback); |
472 | SvREFCNT_dec (req->callback); |
340 | Safefree (req->statdata); |
473 | Safefree (req->statdata); |
341 | |
474 | |
342 | if (req->type == REQ_READDIR && req->result >= 0) |
475 | if (req->type == REQ_READDIR) |
343 | free (req->data2ptr); |
476 | free (req->data2ptr); |
344 | |
477 | |
345 | Safefree (req); |
478 | Safefree (req); |
346 | } |
479 | } |
347 | |
480 | |
|
|
481 | static void req_cancel_subs (aio_req grp) |
|
|
482 | { |
|
|
483 | aio_req sub; |
|
|
484 | |
|
|
485 | if (grp->type != REQ_GROUP) |
|
|
486 | return; |
|
|
487 | |
|
|
488 | SvREFCNT_dec (grp->fh2); |
|
|
489 | grp->fh2 = 0; |
|
|
490 | |
|
|
491 | for (sub = grp->grp_first; sub; sub = sub->grp_next) |
|
|
492 | req_cancel (sub); |
|
|
493 | } |
|
|
494 | |
348 | static void req_cancel (aio_req req) |
495 | static void req_cancel (aio_req req) |
349 | { |
496 | { |
350 | req->flags |= FLAG_CANCELLED; |
497 | req->flags |= FLAG_CANCELLED; |
351 | |
498 | |
352 | if (req->type == REQ_GROUP) |
499 | req_cancel_subs (req); |
353 | { |
|
|
354 | aio_req sub; |
|
|
355 | |
|
|
356 | for (sub = req->grp_first; sub; sub = sub->grp_next) |
|
|
357 | req_cancel (sub); |
|
|
358 | } |
|
|
359 | } |
500 | } |
360 | |
501 | |
361 | static int poll_cb () |
502 | static int poll_cb (int max) |
362 | { |
503 | { |
363 | dSP; |
504 | dSP; |
364 | int count = 0; |
505 | int count = 0; |
365 | int do_croak = 0; |
506 | int do_croak = 0; |
366 | aio_req req; |
507 | aio_req req; |
367 | |
508 | |
368 | for (;;) |
509 | for (;;) |
369 | { |
510 | { |
370 | pthread_mutex_lock (&reslock); |
511 | while (max <= 0 || count < max) |
371 | req = ress; |
|
|
372 | |
|
|
373 | if (req) |
|
|
374 | { |
512 | { |
375 | ress = req->next; |
513 | LOCK (reslock); |
|
|
514 | req = reqq_shift (&res_queue); |
376 | |
515 | |
377 | if (!ress) |
516 | if (req) |
378 | { |
517 | { |
|
|
518 | if (!res_queue.size) |
|
|
519 | { |
379 | /* read any signals sent by the worker threads */ |
520 | /* read any signals sent by the worker threads */ |
380 | char buf [32]; |
521 | char buf [32]; |
381 | while (read (respipe [0], buf, 32) == 32) |
522 | while (read (respipe [0], buf, 32) == 32) |
|
|
523 | ; |
382 | ; |
524 | } |
383 | |
|
|
384 | rese = 0; |
|
|
385 | } |
525 | } |
|
|
526 | |
|
|
527 | UNLOCK (reslock); |
|
|
528 | |
|
|
529 | if (!req) |
|
|
530 | break; |
|
|
531 | |
|
|
532 | --nreqs; |
|
|
533 | |
|
|
534 | if (req->type == REQ_QUIT) |
|
|
535 | --started; |
|
|
536 | else if (req->type == REQ_GROUP && req->length) |
|
|
537 | { |
|
|
538 | req->fd = 1; /* mark request as delayed */ |
|
|
539 | continue; |
|
|
540 | } |
|
|
541 | else |
|
|
542 | { |
|
|
543 | if (req->type == REQ_READ) |
|
|
544 | SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); |
|
|
545 | |
|
|
546 | if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE)) |
|
|
547 | SvREADONLY_off (req->data); |
|
|
548 | |
|
|
549 | if (req->statdata) |
|
|
550 | { |
|
|
551 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
|
|
552 | PL_laststatval = req->result; |
|
|
553 | PL_statcache = *(req->statdata); |
|
|
554 | } |
|
|
555 | |
|
|
556 | req_invoke (req); |
|
|
557 | |
|
|
558 | count++; |
|
|
559 | } |
|
|
560 | |
|
|
561 | req_free (req); |
386 | } |
562 | } |
387 | |
563 | |
388 | pthread_mutex_unlock (&reslock); |
564 | if (nreqs <= max_outstanding) |
389 | |
|
|
390 | if (!req) |
|
|
391 | break; |
565 | break; |
392 | |
566 | |
393 | --nreqs; |
567 | poll_wait (); |
394 | |
568 | |
395 | if (req->type == REQ_QUIT) |
569 | max = 0; |
396 | started--; |
|
|
397 | else if (req->type == REQ_GROUP && req->length) |
|
|
398 | { |
|
|
399 | req->fd = 1; /* mark request as delayed */ |
|
|
400 | continue; |
|
|
401 | } |
|
|
402 | else |
|
|
403 | { |
|
|
404 | if (req->type == REQ_READ) |
|
|
405 | SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); |
|
|
406 | |
|
|
407 | if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE)) |
|
|
408 | SvREADONLY_off (req->data); |
|
|
409 | |
|
|
410 | if (req->statdata) |
|
|
411 | { |
|
|
412 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
|
|
413 | PL_laststatval = req->result; |
|
|
414 | PL_statcache = *(req->statdata); |
|
|
415 | } |
|
|
416 | |
|
|
417 | req_invoke (req); |
|
|
418 | |
|
|
419 | count++; |
|
|
420 | } |
|
|
421 | |
|
|
422 | req_free (req); |
|
|
423 | } |
570 | } |
424 | |
571 | |
425 | return count; |
572 | return count; |
426 | } |
573 | } |
427 | |
574 | |
428 | static void *aio_proc(void *arg); |
575 | static void *aio_proc(void *arg); |
429 | |
576 | |
430 | static void start_thread (void) |
577 | static void start_thread (void) |
431 | { |
578 | { |
432 | sigset_t fullsigset, oldsigset; |
579 | sigset_t fullsigset, oldsigset; |
433 | pthread_t tid; |
|
|
434 | pthread_attr_t attr; |
580 | pthread_attr_t attr; |
|
|
581 | |
|
|
582 | worker *wrk = calloc (1, sizeof (worker)); |
|
|
583 | |
|
|
584 | if (!wrk) |
|
|
585 | croak ("unable to allocate worker thread data"); |
435 | |
586 | |
436 | pthread_attr_init (&attr); |
587 | pthread_attr_init (&attr); |
437 | pthread_attr_setstacksize (&attr, STACKSIZE); |
588 | pthread_attr_setstacksize (&attr, STACKSIZE); |
438 | pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); |
589 | pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); |
439 | |
590 | |
440 | sigfillset (&fullsigset); |
591 | sigfillset (&fullsigset); |
|
|
592 | |
|
|
593 | LOCK (wrklock); |
441 | sigprocmask (SIG_SETMASK, &fullsigset, &oldsigset); |
594 | sigprocmask (SIG_SETMASK, &fullsigset, &oldsigset); |
442 | |
595 | |
443 | if (pthread_create (&tid, &attr, aio_proc, 0) == 0) |
596 | if (pthread_create (&wrk->tid, &attr, aio_proc, (void *)wrk) == 0) |
|
|
597 | { |
|
|
598 | wrk->prev = &wrk_first; |
|
|
599 | wrk->next = wrk_first.next; |
|
|
600 | wrk_first.next->prev = wrk; |
|
|
601 | wrk_first.next = wrk; |
444 | started++; |
602 | ++started; |
|
|
603 | } |
|
|
604 | else |
|
|
605 | free (wrk); |
445 | |
606 | |
446 | sigprocmask (SIG_SETMASK, &oldsigset, 0); |
607 | sigprocmask (SIG_SETMASK, &oldsigset, 0); |
|
|
608 | UNLOCK (wrklock); |
447 | } |
609 | } |
448 | |
610 | |
449 | static void req_send (aio_req req) |
611 | static void req_send (aio_req req) |
450 | { |
612 | { |
451 | while (started < wanted && nreqs >= started) |
613 | while (started < wanted && nreqs >= started) |
452 | start_thread (); |
614 | start_thread (); |
453 | |
615 | |
454 | ++nreqs; |
616 | ++nreqs; |
455 | |
617 | |
456 | pthread_mutex_lock (&reqlock); |
618 | LOCK (reqlock); |
457 | |
619 | reqq_push (&req_queue, req); |
458 | req->next = 0; |
|
|
459 | |
|
|
460 | if (reqe) |
|
|
461 | { |
|
|
462 | reqe->next = req; |
|
|
463 | reqe = req; |
|
|
464 | } |
|
|
465 | else |
|
|
466 | reqe = reqs = req; |
|
|
467 | |
|
|
468 | pthread_cond_signal (&reqwait); |
620 | pthread_cond_signal (&reqwait); |
469 | pthread_mutex_unlock (&reqlock); |
621 | UNLOCK (reqlock); |
470 | |
|
|
471 | if (nreqs > max_outstanding) |
|
|
472 | for (;;) |
|
|
473 | { |
|
|
474 | poll_cb (); |
|
|
475 | |
|
|
476 | if (nreqs <= max_outstanding) |
|
|
477 | break; |
|
|
478 | |
|
|
479 | poll_wait (); |
|
|
480 | } |
|
|
481 | } |
622 | } |
482 | |
623 | |
483 | static void end_thread (void) |
624 | static void end_thread (void) |
484 | { |
625 | { |
485 | aio_req req; |
626 | aio_req req; |
|
|
627 | |
486 | Newz (0, req, 1, aio_cb); |
628 | Newz (0, req, 1, aio_cb); |
|
|
629 | |
487 | req->type = REQ_QUIT; |
630 | req->type = REQ_QUIT; |
|
|
631 | req->pri = PRI_MAX + PRI_BIAS; |
488 | |
632 | |
489 | req_send (req); |
633 | req_send (req); |
490 | } |
634 | } |
491 | |
635 | |
492 | static void min_parallel (int nthreads) |
636 | static void min_parallel (int nthreads) |
… | |
… | |
509 | } |
653 | } |
510 | |
654 | |
511 | while (started > wanted) |
655 | while (started > wanted) |
512 | { |
656 | { |
513 | poll_wait (); |
657 | poll_wait (); |
514 | poll_cb (); |
658 | poll_cb (0); |
515 | } |
659 | } |
516 | } |
660 | } |
517 | |
661 | |
518 | static void create_pipe () |
662 | static void create_pipe () |
519 | { |
663 | { |
… | |
… | |
544 | static ssize_t pread (int fd, void *buf, size_t count, off_t offset) |
688 | static ssize_t pread (int fd, void *buf, size_t count, off_t offset) |
545 | { |
689 | { |
546 | ssize_t res; |
690 | ssize_t res; |
547 | off_t ooffset; |
691 | off_t ooffset; |
548 | |
692 | |
549 | pthread_mutex_lock (&preadwritelock); |
693 | LOCK (preadwritelock); |
550 | ooffset = lseek (fd, 0, SEEK_CUR); |
694 | ooffset = lseek (fd, 0, SEEK_CUR); |
551 | lseek (fd, offset, SEEK_SET); |
695 | lseek (fd, offset, SEEK_SET); |
552 | res = read (fd, buf, count); |
696 | res = read (fd, buf, count); |
553 | lseek (fd, ooffset, SEEK_SET); |
697 | lseek (fd, ooffset, SEEK_SET); |
554 | pthread_mutex_unlock (&preadwritelock); |
698 | UNLOCK (preadwritelock); |
555 | |
699 | |
556 | return res; |
700 | return res; |
557 | } |
701 | } |
558 | |
702 | |
559 | static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset) |
703 | static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset) |
560 | { |
704 | { |
561 | ssize_t res; |
705 | ssize_t res; |
562 | off_t ooffset; |
706 | off_t ooffset; |
563 | |
707 | |
564 | pthread_mutex_lock (&preadwritelock); |
708 | LOCK (preadwritelock); |
565 | ooffset = lseek (fd, 0, SEEK_CUR); |
709 | ooffset = lseek (fd, 0, SEEK_CUR); |
566 | lseek (fd, offset, SEEK_SET); |
710 | lseek (fd, offset, SEEK_SET); |
567 | res = write (fd, buf, count); |
711 | res = write (fd, buf, count); |
568 | lseek (fd, offset, SEEK_SET); |
712 | lseek (fd, offset, SEEK_SET); |
569 | pthread_mutex_unlock (&preadwritelock); |
713 | UNLOCK (preadwritelock); |
570 | |
714 | |
571 | return res; |
715 | return res; |
572 | } |
716 | } |
573 | #endif |
717 | #endif |
574 | |
718 | |
575 | #if !HAVE_FDATASYNC |
719 | #if !HAVE_FDATASYNC |
576 | # define fdatasync fsync |
720 | # define fdatasync fsync |
577 | #endif |
721 | #endif |
578 | |
722 | |
579 | #if !HAVE_READAHEAD |
723 | #if !HAVE_READAHEAD |
580 | # define readahead aio_readahead |
724 | # define readahead(fd,offset,count) aio_readahead (fd, offset, count, self) |
581 | |
725 | |
582 | static ssize_t readahead (int fd, off_t offset, size_t count) |
726 | static ssize_t aio_readahead (int fd, off_t offset, size_t count, worker *self) |
583 | { |
727 | { |
584 | char readahead_buf[4096]; |
728 | dBUF; |
585 | |
729 | |
586 | while (count > 0) |
730 | while (count > 0) |
587 | { |
731 | { |
588 | size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); |
732 | size_t len = count < AIO_BUFSIZE ? count : AIO_BUFSIZE; |
589 | |
733 | |
590 | pread (fd, readahead_buf, len, offset); |
734 | pread (fd, aio_buf, len, offset); |
591 | offset += len; |
735 | offset += len; |
592 | count -= len; |
736 | count -= len; |
593 | } |
737 | } |
594 | |
738 | |
595 | errno = 0; |
739 | errno = 0; |
596 | } |
740 | } |
|
|
741 | |
597 | #endif |
742 | #endif |
598 | |
743 | |
599 | #if !HAVE_READDIR_R |
744 | #if !HAVE_READDIR_R |
600 | # define readdir_r aio_readdir_r |
745 | # define readdir_r aio_readdir_r |
601 | |
746 | |
… | |
… | |
604 | static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) |
749 | static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) |
605 | { |
750 | { |
606 | struct dirent *e; |
751 | struct dirent *e; |
607 | int errorno; |
752 | int errorno; |
608 | |
753 | |
609 | pthread_mutex_lock (&readdirlock); |
754 | LOCK (readdirlock); |
610 | |
755 | |
611 | e = readdir (dirp); |
756 | e = readdir (dirp); |
612 | errorno = errno; |
757 | errorno = errno; |
613 | |
758 | |
614 | if (e) |
759 | if (e) |
… | |
… | |
617 | strcpy (ent->d_name, e->d_name); |
762 | strcpy (ent->d_name, e->d_name); |
618 | } |
763 | } |
619 | else |
764 | else |
620 | *res = 0; |
765 | *res = 0; |
621 | |
766 | |
622 | pthread_mutex_unlock (&readdirlock); |
767 | UNLOCK (readdirlock); |
623 | |
768 | |
624 | errno = errorno; |
769 | errno = errorno; |
625 | return e ? 0 : -1; |
770 | return e ? 0 : -1; |
626 | } |
771 | } |
627 | #endif |
772 | #endif |
628 | |
773 | |
629 | /* sendfile always needs emulation */ |
774 | /* sendfile always needs emulation */ |
630 | static ssize_t sendfile_ (int ofd, int ifd, off_t offset, size_t count) |
775 | static ssize_t sendfile_ (int ofd, int ifd, off_t offset, size_t count, worker *self) |
631 | { |
776 | { |
632 | ssize_t res; |
777 | ssize_t res; |
633 | |
778 | |
634 | if (!count) |
779 | if (!count) |
635 | return 0; |
780 | return 0; |
… | |
… | |
646 | { |
791 | { |
647 | off_t sbytes; |
792 | off_t sbytes; |
648 | res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); |
793 | res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); |
649 | |
794 | |
650 | if (res < 0 && sbytes) |
795 | if (res < 0 && sbytes) |
651 | /* maybe only on EAGAIN only: as usual, the manpage leaves you guessing */ |
796 | /* maybe only on EAGAIN: as usual, the manpage leaves you guessing */ |
652 | res = sbytes; |
797 | res = sbytes; |
653 | } |
798 | } |
654 | |
799 | |
655 | # elif __hpux |
800 | # elif __hpux |
656 | res = sendfile (ofd, ifd, offset, count, 0, 0); |
801 | res = sendfile (ofd, ifd, offset, count, 0, 0); |
… | |
… | |
684 | #endif |
829 | #endif |
685 | ) |
830 | ) |
686 | ) |
831 | ) |
687 | { |
832 | { |
688 | /* emulate sendfile. this is a major pain in the ass */ |
833 | /* emulate sendfile. this is a major pain in the ass */ |
689 | char buf[4096]; |
834 | dBUF; |
|
|
835 | |
690 | res = 0; |
836 | res = 0; |
691 | |
837 | |
692 | while (count) |
838 | while (count) |
693 | { |
839 | { |
694 | ssize_t cnt; |
840 | ssize_t cnt; |
695 | |
841 | |
696 | cnt = pread (ifd, buf, count > 4096 ? 4096 : count, offset); |
842 | cnt = pread (ifd, aio_buf, count > AIO_BUFSIZE ? AIO_BUFSIZE : count, offset); |
697 | |
843 | |
698 | if (cnt <= 0) |
844 | if (cnt <= 0) |
699 | { |
845 | { |
700 | if (cnt && !res) res = -1; |
846 | if (cnt && !res) res = -1; |
701 | break; |
847 | break; |
702 | } |
848 | } |
703 | |
849 | |
704 | cnt = write (ofd, buf, cnt); |
850 | cnt = write (ofd, aio_buf, cnt); |
705 | |
851 | |
706 | if (cnt <= 0) |
852 | if (cnt <= 0) |
707 | { |
853 | { |
708 | if (cnt && !res) res = -1; |
854 | if (cnt && !res) res = -1; |
709 | break; |
855 | break; |
… | |
… | |
717 | |
863 | |
718 | return res; |
864 | return res; |
719 | } |
865 | } |
720 | |
866 | |
721 | /* read a full directory */ |
867 | /* read a full directory */ |
722 | static int scandir_ (const char *path, void **namesp) |
868 | static void scandir_ (aio_req req, worker *self) |
723 | { |
869 | { |
724 | DIR *dirp = opendir (path); |
870 | DIR *dirp; |
725 | union |
871 | union |
726 | { |
872 | { |
727 | struct dirent d; |
873 | struct dirent d; |
728 | char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; |
874 | char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; |
729 | } u; |
875 | } *u; |
730 | struct dirent *entp; |
876 | struct dirent *entp; |
731 | char *name, *names; |
877 | char *name, *names; |
732 | int memlen = 4096; |
878 | int memlen = 4096; |
733 | int memofs = 0; |
879 | int memofs = 0; |
734 | int res = 0; |
880 | int res = 0; |
735 | int errorno; |
881 | int errorno; |
736 | |
882 | |
737 | if (!dirp) |
883 | LOCK (wrklock); |
738 | return -1; |
884 | self->dirp = dirp = opendir (req->dataptr); |
739 | |
885 | self->dbuf = u = malloc (sizeof (*u)); |
740 | names = malloc (memlen); |
886 | req->data2ptr = names = malloc (memlen); |
|
|
887 | UNLOCK (wrklock); |
741 | |
888 | |
|
|
889 | if (dirp && u && names) |
742 | for (;;) |
890 | for (;;) |
743 | { |
891 | { |
|
|
892 | errno = 0; |
744 | errno = 0, readdir_r (dirp, &u.d, &entp); |
893 | readdir_r (dirp, &u->d, &entp); |
745 | |
894 | |
746 | if (!entp) |
895 | if (!entp) |
747 | break; |
896 | break; |
748 | |
897 | |
749 | name = entp->d_name; |
898 | name = entp->d_name; |
750 | |
899 | |
751 | if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) |
900 | if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) |
752 | { |
901 | { |
753 | int len = strlen (name) + 1; |
902 | int len = strlen (name) + 1; |
754 | |
903 | |
755 | res++; |
904 | res++; |
756 | |
905 | |
757 | while (memofs + len > memlen) |
906 | while (memofs + len > memlen) |
758 | { |
907 | { |
759 | memlen *= 2; |
908 | memlen *= 2; |
|
|
909 | LOCK (wrklock); |
760 | names = realloc (names, memlen); |
910 | req->data2ptr = names = realloc (names, memlen); |
|
|
911 | UNLOCK (wrklock); |
|
|
912 | |
761 | if (!names) |
913 | if (!names) |
762 | break; |
914 | break; |
763 | } |
915 | } |
764 | |
916 | |
765 | memcpy (names + memofs, name, len); |
917 | memcpy (names + memofs, name, len); |
766 | memofs += len; |
918 | memofs += len; |
767 | } |
919 | } |
768 | } |
920 | } |
769 | |
921 | |
770 | errorno = errno; |
|
|
771 | closedir (dirp); |
|
|
772 | |
|
|
773 | if (errorno) |
922 | if (errno) |
774 | { |
|
|
775 | free (names); |
|
|
776 | errno = errorno; |
|
|
777 | res = -1; |
923 | res = -1; |
778 | } |
924 | |
779 | |
925 | req->result = res; |
780 | *namesp = (void *)names; |
|
|
781 | return res; |
|
|
782 | } |
926 | } |
783 | |
927 | |
784 | /*****************************************************************************/ |
928 | /*****************************************************************************/ |
785 | |
929 | |
786 | static void *aio_proc (void *thr_arg) |
930 | static void *aio_proc (void *thr_arg) |
787 | { |
931 | { |
788 | aio_req req; |
932 | aio_req req; |
789 | int type; |
933 | int type; |
|
|
934 | worker *self = (worker *)thr_arg; |
790 | |
935 | |
791 | do |
936 | do |
792 | { |
937 | { |
793 | pthread_mutex_lock (&reqlock); |
938 | LOCK (reqlock); |
794 | |
939 | |
795 | for (;;) |
940 | for (;;) |
796 | { |
941 | { |
797 | req = reqs; |
942 | self->req = req = reqq_shift (&req_queue); |
798 | |
|
|
799 | if (reqs) |
|
|
800 | { |
|
|
801 | reqs = reqs->next; |
|
|
802 | if (!reqs) reqe = 0; |
|
|
803 | } |
|
|
804 | |
943 | |
805 | if (req) |
944 | if (req) |
806 | break; |
945 | break; |
807 | |
946 | |
808 | pthread_cond_wait (&reqwait, &reqlock); |
947 | pthread_cond_wait (&reqwait, &reqlock); |
809 | } |
948 | } |
810 | |
949 | |
811 | pthread_mutex_unlock (&reqlock); |
950 | UNLOCK (reqlock); |
812 | |
951 | |
813 | errno = 0; /* strictly unnecessary */ |
952 | errno = 0; /* strictly unnecessary */ |
814 | type = req->type; /* remember type for QUIT check */ |
953 | type = req->type; /* remember type for QUIT check */ |
815 | |
954 | |
816 | if (!(req->flags & FLAG_CANCELLED)) |
955 | if (!(req->flags & FLAG_CANCELLED)) |
… | |
… | |
818 | { |
957 | { |
819 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
958 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
820 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
959 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
821 | |
960 | |
822 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
961 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
823 | case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length); break; |
962 | case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length, self); break; |
824 | |
963 | |
825 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
964 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
826 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
965 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
827 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
966 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
828 | |
967 | |
… | |
… | |
834 | case REQ_LINK: req->result = link (req->data2ptr, req->dataptr); break; |
973 | case REQ_LINK: req->result = link (req->data2ptr, req->dataptr); break; |
835 | case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; |
974 | case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; |
836 | |
975 | |
837 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
976 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
838 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
977 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
839 | case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break; |
978 | case REQ_READDIR: scandir_ (req, self); break; |
840 | |
979 | |
841 | case REQ_SLEEP: |
980 | case REQ_BUSY: |
842 | { |
981 | { |
843 | struct timeval tv; |
982 | struct timeval tv; |
844 | |
983 | |
845 | tv.tv_sec = req->fd; |
984 | tv.tv_sec = req->fd; |
846 | tv.tv_usec = req->fd2; |
985 | tv.tv_usec = req->fd2; |
… | |
… | |
858 | break; |
997 | break; |
859 | } |
998 | } |
860 | |
999 | |
861 | req->errorno = errno; |
1000 | req->errorno = errno; |
862 | |
1001 | |
863 | pthread_mutex_lock (&reslock); |
1002 | LOCK (reslock); |
864 | |
1003 | |
865 | req->next = 0; |
1004 | if (!reqq_push (&res_queue, req)) |
866 | |
|
|
867 | if (rese) |
|
|
868 | { |
|
|
869 | rese->next = req; |
|
|
870 | rese = req; |
|
|
871 | } |
|
|
872 | else |
|
|
873 | { |
|
|
874 | rese = ress = req; |
|
|
875 | |
|
|
876 | /* write a dummy byte to the pipe so fh becomes ready */ |
1005 | /* write a dummy byte to the pipe so fh becomes ready */ |
877 | write (respipe [1], &respipe, 1); |
1006 | write (respipe [1], &respipe, 1); |
878 | } |
|
|
879 | |
1007 | |
880 | pthread_mutex_unlock (&reslock); |
1008 | self->req = 0; |
|
|
1009 | worker_clear (self); |
|
|
1010 | |
|
|
1011 | UNLOCK (reslock); |
881 | } |
1012 | } |
882 | while (type != REQ_QUIT); |
1013 | while (type != REQ_QUIT); |
883 | |
1014 | |
|
|
1015 | LOCK (wrklock); |
|
|
1016 | worker_free (self); |
|
|
1017 | UNLOCK (wrklock); |
|
|
1018 | |
884 | return 0; |
1019 | return 0; |
885 | } |
1020 | } |
886 | |
1021 | |
887 | /*****************************************************************************/ |
1022 | /*****************************************************************************/ |
888 | |
1023 | |
889 | static void atfork_prepare (void) |
1024 | static void atfork_prepare (void) |
890 | { |
1025 | { |
891 | pthread_mutex_lock (&reqlock); |
1026 | LOCK (wrklock); |
892 | pthread_mutex_lock (&reslock); |
1027 | LOCK (reqlock); |
|
|
1028 | LOCK (reslock); |
893 | #if !HAVE_PREADWRITE |
1029 | #if !HAVE_PREADWRITE |
894 | pthread_mutex_lock (&preadwritelock); |
1030 | LOCK (preadwritelock); |
895 | #endif |
1031 | #endif |
896 | #if !HAVE_READDIR_R |
1032 | #if !HAVE_READDIR_R |
897 | pthread_mutex_lock (&readdirlock); |
1033 | LOCK (readdirlock); |
898 | #endif |
1034 | #endif |
899 | } |
1035 | } |
900 | |
1036 | |
901 | static void atfork_parent (void) |
1037 | static void atfork_parent (void) |
902 | { |
1038 | { |
903 | #if !HAVE_READDIR_R |
1039 | #if !HAVE_READDIR_R |
904 | pthread_mutex_unlock (&readdirlock); |
1040 | UNLOCK (readdirlock); |
905 | #endif |
1041 | #endif |
906 | #if !HAVE_PREADWRITE |
1042 | #if !HAVE_PREADWRITE |
907 | pthread_mutex_unlock (&preadwritelock); |
1043 | UNLOCK (preadwritelock); |
908 | #endif |
1044 | #endif |
909 | pthread_mutex_unlock (&reslock); |
1045 | UNLOCK (reslock); |
910 | pthread_mutex_unlock (&reqlock); |
1046 | UNLOCK (reqlock); |
|
|
1047 | UNLOCK (wrklock); |
911 | } |
1048 | } |
912 | |
1049 | |
913 | static void atfork_child (void) |
1050 | static void atfork_child (void) |
914 | { |
1051 | { |
915 | aio_req prv; |
1052 | aio_req prv; |
916 | |
1053 | |
|
|
1054 | while (prv = reqq_shift (&req_queue)) |
|
|
1055 | req_free (prv); |
|
|
1056 | |
|
|
1057 | while (prv = reqq_shift (&res_queue)) |
|
|
1058 | req_free (prv); |
|
|
1059 | |
|
|
1060 | while (wrk_first.next != &wrk_first) |
|
|
1061 | { |
|
|
1062 | worker *wrk = wrk_first.next; |
|
|
1063 | |
|
|
1064 | if (wrk->req) |
|
|
1065 | req_free (wrk->req); |
|
|
1066 | |
|
|
1067 | worker_clear (wrk); |
|
|
1068 | worker_free (wrk); |
|
|
1069 | } |
|
|
1070 | |
917 | started = 0; |
1071 | started = 0; |
918 | |
1072 | nreqs = 0; |
919 | while (reqs) |
|
|
920 | { |
|
|
921 | prv = reqs; |
|
|
922 | reqs = prv->next; |
|
|
923 | req_free (prv); |
|
|
924 | } |
|
|
925 | |
|
|
926 | reqs = reqe = 0; |
|
|
927 | |
|
|
928 | while (ress) |
|
|
929 | { |
|
|
930 | prv = ress; |
|
|
931 | ress = prv->next; |
|
|
932 | req_free (prv); |
|
|
933 | } |
|
|
934 | |
|
|
935 | ress = rese = 0; |
|
|
936 | |
1073 | |
937 | close (respipe [0]); |
1074 | close (respipe [0]); |
938 | close (respipe [1]); |
1075 | close (respipe [1]); |
939 | create_pipe (); |
1076 | create_pipe (); |
940 | |
1077 | |
… | |
… | |
976 | create_pipe (); |
1113 | create_pipe (); |
977 | pthread_atfork (atfork_prepare, atfork_parent, atfork_child); |
1114 | pthread_atfork (atfork_prepare, atfork_parent, atfork_child); |
978 | } |
1115 | } |
979 | |
1116 | |
980 | void |
1117 | void |
981 | min_parallel (nthreads) |
1118 | min_parallel (int nthreads) |
982 | int nthreads |
|
|
983 | PROTOTYPE: $ |
1119 | PROTOTYPE: $ |
984 | |
1120 | |
985 | void |
1121 | void |
986 | max_parallel (nthreads) |
1122 | max_parallel (int nthreads) |
987 | int nthreads |
|
|
988 | PROTOTYPE: $ |
1123 | PROTOTYPE: $ |
989 | |
1124 | |
990 | int |
1125 | int |
991 | max_outstanding (nreqs) |
1126 | max_outstanding (int maxreqs) |
992 | int nreqs |
1127 | PROTOTYPE: $ |
993 | PROTOTYPE: $ |
|
|
994 | CODE: |
1128 | CODE: |
995 | RETVAL = max_outstanding; |
1129 | RETVAL = max_outstanding; |
996 | max_outstanding = nreqs; |
1130 | max_outstanding = maxreqs; |
|
|
1131 | OUTPUT: |
|
|
1132 | RETVAL |
997 | |
1133 | |
998 | void |
1134 | void |
999 | aio_open (pathname,flags,mode,callback=&PL_sv_undef) |
1135 | aio_open (pathname,flags,mode,callback=&PL_sv_undef) |
1000 | SV * pathname |
1136 | SV * pathname |
1001 | int flags |
1137 | int flags |
… | |
… | |
1216 | |
1352 | |
1217 | REQ_SEND; |
1353 | REQ_SEND; |
1218 | } |
1354 | } |
1219 | |
1355 | |
1220 | void |
1356 | void |
1221 | aio_sleep (delay,callback=&PL_sv_undef) |
1357 | aio_busy (delay,callback=&PL_sv_undef) |
1222 | double delay |
1358 | double delay |
1223 | SV * callback |
1359 | SV * callback |
1224 | PPCODE: |
1360 | PPCODE: |
1225 | { |
1361 | { |
1226 | dREQ; |
1362 | dREQ; |
1227 | |
1363 | |
1228 | req->type = REQ_SLEEP; |
1364 | req->type = REQ_BUSY; |
1229 | req->fd = delay < 0. ? 0 : delay; |
1365 | req->fd = delay < 0. ? 0 : delay; |
1230 | req->fd2 = delay < 0. ? 0 : 1000. * (delay - req->fd); |
1366 | req->fd2 = delay < 0. ? 0 : 1000. * (delay - req->fd); |
1231 | |
1367 | |
1232 | REQ_SEND; |
1368 | REQ_SEND; |
1233 | } |
1369 | } |
… | |
… | |
1256 | req->type = REQ_NOP; |
1392 | req->type = REQ_NOP; |
1257 | |
1393 | |
1258 | REQ_SEND; |
1394 | REQ_SEND; |
1259 | } |
1395 | } |
1260 | |
1396 | |
1261 | #if 0 |
|
|
1262 | |
|
|
1263 | void |
1397 | void |
1264 | aio_pri (int pri = DEFAULT_PRI) |
1398 | aioreq_pri (int pri = DEFAULT_PRI) |
1265 | CODE: |
1399 | CODE: |
1266 | if (pri < PRI_MIN) pri = PRI_MIN; |
1400 | if (pri < PRI_MIN) pri = PRI_MIN; |
1267 | if (pri > PRI_MAX) pri = PRI_MAX; |
1401 | if (pri > PRI_MAX) pri = PRI_MAX; |
1268 | next_pri = pri + PRI_BIAS; |
1402 | next_pri = pri + PRI_BIAS; |
1269 | |
1403 | |
1270 | #endif |
1404 | void |
|
|
1405 | aioreq_nice (int nice = 0) |
|
|
1406 | CODE: |
|
|
1407 | nice = next_pri - nice; |
|
|
1408 | if (nice < PRI_MIN) nice = PRI_MIN; |
|
|
1409 | if (nice > PRI_MAX) nice = PRI_MAX; |
|
|
1410 | next_pri = nice + PRI_BIAS; |
1271 | |
1411 | |
1272 | void |
1412 | void |
1273 | flush () |
1413 | flush () |
1274 | PROTOTYPE: |
1414 | PROTOTYPE: |
1275 | CODE: |
1415 | CODE: |
1276 | while (nreqs) |
1416 | while (nreqs) |
1277 | { |
1417 | { |
1278 | poll_wait (); |
1418 | poll_wait (); |
1279 | poll_cb (); |
1419 | poll_cb (0); |
1280 | } |
1420 | } |
1281 | |
1421 | |
1282 | void |
1422 | void |
1283 | poll() |
1423 | poll() |
1284 | PROTOTYPE: |
1424 | PROTOTYPE: |
1285 | CODE: |
1425 | CODE: |
1286 | if (nreqs) |
1426 | if (nreqs) |
1287 | { |
1427 | { |
1288 | poll_wait (); |
1428 | poll_wait (); |
1289 | poll_cb (); |
1429 | poll_cb (0); |
1290 | } |
1430 | } |
1291 | |
1431 | |
1292 | int |
1432 | int |
1293 | poll_fileno() |
1433 | poll_fileno() |
1294 | PROTOTYPE: |
1434 | PROTOTYPE: |
… | |
… | |
1299 | |
1439 | |
1300 | int |
1440 | int |
1301 | poll_cb(...) |
1441 | poll_cb(...) |
1302 | PROTOTYPE: |
1442 | PROTOTYPE: |
1303 | CODE: |
1443 | CODE: |
1304 | RETVAL = poll_cb (); |
1444 | RETVAL = poll_cb (0); |
|
|
1445 | OUTPUT: |
|
|
1446 | RETVAL |
|
|
1447 | |
|
|
1448 | int |
|
|
1449 | poll_some(int max = 0) |
|
|
1450 | PROTOTYPE: $ |
|
|
1451 | CODE: |
|
|
1452 | RETVAL = poll_cb (max); |
1305 | OUTPUT: |
1453 | OUTPUT: |
1306 | RETVAL |
1454 | RETVAL |
1307 | |
1455 | |
1308 | void |
1456 | void |
1309 | poll_wait() |
1457 | poll_wait() |
… | |
… | |
1324 | |
1472 | |
1325 | MODULE = IO::AIO PACKAGE = IO::AIO::REQ |
1473 | MODULE = IO::AIO PACKAGE = IO::AIO::REQ |
1326 | |
1474 | |
1327 | void |
1475 | void |
1328 | cancel (aio_req_ornot req) |
1476 | cancel (aio_req_ornot req) |
1329 | PROTOTYPE: |
|
|
1330 | CODE: |
1477 | CODE: |
1331 | req_cancel (req); |
1478 | req_cancel (req); |
1332 | |
1479 | |
1333 | void |
1480 | void |
1334 | cb (aio_req_ornot req, SV *callback=&PL_sv_undef) |
1481 | cb (aio_req_ornot req, SV *callback=&PL_sv_undef) |
… | |
… | |
1370 | } |
1517 | } |
1371 | } |
1518 | } |
1372 | } |
1519 | } |
1373 | |
1520 | |
1374 | void |
1521 | void |
|
|
1522 | cancel_subs (aio_req_ornot req) |
|
|
1523 | CODE: |
|
|
1524 | req_cancel_subs (req); |
|
|
1525 | |
|
|
1526 | void |
1375 | result (aio_req grp, ...) |
1527 | result (aio_req grp, ...) |
1376 | CODE: |
1528 | CODE: |
1377 | { |
1529 | { |
1378 | int i; |
1530 | int i; |
1379 | AV *av = newAV (); |
1531 | AV *av = newAV (); |
… | |
… | |
1384 | SvREFCNT_dec (grp->data); |
1536 | SvREFCNT_dec (grp->data); |
1385 | grp->data = (SV *)av; |
1537 | grp->data = (SV *)av; |
1386 | } |
1538 | } |
1387 | |
1539 | |
1388 | void |
1540 | void |
1389 | feed_limit (aio_req grp, int limit) |
1541 | limit (aio_req grp, int limit) |
1390 | CODE: |
1542 | CODE: |
1391 | grp->fd2 = limit; |
1543 | grp->fd2 = limit; |
1392 | aio_grp_feed (grp); |
1544 | aio_grp_feed (grp); |
1393 | |
1545 | |
1394 | void |
1546 | void |