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