… | |
… | |
9 | |
9 | |
10 | #include <pthread.h> |
10 | #include <pthread.h> |
11 | |
11 | |
12 | #include <stddef.h> |
12 | #include <stddef.h> |
13 | #include <errno.h> |
13 | #include <errno.h> |
|
|
14 | #include <sys/time.h> |
|
|
15 | #include <sys/select.h> |
14 | #include <sys/types.h> |
16 | #include <sys/types.h> |
15 | #include <sys/stat.h> |
17 | #include <sys/stat.h> |
16 | #include <limits.h> |
18 | #include <limits.h> |
17 | #include <unistd.h> |
19 | #include <unistd.h> |
18 | #include <fcntl.h> |
20 | #include <fcntl.h> |
… | |
… | |
53 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
55 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
54 | REQ_FSYNC, REQ_FDATASYNC, |
56 | REQ_FSYNC, REQ_FDATASYNC, |
55 | REQ_UNLINK, REQ_RMDIR, REQ_RENAME, |
57 | REQ_UNLINK, REQ_RMDIR, REQ_RENAME, |
56 | REQ_READDIR, |
58 | REQ_READDIR, |
57 | REQ_LINK, REQ_SYMLINK, |
59 | REQ_LINK, REQ_SYMLINK, |
|
|
60 | REQ_GROUP, REQ_NOP, |
|
|
61 | REQ_SLEEP, |
58 | }; |
62 | }; |
59 | |
63 | |
60 | #define AIO_CB_KLASS "IO::AIO::CB" |
64 | #define AIO_REQ_KLASS "IO::AIO::REQ" |
|
|
65 | #define AIO_GRP_KLASS "IO::AIO::GRP" |
61 | |
66 | |
62 | typedef struct aio_cb |
67 | typedef struct aio_cb |
63 | { |
68 | { |
64 | struct aio_cb *grp_prev, *grp_next; |
|
|
65 | struct aio_grp *grp; |
|
|
66 | |
|
|
67 | struct aio_cb *volatile next; |
69 | struct aio_cb *volatile next; |
|
|
70 | |
|
|
71 | struct aio_cb *grp, *grp_prev, *grp_next, *grp_first; |
68 | |
72 | |
69 | SV *self; /* the perl counterpart of this request, if any */ |
73 | SV *self; /* the perl counterpart of this request, if any */ |
70 | |
74 | |
71 | SV *data, *callback; |
75 | SV *data, *callback; |
72 | SV *fh, *fh2; |
76 | SV *fh, *fh2; |
… | |
… | |
97 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
101 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
98 | |
102 | |
99 | static volatile aio_req reqs, reqe; /* queue start, queue end */ |
103 | static volatile aio_req reqs, reqe; /* queue start, queue end */ |
100 | static volatile aio_req ress, rese; /* queue start, queue end */ |
104 | static volatile aio_req ress, rese; /* queue start, queue end */ |
101 | |
105 | |
102 | typedef struct aio_grp |
106 | static void req_invoke (aio_req req); |
103 | { |
107 | static void req_free (aio_req req); |
104 | struct aio_cb *first, *last; |
|
|
105 | SV *callback; |
|
|
106 | int busycount; |
|
|
107 | } aio_grp; |
|
|
108 | |
|
|
109 | static void aio_grp_begin (aio_grp *grp) |
|
|
110 | { |
|
|
111 | ++grp->busycount; |
|
|
112 | } |
|
|
113 | |
|
|
114 | static void aio_grp_end (aio_grp *grp) |
|
|
115 | { |
|
|
116 | --grp->busycount; |
|
|
117 | |
|
|
118 | if (grp->busycount) |
|
|
119 | return; |
|
|
120 | |
|
|
121 | SvREFCNT_dec (grp->callback); |
|
|
122 | grp->callback = 0; |
|
|
123 | } |
|
|
124 | |
|
|
125 | static aio_grp *aio_grp_new () |
|
|
126 | { |
|
|
127 | aio_grp *grp; |
|
|
128 | |
|
|
129 | Newz (0, grp, 1, aio_grp); |
|
|
130 | aio_grp_begin (grp); |
|
|
131 | |
|
|
132 | return grp; |
|
|
133 | } |
|
|
134 | |
108 | |
135 | /* must be called at most once */ |
109 | /* must be called at most once */ |
136 | static SV *req_sv (aio_req req) |
110 | static SV *req_sv (aio_req req, const char *klass) |
137 | { |
111 | { |
|
|
112 | if (!req->self) |
|
|
113 | { |
138 | req->self = (SV *)newHV (); |
114 | req->self = (SV *)newHV (); |
139 | sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0); |
115 | sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0); |
|
|
116 | } |
140 | |
117 | |
141 | return sv_bless (newRV_noinc (req->self), gv_stashpv (AIO_CB_KLASS, 1)); |
118 | return sv_2mortal (sv_bless (newRV_inc (req->self), gv_stashpv (klass, 1))); |
142 | } |
119 | } |
143 | |
120 | |
144 | static aio_req SvAIO_REQ (SV *sv) |
121 | static aio_req SvAIO_REQ (SV *sv) |
145 | { |
122 | { |
|
|
123 | MAGIC *mg; |
|
|
124 | |
146 | if (!sv_derived_from (sv, AIO_CB_KLASS) || !SvROK (sv)) |
125 | if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv)) |
147 | croak ("object of class " AIO_CB_KLASS " expected"); |
126 | croak ("object of class " AIO_REQ_KLASS " expected"); |
148 | |
127 | |
149 | MAGIC *mg = mg_find (SvRV (sv), PERL_MAGIC_ext); |
128 | mg = mg_find (SvRV (sv), PERL_MAGIC_ext); |
150 | |
129 | |
151 | return mg ? (aio_req)mg->mg_ptr : 0; |
130 | return mg ? (aio_req)mg->mg_ptr : 0; |
152 | } |
131 | } |
153 | |
132 | |
154 | static void req_free (aio_req req) |
133 | static void aio_grp_feed (aio_req grp) |
155 | { |
134 | { |
156 | if (req->self) |
135 | while (grp->length < grp->fd2) |
157 | { |
|
|
158 | sv_unmagic (req->self, PERL_MAGIC_ext); |
|
|
159 | SvREFCNT_dec (req->self); |
|
|
160 | } |
136 | { |
|
|
137 | int old_len = grp->length; |
161 | |
138 | |
162 | if (req->data) |
139 | if (grp->fh2 && SvOK (grp->fh2)) |
163 | SvREFCNT_dec (req->data); |
140 | { |
|
|
141 | dSP; |
164 | |
142 | |
165 | if (req->fh) |
143 | ENTER; |
166 | SvREFCNT_dec (req->fh); |
144 | SAVETMPS; |
|
|
145 | PUSHMARK (SP); |
|
|
146 | XPUSHs (req_sv (grp, AIO_GRP_KLASS)); |
|
|
147 | PUTBACK; |
|
|
148 | call_sv (grp->fh2, G_VOID | G_EVAL); |
|
|
149 | SPAGAIN; |
|
|
150 | FREETMPS; |
|
|
151 | LEAVE; |
|
|
152 | } |
167 | |
153 | |
168 | if (req->fh2) |
154 | /* stop if no progress has been made */ |
|
|
155 | if (old_len == grp->length) |
|
|
156 | { |
169 | SvREFCNT_dec (req->fh2); |
157 | SvREFCNT_dec (grp->fh2); |
170 | |
158 | grp->fh2 = 0; |
171 | if (req->statdata) |
159 | break; |
172 | Safefree (req->statdata); |
160 | } |
173 | |
161 | } |
174 | if (req->callback) |
|
|
175 | SvREFCNT_dec (req->callback); |
|
|
176 | |
|
|
177 | if (req->type == REQ_READDIR && req->result >= 0) |
|
|
178 | free (req->data2ptr); |
|
|
179 | |
|
|
180 | Safefree (req); |
|
|
181 | } |
162 | } |
182 | |
163 | |
183 | static void |
164 | static void aio_grp_dec (aio_req grp) |
184 | poll_wait () |
165 | { |
|
|
166 | --grp->length; |
|
|
167 | |
|
|
168 | /* call feeder, if applicable */ |
|
|
169 | aio_grp_feed (grp); |
|
|
170 | |
|
|
171 | /* finish, if done */ |
|
|
172 | if (!grp->length && grp->fd) |
|
|
173 | { |
|
|
174 | req_invoke (grp); |
|
|
175 | req_free (grp); |
|
|
176 | } |
|
|
177 | } |
|
|
178 | |
|
|
179 | static void poll_wait () |
185 | { |
180 | { |
186 | if (nreqs && !ress) |
181 | if (nreqs && !ress) |
187 | { |
182 | { |
188 | fd_set rfd; |
183 | fd_set rfd; |
189 | FD_ZERO(&rfd); |
184 | FD_ZERO(&rfd); |
… | |
… | |
191 | |
186 | |
192 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
187 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
193 | } |
188 | } |
194 | } |
189 | } |
195 | |
190 | |
196 | static int |
191 | static void req_invoke (aio_req req) |
197 | poll_cb () |
192 | { |
|
|
193 | dSP; |
|
|
194 | int errorno = errno; |
|
|
195 | |
|
|
196 | if (req->cancelled || !SvOK (req->callback)) |
|
|
197 | return; |
|
|
198 | |
|
|
199 | errno = req->errorno; |
|
|
200 | |
|
|
201 | ENTER; |
|
|
202 | SAVETMPS; |
|
|
203 | PUSHMARK (SP); |
|
|
204 | EXTEND (SP, 1); |
|
|
205 | |
|
|
206 | switch (req->type) |
|
|
207 | { |
|
|
208 | case REQ_READDIR: |
|
|
209 | { |
|
|
210 | SV *rv = &PL_sv_undef; |
|
|
211 | |
|
|
212 | if (req->result >= 0) |
|
|
213 | { |
|
|
214 | char *buf = req->data2ptr; |
|
|
215 | AV *av = newAV (); |
|
|
216 | |
|
|
217 | while (req->result) |
|
|
218 | { |
|
|
219 | SV *sv = newSVpv (buf, 0); |
|
|
220 | |
|
|
221 | av_push (av, sv); |
|
|
222 | buf += SvCUR (sv) + 1; |
|
|
223 | req->result--; |
|
|
224 | } |
|
|
225 | |
|
|
226 | rv = sv_2mortal (newRV_noinc ((SV *)av)); |
|
|
227 | } |
|
|
228 | |
|
|
229 | PUSHs (rv); |
|
|
230 | } |
|
|
231 | break; |
|
|
232 | |
|
|
233 | case REQ_OPEN: |
|
|
234 | { |
|
|
235 | /* convert fd to fh */ |
|
|
236 | SV *fh; |
|
|
237 | |
|
|
238 | PUSHs (sv_2mortal (newSViv (req->result))); |
|
|
239 | 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); |
|
|
276 | SPAGAIN; |
|
|
277 | |
|
|
278 | FREETMPS; |
|
|
279 | LEAVE; |
|
|
280 | |
|
|
281 | errno = errorno; |
|
|
282 | |
|
|
283 | if (SvTRUE (ERRSV)) |
|
|
284 | { |
|
|
285 | req_free (req); |
|
|
286 | croak (0); |
|
|
287 | } |
|
|
288 | } |
|
|
289 | |
|
|
290 | static void req_free (aio_req req) |
|
|
291 | { |
|
|
292 | if (req->grp) |
|
|
293 | { |
|
|
294 | aio_req grp = req->grp; |
|
|
295 | |
|
|
296 | /* unlink request */ |
|
|
297 | if (req->grp_next) req->grp_next->grp_prev = req->grp_prev; |
|
|
298 | if (req->grp_prev) req->grp_prev->grp_next = req->grp_next; |
|
|
299 | |
|
|
300 | if (grp->grp_first == req) |
|
|
301 | grp->grp_first = req->grp_next; |
|
|
302 | |
|
|
303 | aio_grp_dec (grp); |
|
|
304 | } |
|
|
305 | |
|
|
306 | if (req->self) |
|
|
307 | { |
|
|
308 | sv_unmagic (req->self, PERL_MAGIC_ext); |
|
|
309 | SvREFCNT_dec (req->self); |
|
|
310 | } |
|
|
311 | |
|
|
312 | SvREFCNT_dec (req->data); |
|
|
313 | SvREFCNT_dec (req->fh); |
|
|
314 | SvREFCNT_dec (req->fh2); |
|
|
315 | SvREFCNT_dec (req->callback); |
|
|
316 | Safefree (req->statdata); |
|
|
317 | |
|
|
318 | if (req->type == REQ_READDIR && req->result >= 0) |
|
|
319 | free (req->data2ptr); |
|
|
320 | |
|
|
321 | Safefree (req); |
|
|
322 | } |
|
|
323 | |
|
|
324 | static void req_cancel (aio_req req) |
|
|
325 | { |
|
|
326 | req->cancelled = 1; |
|
|
327 | |
|
|
328 | if (req->type == REQ_GROUP) |
|
|
329 | { |
|
|
330 | aio_req sub; |
|
|
331 | |
|
|
332 | for (sub = req->grp_first; sub; sub = sub->grp_next) |
|
|
333 | req_cancel (sub); |
|
|
334 | } |
|
|
335 | } |
|
|
336 | |
|
|
337 | static int poll_cb () |
198 | { |
338 | { |
199 | dSP; |
339 | dSP; |
200 | int count = 0; |
340 | int count = 0; |
201 | int do_croak = 0; |
341 | int do_croak = 0; |
202 | aio_req req; |
342 | aio_req req; |
… | |
… | |
224 | pthread_mutex_unlock (&reslock); |
364 | pthread_mutex_unlock (&reslock); |
225 | |
365 | |
226 | if (!req) |
366 | if (!req) |
227 | break; |
367 | break; |
228 | |
368 | |
229 | nreqs--; |
369 | --nreqs; |
230 | |
370 | |
231 | if (req->type == REQ_QUIT) |
371 | if (req->type == REQ_QUIT) |
232 | started--; |
372 | started--; |
|
|
373 | else if (req->type == REQ_GROUP && req->length) |
|
|
374 | { |
|
|
375 | req->fd = 1; /* mark request as delayed */ |
|
|
376 | continue; |
|
|
377 | } |
233 | else |
378 | else |
234 | { |
379 | { |
235 | int errorno = errno; |
|
|
236 | errno = req->errorno; |
|
|
237 | |
|
|
238 | if (req->type == REQ_READ) |
380 | if (req->type == REQ_READ) |
239 | SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); |
381 | SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); |
240 | |
382 | |
241 | if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE)) |
383 | if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE)) |
242 | SvREADONLY_off (req->data); |
384 | SvREADONLY_off (req->data); |
… | |
… | |
246 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
388 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
247 | PL_laststatval = req->result; |
389 | PL_laststatval = req->result; |
248 | PL_statcache = *(req->statdata); |
390 | PL_statcache = *(req->statdata); |
249 | } |
391 | } |
250 | |
392 | |
251 | ENTER; |
393 | req_invoke (req); |
252 | PUSHMARK (SP); |
|
|
253 | |
394 | |
254 | if (req->type == REQ_READDIR) |
|
|
255 | { |
|
|
256 | SV *rv = &PL_sv_undef; |
|
|
257 | |
|
|
258 | if (req->result >= 0) |
|
|
259 | { |
|
|
260 | char *buf = req->data2ptr; |
|
|
261 | AV *av = newAV (); |
|
|
262 | |
|
|
263 | while (req->result) |
|
|
264 | { |
|
|
265 | SV *sv = newSVpv (buf, 0); |
|
|
266 | |
|
|
267 | av_push (av, sv); |
|
|
268 | buf += SvCUR (sv) + 1; |
|
|
269 | req->result--; |
|
|
270 | } |
|
|
271 | |
|
|
272 | rv = sv_2mortal (newRV_noinc ((SV *)av)); |
|
|
273 | } |
|
|
274 | |
|
|
275 | XPUSHs (rv); |
|
|
276 | } |
|
|
277 | else |
|
|
278 | { |
|
|
279 | XPUSHs (sv_2mortal (newSViv (req->result))); |
|
|
280 | |
|
|
281 | if (req->type == REQ_OPEN) |
|
|
282 | { |
|
|
283 | /* convert fd to fh */ |
|
|
284 | SV *fh; |
|
|
285 | |
|
|
286 | PUTBACK; |
|
|
287 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
|
|
288 | SPAGAIN; |
|
|
289 | |
|
|
290 | fh = SvREFCNT_inc (POPs); |
|
|
291 | |
|
|
292 | PUSHMARK (SP); |
|
|
293 | XPUSHs (sv_2mortal (fh)); |
|
|
294 | } |
|
|
295 | } |
|
|
296 | |
|
|
297 | if (SvOK (req->callback) && !req->cancelled) |
|
|
298 | { |
|
|
299 | PUTBACK; |
|
|
300 | call_sv (req->callback, G_VOID | G_EVAL); |
|
|
301 | SPAGAIN; |
|
|
302 | |
|
|
303 | if (SvTRUE (ERRSV)) |
|
|
304 | { |
|
|
305 | req_free (req); |
|
|
306 | croak (0); |
|
|
307 | } |
|
|
308 | } |
|
|
309 | |
|
|
310 | LEAVE; |
|
|
311 | |
|
|
312 | errno = errorno; |
|
|
313 | count++; |
395 | count++; |
314 | } |
396 | } |
315 | |
397 | |
316 | req_free (req); |
398 | req_free (req); |
317 | } |
399 | } |
… | |
… | |
319 | return count; |
401 | return count; |
320 | } |
402 | } |
321 | |
403 | |
322 | static void *aio_proc(void *arg); |
404 | static void *aio_proc(void *arg); |
323 | |
405 | |
324 | static void |
|
|
325 | start_thread (void) |
406 | static void start_thread (void) |
326 | { |
407 | { |
327 | sigset_t fullsigset, oldsigset; |
408 | sigset_t fullsigset, oldsigset; |
328 | pthread_t tid; |
409 | pthread_t tid; |
329 | pthread_attr_t attr; |
410 | pthread_attr_t attr; |
330 | |
411 | |
… | |
… | |
339 | started++; |
420 | started++; |
340 | |
421 | |
341 | sigprocmask (SIG_SETMASK, &oldsigset, 0); |
422 | sigprocmask (SIG_SETMASK, &oldsigset, 0); |
342 | } |
423 | } |
343 | |
424 | |
344 | static void |
|
|
345 | req_send (aio_req req) |
425 | static void req_send (aio_req req) |
346 | { |
426 | { |
347 | while (started < wanted && nreqs >= started) |
427 | while (started < wanted && nreqs >= started) |
348 | start_thread (); |
428 | start_thread (); |
349 | |
429 | |
350 | nreqs++; |
430 | ++nreqs; |
351 | |
431 | |
352 | pthread_mutex_lock (&reqlock); |
432 | pthread_mutex_lock (&reqlock); |
353 | |
433 | |
354 | req->next = 0; |
434 | req->next = 0; |
355 | |
435 | |
… | |
… | |
374 | |
454 | |
375 | poll_wait (); |
455 | poll_wait (); |
376 | } |
456 | } |
377 | } |
457 | } |
378 | |
458 | |
379 | static void |
459 | static void end_thread (void) |
380 | end_thread (void) |
|
|
381 | { |
460 | { |
382 | aio_req req; |
461 | aio_req req; |
383 | Newz (0, req, 1, aio_cb); |
462 | Newz (0, req, 1, aio_cb); |
384 | req->type = REQ_QUIT; |
463 | req->type = REQ_QUIT; |
385 | |
464 | |
… | |
… | |
436 | * normal read/write by using a mutex. slows down execution a lot, |
515 | * normal read/write by using a mutex. slows down execution a lot, |
437 | * but that's your problem, not mine. |
516 | * but that's your problem, not mine. |
438 | */ |
517 | */ |
439 | static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER; |
518 | static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER; |
440 | |
519 | |
441 | static ssize_t |
520 | static ssize_t pread (int fd, void *buf, size_t count, off_t offset) |
442 | pread (int fd, void *buf, size_t count, off_t offset) |
|
|
443 | { |
521 | { |
444 | ssize_t res; |
522 | ssize_t res; |
445 | off_t ooffset; |
523 | off_t ooffset; |
446 | |
524 | |
447 | pthread_mutex_lock (&preadwritelock); |
525 | pthread_mutex_lock (&preadwritelock); |
… | |
… | |
452 | pthread_mutex_unlock (&preadwritelock); |
530 | pthread_mutex_unlock (&preadwritelock); |
453 | |
531 | |
454 | return res; |
532 | return res; |
455 | } |
533 | } |
456 | |
534 | |
457 | static ssize_t |
|
|
458 | pwrite (int fd, void *buf, size_t count, off_t offset) |
535 | static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset) |
459 | { |
536 | { |
460 | ssize_t res; |
537 | ssize_t res; |
461 | off_t ooffset; |
538 | off_t ooffset; |
462 | |
539 | |
463 | pthread_mutex_lock (&preadwritelock); |
540 | pthread_mutex_lock (&preadwritelock); |
… | |
… | |
476 | #endif |
553 | #endif |
477 | |
554 | |
478 | #if !HAVE_READAHEAD |
555 | #if !HAVE_READAHEAD |
479 | # define readahead aio_readahead |
556 | # define readahead aio_readahead |
480 | |
557 | |
481 | static ssize_t |
|
|
482 | readahead (int fd, off_t offset, size_t count) |
558 | static ssize_t readahead (int fd, off_t offset, size_t count) |
483 | { |
559 | { |
484 | char readahead_buf[4096]; |
560 | char readahead_buf[4096]; |
485 | |
561 | |
486 | while (count > 0) |
562 | while (count > 0) |
487 | { |
563 | { |
… | |
… | |
499 | #if !HAVE_READDIR_R |
575 | #if !HAVE_READDIR_R |
500 | # define readdir_r aio_readdir_r |
576 | # define readdir_r aio_readdir_r |
501 | |
577 | |
502 | static pthread_mutex_t readdirlock = PTHREAD_MUTEX_INITIALIZER; |
578 | static pthread_mutex_t readdirlock = PTHREAD_MUTEX_INITIALIZER; |
503 | |
579 | |
504 | static int |
|
|
505 | readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) |
580 | static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) |
506 | { |
581 | { |
507 | struct dirent *e; |
582 | struct dirent *e; |
508 | int errorno; |
583 | int errorno; |
509 | |
584 | |
510 | pthread_mutex_lock (&readdirlock); |
585 | pthread_mutex_lock (&readdirlock); |
… | |
… | |
526 | return e ? 0 : -1; |
601 | return e ? 0 : -1; |
527 | } |
602 | } |
528 | #endif |
603 | #endif |
529 | |
604 | |
530 | /* sendfile always needs emulation */ |
605 | /* sendfile always needs emulation */ |
531 | static ssize_t |
|
|
532 | sendfile_ (int ofd, int ifd, off_t offset, size_t count) |
606 | static ssize_t sendfile_ (int ofd, int ifd, off_t offset, size_t count) |
533 | { |
607 | { |
534 | ssize_t res; |
608 | ssize_t res; |
535 | |
609 | |
536 | if (!count) |
610 | if (!count) |
537 | return 0; |
611 | return 0; |
… | |
… | |
619 | |
693 | |
620 | return res; |
694 | return res; |
621 | } |
695 | } |
622 | |
696 | |
623 | /* read a full directory */ |
697 | /* read a full directory */ |
624 | static int |
|
|
625 | scandir_ (const char *path, void **namesp) |
698 | static int scandir_ (const char *path, void **namesp) |
626 | { |
699 | { |
627 | DIR *dirp = opendir (path); |
700 | DIR *dirp = opendir (path); |
628 | union |
701 | union |
629 | { |
702 | { |
630 | struct dirent d; |
703 | struct dirent d; |
… | |
… | |
684 | return res; |
757 | return res; |
685 | } |
758 | } |
686 | |
759 | |
687 | /*****************************************************************************/ |
760 | /*****************************************************************************/ |
688 | |
761 | |
689 | static void * |
|
|
690 | aio_proc (void *thr_arg) |
762 | static void *aio_proc (void *thr_arg) |
691 | { |
763 | { |
692 | aio_req req; |
764 | aio_req req; |
693 | int type; |
765 | int type; |
694 | |
766 | |
695 | do |
767 | do |
… | |
… | |
715 | pthread_mutex_unlock (&reqlock); |
787 | pthread_mutex_unlock (&reqlock); |
716 | |
788 | |
717 | errno = 0; /* strictly unnecessary */ |
789 | errno = 0; /* strictly unnecessary */ |
718 | |
790 | |
719 | if (!req->cancelled) |
791 | if (!req->cancelled) |
720 | switch (req->type) |
792 | switch (type = req->type) /* remember type for QUIT check */ |
721 | { |
793 | { |
722 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
794 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
723 | case REQ_WRITE: req->result = pwrite (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; |
724 | |
796 | |
725 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
797 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
… | |
… | |
739 | |
811 | |
740 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
812 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
741 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
813 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
742 | case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break; |
814 | case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break; |
743 | |
815 | |
|
|
816 | case REQ_SLEEP: |
|
|
817 | { |
|
|
818 | struct timeval tv; |
|
|
819 | |
|
|
820 | tv.tv_sec = req->fd; |
|
|
821 | tv.tv_usec = req->fd2; |
|
|
822 | |
|
|
823 | req->result = select (0, 0, 0, 0, &tv); |
|
|
824 | } |
|
|
825 | |
|
|
826 | case REQ_GROUP: |
|
|
827 | case REQ_NOP: |
744 | case REQ_QUIT: |
828 | case REQ_QUIT: |
745 | break; |
829 | break; |
746 | |
830 | |
747 | default: |
831 | default: |
748 | req->result = ENOSYS; |
832 | req->result = ENOSYS; |
… | |
… | |
846 | |
930 | |
847 | #define REQ_SEND \ |
931 | #define REQ_SEND \ |
848 | req_send (req); \ |
932 | req_send (req); \ |
849 | \ |
933 | \ |
850 | if (GIMME_V != G_VOID) \ |
934 | if (GIMME_V != G_VOID) \ |
851 | XPUSHs (req_sv (req)); |
935 | XPUSHs (req_sv (req, AIO_REQ_KLASS)); |
852 | |
936 | |
853 | MODULE = IO::AIO PACKAGE = IO::AIO |
937 | MODULE = IO::AIO PACKAGE = IO::AIO |
854 | |
938 | |
855 | PROTOTYPES: ENABLE |
939 | PROTOTYPES: ENABLE |
856 | |
940 | |
… | |
… | |
1103 | req->dataptr = SvPVbyte_nolen (req->data); |
1187 | req->dataptr = SvPVbyte_nolen (req->data); |
1104 | |
1188 | |
1105 | REQ_SEND; |
1189 | REQ_SEND; |
1106 | } |
1190 | } |
1107 | |
1191 | |
1108 | #if 0 |
|
|
1109 | |
|
|
1110 | # undocumented, because it does not cancel active requests |
|
|
1111 | void |
1192 | void |
1112 | cancel_most_requests () |
1193 | aio_sleep (delay,callback=&PL_sv_undef) |
|
|
1194 | double delay |
|
|
1195 | SV * callback |
|
|
1196 | PPCODE: |
|
|
1197 | { |
|
|
1198 | dREQ; |
|
|
1199 | |
|
|
1200 | req->type = REQ_SLEEP; |
|
|
1201 | req->fd = delay < 0. ? 0 : delay; |
|
|
1202 | req->fd2 = delay < 0. ? 0 : 1000. * (delay - req->fd); |
|
|
1203 | |
|
|
1204 | REQ_SEND; |
|
|
1205 | } |
|
|
1206 | |
|
|
1207 | void |
|
|
1208 | aio_group (callback=&PL_sv_undef) |
|
|
1209 | SV * callback |
1113 | PROTOTYPE: |
1210 | PROTOTYPE: ;$ |
1114 | CODE: |
1211 | PPCODE: |
1115 | { |
1212 | { |
1116 | aio_req *req; |
1213 | dREQ; |
1117 | |
1214 | req->type = REQ_GROUP; |
1118 | pthread_mutex_lock (&reqlock); |
1215 | req_send (req); |
1119 | for (req = reqs; req; req = req->next) |
1216 | XPUSHs (req_sv (req, AIO_GRP_KLASS)); |
1120 | req->flags |= 1; |
|
|
1121 | pthread_mutex_unlock (&reqlock); |
|
|
1122 | |
|
|
1123 | pthread_mutex_lock (&reslock); |
|
|
1124 | for (req = ress; req; req = req->next) |
|
|
1125 | req->flags |= 1; |
|
|
1126 | pthread_mutex_unlock (&reslock); |
|
|
1127 | } |
1217 | } |
1128 | |
1218 | |
1129 | #endif |
1219 | void |
|
|
1220 | aio_nop (callback=&PL_sv_undef) |
|
|
1221 | SV * callback |
|
|
1222 | PPCODE: |
|
|
1223 | { |
|
|
1224 | dREQ; |
|
|
1225 | |
|
|
1226 | req->type = REQ_NOP; |
|
|
1227 | |
|
|
1228 | REQ_SEND; |
|
|
1229 | } |
1130 | |
1230 | |
1131 | void |
1231 | void |
1132 | flush () |
1232 | flush () |
1133 | PROTOTYPE: |
1233 | PROTOTYPE: |
1134 | CODE: |
1234 | CODE: |
… | |
… | |
1177 | CODE: |
1277 | CODE: |
1178 | RETVAL = nreqs; |
1278 | RETVAL = nreqs; |
1179 | OUTPUT: |
1279 | OUTPUT: |
1180 | RETVAL |
1280 | RETVAL |
1181 | |
1281 | |
|
|
1282 | PROTOTYPES: DISABLE |
|
|
1283 | |
1182 | MODULE = IO::AIO PACKAGE = IO::AIO::CB |
1284 | MODULE = IO::AIO PACKAGE = IO::AIO::REQ |
1183 | |
1285 | |
1184 | void |
1286 | void |
1185 | cancel (aio_req_ornot req) |
1287 | cancel (aio_req_ornot req) |
1186 | PROTOTYPE: |
1288 | PROTOTYPE: |
1187 | CODE: |
1289 | CODE: |
1188 | req->cancelled = 1; |
1290 | req_cancel (req); |
1189 | |
1291 | |
|
|
1292 | void |
|
|
1293 | cb (aio_req req, SV *callback=&PL_sv_undef) |
|
|
1294 | CODE: |
|
|
1295 | SvREFCNT_dec (req->callback); |
|
|
1296 | req->callback = newSVsv (callback); |
|
|
1297 | |
|
|
1298 | MODULE = IO::AIO PACKAGE = IO::AIO::GRP |
|
|
1299 | |
|
|
1300 | void |
|
|
1301 | add (aio_req grp, ...) |
|
|
1302 | PPCODE: |
|
|
1303 | { |
|
|
1304 | int i; |
|
|
1305 | aio_req req; |
|
|
1306 | |
|
|
1307 | if (grp->fd == 2) |
|
|
1308 | croak ("cannot add requests to IO::AIO::GRP after the group finished"); |
|
|
1309 | |
|
|
1310 | for (i = 1; i < items; ++i ) |
|
|
1311 | { |
|
|
1312 | if (GIMME_V != G_VOID) |
|
|
1313 | XPUSHs (sv_2mortal (newSVsv (ST (i)))); |
|
|
1314 | |
|
|
1315 | req = SvAIO_REQ (ST (i)); |
|
|
1316 | |
|
|
1317 | if (req) |
|
|
1318 | { |
|
|
1319 | ++grp->length; |
|
|
1320 | req->grp = grp; |
|
|
1321 | |
|
|
1322 | req->grp_prev = 0; |
|
|
1323 | req->grp_next = grp->grp_first; |
|
|
1324 | |
|
|
1325 | if (grp->grp_first) |
|
|
1326 | grp->grp_first->grp_prev = req; |
|
|
1327 | |
|
|
1328 | grp->grp_first = req; |
|
|
1329 | } |
|
|
1330 | } |
|
|
1331 | } |
|
|
1332 | |
|
|
1333 | void |
|
|
1334 | result (aio_req grp, ...) |
|
|
1335 | CODE: |
|
|
1336 | { |
|
|
1337 | int i; |
|
|
1338 | AV *av = newAV (); |
|
|
1339 | |
|
|
1340 | for (i = 1; i < items; ++i ) |
|
|
1341 | av_push (av, newSVsv (ST (i))); |
|
|
1342 | |
|
|
1343 | SvREFCNT_dec (grp->data); |
|
|
1344 | grp->data = (SV *)av; |
|
|
1345 | } |
|
|
1346 | |
|
|
1347 | void |
|
|
1348 | feed_limit (aio_req grp, int limit) |
|
|
1349 | CODE: |
|
|
1350 | grp->fd2 = limit; |
|
|
1351 | aio_grp_feed (grp); |
|
|
1352 | |
|
|
1353 | void |
|
|
1354 | feed (aio_req grp, SV *callback=&PL_sv_undef) |
|
|
1355 | CODE: |
|
|
1356 | { |
|
|
1357 | SvREFCNT_dec (grp->fh2); |
|
|
1358 | grp->fh2 = newSVsv (callback); |
|
|
1359 | |
|
|
1360 | if (grp->fd2 <= 0) |
|
|
1361 | grp->fd2 = 2; |
|
|
1362 | |
|
|
1363 | aio_grp_feed (grp); |
|
|
1364 | } |
|
|
1365 | |