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