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