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