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