|
|
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 | |
… | |
… | |
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 int started, wanted; |
110 | static volatile int nreqs; |
138 | static volatile int nreqs; |
111 | static int max_outstanding = 1<<30; |
|
|
112 | static int respipe [2]; |
139 | static int respipe [2]; |
113 | |
140 | |
|
|
141 | #if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) |
|
|
142 | # define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP |
|
|
143 | #else |
|
|
144 | # define AIO_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER |
|
|
145 | #endif |
|
|
146 | |
|
|
147 | #define LOCK(mutex) pthread_mutex_lock (&(mutex)) |
|
|
148 | #define UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) |
|
|
149 | |
|
|
150 | /* worker threads management */ |
|
|
151 | static pthread_mutex_t wrklock = AIO_MUTEX_INIT; |
|
|
152 | |
|
|
153 | typedef struct worker { |
|
|
154 | /* locked by wrklock */ |
|
|
155 | struct worker *prev, *next; |
|
|
156 | |
|
|
157 | pthread_t tid; |
|
|
158 | |
|
|
159 | /* locked by reslock, reqlock or wrklock */ |
|
|
160 | aio_req req; /* currently processed request */ |
|
|
161 | void *dbuf; |
|
|
162 | DIR *dirp; |
|
|
163 | } worker; |
|
|
164 | |
|
|
165 | static worker wrk_first = { &wrk_first, &wrk_first, 0 }; |
|
|
166 | |
|
|
167 | static void worker_clear (worker *wrk) |
|
|
168 | { |
|
|
169 | if (wrk->dirp) |
|
|
170 | { |
|
|
171 | closedir (wrk->dirp); |
|
|
172 | wrk->dirp = 0; |
|
|
173 | } |
|
|
174 | |
|
|
175 | if (wrk->dbuf) |
|
|
176 | { |
|
|
177 | free (wrk->dbuf); |
|
|
178 | wrk->dbuf = 0; |
|
|
179 | } |
|
|
180 | } |
|
|
181 | |
|
|
182 | static void worker_free (worker *wrk) |
|
|
183 | { |
|
|
184 | wrk->next->prev = wrk->prev; |
|
|
185 | wrk->prev->next = wrk->next; |
|
|
186 | |
|
|
187 | free (wrk); |
|
|
188 | } |
|
|
189 | |
114 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
190 | static pthread_mutex_t reslock = AIO_MUTEX_INIT; |
115 | static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; |
191 | static pthread_mutex_t reqlock = AIO_MUTEX_INIT; |
116 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
192 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
117 | |
193 | |
118 | static volatile aio_req reqs, reqe; /* queue start, queue end */ |
194 | /* |
119 | static volatile aio_req ress, rese; /* queue start, queue end */ |
195 | * a somewhat faster data structure might be nice, but |
|
|
196 | * with 8 priorities this actually needs <20 insns |
|
|
197 | * per shift, the most expensive operation. |
|
|
198 | */ |
|
|
199 | typedef struct { |
|
|
200 | aio_req qs[NUM_PRI], qe[NUM_PRI]; /* qstart, qend */ |
|
|
201 | int size; |
|
|
202 | } reqq; |
120 | |
203 | |
|
|
204 | static reqq req_queue; |
|
|
205 | static reqq res_queue; |
|
|
206 | |
|
|
207 | int reqq_push (reqq *q, aio_req req) |
|
|
208 | { |
|
|
209 | int pri = req->pri; |
|
|
210 | req->next = 0; |
|
|
211 | |
|
|
212 | if (q->qe[pri]) |
|
|
213 | { |
|
|
214 | q->qe[pri]->next = req; |
|
|
215 | q->qe[pri] = req; |
|
|
216 | } |
|
|
217 | else |
|
|
218 | q->qe[pri] = q->qs[pri] = req; |
|
|
219 | |
|
|
220 | return q->size++; |
|
|
221 | } |
|
|
222 | |
|
|
223 | aio_req reqq_shift (reqq *q) |
|
|
224 | { |
|
|
225 | int pri; |
|
|
226 | |
|
|
227 | if (!q->size) |
|
|
228 | return 0; |
|
|
229 | |
|
|
230 | --q->size; |
|
|
231 | |
|
|
232 | for (pri = NUM_PRI; pri--; ) |
|
|
233 | { |
|
|
234 | aio_req req = q->qs[pri]; |
|
|
235 | |
|
|
236 | if (req) |
|
|
237 | { |
|
|
238 | if (!(q->qs[pri] = req->next)) |
|
|
239 | q->qe[pri] = 0; |
|
|
240 | |
|
|
241 | return req; |
|
|
242 | } |
|
|
243 | } |
|
|
244 | |
|
|
245 | abort (); |
|
|
246 | } |
|
|
247 | |
|
|
248 | static int poll_cb (int max); |
121 | static void req_invoke (aio_req req); |
249 | static void req_invoke (aio_req req); |
122 | static void req_free (aio_req req); |
250 | static void req_free (aio_req req); |
|
|
251 | static void req_cancel (aio_req req); |
123 | |
252 | |
124 | /* must be called at most once */ |
253 | /* must be called at most once */ |
125 | static SV *req_sv (aio_req req, const char *klass) |
254 | static SV *req_sv (aio_req req, const char *klass) |
126 | { |
255 | { |
127 | if (!req->self) |
256 | if (!req->self) |
… | |
… | |
158 | ENTER; |
287 | ENTER; |
159 | SAVETMPS; |
288 | SAVETMPS; |
160 | PUSHMARK (SP); |
289 | PUSHMARK (SP); |
161 | XPUSHs (req_sv (grp, AIO_GRP_KLASS)); |
290 | XPUSHs (req_sv (grp, AIO_GRP_KLASS)); |
162 | PUTBACK; |
291 | PUTBACK; |
163 | call_sv (grp->fh2, G_VOID | G_EVAL); |
292 | call_sv (grp->fh2, G_VOID | G_EVAL | G_KEEPERR); |
164 | SPAGAIN; |
293 | SPAGAIN; |
165 | FREETMPS; |
294 | FREETMPS; |
166 | LEAVE; |
295 | LEAVE; |
167 | } |
296 | } |
168 | |
297 | |
… | |
… | |
191 | } |
320 | } |
192 | } |
321 | } |
193 | |
322 | |
194 | static void poll_wait () |
323 | static void poll_wait () |
195 | { |
324 | { |
|
|
325 | fd_set rfd; |
|
|
326 | |
196 | while (nreqs) |
327 | while (nreqs) |
197 | { |
328 | { |
198 | aio_req req; |
329 | int size; |
199 | pthread_mutex_lock (&reslock); |
330 | #if !(__i386 || __x86_64) /* safe without sempahore on these archs */ |
200 | req = ress; |
331 | LOCK (reslock); |
201 | pthread_mutex_unlock (&reslock); |
332 | #endif |
|
|
333 | size = res_queue.size; |
|
|
334 | #if !(__i386 || __x86_64) /* safe without sempahore on these archs */ |
|
|
335 | UNLOCK (reslock); |
|
|
336 | #endif |
202 | |
337 | |
203 | if (req) |
338 | if (size) |
204 | return; |
339 | return; |
205 | |
340 | |
206 | fd_set rfd; |
|
|
207 | FD_ZERO(&rfd); |
341 | FD_ZERO(&rfd); |
208 | FD_SET(respipe [0], &rfd); |
342 | FD_SET(respipe [0], &rfd); |
209 | |
343 | |
210 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
344 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
211 | } |
345 | } |
212 | } |
346 | } |
213 | |
347 | |
214 | static void req_invoke (aio_req req) |
348 | static void req_invoke (aio_req req) |
215 | { |
349 | { |
216 | dSP; |
350 | dSP; |
217 | int errorno = errno; |
|
|
218 | |
351 | |
219 | if (req->flags & FLAG_CANCELLED || !SvOK (req->callback)) |
352 | if (!(req->flags & FLAG_CANCELLED) && SvOK (req->callback)) |
220 | return; |
353 | { |
221 | |
|
|
222 | errno = req->errorno; |
354 | errno = req->errorno; |
223 | |
355 | |
224 | ENTER; |
356 | ENTER; |
225 | SAVETMPS; |
357 | SAVETMPS; |
226 | PUSHMARK (SP); |
358 | PUSHMARK (SP); |
227 | EXTEND (SP, 1); |
359 | EXTEND (SP, 1); |
228 | |
360 | |
229 | switch (req->type) |
361 | switch (req->type) |
230 | { |
|
|
231 | case REQ_READDIR: |
|
|
232 | { |
362 | { |
233 | SV *rv = &PL_sv_undef; |
363 | case REQ_READDIR: |
234 | |
|
|
235 | if (req->result >= 0) |
|
|
236 | { |
364 | { |
237 | char *buf = req->data2ptr; |
365 | SV *rv = &PL_sv_undef; |
238 | AV *av = newAV (); |
|
|
239 | |
366 | |
240 | while (req->result) |
367 | if (req->result >= 0) |
241 | { |
368 | { |
|
|
369 | int i; |
|
|
370 | char *buf = req->data2ptr; |
|
|
371 | AV *av = newAV (); |
|
|
372 | |
|
|
373 | av_extend (av, req->result - 1); |
|
|
374 | |
|
|
375 | for (i = 0; i < req->result; ++i) |
|
|
376 | { |
242 | SV *sv = newSVpv (buf, 0); |
377 | SV *sv = newSVpv (buf, 0); |
243 | |
378 | |
244 | av_push (av, sv); |
379 | av_store (av, i, sv); |
245 | buf += SvCUR (sv) + 1; |
380 | buf += SvCUR (sv) + 1; |
246 | req->result--; |
381 | } |
|
|
382 | |
|
|
383 | rv = sv_2mortal (newRV_noinc ((SV *)av)); |
247 | } |
384 | } |
248 | |
385 | |
249 | rv = sv_2mortal (newRV_noinc ((SV *)av)); |
386 | PUSHs (rv); |
250 | } |
387 | } |
|
|
388 | break; |
251 | |
389 | |
252 | PUSHs (rv); |
390 | case REQ_OPEN: |
|
|
391 | { |
|
|
392 | /* convert fd to fh */ |
|
|
393 | SV *fh; |
|
|
394 | |
|
|
395 | PUSHs (sv_2mortal (newSViv (req->result))); |
|
|
396 | PUTBACK; |
|
|
397 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
|
|
398 | SPAGAIN; |
|
|
399 | |
|
|
400 | fh = SvREFCNT_inc (POPs); |
|
|
401 | |
|
|
402 | PUSHMARK (SP); |
|
|
403 | XPUSHs (sv_2mortal (fh)); |
|
|
404 | } |
|
|
405 | break; |
|
|
406 | |
|
|
407 | case REQ_GROUP: |
|
|
408 | req->fd = 2; /* mark group as finished */ |
|
|
409 | |
|
|
410 | if (req->data) |
|
|
411 | { |
|
|
412 | int i; |
|
|
413 | AV *av = (AV *)req->data; |
|
|
414 | |
|
|
415 | EXTEND (SP, AvFILL (av) + 1); |
|
|
416 | for (i = 0; i <= AvFILL (av); ++i) |
|
|
417 | PUSHs (*av_fetch (av, i, 0)); |
|
|
418 | } |
|
|
419 | break; |
|
|
420 | |
|
|
421 | case REQ_NOP: |
|
|
422 | case REQ_BUSY: |
|
|
423 | break; |
|
|
424 | |
|
|
425 | default: |
|
|
426 | PUSHs (sv_2mortal (newSViv (req->result))); |
|
|
427 | break; |
253 | } |
428 | } |
254 | break; |
|
|
255 | |
429 | |
256 | case REQ_OPEN: |
|
|
257 | { |
|
|
258 | /* convert fd to fh */ |
|
|
259 | SV *fh; |
|
|
260 | |
430 | |
261 | PUSHs (sv_2mortal (newSViv (req->result))); |
|
|
262 | PUTBACK; |
431 | 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); |
432 | call_sv (req->callback, G_VOID | G_EVAL); |
299 | SPAGAIN; |
433 | SPAGAIN; |
300 | |
434 | |
301 | FREETMPS; |
435 | FREETMPS; |
302 | LEAVE; |
436 | LEAVE; |
303 | |
|
|
304 | errno = errorno; |
|
|
305 | |
|
|
306 | if (SvTRUE (ERRSV)) |
|
|
307 | { |
437 | } |
308 | req_free (req); |
|
|
309 | croak (0); |
|
|
310 | } |
|
|
311 | } |
|
|
312 | |
438 | |
313 | static void req_free (aio_req req) |
|
|
314 | { |
|
|
315 | if (req->grp) |
439 | if (req->grp) |
316 | { |
440 | { |
317 | aio_req grp = req->grp; |
441 | aio_req grp = req->grp; |
318 | |
442 | |
319 | /* unlink request */ |
443 | /* unlink request */ |
… | |
… | |
324 | grp->grp_first = req->grp_next; |
448 | grp->grp_first = req->grp_next; |
325 | |
449 | |
326 | aio_grp_dec (grp); |
450 | aio_grp_dec (grp); |
327 | } |
451 | } |
328 | |
452 | |
|
|
453 | if (SvTRUE (ERRSV)) |
|
|
454 | { |
|
|
455 | req_free (req); |
|
|
456 | croak (0); |
|
|
457 | } |
|
|
458 | } |
|
|
459 | |
|
|
460 | static void req_free (aio_req req) |
|
|
461 | { |
329 | if (req->self) |
462 | if (req->self) |
330 | { |
463 | { |
331 | sv_unmagic (req->self, PERL_MAGIC_ext); |
464 | sv_unmagic (req->self, PERL_MAGIC_ext); |
332 | SvREFCNT_dec (req->self); |
465 | SvREFCNT_dec (req->self); |
333 | } |
466 | } |
… | |
… | |
336 | SvREFCNT_dec (req->fh); |
469 | SvREFCNT_dec (req->fh); |
337 | SvREFCNT_dec (req->fh2); |
470 | SvREFCNT_dec (req->fh2); |
338 | SvREFCNT_dec (req->callback); |
471 | SvREFCNT_dec (req->callback); |
339 | Safefree (req->statdata); |
472 | Safefree (req->statdata); |
340 | |
473 | |
341 | if (req->type == REQ_READDIR && req->result >= 0) |
474 | if (req->type == REQ_READDIR) |
342 | free (req->data2ptr); |
475 | free (req->data2ptr); |
343 | |
476 | |
344 | Safefree (req); |
477 | Safefree (req); |
345 | } |
478 | } |
346 | |
479 | |
|
|
480 | static void req_cancel_subs (aio_req grp) |
|
|
481 | { |
|
|
482 | aio_req sub; |
|
|
483 | |
|
|
484 | if (grp->type != REQ_GROUP) |
|
|
485 | return; |
|
|
486 | |
|
|
487 | SvREFCNT_dec (grp->fh2); |
|
|
488 | grp->fh2 = 0; |
|
|
489 | |
|
|
490 | for (sub = grp->grp_first; sub; sub = sub->grp_next) |
|
|
491 | req_cancel (sub); |
|
|
492 | } |
|
|
493 | |
347 | static void req_cancel (aio_req req) |
494 | static void req_cancel (aio_req req) |
348 | { |
495 | { |
349 | req->flags |= FLAG_CANCELLED; |
496 | req->flags |= FLAG_CANCELLED; |
350 | |
497 | |
351 | if (req->type == REQ_GROUP) |
498 | 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 | } |
499 | } |
359 | |
500 | |
360 | static int poll_cb () |
501 | static int poll_cb (int max) |
361 | { |
502 | { |
362 | dSP; |
503 | dSP; |
363 | int count = 0; |
504 | int count = 0; |
364 | int do_croak = 0; |
505 | int do_croak = 0; |
365 | aio_req req; |
506 | aio_req req; |
366 | |
507 | |
367 | for (;;) |
508 | while (max <= 0 || count < max) |
368 | { |
509 | { |
369 | pthread_mutex_lock (&reslock); |
510 | LOCK (reslock); |
370 | req = ress; |
511 | req = reqq_shift (&res_queue); |
371 | |
512 | |
372 | if (req) |
513 | if (req) |
373 | { |
514 | { |
374 | ress = req->next; |
|
|
375 | |
|
|
376 | if (!ress) |
515 | if (!res_queue.size) |
377 | { |
516 | { |
378 | /* read any signals sent by the worker threads */ |
517 | /* read any signals sent by the worker threads */ |
379 | char buf [32]; |
518 | char buf [32]; |
380 | while (read (respipe [0], buf, 32) == 32) |
519 | while (read (respipe [0], buf, 32) == 32) |
381 | ; |
520 | ; |
382 | |
|
|
383 | rese = 0; |
|
|
384 | } |
521 | } |
385 | } |
522 | } |
386 | |
523 | |
387 | pthread_mutex_unlock (&reslock); |
524 | UNLOCK (reslock); |
388 | |
525 | |
389 | if (!req) |
526 | if (!req) |
390 | break; |
527 | break; |
391 | |
528 | |
392 | --nreqs; |
529 | --nreqs; |
… | |
… | |
427 | static void *aio_proc(void *arg); |
564 | static void *aio_proc(void *arg); |
428 | |
565 | |
429 | static void start_thread (void) |
566 | static void start_thread (void) |
430 | { |
567 | { |
431 | sigset_t fullsigset, oldsigset; |
568 | sigset_t fullsigset, oldsigset; |
432 | pthread_t tid; |
|
|
433 | pthread_attr_t attr; |
569 | pthread_attr_t attr; |
|
|
570 | |
|
|
571 | worker *wrk = calloc (1, sizeof (worker)); |
|
|
572 | |
|
|
573 | if (!wrk) |
|
|
574 | croak ("unable to allocate worker thread data"); |
434 | |
575 | |
435 | pthread_attr_init (&attr); |
576 | pthread_attr_init (&attr); |
436 | pthread_attr_setstacksize (&attr, STACKSIZE); |
577 | pthread_attr_setstacksize (&attr, STACKSIZE); |
437 | pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); |
578 | pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); |
438 | |
579 | |
439 | sigfillset (&fullsigset); |
580 | sigfillset (&fullsigset); |
|
|
581 | |
|
|
582 | LOCK (wrklock); |
440 | sigprocmask (SIG_SETMASK, &fullsigset, &oldsigset); |
583 | sigprocmask (SIG_SETMASK, &fullsigset, &oldsigset); |
441 | |
584 | |
442 | if (pthread_create (&tid, &attr, aio_proc, 0) == 0) |
585 | if (pthread_create (&wrk->tid, &attr, aio_proc, (void *)wrk) == 0) |
|
|
586 | { |
|
|
587 | wrk->prev = &wrk_first; |
|
|
588 | wrk->next = wrk_first.next; |
|
|
589 | wrk_first.next->prev = wrk; |
|
|
590 | wrk_first.next = wrk; |
443 | started++; |
591 | started++; |
|
|
592 | } |
|
|
593 | else |
|
|
594 | free (wrk); |
444 | |
595 | |
445 | sigprocmask (SIG_SETMASK, &oldsigset, 0); |
596 | sigprocmask (SIG_SETMASK, &oldsigset, 0); |
|
|
597 | UNLOCK (wrklock); |
446 | } |
598 | } |
447 | |
599 | |
448 | static void req_send (aio_req req) |
600 | static void req_send (aio_req req) |
449 | { |
601 | { |
450 | while (started < wanted && nreqs >= started) |
602 | while (started < wanted && nreqs >= started) |
451 | start_thread (); |
603 | start_thread (); |
452 | |
604 | |
453 | ++nreqs; |
605 | ++nreqs; |
454 | |
606 | |
455 | pthread_mutex_lock (&reqlock); |
607 | LOCK (reqlock); |
456 | |
608 | 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); |
609 | pthread_cond_signal (&reqwait); |
468 | pthread_mutex_unlock (&reqlock); |
610 | 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 | } |
611 | } |
481 | |
612 | |
482 | static void end_thread (void) |
613 | static void end_thread (void) |
483 | { |
614 | { |
484 | aio_req req; |
615 | aio_req req; |
|
|
616 | |
485 | Newz (0, req, 1, aio_cb); |
617 | Newz (0, req, 1, aio_cb); |
|
|
618 | |
486 | req->type = REQ_QUIT; |
619 | req->type = REQ_QUIT; |
|
|
620 | req->pri = PRI_MAX + PRI_BIAS; |
487 | |
621 | |
488 | req_send (req); |
622 | req_send (req); |
489 | } |
623 | } |
490 | |
624 | |
491 | static void min_parallel (int nthreads) |
625 | static void min_parallel (int nthreads) |
… | |
… | |
508 | } |
642 | } |
509 | |
643 | |
510 | while (started > wanted) |
644 | while (started > wanted) |
511 | { |
645 | { |
512 | poll_wait (); |
646 | poll_wait (); |
513 | poll_cb (); |
647 | poll_cb (0); |
514 | } |
648 | } |
515 | } |
649 | } |
516 | |
650 | |
517 | static void create_pipe () |
651 | static void create_pipe () |
518 | { |
652 | { |
… | |
… | |
543 | static ssize_t pread (int fd, void *buf, size_t count, off_t offset) |
677 | static ssize_t pread (int fd, void *buf, size_t count, off_t offset) |
544 | { |
678 | { |
545 | ssize_t res; |
679 | ssize_t res; |
546 | off_t ooffset; |
680 | off_t ooffset; |
547 | |
681 | |
548 | pthread_mutex_lock (&preadwritelock); |
682 | LOCK (preadwritelock); |
549 | ooffset = lseek (fd, 0, SEEK_CUR); |
683 | ooffset = lseek (fd, 0, SEEK_CUR); |
550 | lseek (fd, offset, SEEK_SET); |
684 | lseek (fd, offset, SEEK_SET); |
551 | res = read (fd, buf, count); |
685 | res = read (fd, buf, count); |
552 | lseek (fd, ooffset, SEEK_SET); |
686 | lseek (fd, ooffset, SEEK_SET); |
553 | pthread_mutex_unlock (&preadwritelock); |
687 | UNLOCK (preadwritelock); |
554 | |
688 | |
555 | return res; |
689 | return res; |
556 | } |
690 | } |
557 | |
691 | |
558 | static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset) |
692 | static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset) |
559 | { |
693 | { |
560 | ssize_t res; |
694 | ssize_t res; |
561 | off_t ooffset; |
695 | off_t ooffset; |
562 | |
696 | |
563 | pthread_mutex_lock (&preadwritelock); |
697 | LOCK (preadwritelock); |
564 | ooffset = lseek (fd, 0, SEEK_CUR); |
698 | ooffset = lseek (fd, 0, SEEK_CUR); |
565 | lseek (fd, offset, SEEK_SET); |
699 | lseek (fd, offset, SEEK_SET); |
566 | res = write (fd, buf, count); |
700 | res = write (fd, buf, count); |
567 | lseek (fd, offset, SEEK_SET); |
701 | lseek (fd, offset, SEEK_SET); |
568 | pthread_mutex_unlock (&preadwritelock); |
702 | UNLOCK (preadwritelock); |
569 | |
703 | |
570 | return res; |
704 | return res; |
571 | } |
705 | } |
572 | #endif |
706 | #endif |
573 | |
707 | |
574 | #if !HAVE_FDATASYNC |
708 | #if !HAVE_FDATASYNC |
575 | # define fdatasync fsync |
709 | # define fdatasync fsync |
576 | #endif |
710 | #endif |
577 | |
711 | |
578 | #if !HAVE_READAHEAD |
712 | #if !HAVE_READAHEAD |
579 | # define readahead aio_readahead |
713 | # define readahead(fd,offset,count) aio_readahead (fd, offset, count, self) |
580 | |
714 | |
581 | static ssize_t readahead (int fd, off_t offset, size_t count) |
715 | static ssize_t aio_readahead (int fd, off_t offset, size_t count, worker *self) |
582 | { |
716 | { |
583 | char readahead_buf[4096]; |
717 | dBUF; |
584 | |
718 | |
585 | while (count > 0) |
719 | while (count > 0) |
586 | { |
720 | { |
587 | size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); |
721 | size_t len = count < AIO_BUFSIZE ? count : AIO_BUFSIZE; |
588 | |
722 | |
589 | pread (fd, readahead_buf, len, offset); |
723 | pread (fd, aio_buf, len, offset); |
590 | offset += len; |
724 | offset += len; |
591 | count -= len; |
725 | count -= len; |
592 | } |
726 | } |
593 | |
727 | |
594 | errno = 0; |
728 | errno = 0; |
595 | } |
729 | } |
|
|
730 | |
596 | #endif |
731 | #endif |
597 | |
732 | |
598 | #if !HAVE_READDIR_R |
733 | #if !HAVE_READDIR_R |
599 | # define readdir_r aio_readdir_r |
734 | # define readdir_r aio_readdir_r |
600 | |
735 | |
… | |
… | |
603 | static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) |
738 | static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) |
604 | { |
739 | { |
605 | struct dirent *e; |
740 | struct dirent *e; |
606 | int errorno; |
741 | int errorno; |
607 | |
742 | |
608 | pthread_mutex_lock (&readdirlock); |
743 | LOCK (readdirlock); |
609 | |
744 | |
610 | e = readdir (dirp); |
745 | e = readdir (dirp); |
611 | errorno = errno; |
746 | errorno = errno; |
612 | |
747 | |
613 | if (e) |
748 | if (e) |
… | |
… | |
616 | strcpy (ent->d_name, e->d_name); |
751 | strcpy (ent->d_name, e->d_name); |
617 | } |
752 | } |
618 | else |
753 | else |
619 | *res = 0; |
754 | *res = 0; |
620 | |
755 | |
621 | pthread_mutex_unlock (&readdirlock); |
756 | UNLOCK (readdirlock); |
622 | |
757 | |
623 | errno = errorno; |
758 | errno = errorno; |
624 | return e ? 0 : -1; |
759 | return e ? 0 : -1; |
625 | } |
760 | } |
626 | #endif |
761 | #endif |
627 | |
762 | |
628 | /* sendfile always needs emulation */ |
763 | /* sendfile always needs emulation */ |
629 | static ssize_t sendfile_ (int ofd, int ifd, off_t offset, size_t count) |
764 | static ssize_t sendfile_ (int ofd, int ifd, off_t offset, size_t count, worker *self) |
630 | { |
765 | { |
631 | ssize_t res; |
766 | ssize_t res; |
632 | |
767 | |
633 | if (!count) |
768 | if (!count) |
634 | return 0; |
769 | return 0; |
… | |
… | |
645 | { |
780 | { |
646 | off_t sbytes; |
781 | off_t sbytes; |
647 | res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); |
782 | res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); |
648 | |
783 | |
649 | if (res < 0 && sbytes) |
784 | if (res < 0 && sbytes) |
650 | /* maybe only on EAGAIN only: as usual, the manpage leaves you guessing */ |
785 | /* maybe only on EAGAIN: as usual, the manpage leaves you guessing */ |
651 | res = sbytes; |
786 | res = sbytes; |
652 | } |
787 | } |
653 | |
788 | |
654 | # elif __hpux |
789 | # elif __hpux |
655 | res = sendfile (ofd, ifd, offset, count, 0, 0); |
790 | res = sendfile (ofd, ifd, offset, count, 0, 0); |
… | |
… | |
683 | #endif |
818 | #endif |
684 | ) |
819 | ) |
685 | ) |
820 | ) |
686 | { |
821 | { |
687 | /* emulate sendfile. this is a major pain in the ass */ |
822 | /* emulate sendfile. this is a major pain in the ass */ |
688 | char buf[4096]; |
823 | dBUF; |
|
|
824 | |
689 | res = 0; |
825 | res = 0; |
690 | |
826 | |
691 | while (count) |
827 | while (count) |
692 | { |
828 | { |
693 | ssize_t cnt; |
829 | ssize_t cnt; |
694 | |
830 | |
695 | cnt = pread (ifd, buf, count > 4096 ? 4096 : count, offset); |
831 | cnt = pread (ifd, aio_buf, count > AIO_BUFSIZE ? AIO_BUFSIZE : count, offset); |
696 | |
832 | |
697 | if (cnt <= 0) |
833 | if (cnt <= 0) |
698 | { |
834 | { |
699 | if (cnt && !res) res = -1; |
835 | if (cnt && !res) res = -1; |
700 | break; |
836 | break; |
701 | } |
837 | } |
702 | |
838 | |
703 | cnt = write (ofd, buf, cnt); |
839 | cnt = write (ofd, aio_buf, cnt); |
704 | |
840 | |
705 | if (cnt <= 0) |
841 | if (cnt <= 0) |
706 | { |
842 | { |
707 | if (cnt && !res) res = -1; |
843 | if (cnt && !res) res = -1; |
708 | break; |
844 | break; |
… | |
… | |
716 | |
852 | |
717 | return res; |
853 | return res; |
718 | } |
854 | } |
719 | |
855 | |
720 | /* read a full directory */ |
856 | /* read a full directory */ |
721 | static int scandir_ (const char *path, void **namesp) |
857 | static void scandir_ (aio_req req, worker *self) |
722 | { |
858 | { |
723 | DIR *dirp = opendir (path); |
859 | DIR *dirp; |
724 | union |
860 | union |
725 | { |
861 | { |
726 | struct dirent d; |
862 | struct dirent d; |
727 | char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; |
863 | char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; |
728 | } u; |
864 | } *u; |
729 | struct dirent *entp; |
865 | struct dirent *entp; |
730 | char *name, *names; |
866 | char *name, *names; |
731 | int memlen = 4096; |
867 | int memlen = 4096; |
732 | int memofs = 0; |
868 | int memofs = 0; |
733 | int res = 0; |
869 | int res = 0; |
734 | int errorno; |
870 | int errorno; |
735 | |
871 | |
736 | if (!dirp) |
872 | LOCK (wrklock); |
737 | return -1; |
873 | self->dirp = dirp = opendir (req->dataptr); |
738 | |
874 | self->dbuf = u = malloc (sizeof (*u)); |
739 | names = malloc (memlen); |
875 | req->data2ptr = names = malloc (memlen); |
|
|
876 | UNLOCK (wrklock); |
740 | |
877 | |
|
|
878 | if (dirp && u && names) |
741 | for (;;) |
879 | for (;;) |
742 | { |
880 | { |
|
|
881 | errno = 0; |
743 | errno = 0, readdir_r (dirp, &u.d, &entp); |
882 | readdir_r (dirp, &u->d, &entp); |
744 | |
883 | |
745 | if (!entp) |
884 | if (!entp) |
746 | break; |
885 | break; |
747 | |
886 | |
748 | name = entp->d_name; |
887 | name = entp->d_name; |
749 | |
888 | |
750 | if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) |
889 | if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) |
751 | { |
890 | { |
752 | int len = strlen (name) + 1; |
891 | int len = strlen (name) + 1; |
753 | |
892 | |
754 | res++; |
893 | res++; |
755 | |
894 | |
756 | while (memofs + len > memlen) |
895 | while (memofs + len > memlen) |
757 | { |
896 | { |
758 | memlen *= 2; |
897 | memlen *= 2; |
|
|
898 | LOCK (wrklock); |
759 | names = realloc (names, memlen); |
899 | req->data2ptr = names = realloc (names, memlen); |
|
|
900 | UNLOCK (wrklock); |
|
|
901 | |
760 | if (!names) |
902 | if (!names) |
761 | break; |
903 | break; |
762 | } |
904 | } |
763 | |
905 | |
764 | memcpy (names + memofs, name, len); |
906 | memcpy (names + memofs, name, len); |
765 | memofs += len; |
907 | memofs += len; |
766 | } |
908 | } |
767 | } |
909 | } |
768 | |
910 | |
769 | errorno = errno; |
|
|
770 | closedir (dirp); |
|
|
771 | |
|
|
772 | if (errorno) |
911 | if (errno) |
773 | { |
|
|
774 | free (names); |
|
|
775 | errno = errorno; |
|
|
776 | res = -1; |
912 | res = -1; |
777 | } |
913 | |
778 | |
914 | req->result = res; |
779 | *namesp = (void *)names; |
|
|
780 | return res; |
|
|
781 | } |
915 | } |
782 | |
916 | |
783 | /*****************************************************************************/ |
917 | /*****************************************************************************/ |
784 | |
918 | |
785 | static void *aio_proc (void *thr_arg) |
919 | static void *aio_proc (void *thr_arg) |
786 | { |
920 | { |
787 | aio_req req; |
921 | aio_req req; |
788 | int type; |
922 | int type; |
|
|
923 | worker *self = (worker *)thr_arg; |
789 | |
924 | |
790 | do |
925 | do |
791 | { |
926 | { |
792 | pthread_mutex_lock (&reqlock); |
927 | LOCK (reqlock); |
793 | |
928 | |
794 | for (;;) |
929 | for (;;) |
795 | { |
930 | { |
796 | req = reqs; |
931 | self->req = req = reqq_shift (&req_queue); |
797 | |
|
|
798 | if (reqs) |
|
|
799 | { |
|
|
800 | reqs = reqs->next; |
|
|
801 | if (!reqs) reqe = 0; |
|
|
802 | } |
|
|
803 | |
932 | |
804 | if (req) |
933 | if (req) |
805 | break; |
934 | break; |
806 | |
935 | |
807 | pthread_cond_wait (&reqwait, &reqlock); |
936 | pthread_cond_wait (&reqwait, &reqlock); |
808 | } |
937 | } |
809 | |
938 | |
810 | pthread_mutex_unlock (&reqlock); |
939 | UNLOCK (reqlock); |
811 | |
940 | |
812 | errno = 0; /* strictly unnecessary */ |
941 | errno = 0; /* strictly unnecessary */ |
813 | type = req->type; /* remember type for QUIT check */ |
942 | type = req->type; /* remember type for QUIT check */ |
814 | |
943 | |
815 | if (!(req->flags & FLAG_CANCELLED)) |
944 | if (!(req->flags & FLAG_CANCELLED)) |
… | |
… | |
817 | { |
946 | { |
818 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
947 | 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; |
948 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
820 | |
949 | |
821 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
950 | 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; |
951 | case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length, self); break; |
823 | |
952 | |
824 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
953 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
825 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
954 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
826 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
955 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
827 | |
956 | |
… | |
… | |
833 | case REQ_LINK: req->result = link (req->data2ptr, req->dataptr); break; |
962 | case REQ_LINK: req->result = link (req->data2ptr, req->dataptr); break; |
834 | case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; |
963 | case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; |
835 | |
964 | |
836 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
965 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
837 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
966 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
838 | case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break; |
967 | case REQ_READDIR: scandir_ (req, self); break; |
839 | |
968 | |
840 | case REQ_SLEEP: |
969 | case REQ_BUSY: |
841 | { |
970 | { |
842 | struct timeval tv; |
971 | struct timeval tv; |
843 | |
972 | |
844 | tv.tv_sec = req->fd; |
973 | tv.tv_sec = req->fd; |
845 | tv.tv_usec = req->fd2; |
974 | tv.tv_usec = req->fd2; |
… | |
… | |
857 | break; |
986 | break; |
858 | } |
987 | } |
859 | |
988 | |
860 | req->errorno = errno; |
989 | req->errorno = errno; |
861 | |
990 | |
862 | pthread_mutex_lock (&reslock); |
991 | LOCK (reslock); |
863 | |
992 | |
864 | req->next = 0; |
993 | 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 */ |
994 | /* write a dummy byte to the pipe so fh becomes ready */ |
876 | write (respipe [1], &respipe, 1); |
995 | write (respipe [1], &respipe, 1); |
877 | } |
|
|
878 | |
996 | |
879 | pthread_mutex_unlock (&reslock); |
997 | self->req = 0; |
|
|
998 | worker_clear (self); |
|
|
999 | |
|
|
1000 | UNLOCK (reslock); |
880 | } |
1001 | } |
881 | while (type != REQ_QUIT); |
1002 | while (type != REQ_QUIT); |
882 | |
1003 | |
|
|
1004 | LOCK (wrklock); |
|
|
1005 | worker_free (self); |
|
|
1006 | UNLOCK (wrklock); |
|
|
1007 | |
883 | return 0; |
1008 | return 0; |
884 | } |
1009 | } |
885 | |
1010 | |
886 | /*****************************************************************************/ |
1011 | /*****************************************************************************/ |
887 | |
1012 | |
888 | static void atfork_prepare (void) |
1013 | static void atfork_prepare (void) |
889 | { |
1014 | { |
890 | pthread_mutex_lock (&reqlock); |
1015 | LOCK (wrklock); |
891 | pthread_mutex_lock (&reslock); |
1016 | LOCK (reqlock); |
|
|
1017 | LOCK (reslock); |
892 | #if !HAVE_PREADWRITE |
1018 | #if !HAVE_PREADWRITE |
893 | pthread_mutex_lock (&preadwritelock); |
1019 | LOCK (preadwritelock); |
894 | #endif |
1020 | #endif |
895 | #if !HAVE_READDIR_R |
1021 | #if !HAVE_READDIR_R |
896 | pthread_mutex_lock (&readdirlock); |
1022 | LOCK (readdirlock); |
897 | #endif |
1023 | #endif |
898 | } |
1024 | } |
899 | |
1025 | |
900 | static void atfork_parent (void) |
1026 | static void atfork_parent (void) |
901 | { |
1027 | { |
902 | #if !HAVE_READDIR_R |
1028 | #if !HAVE_READDIR_R |
903 | pthread_mutex_unlock (&readdirlock); |
1029 | UNLOCK (readdirlock); |
904 | #endif |
1030 | #endif |
905 | #if !HAVE_PREADWRITE |
1031 | #if !HAVE_PREADWRITE |
906 | pthread_mutex_unlock (&preadwritelock); |
1032 | UNLOCK (preadwritelock); |
907 | #endif |
1033 | #endif |
908 | pthread_mutex_unlock (&reslock); |
1034 | UNLOCK (reslock); |
909 | pthread_mutex_unlock (&reqlock); |
1035 | UNLOCK (reqlock); |
|
|
1036 | UNLOCK (wrklock); |
910 | } |
1037 | } |
911 | |
1038 | |
912 | static void atfork_child (void) |
1039 | static void atfork_child (void) |
913 | { |
1040 | { |
914 | aio_req prv; |
1041 | aio_req prv; |
915 | |
1042 | |
|
|
1043 | while (prv = reqq_shift (&req_queue)) |
|
|
1044 | req_free (prv); |
|
|
1045 | |
|
|
1046 | while (prv = reqq_shift (&res_queue)) |
|
|
1047 | req_free (prv); |
|
|
1048 | |
|
|
1049 | while (wrk_first.next != &wrk_first) |
|
|
1050 | { |
|
|
1051 | worker *wrk = wrk_first.next; |
|
|
1052 | |
|
|
1053 | if (wrk->req) |
|
|
1054 | req_free (wrk->req); |
|
|
1055 | |
|
|
1056 | worker_clear (wrk); |
|
|
1057 | worker_free (wrk); |
|
|
1058 | } |
|
|
1059 | |
916 | started = 0; |
1060 | started = 0; |
917 | |
1061 | 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 | |
1062 | |
936 | close (respipe [0]); |
1063 | close (respipe [0]); |
937 | close (respipe [1]); |
1064 | close (respipe [1]); |
938 | create_pipe (); |
1065 | create_pipe (); |
939 | |
1066 | |
… | |
… | |
984 | void |
1111 | void |
985 | max_parallel (nthreads) |
1112 | max_parallel (nthreads) |
986 | int nthreads |
1113 | int nthreads |
987 | PROTOTYPE: $ |
1114 | PROTOTYPE: $ |
988 | |
1115 | |
989 | int |
|
|
990 | max_outstanding (nreqs) |
|
|
991 | int nreqs |
|
|
992 | PROTOTYPE: $ |
|
|
993 | CODE: |
|
|
994 | RETVAL = max_outstanding; |
|
|
995 | max_outstanding = nreqs; |
|
|
996 | |
|
|
997 | void |
1116 | void |
998 | aio_open (pathname,flags,mode,callback=&PL_sv_undef) |
1117 | aio_open (pathname,flags,mode,callback=&PL_sv_undef) |
999 | SV * pathname |
1118 | SV * pathname |
1000 | int flags |
1119 | int flags |
1001 | int mode |
1120 | int mode |
… | |
… | |
1215 | |
1334 | |
1216 | REQ_SEND; |
1335 | REQ_SEND; |
1217 | } |
1336 | } |
1218 | |
1337 | |
1219 | void |
1338 | void |
1220 | aio_sleep (delay,callback=&PL_sv_undef) |
1339 | aio_busy (delay,callback=&PL_sv_undef) |
1221 | double delay |
1340 | double delay |
1222 | SV * callback |
1341 | SV * callback |
1223 | PPCODE: |
1342 | PPCODE: |
1224 | { |
1343 | { |
1225 | dREQ; |
1344 | dREQ; |
1226 | |
1345 | |
1227 | req->type = REQ_SLEEP; |
1346 | req->type = REQ_BUSY; |
1228 | req->fd = delay < 0. ? 0 : delay; |
1347 | req->fd = delay < 0. ? 0 : delay; |
1229 | req->fd2 = delay < 0. ? 0 : 1000. * (delay - req->fd); |
1348 | req->fd2 = delay < 0. ? 0 : 1000. * (delay - req->fd); |
1230 | |
1349 | |
1231 | REQ_SEND; |
1350 | REQ_SEND; |
1232 | } |
1351 | } |
… | |
… | |
1255 | req->type = REQ_NOP; |
1374 | req->type = REQ_NOP; |
1256 | |
1375 | |
1257 | REQ_SEND; |
1376 | REQ_SEND; |
1258 | } |
1377 | } |
1259 | |
1378 | |
1260 | #if 0 |
|
|
1261 | |
|
|
1262 | void |
1379 | void |
1263 | aio_pri (int pri = DEFAULT_PRI) |
1380 | aioreq_pri (int pri = DEFAULT_PRI) |
1264 | CODE: |
1381 | CODE: |
1265 | if (pri < PRI_MIN) pri = PRI_MIN; |
1382 | if (pri < PRI_MIN) pri = PRI_MIN; |
1266 | if (pri > PRI_MAX) pri = PRI_MAX; |
1383 | if (pri > PRI_MAX) pri = PRI_MAX; |
1267 | next_pri = pri + PRI_BIAS; |
1384 | next_pri = pri + PRI_BIAS; |
1268 | |
1385 | |
1269 | #endif |
1386 | void |
|
|
1387 | aioreq_nice (int nice = 0) |
|
|
1388 | CODE: |
|
|
1389 | nice = next_pri - nice; |
|
|
1390 | if (nice < PRI_MIN) nice = PRI_MIN; |
|
|
1391 | if (nice > PRI_MAX) nice = PRI_MAX; |
|
|
1392 | next_pri = nice + PRI_BIAS; |
1270 | |
1393 | |
1271 | void |
1394 | void |
1272 | flush () |
1395 | flush () |
1273 | PROTOTYPE: |
1396 | PROTOTYPE: |
1274 | CODE: |
1397 | CODE: |
1275 | while (nreqs) |
1398 | while (nreqs) |
1276 | { |
1399 | { |
1277 | poll_wait (); |
1400 | poll_wait (); |
1278 | poll_cb (); |
1401 | poll_cb (0); |
1279 | } |
1402 | } |
1280 | |
1403 | |
1281 | void |
1404 | void |
1282 | poll() |
1405 | poll() |
1283 | PROTOTYPE: |
1406 | PROTOTYPE: |
1284 | CODE: |
1407 | CODE: |
1285 | if (nreqs) |
1408 | if (nreqs) |
1286 | { |
1409 | { |
1287 | poll_wait (); |
1410 | poll_wait (); |
1288 | poll_cb (); |
1411 | poll_cb (0); |
1289 | } |
1412 | } |
1290 | |
1413 | |
1291 | int |
1414 | int |
1292 | poll_fileno() |
1415 | poll_fileno() |
1293 | PROTOTYPE: |
1416 | PROTOTYPE: |
… | |
… | |
1298 | |
1421 | |
1299 | int |
1422 | int |
1300 | poll_cb(...) |
1423 | poll_cb(...) |
1301 | PROTOTYPE: |
1424 | PROTOTYPE: |
1302 | CODE: |
1425 | CODE: |
1303 | RETVAL = poll_cb (); |
1426 | RETVAL = poll_cb (0); |
|
|
1427 | OUTPUT: |
|
|
1428 | RETVAL |
|
|
1429 | |
|
|
1430 | int |
|
|
1431 | poll_some(int max = 0) |
|
|
1432 | PROTOTYPE: $ |
|
|
1433 | CODE: |
|
|
1434 | RETVAL = poll_cb (max); |
1304 | OUTPUT: |
1435 | OUTPUT: |
1305 | RETVAL |
1436 | RETVAL |
1306 | |
1437 | |
1307 | void |
1438 | void |
1308 | poll_wait() |
1439 | poll_wait() |
… | |
… | |
1323 | |
1454 | |
1324 | MODULE = IO::AIO PACKAGE = IO::AIO::REQ |
1455 | MODULE = IO::AIO PACKAGE = IO::AIO::REQ |
1325 | |
1456 | |
1326 | void |
1457 | void |
1327 | cancel (aio_req_ornot req) |
1458 | cancel (aio_req_ornot req) |
1328 | PROTOTYPE: |
|
|
1329 | CODE: |
1459 | CODE: |
1330 | req_cancel (req); |
1460 | req_cancel (req); |
1331 | |
1461 | |
1332 | void |
1462 | void |
1333 | cb (aio_req_ornot req, SV *callback=&PL_sv_undef) |
1463 | cb (aio_req_ornot req, SV *callback=&PL_sv_undef) |
… | |
… | |
1369 | } |
1499 | } |
1370 | } |
1500 | } |
1371 | } |
1501 | } |
1372 | |
1502 | |
1373 | void |
1503 | void |
|
|
1504 | cancel_subs (aio_req_ornot req) |
|
|
1505 | CODE: |
|
|
1506 | req_cancel_subs (req); |
|
|
1507 | |
|
|
1508 | void |
1374 | result (aio_req grp, ...) |
1509 | result (aio_req grp, ...) |
1375 | CODE: |
1510 | CODE: |
1376 | { |
1511 | { |
1377 | int i; |
1512 | int i; |
1378 | AV *av = newAV (); |
1513 | AV *av = newAV (); |
… | |
… | |
1383 | SvREFCNT_dec (grp->data); |
1518 | SvREFCNT_dec (grp->data); |
1384 | grp->data = (SV *)av; |
1519 | grp->data = (SV *)av; |
1385 | } |
1520 | } |
1386 | |
1521 | |
1387 | void |
1522 | void |
1388 | feed_limit (aio_req grp, int limit) |
1523 | limit (aio_req grp, int limit) |
1389 | CODE: |
1524 | CODE: |
1390 | grp->fd2 = limit; |
1525 | grp->fd2 = limit; |
1391 | aio_grp_feed (grp); |
1526 | aio_grp_feed (grp); |
1392 | |
1527 | |
1393 | void |
1528 | void |