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