|
|
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; |
|
|
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; |
93 | |
126 | |
94 | static int started, wanted; |
127 | static int started, wanted; |
95 | static volatile int nreqs; |
128 | static volatile int nreqs; |
96 | static int max_outstanding = 1<<30; |
129 | static int max_outstanding = 1<<30; |
97 | static int respipe [2]; |
130 | static int respipe [2]; |
98 | |
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 | |
99 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
138 | static pthread_mutex_t reslock = AIO_MUTEX_INIT; |
100 | static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; |
139 | static pthread_mutex_t reqlock = AIO_MUTEX_INIT; |
101 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
140 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
102 | |
141 | |
103 | static volatile aio_req reqs, reqe; /* queue start, queue end */ |
142 | /* |
104 | 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; |
105 | |
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); |
106 | static void req_free (aio_req req); |
197 | static void req_free (aio_req req); |
107 | |
198 | |
108 | /* must be called at most once */ |
199 | /* must be called at most once */ |
109 | static SV *req_sv (aio_req req, const char *klass) |
200 | static SV *req_sv (aio_req req, const char *klass) |
110 | { |
201 | { |
|
|
202 | if (!req->self) |
|
|
203 | { |
111 | req->self = (SV *)newHV (); |
204 | req->self = (SV *)newHV (); |
112 | sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0); |
205 | sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0); |
|
|
206 | } |
113 | |
207 | |
114 | 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))); |
115 | } |
209 | } |
116 | |
210 | |
117 | static aio_req SvAIO_REQ (SV *sv) |
211 | static aio_req SvAIO_REQ (SV *sv) |
118 | { |
212 | { |
|
|
213 | MAGIC *mg; |
|
|
214 | |
119 | if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv)) |
215 | if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv)) |
120 | croak ("object of class " AIO_REQ_KLASS " expected"); |
216 | croak ("object of class " AIO_REQ_KLASS " expected"); |
121 | |
217 | |
122 | MAGIC *mg = mg_find (SvRV (sv), PERL_MAGIC_ext); |
218 | mg = mg_find (SvRV (sv), PERL_MAGIC_ext); |
123 | |
219 | |
124 | return mg ? (aio_req)mg->mg_ptr : 0; |
220 | return mg ? (aio_req)mg->mg_ptr : 0; |
125 | } |
221 | } |
126 | |
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 | |
127 | static void poll_wait () |
269 | static void poll_wait () |
128 | { |
270 | { |
129 | if (nreqs && !ress) |
|
|
130 | { |
|
|
131 | 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 | |
132 | FD_ZERO(&rfd); |
287 | FD_ZERO(&rfd); |
133 | FD_SET(respipe [0], &rfd); |
288 | FD_SET(respipe [0], &rfd); |
134 | |
289 | |
135 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
290 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
136 | } |
291 | } |
137 | } |
292 | } |
138 | |
293 | |
139 | static void req_invoke (aio_req req) |
294 | static void req_invoke (aio_req req) |
140 | { |
295 | { |
141 | dSP; |
296 | dSP; |
142 | int errorno = errno; |
|
|
143 | |
297 | |
144 | if (req->cancelled || !SvOK (req->callback)) |
298 | if (!(req->flags & FLAG_CANCELLED) && SvOK (req->callback)) |
145 | return; |
299 | { |
146 | |
|
|
147 | errno = req->errorno; |
300 | errno = req->errorno; |
148 | |
301 | |
149 | ENTER; |
302 | ENTER; |
|
|
303 | SAVETMPS; |
150 | PUSHMARK (SP); |
304 | PUSHMARK (SP); |
|
|
305 | EXTEND (SP, 1); |
151 | |
306 | |
152 | switch (req->type) |
307 | switch (req->type) |
153 | { |
|
|
154 | case REQ_READDIR: |
|
|
155 | { |
308 | { |
156 | SV *rv = &PL_sv_undef; |
309 | case REQ_READDIR: |
157 | |
|
|
158 | if (req->result >= 0) |
|
|
159 | { |
310 | { |
160 | char *buf = req->data2ptr; |
311 | SV *rv = &PL_sv_undef; |
161 | AV *av = newAV (); |
|
|
162 | |
312 | |
163 | while (req->result) |
313 | if (req->result >= 0) |
164 | { |
314 | { |
|
|
315 | char *buf = req->data2ptr; |
|
|
316 | AV *av = newAV (); |
|
|
317 | |
|
|
318 | while (req->result) |
|
|
319 | { |
165 | SV *sv = newSVpv (buf, 0); |
320 | SV *sv = newSVpv (buf, 0); |
166 | |
321 | |
167 | av_push (av, sv); |
322 | av_push (av, sv); |
168 | buf += SvCUR (sv) + 1; |
323 | buf += SvCUR (sv) + 1; |
169 | req->result--; |
324 | req->result--; |
|
|
325 | } |
|
|
326 | |
|
|
327 | rv = sv_2mortal (newRV_noinc ((SV *)av)); |
170 | } |
328 | } |
171 | |
329 | |
172 | rv = sv_2mortal (newRV_noinc ((SV *)av)); |
330 | PUSHs (rv); |
173 | } |
331 | } |
|
|
332 | break; |
174 | |
333 | |
175 | 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; |
176 | } |
372 | } |
177 | break; |
|
|
178 | |
373 | |
179 | case REQ_OPEN: |
|
|
180 | { |
|
|
181 | /* convert fd to fh */ |
|
|
182 | SV *fh; |
|
|
183 | |
374 | |
184 | XPUSHs (sv_2mortal (newSViv (req->result))); |
|
|
185 | PUTBACK; |
375 | PUTBACK; |
186 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
|
|
187 | SPAGAIN; |
|
|
188 | |
|
|
189 | fh = SvREFCNT_inc (POPs); |
|
|
190 | |
|
|
191 | PUSHMARK (SP); |
|
|
192 | XPUSHs (sv_2mortal (fh)); |
|
|
193 | } |
|
|
194 | break; |
|
|
195 | |
|
|
196 | case REQ_SLEEP: |
|
|
197 | case REQ_GROUP: |
|
|
198 | break; |
|
|
199 | |
|
|
200 | default: |
|
|
201 | XPUSHs (sv_2mortal (newSViv (req->result))); |
|
|
202 | break; |
|
|
203 | } |
|
|
204 | |
|
|
205 | |
|
|
206 | PUTBACK; |
|
|
207 | call_sv (req->callback, G_VOID | G_EVAL); |
376 | call_sv (req->callback, G_VOID | G_EVAL); |
208 | 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 | } |
209 | |
396 | |
210 | if (SvTRUE (ERRSV)) |
397 | if (SvTRUE (ERRSV)) |
211 | { |
398 | { |
212 | req_free (req); |
399 | req_free (req); |
213 | croak (0); |
400 | croak (0); |
214 | } |
401 | } |
215 | |
|
|
216 | LEAVE; |
|
|
217 | |
|
|
218 | errno = errorno; |
|
|
219 | } |
402 | } |
220 | |
403 | |
221 | static void req_free (aio_req req) |
404 | static void req_free (aio_req req) |
222 | { |
405 | { |
223 | if (req->grp) |
|
|
224 | { |
|
|
225 | aio_req grp = req->grp; |
|
|
226 | |
|
|
227 | /* unlink request */ |
|
|
228 | req->grp_next->grp_prev = req->grp_prev; |
|
|
229 | req->grp_prev->grp_next = req->grp_next; |
|
|
230 | |
|
|
231 | if (grp->grp_next == grp && grp->fd) |
|
|
232 | { |
|
|
233 | req_invoke (grp); |
|
|
234 | req_free (grp); |
|
|
235 | } |
|
|
236 | } |
|
|
237 | |
|
|
238 | if (req->self) |
406 | if (req->self) |
239 | { |
407 | { |
240 | sv_unmagic (req->self, PERL_MAGIC_ext); |
408 | sv_unmagic (req->self, PERL_MAGIC_ext); |
241 | SvREFCNT_dec (req->self); |
409 | SvREFCNT_dec (req->self); |
242 | } |
410 | } |
243 | |
411 | |
244 | if (req->data) |
|
|
245 | SvREFCNT_dec (req->data); |
412 | SvREFCNT_dec (req->data); |
246 | |
|
|
247 | if (req->fh) |
|
|
248 | SvREFCNT_dec (req->fh); |
413 | SvREFCNT_dec (req->fh); |
249 | |
|
|
250 | if (req->fh2) |
|
|
251 | SvREFCNT_dec (req->fh2); |
414 | SvREFCNT_dec (req->fh2); |
252 | |
|
|
253 | if (req->statdata) |
|
|
254 | Safefree (req->statdata); |
|
|
255 | |
|
|
256 | if (req->callback) |
|
|
257 | SvREFCNT_dec (req->callback); |
415 | SvREFCNT_dec (req->callback); |
|
|
416 | Safefree (req->statdata); |
258 | |
417 | |
259 | if (req->type == REQ_READDIR && req->result >= 0) |
418 | if (req->type == REQ_READDIR && req->result >= 0) |
260 | free (req->data2ptr); |
419 | free (req->data2ptr); |
261 | |
420 | |
262 | Safefree (req); |
421 | Safefree (req); |
263 | } |
422 | } |
264 | |
423 | |
265 | static void req_cancel (aio_req req) |
424 | static void req_cancel (aio_req req) |
266 | { |
425 | { |
267 | req->cancelled = 1; |
426 | req->flags |= FLAG_CANCELLED; |
268 | |
427 | |
269 | if (req->type == REQ_GROUP) |
428 | if (req->type == REQ_GROUP) |
270 | { |
429 | { |
271 | aio_req sub; |
430 | aio_req sub; |
272 | |
431 | |
273 | for (sub = req->grp_next; sub != req; sub = sub->grp_next) |
432 | for (sub = req->grp_first; sub; sub = sub->grp_next) |
274 | req_cancel (sub); |
433 | req_cancel (sub); |
275 | } |
434 | } |
276 | } |
435 | } |
277 | |
436 | |
278 | static int poll_cb () |
437 | static int poll_cb () |
… | |
… | |
283 | aio_req req; |
442 | aio_req req; |
284 | |
443 | |
285 | for (;;) |
444 | for (;;) |
286 | { |
445 | { |
287 | pthread_mutex_lock (&reslock); |
446 | pthread_mutex_lock (&reslock); |
288 | req = ress; |
447 | req = reqq_shift (&res_queue); |
289 | |
448 | |
290 | if (req) |
449 | if (req) |
291 | { |
450 | { |
292 | ress = req->next; |
|
|
293 | |
|
|
294 | if (!ress) |
451 | if (!res_queue.size) |
295 | { |
452 | { |
296 | /* read any signals sent by the worker threads */ |
453 | /* read any signals sent by the worker threads */ |
297 | char buf [32]; |
454 | char buf [32]; |
298 | while (read (respipe [0], buf, 32) == 32) |
455 | while (read (respipe [0], buf, 32) == 32) |
299 | ; |
456 | ; |
300 | |
|
|
301 | rese = 0; |
|
|
302 | } |
457 | } |
303 | } |
458 | } |
304 | |
459 | |
305 | pthread_mutex_unlock (&reslock); |
460 | pthread_mutex_unlock (&reslock); |
306 | |
461 | |
307 | if (!req) |
462 | if (!req) |
308 | break; |
463 | break; |
309 | |
464 | |
310 | nreqs--; |
465 | --nreqs; |
311 | |
466 | |
312 | if (req->type == REQ_QUIT) |
467 | if (req->type == REQ_QUIT) |
313 | started--; |
468 | started--; |
314 | else if (req->type == REQ_GROUP && req->grp_next != req) |
469 | else if (req->type == REQ_GROUP && req->length) |
315 | { |
470 | { |
316 | req->fd = 1; /* mark request as delayed */ |
471 | req->fd = 1; /* mark request as delayed */ |
317 | continue; |
472 | continue; |
318 | } |
473 | } |
319 | else |
474 | else |
… | |
… | |
366 | static void req_send (aio_req req) |
521 | static void req_send (aio_req req) |
367 | { |
522 | { |
368 | while (started < wanted && nreqs >= started) |
523 | while (started < wanted && nreqs >= started) |
369 | start_thread (); |
524 | start_thread (); |
370 | |
525 | |
371 | nreqs++; |
526 | ++nreqs; |
372 | |
527 | |
373 | pthread_mutex_lock (&reqlock); |
528 | pthread_mutex_lock (&reqlock); |
374 | |
529 | reqq_push (&req_queue, req); |
375 | req->next = 0; |
|
|
376 | |
|
|
377 | if (reqe) |
|
|
378 | { |
|
|
379 | reqe->next = req; |
|
|
380 | reqe = req; |
|
|
381 | } |
|
|
382 | else |
|
|
383 | reqe = reqs = req; |
|
|
384 | |
|
|
385 | pthread_cond_signal (&reqwait); |
530 | pthread_cond_signal (&reqwait); |
386 | pthread_mutex_unlock (&reqlock); |
531 | pthread_mutex_unlock (&reqlock); |
387 | |
532 | |
388 | if (nreqs > max_outstanding) |
533 | if (nreqs > max_outstanding) |
389 | for (;;) |
534 | for (;;) |
… | |
… | |
398 | } |
543 | } |
399 | |
544 | |
400 | static void end_thread (void) |
545 | static void end_thread (void) |
401 | { |
546 | { |
402 | aio_req req; |
547 | aio_req req; |
|
|
548 | |
403 | Newz (0, req, 1, aio_cb); |
549 | Newz (0, req, 1, aio_cb); |
|
|
550 | |
404 | req->type = REQ_QUIT; |
551 | req->type = REQ_QUIT; |
|
|
552 | req->pri = PRI_MAX + PRI_BIAS; |
405 | |
553 | |
406 | req_send (req); |
554 | req_send (req); |
407 | } |
555 | } |
408 | |
556 | |
409 | static void min_parallel (int nthreads) |
557 | static void min_parallel (int nthreads) |
… | |
… | |
496 | #if !HAVE_READAHEAD |
644 | #if !HAVE_READAHEAD |
497 | # define readahead aio_readahead |
645 | # define readahead aio_readahead |
498 | |
646 | |
499 | 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) |
500 | { |
648 | { |
501 | char readahead_buf[4096]; |
649 | dBUF; |
502 | |
650 | |
503 | while (count > 0) |
651 | while (count > 0) |
504 | { |
652 | { |
505 | size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); |
653 | size_t len = count < AIO_BUFSIZE ? count : AIO_BUFSIZE; |
506 | |
654 | |
507 | pread (fd, readahead_buf, len, offset); |
655 | pread (fd, aio_buf, len, offset); |
508 | offset += len; |
656 | offset += len; |
509 | count -= len; |
657 | count -= len; |
510 | } |
658 | } |
|
|
659 | |
|
|
660 | fBUF; |
511 | |
661 | |
512 | errno = 0; |
662 | errno = 0; |
513 | } |
663 | } |
514 | #endif |
664 | #endif |
515 | |
665 | |
… | |
… | |
601 | #endif |
751 | #endif |
602 | ) |
752 | ) |
603 | ) |
753 | ) |
604 | { |
754 | { |
605 | /* emulate sendfile. this is a major pain in the ass */ |
755 | /* emulate sendfile. this is a major pain in the ass */ |
606 | char buf[4096]; |
756 | dBUF; |
|
|
757 | |
607 | res = 0; |
758 | res = 0; |
608 | |
759 | |
609 | while (count) |
760 | while (count) |
610 | { |
761 | { |
611 | ssize_t cnt; |
762 | ssize_t cnt; |
612 | |
763 | |
613 | cnt = pread (ifd, buf, count > 4096 ? 4096 : count, offset); |
764 | cnt = pread (ifd, aio_buf, count > AIO_BUFSIZE ? AIO_BUFSIZE : count, offset); |
614 | |
765 | |
615 | if (cnt <= 0) |
766 | if (cnt <= 0) |
616 | { |
767 | { |
617 | if (cnt && !res) res = -1; |
768 | if (cnt && !res) res = -1; |
618 | break; |
769 | break; |
619 | } |
770 | } |
620 | |
771 | |
621 | cnt = write (ofd, buf, cnt); |
772 | cnt = write (ofd, aio_buf, cnt); |
622 | |
773 | |
623 | if (cnt <= 0) |
774 | if (cnt <= 0) |
624 | { |
775 | { |
625 | if (cnt && !res) res = -1; |
776 | if (cnt && !res) res = -1; |
626 | break; |
777 | break; |
… | |
… | |
628 | |
779 | |
629 | offset += cnt; |
780 | offset += cnt; |
630 | res += cnt; |
781 | res += cnt; |
631 | count -= cnt; |
782 | count -= cnt; |
632 | } |
783 | } |
|
|
784 | |
|
|
785 | fBUF; |
633 | } |
786 | } |
634 | |
787 | |
635 | return res; |
788 | return res; |
636 | } |
789 | } |
637 | |
790 | |
638 | /* read a full directory */ |
791 | /* read a full directory */ |
639 | static int scandir_ (const char *path, void **namesp) |
792 | static int scandir_ (const char *path, void **namesp) |
640 | { |
793 | { |
641 | DIR *dirp = opendir (path); |
794 | DIR *dirp; |
642 | union |
795 | union |
643 | { |
796 | { |
644 | struct dirent d; |
797 | struct dirent d; |
645 | char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; |
798 | char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; |
646 | } u; |
799 | } *u; |
647 | struct dirent *entp; |
800 | struct dirent *entp; |
648 | char *name, *names; |
801 | char *name, *names; |
649 | int memlen = 4096; |
802 | int memlen = 4096; |
650 | int memofs = 0; |
803 | int memofs = 0; |
651 | int res = 0; |
804 | int res = 0; |
652 | int errorno; |
805 | int errorno; |
653 | |
806 | |
|
|
807 | dirp = opendir (path); |
654 | if (!dirp) |
808 | if (!dirp) |
655 | return -1; |
809 | return -1; |
656 | |
810 | |
|
|
811 | u = malloc (sizeof (*u)); |
657 | names = malloc (memlen); |
812 | names = malloc (memlen); |
658 | |
813 | |
|
|
814 | if (u && names) |
659 | for (;;) |
815 | for (;;) |
660 | { |
816 | { |
|
|
817 | errno = 0; |
661 | errno = 0, readdir_r (dirp, &u.d, &entp); |
818 | readdir_r (dirp, &u->d, &entp); |
662 | |
819 | |
663 | if (!entp) |
820 | if (!entp) |
664 | break; |
821 | break; |
665 | |
822 | |
666 | name = entp->d_name; |
823 | name = entp->d_name; |
667 | |
824 | |
668 | if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) |
825 | if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) |
669 | { |
826 | { |
670 | int len = strlen (name) + 1; |
827 | int len = strlen (name) + 1; |
671 | |
828 | |
672 | res++; |
829 | res++; |
673 | |
830 | |
674 | while (memofs + len > memlen) |
831 | while (memofs + len > memlen) |
675 | { |
832 | { |
676 | memlen *= 2; |
833 | memlen *= 2; |
677 | names = realloc (names, memlen); |
834 | names = realloc (names, memlen); |
678 | if (!names) |
835 | if (!names) |
679 | break; |
836 | break; |
680 | } |
837 | } |
681 | |
838 | |
682 | memcpy (names + memofs, name, len); |
839 | memcpy (names + memofs, name, len); |
683 | memofs += len; |
840 | memofs += len; |
684 | } |
841 | } |
685 | } |
842 | } |
686 | |
843 | |
687 | errorno = errno; |
844 | errorno = errno; |
|
|
845 | free (u); |
688 | closedir (dirp); |
846 | closedir (dirp); |
689 | |
847 | |
690 | if (errorno) |
848 | if (errorno) |
691 | { |
849 | { |
692 | free (names); |
850 | free (names); |
… | |
… | |
709 | { |
867 | { |
710 | pthread_mutex_lock (&reqlock); |
868 | pthread_mutex_lock (&reqlock); |
711 | |
869 | |
712 | for (;;) |
870 | for (;;) |
713 | { |
871 | { |
714 | req = reqs; |
872 | req = reqq_shift (&req_queue); |
715 | |
|
|
716 | if (reqs) |
|
|
717 | { |
|
|
718 | reqs = reqs->next; |
|
|
719 | if (!reqs) reqe = 0; |
|
|
720 | } |
|
|
721 | |
873 | |
722 | if (req) |
874 | if (req) |
723 | break; |
875 | break; |
724 | |
876 | |
725 | pthread_cond_wait (&reqwait, &reqlock); |
877 | pthread_cond_wait (&reqwait, &reqlock); |
726 | } |
878 | } |
727 | |
879 | |
728 | pthread_mutex_unlock (&reqlock); |
880 | pthread_mutex_unlock (&reqlock); |
729 | |
881 | |
730 | errno = 0; /* strictly unnecessary */ |
882 | errno = 0; /* strictly unnecessary */ |
|
|
883 | type = req->type; /* remember type for QUIT check */ |
731 | |
884 | |
732 | if (!req->cancelled) |
885 | if (!(req->flags & FLAG_CANCELLED)) |
733 | switch (req->type) |
886 | switch (type) |
734 | { |
887 | { |
735 | 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; |
736 | 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; |
737 | |
890 | |
738 | 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; |
… | |
… | |
762 | tv.tv_usec = req->fd2; |
915 | tv.tv_usec = req->fd2; |
763 | |
916 | |
764 | req->result = select (0, 0, 0, 0, &tv); |
917 | req->result = select (0, 0, 0, 0, &tv); |
765 | } |
918 | } |
766 | |
919 | |
|
|
920 | case REQ_GROUP: |
|
|
921 | case REQ_NOP: |
767 | case REQ_QUIT: |
922 | case REQ_QUIT: |
768 | break; |
923 | break; |
769 | |
924 | |
770 | default: |
925 | default: |
771 | req->result = ENOSYS; |
926 | req->result = ENOSYS; |
… | |
… | |
774 | |
929 | |
775 | req->errorno = errno; |
930 | req->errorno = errno; |
776 | |
931 | |
777 | pthread_mutex_lock (&reslock); |
932 | pthread_mutex_lock (&reslock); |
778 | |
933 | |
779 | req->next = 0; |
934 | if (!reqq_push (&res_queue, req)) |
780 | |
|
|
781 | if (rese) |
|
|
782 | { |
|
|
783 | rese->next = req; |
|
|
784 | rese = req; |
|
|
785 | } |
|
|
786 | else |
|
|
787 | { |
|
|
788 | rese = ress = req; |
|
|
789 | |
|
|
790 | /* write a dummy byte to the pipe so fh becomes ready */ |
935 | /* write a dummy byte to the pipe so fh becomes ready */ |
791 | write (respipe [1], &respipe, 1); |
936 | write (respipe [1], &respipe, 1); |
792 | } |
|
|
793 | |
937 | |
794 | pthread_mutex_unlock (&reslock); |
938 | pthread_mutex_unlock (&reslock); |
795 | } |
939 | } |
796 | while (type != REQ_QUIT); |
940 | while (type != REQ_QUIT); |
797 | |
941 | |
… | |
… | |
828 | { |
972 | { |
829 | aio_req prv; |
973 | aio_req prv; |
830 | |
974 | |
831 | started = 0; |
975 | started = 0; |
832 | |
976 | |
833 | while (reqs) |
977 | while (prv = reqq_shift (&req_queue)) |
834 | { |
|
|
835 | prv = reqs; |
|
|
836 | reqs = prv->next; |
|
|
837 | req_free (prv); |
978 | req_free (prv); |
838 | } |
|
|
839 | |
979 | |
840 | reqs = reqe = 0; |
980 | while (prv = reqq_shift (&res_queue)) |
841 | |
|
|
842 | while (ress) |
|
|
843 | { |
|
|
844 | prv = ress; |
|
|
845 | ress = prv->next; |
|
|
846 | req_free (prv); |
981 | req_free (prv); |
847 | } |
982 | |
848 | |
|
|
849 | ress = rese = 0; |
|
|
850 | |
|
|
851 | close (respipe [0]); |
983 | close (respipe [0]); |
852 | close (respipe [1]); |
984 | close (respipe [1]); |
853 | create_pipe (); |
985 | create_pipe (); |
854 | |
986 | |
855 | atfork_parent (); |
987 | atfork_parent (); |
856 | } |
988 | } |
857 | |
989 | |
858 | #define dREQ \ |
990 | #define dREQ \ |
859 | aio_req req; \ |
991 | aio_req req; \ |
|
|
992 | int req_pri = next_pri; \ |
|
|
993 | next_pri = DEFAULT_PRI + PRI_BIAS; \ |
860 | \ |
994 | \ |
861 | if (SvOK (callback) && !SvROK (callback)) \ |
995 | if (SvOK (callback) && !SvROK (callback)) \ |
862 | croak ("callback must be undef or of reference type"); \ |
996 | croak ("callback must be undef or of reference type"); \ |
863 | \ |
997 | \ |
864 | Newz (0, req, 1, aio_cb); \ |
998 | Newz (0, req, 1, aio_cb); \ |
865 | if (!req) \ |
999 | if (!req) \ |
866 | croak ("out of memory during aio_req allocation"); \ |
1000 | croak ("out of memory during aio_req allocation"); \ |
867 | \ |
1001 | \ |
868 | req->callback = newSVsv (callback) |
1002 | req->callback = newSVsv (callback); \ |
|
|
1003 | req->pri = req_pri |
869 | |
1004 | |
870 | #define REQ_SEND \ |
1005 | #define REQ_SEND \ |
871 | req_send (req); \ |
1006 | req_send (req); \ |
872 | \ |
1007 | \ |
873 | if (GIMME_V != G_VOID) \ |
1008 | if (GIMME_V != G_VOID) \ |
… | |
… | |
1148 | SV * callback |
1283 | SV * callback |
1149 | PROTOTYPE: ;$ |
1284 | PROTOTYPE: ;$ |
1150 | PPCODE: |
1285 | PPCODE: |
1151 | { |
1286 | { |
1152 | dREQ; |
1287 | dREQ; |
|
|
1288 | |
1153 | req->type = REQ_GROUP; |
1289 | req->type = REQ_GROUP; |
1154 | req->grp_next = req; |
|
|
1155 | req->grp_prev = req; |
|
|
1156 | |
|
|
1157 | req_send (req); |
1290 | req_send (req); |
|
|
1291 | |
1158 | XPUSHs (req_sv (req, AIO_GRP_KLASS)); |
1292 | XPUSHs (req_sv (req, AIO_GRP_KLASS)); |
1159 | } |
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; |
1160 | |
1313 | |
1161 | void |
1314 | void |
1162 | flush () |
1315 | flush () |
1163 | PROTOTYPE: |
1316 | PROTOTYPE: |
1164 | CODE: |
1317 | CODE: |
… | |
… | |
1207 | CODE: |
1360 | CODE: |
1208 | RETVAL = nreqs; |
1361 | RETVAL = nreqs; |
1209 | OUTPUT: |
1362 | OUTPUT: |
1210 | RETVAL |
1363 | RETVAL |
1211 | |
1364 | |
|
|
1365 | PROTOTYPES: DISABLE |
|
|
1366 | |
1212 | MODULE = IO::AIO PACKAGE = IO::AIO::REQ |
1367 | MODULE = IO::AIO PACKAGE = IO::AIO::REQ |
1213 | |
1368 | |
1214 | void |
1369 | void |
1215 | cancel (aio_req_ornot req) |
1370 | cancel (aio_req_ornot req) |
1216 | PROTOTYPE: |
|
|
1217 | CODE: |
1371 | CODE: |
1218 | req_cancel (req); |
1372 | req_cancel (req); |
1219 | |
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 | |
1220 | MODULE = IO::AIO PACKAGE = IO::AIO::GRP |
1380 | MODULE = IO::AIO PACKAGE = IO::AIO::GRP |
1221 | |
1381 | |
1222 | void |
1382 | void |
1223 | add (aio_req grp, ...) |
1383 | add (aio_req grp, ...) |
1224 | PROTOTYPE: $;@ |
|
|
1225 | PPCODE: |
1384 | PPCODE: |
1226 | { |
1385 | { |
1227 | 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"); |
1228 | |
1391 | |
1229 | for (i = 1; i < items; ++i ) |
1392 | for (i = 1; i < items; ++i ) |
1230 | { |
1393 | { |
1231 | if (GIMME_V != G_VOID) |
1394 | if (GIMME_V != G_VOID) |
1232 | XPUSHs (sv_2mortal (newSVsv (ST (i)))); |
1395 | XPUSHs (sv_2mortal (newSVsv (ST (i)))); |
1233 | |
1396 | |
1234 | aio_req req = SvAIO_REQ (ST (i)); |
1397 | req = SvAIO_REQ (ST (i)); |
1235 | printf ("req = %p\n", req);//D |
|
|
1236 | |
1398 | |
1237 | if (req) |
1399 | if (req) |
1238 | { |
1400 | { |
1239 | req->grp_prev = grp; |
1401 | ++grp->length; |
1240 | req->grp_next = grp->grp_next; |
|
|
1241 | grp->grp_next->grp_prev = req; |
|
|
1242 | grp->grp_next = req; |
|
|
1243 | |
|
|
1244 | req->grp = grp; |
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; |
1245 | } |
1411 | } |
1246 | } |
1412 | } |
1247 | } |
1413 | } |
1248 | |
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 | |