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