|
|
1 | #if __linux |
|
|
2 | # define _GNU_SOURCE |
|
|
3 | #endif |
|
|
4 | |
1 | #define _REENTRANT 1 |
5 | #define _REENTRANT 1 |
|
|
6 | |
2 | #include <errno.h> |
7 | #include <errno.h> |
3 | |
8 | |
4 | #include "EXTERN.h" |
9 | #include "EXTERN.h" |
5 | #include "perl.h" |
10 | #include "perl.h" |
6 | #include "XSUB.h" |
11 | #include "XSUB.h" |
… | |
… | |
41 | # define NAME_MAX 4096 |
46 | # define NAME_MAX 4096 |
42 | #endif |
47 | #endif |
43 | |
48 | |
44 | #if __ia64 |
49 | #if __ia64 |
45 | # define STACKSIZE 65536 |
50 | # define STACKSIZE 65536 |
|
|
51 | #elif __i386 || __x86_64 /* 16k is unreasonably high :( */ |
|
|
52 | # define STACKSIZE PTHREAD_STACK_MIN |
46 | #else |
53 | #else |
47 | # define STACKSIZE 8192 |
54 | # define STACKSIZE 16384 |
48 | #endif |
55 | #endif |
|
|
56 | |
|
|
57 | /* buffer size for various temporary buffers */ |
|
|
58 | #define AIO_BUFSIZE 65536 |
|
|
59 | |
|
|
60 | #define dBUF \ |
|
|
61 | char *aio_buf = malloc (AIO_BUFSIZE); \ |
|
|
62 | if (!aio_buf) \ |
|
|
63 | return -1; |
|
|
64 | |
|
|
65 | #define fBUF free (aio_buf) |
49 | |
66 | |
50 | enum { |
67 | enum { |
51 | REQ_QUIT, |
68 | REQ_QUIT, |
52 | REQ_OPEN, REQ_CLOSE, |
69 | REQ_OPEN, REQ_CLOSE, |
53 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
70 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
… | |
… | |
55 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
72 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
56 | REQ_FSYNC, REQ_FDATASYNC, |
73 | REQ_FSYNC, REQ_FDATASYNC, |
57 | REQ_UNLINK, REQ_RMDIR, REQ_RENAME, |
74 | REQ_UNLINK, REQ_RMDIR, REQ_RENAME, |
58 | REQ_READDIR, |
75 | REQ_READDIR, |
59 | REQ_LINK, REQ_SYMLINK, |
76 | REQ_LINK, REQ_SYMLINK, |
|
|
77 | REQ_GROUP, REQ_NOP, |
60 | REQ_SLEEP, |
78 | REQ_SLEEP, |
61 | REQ_GROUP, |
|
|
62 | }; |
79 | }; |
63 | |
80 | |
64 | #define AIO_REQ_KLASS "IO::AIO::REQ" |
81 | #define AIO_REQ_KLASS "IO::AIO::REQ" |
65 | #define AIO_GRP_KLASS "IO::AIO::GRP" |
82 | #define AIO_GRP_KLASS "IO::AIO::GRP" |
66 | |
83 | |
67 | typedef struct aio_cb |
84 | typedef struct aio_cb |
68 | { |
85 | { |
69 | struct aio_cb *grp, *grp_prev, *grp_next; |
|
|
70 | |
|
|
71 | struct aio_cb *volatile next; |
86 | struct aio_cb *volatile next; |
72 | |
|
|
73 | SV *self; /* the perl counterpart of this request, if any */ |
|
|
74 | |
87 | |
75 | SV *data, *callback; |
88 | SV *data, *callback; |
76 | SV *fh, *fh2; |
89 | SV *fh, *fh2; |
77 | void *dataptr, *data2ptr; |
90 | void *dataptr, *data2ptr; |
78 | Stat_t *statdata; |
91 | Stat_t *statdata; |
79 | off_t offset; |
92 | off_t offset; |
80 | size_t length; |
93 | size_t length; |
81 | ssize_t result; |
94 | ssize_t result; |
82 | |
95 | |
|
|
96 | STRLEN dataoffset; |
83 | int type; |
97 | int type; |
84 | int fd, fd2; |
98 | int fd, fd2; |
85 | int errorno; |
99 | int errorno; |
86 | STRLEN dataoffset; |
|
|
87 | mode_t mode; /* open */ |
100 | mode_t mode; /* open */ |
|
|
101 | |
88 | unsigned char cancelled; |
102 | unsigned char flags; |
|
|
103 | unsigned char pri; |
|
|
104 | |
|
|
105 | SV *self; /* the perl counterpart of this request, if any */ |
|
|
106 | struct aio_cb *grp, *grp_prev, *grp_next, *grp_first; |
89 | } aio_cb; |
107 | } aio_cb; |
|
|
108 | |
|
|
109 | enum { |
|
|
110 | FLAG_CANCELLED = 0x01, |
|
|
111 | }; |
90 | |
112 | |
91 | typedef aio_cb *aio_req; |
113 | typedef aio_cb *aio_req; |
92 | typedef aio_cb *aio_req_ornot; |
114 | typedef aio_cb *aio_req_ornot; |
93 | typedef aio_cb *aio_group; |
115 | |
|
|
116 | enum { |
|
|
117 | PRI_MIN = -4, |
|
|
118 | PRI_MAX = 4, |
|
|
119 | |
|
|
120 | DEFAULT_PRI = 0, |
|
|
121 | PRI_BIAS = -PRI_MIN, |
|
|
122 | NUM_PRI = PRI_MAX + PRI_BIAS + 1, |
|
|
123 | }; |
|
|
124 | |
|
|
125 | static int next_pri = DEFAULT_PRI + PRI_BIAS; |
94 | |
126 | |
95 | static int started, wanted; |
127 | static int started, wanted; |
96 | static volatile int nreqs; |
128 | static volatile int nreqs; |
97 | static int max_outstanding = 1<<30; |
129 | static int max_outstanding = 1<<30; |
98 | static int respipe [2]; |
130 | static int respipe [2]; |
99 | |
131 | |
|
|
132 | #if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) |
|
|
133 | # define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP |
|
|
134 | #else |
|
|
135 | # define AIO_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER |
|
|
136 | #endif |
|
|
137 | |
100 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
138 | static pthread_mutex_t reslock = AIO_MUTEX_INIT; |
101 | static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; |
139 | static pthread_mutex_t reqlock = AIO_MUTEX_INIT; |
102 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
140 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
103 | |
141 | |
104 | static volatile aio_req reqs, reqe; /* queue start, queue end */ |
142 | /* |
105 | static volatile aio_req ress, rese; /* queue start, queue end */ |
143 | * a somewhat faster data structure might be nice, but |
|
|
144 | * with 8 priorities this actually needs <20 insns |
|
|
145 | * per shift, the most expensive operation. |
|
|
146 | */ |
|
|
147 | typedef struct { |
|
|
148 | aio_req qs[NUM_PRI], qe[NUM_PRI]; /* qstart, qend */ |
|
|
149 | int size; |
|
|
150 | } reqq; |
106 | |
151 | |
|
|
152 | static reqq req_queue; |
|
|
153 | static reqq res_queue; |
|
|
154 | |
|
|
155 | int reqq_push (reqq *q, aio_req req) |
|
|
156 | { |
|
|
157 | int pri = req->pri; |
|
|
158 | req->next = 0; |
|
|
159 | |
|
|
160 | if (q->qe[pri]) |
|
|
161 | { |
|
|
162 | q->qe[pri]->next = req; |
|
|
163 | q->qe[pri] = req; |
|
|
164 | } |
|
|
165 | else |
|
|
166 | q->qe[pri] = q->qs[pri] = req; |
|
|
167 | |
|
|
168 | return q->size++; |
|
|
169 | } |
|
|
170 | |
|
|
171 | aio_req reqq_shift (reqq *q) |
|
|
172 | { |
|
|
173 | int pri; |
|
|
174 | |
|
|
175 | if (!q->size) |
|
|
176 | return 0; |
|
|
177 | |
|
|
178 | --q->size; |
|
|
179 | |
|
|
180 | for (pri = NUM_PRI; pri--; ) |
|
|
181 | { |
|
|
182 | aio_req req = q->qs[pri]; |
|
|
183 | |
|
|
184 | if (req) |
|
|
185 | { |
|
|
186 | if (!(q->qs[pri] = req->next)) |
|
|
187 | q->qe[pri] = 0; |
|
|
188 | |
|
|
189 | return req; |
|
|
190 | } |
|
|
191 | } |
|
|
192 | |
|
|
193 | abort (); |
|
|
194 | } |
|
|
195 | |
|
|
196 | static void req_invoke (aio_req req); |
107 | static void req_free (aio_req req); |
197 | static void req_free (aio_req req); |
108 | |
198 | |
109 | /* must be called at most once */ |
199 | /* must be called at most once */ |
110 | static SV *req_sv (aio_req req, const char *klass) |
200 | static SV *req_sv (aio_req req, const char *klass) |
111 | { |
201 | { |
|
|
202 | if (!req->self) |
|
|
203 | { |
112 | req->self = (SV *)newHV (); |
204 | req->self = (SV *)newHV (); |
113 | sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0); |
205 | sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0); |
|
|
206 | } |
114 | |
207 | |
115 | return sv_2mortal (sv_bless (newRV_inc (req->self), gv_stashpv (klass, 1))); |
208 | return sv_2mortal (sv_bless (newRV_inc (req->self), gv_stashpv (klass, 1))); |
116 | } |
209 | } |
117 | |
210 | |
118 | static aio_req SvAIO_REQ (SV *sv) |
211 | static aio_req SvAIO_REQ (SV *sv) |
119 | { |
212 | { |
|
|
213 | MAGIC *mg; |
|
|
214 | |
120 | if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv)) |
215 | if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv)) |
121 | croak ("object of class " AIO_REQ_KLASS " expected"); |
216 | croak ("object of class " AIO_REQ_KLASS " expected"); |
122 | |
217 | |
123 | MAGIC *mg = mg_find (SvRV (sv), PERL_MAGIC_ext); |
218 | mg = mg_find (SvRV (sv), PERL_MAGIC_ext); |
124 | |
219 | |
125 | return mg ? (aio_req)mg->mg_ptr : 0; |
220 | return mg ? (aio_req)mg->mg_ptr : 0; |
126 | } |
221 | } |
127 | |
222 | |
|
|
223 | static void aio_grp_feed (aio_req grp) |
|
|
224 | { |
|
|
225 | while (grp->length < grp->fd2 && !(grp->flags & FLAG_CANCELLED)) |
|
|
226 | { |
|
|
227 | int old_len = grp->length; |
|
|
228 | |
|
|
229 | if (grp->fh2 && SvOK (grp->fh2)) |
|
|
230 | { |
|
|
231 | dSP; |
|
|
232 | |
|
|
233 | ENTER; |
|
|
234 | SAVETMPS; |
|
|
235 | PUSHMARK (SP); |
|
|
236 | XPUSHs (req_sv (grp, AIO_GRP_KLASS)); |
|
|
237 | PUTBACK; |
|
|
238 | call_sv (grp->fh2, G_VOID | G_EVAL | G_KEEPERR); |
|
|
239 | SPAGAIN; |
|
|
240 | FREETMPS; |
|
|
241 | LEAVE; |
|
|
242 | } |
|
|
243 | |
|
|
244 | /* stop if no progress has been made */ |
|
|
245 | if (old_len == grp->length) |
|
|
246 | { |
|
|
247 | SvREFCNT_dec (grp->fh2); |
|
|
248 | grp->fh2 = 0; |
|
|
249 | break; |
|
|
250 | } |
|
|
251 | } |
|
|
252 | } |
|
|
253 | |
|
|
254 | static void aio_grp_dec (aio_req grp) |
|
|
255 | { |
|
|
256 | --grp->length; |
|
|
257 | |
|
|
258 | /* call feeder, if applicable */ |
|
|
259 | aio_grp_feed (grp); |
|
|
260 | |
|
|
261 | /* finish, if done */ |
|
|
262 | if (!grp->length && grp->fd) |
|
|
263 | { |
|
|
264 | req_invoke (grp); |
|
|
265 | req_free (grp); |
|
|
266 | } |
|
|
267 | } |
|
|
268 | |
128 | static void poll_wait () |
269 | static void poll_wait () |
129 | { |
270 | { |
130 | if (nreqs && !ress) |
|
|
131 | { |
|
|
132 | fd_set rfd; |
271 | fd_set rfd; |
|
|
272 | |
|
|
273 | while (nreqs) |
|
|
274 | { |
|
|
275 | int size; |
|
|
276 | #if !(__i386 || __x86_64) /* safe without sempahore on this archs */ |
|
|
277 | pthread_mutex_lock (&reslock); |
|
|
278 | #endif |
|
|
279 | size = res_queue.size; |
|
|
280 | #if !(__i386 || __x86_64) /* safe without sempahore on this archs */ |
|
|
281 | pthread_mutex_unlock (&reslock); |
|
|
282 | #endif |
|
|
283 | |
|
|
284 | if (size) |
|
|
285 | return; |
|
|
286 | |
133 | FD_ZERO(&rfd); |
287 | FD_ZERO(&rfd); |
134 | FD_SET(respipe [0], &rfd); |
288 | FD_SET(respipe [0], &rfd); |
135 | |
289 | |
136 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
290 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
137 | } |
291 | } |
138 | } |
292 | } |
139 | |
293 | |
140 | static void req_invoke (aio_req req) |
294 | static void req_invoke (aio_req req) |
141 | { |
295 | { |
142 | dSP; |
296 | dSP; |
143 | int errorno = errno; |
|
|
144 | |
297 | |
145 | if (req->cancelled || !SvOK (req->callback)) |
298 | if (!(req->flags & FLAG_CANCELLED) && SvOK (req->callback)) |
146 | return; |
299 | { |
147 | |
|
|
148 | errno = req->errorno; |
300 | errno = req->errorno; |
149 | |
301 | |
150 | ENTER; |
302 | ENTER; |
|
|
303 | SAVETMPS; |
151 | PUSHMARK (SP); |
304 | PUSHMARK (SP); |
|
|
305 | EXTEND (SP, 1); |
152 | |
306 | |
153 | switch (req->type) |
307 | switch (req->type) |
154 | { |
|
|
155 | case REQ_READDIR: |
|
|
156 | { |
308 | { |
157 | SV *rv = &PL_sv_undef; |
309 | case REQ_READDIR: |
158 | |
|
|
159 | if (req->result >= 0) |
|
|
160 | { |
310 | { |
161 | char *buf = req->data2ptr; |
311 | SV *rv = &PL_sv_undef; |
162 | AV *av = newAV (); |
|
|
163 | |
312 | |
164 | while (req->result) |
313 | if (req->result >= 0) |
165 | { |
314 | { |
|
|
315 | char *buf = req->data2ptr; |
|
|
316 | AV *av = newAV (); |
|
|
317 | |
|
|
318 | while (req->result) |
|
|
319 | { |
166 | SV *sv = newSVpv (buf, 0); |
320 | SV *sv = newSVpv (buf, 0); |
167 | |
321 | |
168 | av_push (av, sv); |
322 | av_push (av, sv); |
169 | buf += SvCUR (sv) + 1; |
323 | buf += SvCUR (sv) + 1; |
170 | req->result--; |
324 | req->result--; |
|
|
325 | } |
|
|
326 | |
|
|
327 | rv = sv_2mortal (newRV_noinc ((SV *)av)); |
171 | } |
328 | } |
172 | |
329 | |
173 | rv = sv_2mortal (newRV_noinc ((SV *)av)); |
330 | PUSHs (rv); |
174 | } |
331 | } |
|
|
332 | break; |
175 | |
333 | |
176 | XPUSHs (rv); |
334 | case REQ_OPEN: |
|
|
335 | { |
|
|
336 | /* convert fd to fh */ |
|
|
337 | SV *fh; |
|
|
338 | |
|
|
339 | PUSHs (sv_2mortal (newSViv (req->result))); |
|
|
340 | PUTBACK; |
|
|
341 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
|
|
342 | SPAGAIN; |
|
|
343 | |
|
|
344 | fh = SvREFCNT_inc (POPs); |
|
|
345 | |
|
|
346 | PUSHMARK (SP); |
|
|
347 | XPUSHs (sv_2mortal (fh)); |
|
|
348 | } |
|
|
349 | break; |
|
|
350 | |
|
|
351 | case REQ_GROUP: |
|
|
352 | req->fd = 2; /* mark group as finished */ |
|
|
353 | |
|
|
354 | if (req->data) |
|
|
355 | { |
|
|
356 | int i; |
|
|
357 | AV *av = (AV *)req->data; |
|
|
358 | |
|
|
359 | EXTEND (SP, AvFILL (av) + 1); |
|
|
360 | for (i = 0; i <= AvFILL (av); ++i) |
|
|
361 | PUSHs (*av_fetch (av, i, 0)); |
|
|
362 | } |
|
|
363 | break; |
|
|
364 | |
|
|
365 | case REQ_NOP: |
|
|
366 | case REQ_SLEEP: |
|
|
367 | break; |
|
|
368 | |
|
|
369 | default: |
|
|
370 | PUSHs (sv_2mortal (newSViv (req->result))); |
|
|
371 | break; |
177 | } |
372 | } |
178 | break; |
|
|
179 | |
373 | |
180 | case REQ_OPEN: |
|
|
181 | { |
|
|
182 | /* convert fd to fh */ |
|
|
183 | SV *fh; |
|
|
184 | |
374 | |
185 | XPUSHs (sv_2mortal (newSViv (req->result))); |
|
|
186 | PUTBACK; |
375 | PUTBACK; |
187 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
|
|
188 | SPAGAIN; |
|
|
189 | |
|
|
190 | fh = SvREFCNT_inc (POPs); |
|
|
191 | |
|
|
192 | PUSHMARK (SP); |
|
|
193 | XPUSHs (sv_2mortal (fh)); |
|
|
194 | } |
|
|
195 | break; |
|
|
196 | |
|
|
197 | case REQ_SLEEP: |
|
|
198 | case REQ_GROUP: |
|
|
199 | break; |
|
|
200 | |
|
|
201 | default: |
|
|
202 | XPUSHs (sv_2mortal (newSViv (req->result))); |
|
|
203 | break; |
|
|
204 | } |
|
|
205 | |
|
|
206 | |
|
|
207 | PUTBACK; |
|
|
208 | call_sv (req->callback, G_VOID | G_EVAL); |
376 | call_sv (req->callback, G_VOID | G_EVAL); |
209 | SPAGAIN; |
377 | SPAGAIN; |
|
|
378 | |
|
|
379 | FREETMPS; |
|
|
380 | LEAVE; |
|
|
381 | } |
|
|
382 | |
|
|
383 | if (req->grp) |
|
|
384 | { |
|
|
385 | aio_req grp = req->grp; |
|
|
386 | |
|
|
387 | /* unlink request */ |
|
|
388 | if (req->grp_next) req->grp_next->grp_prev = req->grp_prev; |
|
|
389 | if (req->grp_prev) req->grp_prev->grp_next = req->grp_next; |
|
|
390 | |
|
|
391 | if (grp->grp_first == req) |
|
|
392 | grp->grp_first = req->grp_next; |
|
|
393 | |
|
|
394 | aio_grp_dec (grp); |
|
|
395 | } |
210 | |
396 | |
211 | if (SvTRUE (ERRSV)) |
397 | if (SvTRUE (ERRSV)) |
212 | { |
398 | { |
213 | req_free (req); |
399 | req_free (req); |
214 | croak (0); |
400 | croak (0); |
215 | } |
401 | } |
216 | |
|
|
217 | LEAVE; |
|
|
218 | |
|
|
219 | errno = errorno; |
|
|
220 | } |
402 | } |
221 | |
403 | |
222 | static void req_free (aio_req req) |
404 | static void req_free (aio_req req) |
223 | { |
405 | { |
224 | if (req->grp) |
|
|
225 | { |
|
|
226 | aio_req grp = req->grp; |
|
|
227 | |
|
|
228 | /* unlink request */ |
|
|
229 | req->grp_next->grp_prev = req->grp_prev; |
|
|
230 | req->grp_prev->grp_next = req->grp_next; |
|
|
231 | |
|
|
232 | if (grp->grp_next == grp) |
|
|
233 | { |
|
|
234 | req_invoke (grp); |
|
|
235 | req_free (grp); |
|
|
236 | } |
|
|
237 | } |
|
|
238 | |
|
|
239 | if (req->self) |
406 | if (req->self) |
240 | { |
407 | { |
241 | sv_unmagic (req->self, PERL_MAGIC_ext); |
408 | sv_unmagic (req->self, PERL_MAGIC_ext); |
242 | SvREFCNT_dec (req->self); |
409 | SvREFCNT_dec (req->self); |
243 | } |
410 | } |
244 | |
411 | |
245 | if (req->data) |
|
|
246 | SvREFCNT_dec (req->data); |
412 | SvREFCNT_dec (req->data); |
247 | |
|
|
248 | if (req->fh) |
|
|
249 | SvREFCNT_dec (req->fh); |
413 | SvREFCNT_dec (req->fh); |
250 | |
|
|
251 | if (req->fh2) |
|
|
252 | SvREFCNT_dec (req->fh2); |
414 | SvREFCNT_dec (req->fh2); |
253 | |
|
|
254 | if (req->statdata) |
|
|
255 | Safefree (req->statdata); |
|
|
256 | |
|
|
257 | if (req->callback) |
|
|
258 | SvREFCNT_dec (req->callback); |
415 | SvREFCNT_dec (req->callback); |
|
|
416 | Safefree (req->statdata); |
259 | |
417 | |
260 | if (req->type == REQ_READDIR && req->result >= 0) |
418 | if (req->type == REQ_READDIR && req->result >= 0) |
261 | free (req->data2ptr); |
419 | free (req->data2ptr); |
262 | |
420 | |
263 | Safefree (req); |
421 | Safefree (req); |
264 | } |
422 | } |
265 | |
423 | |
266 | static void req_cancel (aio_req req) |
424 | static void req_cancel (aio_req req) |
267 | { |
425 | { |
268 | req->cancelled = 1; |
426 | req->flags |= FLAG_CANCELLED; |
269 | |
427 | |
270 | if (req->type == REQ_GROUP) |
428 | if (req->type == REQ_GROUP) |
271 | { |
429 | { |
272 | aio_req sub; |
430 | aio_req sub; |
273 | |
431 | |
274 | for (sub = req->grp_next; sub != req; sub = sub->grp_next) |
432 | for (sub = req->grp_first; sub; sub = sub->grp_next) |
275 | req_cancel (sub); |
433 | req_cancel (sub); |
276 | } |
434 | } |
277 | } |
435 | } |
278 | |
436 | |
279 | static int poll_cb () |
437 | static int poll_cb () |
… | |
… | |
284 | aio_req req; |
442 | aio_req req; |
285 | |
443 | |
286 | for (;;) |
444 | for (;;) |
287 | { |
445 | { |
288 | pthread_mutex_lock (&reslock); |
446 | pthread_mutex_lock (&reslock); |
289 | req = ress; |
447 | req = reqq_shift (&res_queue); |
290 | |
448 | |
291 | if (req) |
449 | if (req) |
292 | { |
450 | { |
293 | ress = req->next; |
|
|
294 | |
|
|
295 | if (!ress) |
451 | if (!res_queue.size) |
296 | { |
452 | { |
297 | /* read any signals sent by the worker threads */ |
453 | /* read any signals sent by the worker threads */ |
298 | char buf [32]; |
454 | char buf [32]; |
299 | while (read (respipe [0], buf, 32) == 32) |
455 | while (read (respipe [0], buf, 32) == 32) |
300 | ; |
456 | ; |
301 | |
|
|
302 | rese = 0; |
|
|
303 | } |
457 | } |
304 | } |
458 | } |
305 | |
459 | |
306 | pthread_mutex_unlock (&reslock); |
460 | pthread_mutex_unlock (&reslock); |
307 | |
461 | |
308 | if (!req) |
462 | if (!req) |
309 | break; |
463 | break; |
310 | |
464 | |
311 | nreqs--; |
465 | --nreqs; |
312 | |
466 | |
313 | if (req->type == REQ_QUIT) |
467 | if (req->type == REQ_QUIT) |
314 | started--; |
468 | started--; |
315 | else if (req->type == REQ_GROUP && req->grp_next != req) |
469 | else if (req->type == REQ_GROUP && req->length) |
|
|
470 | { |
|
|
471 | req->fd = 1; /* mark request as delayed */ |
316 | continue; |
472 | continue; |
|
|
473 | } |
317 | else |
474 | else |
318 | { |
475 | { |
319 | if (req->type == REQ_READ) |
476 | if (req->type == REQ_READ) |
320 | SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); |
477 | SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); |
321 | |
478 | |
… | |
… | |
364 | static void req_send (aio_req req) |
521 | static void req_send (aio_req req) |
365 | { |
522 | { |
366 | while (started < wanted && nreqs >= started) |
523 | while (started < wanted && nreqs >= started) |
367 | start_thread (); |
524 | start_thread (); |
368 | |
525 | |
369 | nreqs++; |
526 | ++nreqs; |
370 | |
527 | |
371 | pthread_mutex_lock (&reqlock); |
528 | pthread_mutex_lock (&reqlock); |
372 | |
529 | reqq_push (&req_queue, req); |
373 | req->next = 0; |
|
|
374 | |
|
|
375 | if (reqe) |
|
|
376 | { |
|
|
377 | reqe->next = req; |
|
|
378 | reqe = req; |
|
|
379 | } |
|
|
380 | else |
|
|
381 | reqe = reqs = req; |
|
|
382 | |
|
|
383 | pthread_cond_signal (&reqwait); |
530 | pthread_cond_signal (&reqwait); |
384 | pthread_mutex_unlock (&reqlock); |
531 | pthread_mutex_unlock (&reqlock); |
385 | |
532 | |
386 | if (nreqs > max_outstanding) |
533 | if (nreqs > max_outstanding) |
387 | for (;;) |
534 | for (;;) |
… | |
… | |
396 | } |
543 | } |
397 | |
544 | |
398 | static void end_thread (void) |
545 | static void end_thread (void) |
399 | { |
546 | { |
400 | aio_req req; |
547 | aio_req req; |
|
|
548 | |
401 | Newz (0, req, 1, aio_cb); |
549 | Newz (0, req, 1, aio_cb); |
|
|
550 | |
402 | req->type = REQ_QUIT; |
551 | req->type = REQ_QUIT; |
|
|
552 | req->pri = PRI_MAX + PRI_BIAS; |
403 | |
553 | |
404 | req_send (req); |
554 | req_send (req); |
405 | } |
555 | } |
406 | |
556 | |
407 | static void min_parallel (int nthreads) |
557 | static void min_parallel (int nthreads) |
… | |
… | |
494 | #if !HAVE_READAHEAD |
644 | #if !HAVE_READAHEAD |
495 | # define readahead aio_readahead |
645 | # define readahead aio_readahead |
496 | |
646 | |
497 | static ssize_t readahead (int fd, off_t offset, size_t count) |
647 | static ssize_t readahead (int fd, off_t offset, size_t count) |
498 | { |
648 | { |
499 | char readahead_buf[4096]; |
649 | dBUF; |
500 | |
650 | |
501 | while (count > 0) |
651 | while (count > 0) |
502 | { |
652 | { |
503 | size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); |
653 | size_t len = count < AIO_BUFSIZE ? count : AIO_BUFSIZE; |
504 | |
654 | |
505 | pread (fd, readahead_buf, len, offset); |
655 | pread (fd, aio_buf, len, offset); |
506 | offset += len; |
656 | offset += len; |
507 | count -= len; |
657 | count -= len; |
508 | } |
658 | } |
|
|
659 | |
|
|
660 | fBUF; |
509 | |
661 | |
510 | errno = 0; |
662 | errno = 0; |
511 | } |
663 | } |
512 | #endif |
664 | #endif |
513 | |
665 | |
… | |
… | |
599 | #endif |
751 | #endif |
600 | ) |
752 | ) |
601 | ) |
753 | ) |
602 | { |
754 | { |
603 | /* emulate sendfile. this is a major pain in the ass */ |
755 | /* emulate sendfile. this is a major pain in the ass */ |
604 | char buf[4096]; |
756 | dBUF; |
|
|
757 | |
605 | res = 0; |
758 | res = 0; |
606 | |
759 | |
607 | while (count) |
760 | while (count) |
608 | { |
761 | { |
609 | ssize_t cnt; |
762 | ssize_t cnt; |
610 | |
763 | |
611 | cnt = pread (ifd, buf, count > 4096 ? 4096 : count, offset); |
764 | cnt = pread (ifd, aio_buf, count > AIO_BUFSIZE ? AIO_BUFSIZE : count, offset); |
612 | |
765 | |
613 | if (cnt <= 0) |
766 | if (cnt <= 0) |
614 | { |
767 | { |
615 | if (cnt && !res) res = -1; |
768 | if (cnt && !res) res = -1; |
616 | break; |
769 | break; |
617 | } |
770 | } |
618 | |
771 | |
619 | cnt = write (ofd, buf, cnt); |
772 | cnt = write (ofd, aio_buf, cnt); |
620 | |
773 | |
621 | if (cnt <= 0) |
774 | if (cnt <= 0) |
622 | { |
775 | { |
623 | if (cnt && !res) res = -1; |
776 | if (cnt && !res) res = -1; |
624 | break; |
777 | break; |
… | |
… | |
626 | |
779 | |
627 | offset += cnt; |
780 | offset += cnt; |
628 | res += cnt; |
781 | res += cnt; |
629 | count -= cnt; |
782 | count -= cnt; |
630 | } |
783 | } |
|
|
784 | |
|
|
785 | fBUF; |
631 | } |
786 | } |
632 | |
787 | |
633 | return res; |
788 | return res; |
634 | } |
789 | } |
635 | |
790 | |
636 | /* read a full directory */ |
791 | /* read a full directory */ |
637 | static int scandir_ (const char *path, void **namesp) |
792 | static int scandir_ (const char *path, void **namesp) |
638 | { |
793 | { |
639 | DIR *dirp = opendir (path); |
794 | DIR *dirp; |
640 | union |
795 | union |
641 | { |
796 | { |
642 | struct dirent d; |
797 | struct dirent d; |
643 | char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; |
798 | char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; |
644 | } u; |
799 | } *u; |
645 | struct dirent *entp; |
800 | struct dirent *entp; |
646 | char *name, *names; |
801 | char *name, *names; |
647 | int memlen = 4096; |
802 | int memlen = 4096; |
648 | int memofs = 0; |
803 | int memofs = 0; |
649 | int res = 0; |
804 | int res = 0; |
650 | int errorno; |
805 | int errorno; |
651 | |
806 | |
|
|
807 | dirp = opendir (path); |
652 | if (!dirp) |
808 | if (!dirp) |
653 | return -1; |
809 | return -1; |
654 | |
810 | |
|
|
811 | u = malloc (sizeof (*u)); |
655 | names = malloc (memlen); |
812 | names = malloc (memlen); |
656 | |
813 | |
|
|
814 | if (u && names) |
657 | for (;;) |
815 | for (;;) |
658 | { |
816 | { |
|
|
817 | errno = 0; |
659 | errno = 0, readdir_r (dirp, &u.d, &entp); |
818 | readdir_r (dirp, &u->d, &entp); |
660 | |
819 | |
661 | if (!entp) |
820 | if (!entp) |
662 | break; |
821 | break; |
663 | |
822 | |
664 | name = entp->d_name; |
823 | name = entp->d_name; |
665 | |
824 | |
666 | if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) |
825 | if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) |
667 | { |
826 | { |
668 | int len = strlen (name) + 1; |
827 | int len = strlen (name) + 1; |
669 | |
828 | |
670 | res++; |
829 | res++; |
671 | |
830 | |
672 | while (memofs + len > memlen) |
831 | while (memofs + len > memlen) |
673 | { |
832 | { |
674 | memlen *= 2; |
833 | memlen *= 2; |
675 | names = realloc (names, memlen); |
834 | names = realloc (names, memlen); |
676 | if (!names) |
835 | if (!names) |
677 | break; |
836 | break; |
678 | } |
837 | } |
679 | |
838 | |
680 | memcpy (names + memofs, name, len); |
839 | memcpy (names + memofs, name, len); |
681 | memofs += len; |
840 | memofs += len; |
682 | } |
841 | } |
683 | } |
842 | } |
684 | |
843 | |
685 | errorno = errno; |
844 | errorno = errno; |
|
|
845 | free (u); |
686 | closedir (dirp); |
846 | closedir (dirp); |
687 | |
847 | |
688 | if (errorno) |
848 | if (errorno) |
689 | { |
849 | { |
690 | free (names); |
850 | free (names); |
… | |
… | |
707 | { |
867 | { |
708 | pthread_mutex_lock (&reqlock); |
868 | pthread_mutex_lock (&reqlock); |
709 | |
869 | |
710 | for (;;) |
870 | for (;;) |
711 | { |
871 | { |
712 | req = reqs; |
872 | req = reqq_shift (&req_queue); |
713 | |
|
|
714 | if (reqs) |
|
|
715 | { |
|
|
716 | reqs = reqs->next; |
|
|
717 | if (!reqs) reqe = 0; |
|
|
718 | } |
|
|
719 | |
873 | |
720 | if (req) |
874 | if (req) |
721 | break; |
875 | break; |
722 | |
876 | |
723 | pthread_cond_wait (&reqwait, &reqlock); |
877 | pthread_cond_wait (&reqwait, &reqlock); |
724 | } |
878 | } |
725 | |
879 | |
726 | pthread_mutex_unlock (&reqlock); |
880 | pthread_mutex_unlock (&reqlock); |
727 | |
881 | |
728 | errno = 0; /* strictly unnecessary */ |
882 | errno = 0; /* strictly unnecessary */ |
|
|
883 | type = req->type; /* remember type for QUIT check */ |
729 | |
884 | |
730 | if (!req->cancelled) |
885 | if (!(req->flags & FLAG_CANCELLED)) |
731 | switch (req->type) |
886 | switch (type) |
732 | { |
887 | { |
733 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
888 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
734 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
889 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
735 | |
890 | |
736 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
891 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
… | |
… | |
760 | tv.tv_usec = req->fd2; |
915 | tv.tv_usec = req->fd2; |
761 | |
916 | |
762 | req->result = select (0, 0, 0, 0, &tv); |
917 | req->result = select (0, 0, 0, 0, &tv); |
763 | } |
918 | } |
764 | |
919 | |
|
|
920 | case REQ_GROUP: |
|
|
921 | case REQ_NOP: |
765 | case REQ_QUIT: |
922 | case REQ_QUIT: |
766 | break; |
923 | break; |
767 | |
924 | |
768 | default: |
925 | default: |
769 | req->result = ENOSYS; |
926 | req->result = ENOSYS; |
… | |
… | |
772 | |
929 | |
773 | req->errorno = errno; |
930 | req->errorno = errno; |
774 | |
931 | |
775 | pthread_mutex_lock (&reslock); |
932 | pthread_mutex_lock (&reslock); |
776 | |
933 | |
777 | req->next = 0; |
934 | if (!reqq_push (&res_queue, req)) |
778 | |
|
|
779 | if (rese) |
|
|
780 | { |
|
|
781 | rese->next = req; |
|
|
782 | rese = req; |
|
|
783 | } |
|
|
784 | else |
|
|
785 | { |
|
|
786 | rese = ress = req; |
|
|
787 | |
|
|
788 | /* write a dummy byte to the pipe so fh becomes ready */ |
935 | /* write a dummy byte to the pipe so fh becomes ready */ |
789 | write (respipe [1], &respipe, 1); |
936 | write (respipe [1], &respipe, 1); |
790 | } |
|
|
791 | |
937 | |
792 | pthread_mutex_unlock (&reslock); |
938 | pthread_mutex_unlock (&reslock); |
793 | } |
939 | } |
794 | while (type != REQ_QUIT); |
940 | while (type != REQ_QUIT); |
795 | |
941 | |
… | |
… | |
826 | { |
972 | { |
827 | aio_req prv; |
973 | aio_req prv; |
828 | |
974 | |
829 | started = 0; |
975 | started = 0; |
830 | |
976 | |
831 | while (reqs) |
977 | while (prv = reqq_shift (&req_queue)) |
832 | { |
|
|
833 | prv = reqs; |
|
|
834 | reqs = prv->next; |
|
|
835 | req_free (prv); |
978 | req_free (prv); |
836 | } |
|
|
837 | |
979 | |
838 | reqs = reqe = 0; |
980 | while (prv = reqq_shift (&res_queue)) |
839 | |
|
|
840 | while (ress) |
|
|
841 | { |
|
|
842 | prv = ress; |
|
|
843 | ress = prv->next; |
|
|
844 | req_free (prv); |
981 | req_free (prv); |
845 | } |
982 | |
846 | |
|
|
847 | ress = rese = 0; |
|
|
848 | |
|
|
849 | close (respipe [0]); |
983 | close (respipe [0]); |
850 | close (respipe [1]); |
984 | close (respipe [1]); |
851 | create_pipe (); |
985 | create_pipe (); |
852 | |
986 | |
853 | atfork_parent (); |
987 | atfork_parent (); |
854 | } |
988 | } |
855 | |
989 | |
856 | #define dREQ \ |
990 | #define dREQ \ |
857 | aio_req req; \ |
991 | aio_req req; \ |
|
|
992 | int req_pri = next_pri; \ |
|
|
993 | next_pri = DEFAULT_PRI + PRI_BIAS; \ |
858 | \ |
994 | \ |
859 | if (SvOK (callback) && !SvROK (callback)) \ |
995 | if (SvOK (callback) && !SvROK (callback)) \ |
860 | croak ("callback must be undef or of reference type"); \ |
996 | croak ("callback must be undef or of reference type"); \ |
861 | \ |
997 | \ |
862 | Newz (0, req, 1, aio_cb); \ |
998 | Newz (0, req, 1, aio_cb); \ |
863 | if (!req) \ |
999 | if (!req) \ |
864 | croak ("out of memory during aio_req allocation"); \ |
1000 | croak ("out of memory during aio_req allocation"); \ |
865 | \ |
1001 | \ |
866 | req->callback = newSVsv (callback) |
1002 | req->callback = newSVsv (callback); \ |
|
|
1003 | req->pri = req_pri |
867 | |
1004 | |
868 | #define REQ_SEND \ |
1005 | #define REQ_SEND \ |
869 | req_send (req); \ |
1006 | req_send (req); \ |
870 | \ |
1007 | \ |
871 | if (GIMME_V != G_VOID) \ |
1008 | if (GIMME_V != G_VOID) \ |
… | |
… | |
1142 | } |
1279 | } |
1143 | |
1280 | |
1144 | void |
1281 | void |
1145 | aio_group (callback=&PL_sv_undef) |
1282 | aio_group (callback=&PL_sv_undef) |
1146 | SV * callback |
1283 | SV * callback |
1147 | PROTOTYPE: ;& |
1284 | PROTOTYPE: ;$ |
1148 | PPCODE: |
1285 | PPCODE: |
1149 | { |
1286 | { |
1150 | dREQ; |
1287 | dREQ; |
|
|
1288 | |
1151 | req->type = REQ_GROUP; |
1289 | req->type = REQ_GROUP; |
1152 | req->grp_next = req; |
|
|
1153 | req->grp_prev = req; |
|
|
1154 | |
|
|
1155 | req_send (req); |
1290 | req_send (req); |
|
|
1291 | |
1156 | XPUSHs (req_sv (req, AIO_GRP_KLASS)); |
1292 | XPUSHs (req_sv (req, AIO_GRP_KLASS)); |
1157 | } |
1293 | } |
|
|
1294 | |
|
|
1295 | void |
|
|
1296 | aio_nop (callback=&PL_sv_undef) |
|
|
1297 | SV * callback |
|
|
1298 | PPCODE: |
|
|
1299 | { |
|
|
1300 | dREQ; |
|
|
1301 | |
|
|
1302 | req->type = REQ_NOP; |
|
|
1303 | |
|
|
1304 | REQ_SEND; |
|
|
1305 | } |
|
|
1306 | |
|
|
1307 | void |
|
|
1308 | aioreq_pri (int pri = DEFAULT_PRI) |
|
|
1309 | CODE: |
|
|
1310 | if (pri < PRI_MIN) pri = PRI_MIN; |
|
|
1311 | if (pri > PRI_MAX) pri = PRI_MAX; |
|
|
1312 | next_pri = pri + PRI_BIAS; |
1158 | |
1313 | |
1159 | void |
1314 | void |
1160 | flush () |
1315 | flush () |
1161 | PROTOTYPE: |
1316 | PROTOTYPE: |
1162 | CODE: |
1317 | CODE: |
… | |
… | |
1205 | CODE: |
1360 | CODE: |
1206 | RETVAL = nreqs; |
1361 | RETVAL = nreqs; |
1207 | OUTPUT: |
1362 | OUTPUT: |
1208 | RETVAL |
1363 | RETVAL |
1209 | |
1364 | |
|
|
1365 | PROTOTYPES: DISABLE |
|
|
1366 | |
1210 | MODULE = IO::AIO PACKAGE = IO::AIO::REQ |
1367 | MODULE = IO::AIO PACKAGE = IO::AIO::REQ |
1211 | |
1368 | |
1212 | void |
1369 | void |
1213 | cancel (aio_req_ornot req) |
1370 | cancel (aio_req_ornot req) |
1214 | PROTOTYPE: |
|
|
1215 | CODE: |
1371 | CODE: |
1216 | req_cancel (req); |
1372 | req_cancel (req); |
1217 | |
1373 | |
|
|
1374 | void |
|
|
1375 | cb (aio_req_ornot req, SV *callback=&PL_sv_undef) |
|
|
1376 | CODE: |
|
|
1377 | SvREFCNT_dec (req->callback); |
|
|
1378 | req->callback = newSVsv (callback); |
|
|
1379 | |
1218 | MODULE = IO::AIO PACKAGE = IO::AIO::GRP |
1380 | MODULE = IO::AIO PACKAGE = IO::AIO::GRP |
1219 | |
1381 | |
1220 | void |
1382 | void |
1221 | add (aio_req grp, ...) |
1383 | add (aio_req grp, ...) |
1222 | PROTOTYPE: $;@ |
|
|
1223 | PPCODE: |
1384 | PPCODE: |
1224 | { |
1385 | { |
1225 | int i; |
1386 | int i; |
|
|
1387 | aio_req req; |
|
|
1388 | |
|
|
1389 | if (grp->fd == 2) |
|
|
1390 | croak ("cannot add requests to IO::AIO::GRP after the group finished"); |
1226 | |
1391 | |
1227 | for (i = 1; i < items; ++i ) |
1392 | for (i = 1; i < items; ++i ) |
1228 | { |
1393 | { |
1229 | aio_req req = SvAIO_REQ (ST (i)); |
|
|
1230 | |
|
|
1231 | req->grp_prev = grp; |
|
|
1232 | req->grp_next = grp->grp_next; |
|
|
1233 | grp->grp_next->grp_prev = req; |
|
|
1234 | grp->grp_next = req; |
|
|
1235 | |
|
|
1236 | req->grp = grp; |
|
|
1237 | |
|
|
1238 | if (GIMME_V != G_VOID) |
1394 | if (GIMME_V != G_VOID) |
1239 | XPUSHs (sv_2mortal (newSVsv (ST (i)))); |
1395 | XPUSHs (sv_2mortal (newSVsv (ST (i)))); |
|
|
1396 | |
|
|
1397 | req = SvAIO_REQ (ST (i)); |
|
|
1398 | |
|
|
1399 | if (req) |
|
|
1400 | { |
|
|
1401 | ++grp->length; |
|
|
1402 | req->grp = grp; |
|
|
1403 | |
|
|
1404 | req->grp_prev = 0; |
|
|
1405 | req->grp_next = grp->grp_first; |
|
|
1406 | |
|
|
1407 | if (grp->grp_first) |
|
|
1408 | grp->grp_first->grp_prev = req; |
|
|
1409 | |
|
|
1410 | grp->grp_first = req; |
|
|
1411 | } |
1240 | } |
1412 | } |
1241 | } |
1413 | } |
1242 | |
1414 | |
|
|
1415 | void |
|
|
1416 | result (aio_req grp, ...) |
|
|
1417 | CODE: |
|
|
1418 | { |
|
|
1419 | int i; |
|
|
1420 | AV *av = newAV (); |
|
|
1421 | |
|
|
1422 | for (i = 1; i < items; ++i ) |
|
|
1423 | av_push (av, newSVsv (ST (i))); |
|
|
1424 | |
|
|
1425 | SvREFCNT_dec (grp->data); |
|
|
1426 | grp->data = (SV *)av; |
|
|
1427 | } |
|
|
1428 | |
|
|
1429 | void |
|
|
1430 | limit (aio_req grp, int limit) |
|
|
1431 | CODE: |
|
|
1432 | grp->fd2 = limit; |
|
|
1433 | aio_grp_feed (grp); |
|
|
1434 | |
|
|
1435 | void |
|
|
1436 | feed (aio_req grp, SV *callback=&PL_sv_undef) |
|
|
1437 | CODE: |
|
|
1438 | { |
|
|
1439 | SvREFCNT_dec (grp->fh2); |
|
|
1440 | grp->fh2 = newSVsv (callback); |
|
|
1441 | |
|
|
1442 | if (grp->fd2 <= 0) |
|
|
1443 | grp->fd2 = 2; |
|
|
1444 | |
|
|
1445 | aio_grp_feed (grp); |
|
|
1446 | } |
|
|
1447 | |