… | |
… | |
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_SLEEP, |
|
|
61 | REQ_GROUP, |
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_free (aio_req req); |
103 | { |
|
|
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 | |
107 | |
135 | /* must be called at most once */ |
108 | /* must be called at most once */ |
136 | static SV *req_sv (aio_req req) |
109 | static SV *req_sv (aio_req req, const char *klass) |
137 | { |
110 | { |
|
|
111 | if (!req->self) |
|
|
112 | { |
138 | req->self = (SV *)newHV (); |
113 | req->self = (SV *)newHV (); |
139 | sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0); |
114 | sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0); |
|
|
115 | } |
140 | |
116 | |
141 | return sv_bless (newRV_noinc (req->self), gv_stashpv (AIO_CB_KLASS, 1)); |
117 | return sv_2mortal (sv_bless (newRV_inc (req->self), gv_stashpv (klass, 1))); |
142 | } |
118 | } |
143 | |
119 | |
144 | static aio_req SvAIO_REQ (SV *sv) |
120 | static aio_req SvAIO_REQ (SV *sv) |
145 | { |
121 | { |
146 | if (!sv_derived_from (sv, AIO_CB_KLASS) || !SvROK (sv)) |
122 | if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv)) |
147 | croak ("object of class " AIO_CB_KLASS " expected"); |
123 | croak ("object of class " AIO_REQ_KLASS " expected"); |
148 | |
124 | |
149 | MAGIC *mg = mg_find (SvRV (sv), PERL_MAGIC_ext); |
125 | MAGIC *mg = mg_find (SvRV (sv), PERL_MAGIC_ext); |
150 | |
126 | |
151 | return mg ? (aio_req)mg->mg_ptr : 0; |
127 | return mg ? (aio_req)mg->mg_ptr : 0; |
152 | } |
128 | } |
153 | |
129 | |
154 | static void req_free (aio_req req) |
130 | static void aio_grp_feed (aio_req grp) |
155 | { |
131 | { |
156 | if (req->self) |
132 | while (grp->length < grp->fd2) |
157 | { |
|
|
158 | sv_unmagic (req->self, PERL_MAGIC_ext); |
|
|
159 | SvREFCNT_dec (req->self); |
|
|
160 | } |
133 | { |
|
|
134 | int old_len = grp->length; |
161 | |
135 | |
162 | if (req->data) |
136 | if (grp->fh2 && SvOK (grp->fh2)) |
163 | SvREFCNT_dec (req->data); |
137 | { |
|
|
138 | dSP; |
164 | |
139 | |
165 | if (req->fh) |
140 | ENTER; |
166 | SvREFCNT_dec (req->fh); |
141 | SAVETMPS; |
|
|
142 | PUSHMARK (SP); |
|
|
143 | XPUSHs (req_sv (grp, AIO_GRP_KLASS)); |
|
|
144 | PUTBACK; |
|
|
145 | call_sv (grp->fh2, G_VOID | G_EVAL); |
|
|
146 | SPAGAIN; |
|
|
147 | FREETMPS; |
|
|
148 | LEAVE; |
|
|
149 | } |
167 | |
150 | |
168 | if (req->fh2) |
151 | /* stop if no progress has been made */ |
|
|
152 | if (old_len == grp->length) |
|
|
153 | { |
169 | SvREFCNT_dec (req->fh2); |
154 | SvREFCNT_dec (grp->fh2); |
170 | |
155 | grp->fh2 = 0; |
171 | if (req->statdata) |
156 | break; |
172 | Safefree (req->statdata); |
157 | } |
173 | |
158 | } |
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 | } |
159 | } |
182 | |
160 | |
183 | static void |
161 | static void poll_wait () |
184 | poll_wait () |
|
|
185 | { |
162 | { |
186 | if (nreqs && !ress) |
163 | if (nreqs && !ress) |
187 | { |
164 | { |
188 | fd_set rfd; |
165 | fd_set rfd; |
189 | FD_ZERO(&rfd); |
166 | FD_ZERO(&rfd); |
… | |
… | |
191 | |
168 | |
192 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
169 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
193 | } |
170 | } |
194 | } |
171 | } |
195 | |
172 | |
196 | static int |
173 | static void req_invoke (aio_req req) |
197 | poll_cb () |
174 | { |
|
|
175 | dSP; |
|
|
176 | int errorno = errno; |
|
|
177 | |
|
|
178 | if (req->cancelled || !SvOK (req->callback)) |
|
|
179 | return; |
|
|
180 | |
|
|
181 | errno = req->errorno; |
|
|
182 | |
|
|
183 | ENTER; |
|
|
184 | SAVETMPS; |
|
|
185 | PUSHMARK (SP); |
|
|
186 | EXTEND (SP, 1); |
|
|
187 | |
|
|
188 | switch (req->type) |
|
|
189 | { |
|
|
190 | case REQ_READDIR: |
|
|
191 | { |
|
|
192 | SV *rv = &PL_sv_undef; |
|
|
193 | |
|
|
194 | if (req->result >= 0) |
|
|
195 | { |
|
|
196 | char *buf = req->data2ptr; |
|
|
197 | AV *av = newAV (); |
|
|
198 | |
|
|
199 | while (req->result) |
|
|
200 | { |
|
|
201 | SV *sv = newSVpv (buf, 0); |
|
|
202 | |
|
|
203 | av_push (av, sv); |
|
|
204 | buf += SvCUR (sv) + 1; |
|
|
205 | req->result--; |
|
|
206 | } |
|
|
207 | |
|
|
208 | rv = sv_2mortal (newRV_noinc ((SV *)av)); |
|
|
209 | } |
|
|
210 | |
|
|
211 | PUSHs (rv); |
|
|
212 | } |
|
|
213 | break; |
|
|
214 | |
|
|
215 | case REQ_OPEN: |
|
|
216 | { |
|
|
217 | /* convert fd to fh */ |
|
|
218 | SV *fh; |
|
|
219 | |
|
|
220 | PUSHs (sv_2mortal (newSViv (req->result))); |
|
|
221 | PUTBACK; |
|
|
222 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
|
|
223 | SPAGAIN; |
|
|
224 | |
|
|
225 | fh = SvREFCNT_inc (POPs); |
|
|
226 | |
|
|
227 | PUSHMARK (SP); |
|
|
228 | XPUSHs (sv_2mortal (fh)); |
|
|
229 | } |
|
|
230 | break; |
|
|
231 | |
|
|
232 | case REQ_GROUP: |
|
|
233 | req->fd = 2; /* mark group as finished */ |
|
|
234 | |
|
|
235 | if (req->data) |
|
|
236 | { |
|
|
237 | int i; |
|
|
238 | AV *av = (AV *)req->data; |
|
|
239 | |
|
|
240 | EXTEND (SP, AvFILL (av) + 1); |
|
|
241 | for (i = 0; i <= AvFILL (av); ++i) |
|
|
242 | PUSHs (*av_fetch (av, i, 0)); |
|
|
243 | } |
|
|
244 | break; |
|
|
245 | |
|
|
246 | case REQ_SLEEP: |
|
|
247 | break; |
|
|
248 | |
|
|
249 | default: |
|
|
250 | PUSHs (sv_2mortal (newSViv (req->result))); |
|
|
251 | break; |
|
|
252 | } |
|
|
253 | |
|
|
254 | |
|
|
255 | PUTBACK; |
|
|
256 | call_sv (req->callback, G_VOID | G_EVAL); |
|
|
257 | SPAGAIN; |
|
|
258 | |
|
|
259 | if (SvTRUE (ERRSV)) |
|
|
260 | { |
|
|
261 | req_free (req); |
|
|
262 | croak (0); |
|
|
263 | } |
|
|
264 | |
|
|
265 | FREETMPS; |
|
|
266 | LEAVE; |
|
|
267 | |
|
|
268 | errno = errorno; |
|
|
269 | } |
|
|
270 | |
|
|
271 | static void req_free (aio_req req) |
|
|
272 | { |
|
|
273 | if (req->grp) |
|
|
274 | { |
|
|
275 | aio_req grp = req->grp; |
|
|
276 | |
|
|
277 | /* unlink request */ |
|
|
278 | if (req->grp_next) req->grp_next->grp_prev = req->grp_prev; |
|
|
279 | if (req->grp_prev) req->grp_prev->grp_next = req->grp_next; |
|
|
280 | |
|
|
281 | if (grp->grp_first == req) |
|
|
282 | grp->grp_first = req->grp_next; |
|
|
283 | |
|
|
284 | --grp->length; |
|
|
285 | |
|
|
286 | /* call feeder, if applicable */ |
|
|
287 | aio_grp_feed (grp); |
|
|
288 | |
|
|
289 | /* finish, if done */ |
|
|
290 | if (!grp->length && grp->fd) |
|
|
291 | { |
|
|
292 | req_invoke (grp); |
|
|
293 | req_free (grp); |
|
|
294 | } |
|
|
295 | } |
|
|
296 | |
|
|
297 | if (req->self) |
|
|
298 | { |
|
|
299 | sv_unmagic (req->self, PERL_MAGIC_ext); |
|
|
300 | SvREFCNT_dec (req->self); |
|
|
301 | } |
|
|
302 | |
|
|
303 | SvREFCNT_dec (req->data); |
|
|
304 | SvREFCNT_dec (req->fh); |
|
|
305 | SvREFCNT_dec (req->fh2); |
|
|
306 | SvREFCNT_dec (req->callback); |
|
|
307 | Safefree (req->statdata); |
|
|
308 | |
|
|
309 | if (req->type == REQ_READDIR && req->result >= 0) |
|
|
310 | free (req->data2ptr); |
|
|
311 | |
|
|
312 | Safefree (req); |
|
|
313 | } |
|
|
314 | |
|
|
315 | static void req_cancel (aio_req req) |
|
|
316 | { |
|
|
317 | req->cancelled = 1; |
|
|
318 | |
|
|
319 | if (req->type == REQ_GROUP) |
|
|
320 | { |
|
|
321 | aio_req sub; |
|
|
322 | |
|
|
323 | for (sub = req->grp_first; sub; sub = sub->grp_next) |
|
|
324 | req_cancel (sub); |
|
|
325 | } |
|
|
326 | } |
|
|
327 | |
|
|
328 | static int poll_cb () |
198 | { |
329 | { |
199 | dSP; |
330 | dSP; |
200 | int count = 0; |
331 | int count = 0; |
201 | int do_croak = 0; |
332 | int do_croak = 0; |
202 | aio_req req; |
333 | aio_req req; |
… | |
… | |
228 | |
359 | |
229 | nreqs--; |
360 | nreqs--; |
230 | |
361 | |
231 | if (req->type == REQ_QUIT) |
362 | if (req->type == REQ_QUIT) |
232 | started--; |
363 | started--; |
|
|
364 | else if (req->type == REQ_GROUP && req->length) |
|
|
365 | { |
|
|
366 | req->fd = 1; /* mark request as delayed */ |
|
|
367 | continue; |
|
|
368 | } |
233 | else |
369 | else |
234 | { |
370 | { |
235 | int errorno = errno; |
|
|
236 | errno = req->errorno; |
|
|
237 | |
|
|
238 | if (req->type == REQ_READ) |
371 | if (req->type == REQ_READ) |
239 | SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); |
372 | SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); |
240 | |
373 | |
241 | if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE)) |
374 | if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE)) |
242 | SvREADONLY_off (req->data); |
375 | SvREADONLY_off (req->data); |
… | |
… | |
246 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
379 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
247 | PL_laststatval = req->result; |
380 | PL_laststatval = req->result; |
248 | PL_statcache = *(req->statdata); |
381 | PL_statcache = *(req->statdata); |
249 | } |
382 | } |
250 | |
383 | |
251 | ENTER; |
384 | req_invoke (req); |
252 | PUSHMARK (SP); |
|
|
253 | |
385 | |
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++; |
386 | count++; |
314 | } |
387 | } |
315 | |
388 | |
316 | req_free (req); |
389 | req_free (req); |
317 | } |
390 | } |
… | |
… | |
319 | return count; |
392 | return count; |
320 | } |
393 | } |
321 | |
394 | |
322 | static void *aio_proc(void *arg); |
395 | static void *aio_proc(void *arg); |
323 | |
396 | |
324 | static void |
|
|
325 | start_thread (void) |
397 | static void start_thread (void) |
326 | { |
398 | { |
327 | sigset_t fullsigset, oldsigset; |
399 | sigset_t fullsigset, oldsigset; |
328 | pthread_t tid; |
400 | pthread_t tid; |
329 | pthread_attr_t attr; |
401 | pthread_attr_t attr; |
330 | |
402 | |
… | |
… | |
339 | started++; |
411 | started++; |
340 | |
412 | |
341 | sigprocmask (SIG_SETMASK, &oldsigset, 0); |
413 | sigprocmask (SIG_SETMASK, &oldsigset, 0); |
342 | } |
414 | } |
343 | |
415 | |
344 | static void |
|
|
345 | req_send (aio_req req) |
416 | static void req_send (aio_req req) |
346 | { |
417 | { |
347 | while (started < wanted && nreqs >= started) |
418 | while (started < wanted && nreqs >= started) |
348 | start_thread (); |
419 | start_thread (); |
349 | |
420 | |
350 | nreqs++; |
421 | nreqs++; |
… | |
… | |
374 | |
445 | |
375 | poll_wait (); |
446 | poll_wait (); |
376 | } |
447 | } |
377 | } |
448 | } |
378 | |
449 | |
379 | static void |
450 | static void end_thread (void) |
380 | end_thread (void) |
|
|
381 | { |
451 | { |
382 | aio_req req; |
452 | aio_req req; |
383 | Newz (0, req, 1, aio_cb); |
453 | Newz (0, req, 1, aio_cb); |
384 | req->type = REQ_QUIT; |
454 | req->type = REQ_QUIT; |
385 | |
455 | |
… | |
… | |
436 | * normal read/write by using a mutex. slows down execution a lot, |
506 | * normal read/write by using a mutex. slows down execution a lot, |
437 | * but that's your problem, not mine. |
507 | * but that's your problem, not mine. |
438 | */ |
508 | */ |
439 | static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER; |
509 | static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER; |
440 | |
510 | |
441 | static ssize_t |
511 | 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 | { |
512 | { |
444 | ssize_t res; |
513 | ssize_t res; |
445 | off_t ooffset; |
514 | off_t ooffset; |
446 | |
515 | |
447 | pthread_mutex_lock (&preadwritelock); |
516 | pthread_mutex_lock (&preadwritelock); |
… | |
… | |
452 | pthread_mutex_unlock (&preadwritelock); |
521 | pthread_mutex_unlock (&preadwritelock); |
453 | |
522 | |
454 | return res; |
523 | return res; |
455 | } |
524 | } |
456 | |
525 | |
457 | static ssize_t |
|
|
458 | pwrite (int fd, void *buf, size_t count, off_t offset) |
526 | static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset) |
459 | { |
527 | { |
460 | ssize_t res; |
528 | ssize_t res; |
461 | off_t ooffset; |
529 | off_t ooffset; |
462 | |
530 | |
463 | pthread_mutex_lock (&preadwritelock); |
531 | pthread_mutex_lock (&preadwritelock); |
… | |
… | |
476 | #endif |
544 | #endif |
477 | |
545 | |
478 | #if !HAVE_READAHEAD |
546 | #if !HAVE_READAHEAD |
479 | # define readahead aio_readahead |
547 | # define readahead aio_readahead |
480 | |
548 | |
481 | static ssize_t |
|
|
482 | readahead (int fd, off_t offset, size_t count) |
549 | static ssize_t readahead (int fd, off_t offset, size_t count) |
483 | { |
550 | { |
484 | char readahead_buf[4096]; |
551 | char readahead_buf[4096]; |
485 | |
552 | |
486 | while (count > 0) |
553 | while (count > 0) |
487 | { |
554 | { |
… | |
… | |
499 | #if !HAVE_READDIR_R |
566 | #if !HAVE_READDIR_R |
500 | # define readdir_r aio_readdir_r |
567 | # define readdir_r aio_readdir_r |
501 | |
568 | |
502 | static pthread_mutex_t readdirlock = PTHREAD_MUTEX_INITIALIZER; |
569 | static pthread_mutex_t readdirlock = PTHREAD_MUTEX_INITIALIZER; |
503 | |
570 | |
504 | static int |
|
|
505 | readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) |
571 | static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) |
506 | { |
572 | { |
507 | struct dirent *e; |
573 | struct dirent *e; |
508 | int errorno; |
574 | int errorno; |
509 | |
575 | |
510 | pthread_mutex_lock (&readdirlock); |
576 | pthread_mutex_lock (&readdirlock); |
… | |
… | |
526 | return e ? 0 : -1; |
592 | return e ? 0 : -1; |
527 | } |
593 | } |
528 | #endif |
594 | #endif |
529 | |
595 | |
530 | /* sendfile always needs emulation */ |
596 | /* sendfile always needs emulation */ |
531 | static ssize_t |
|
|
532 | sendfile_ (int ofd, int ifd, off_t offset, size_t count) |
597 | static ssize_t sendfile_ (int ofd, int ifd, off_t offset, size_t count) |
533 | { |
598 | { |
534 | ssize_t res; |
599 | ssize_t res; |
535 | |
600 | |
536 | if (!count) |
601 | if (!count) |
537 | return 0; |
602 | return 0; |
… | |
… | |
619 | |
684 | |
620 | return res; |
685 | return res; |
621 | } |
686 | } |
622 | |
687 | |
623 | /* read a full directory */ |
688 | /* read a full directory */ |
624 | static int |
|
|
625 | scandir_ (const char *path, void **namesp) |
689 | static int scandir_ (const char *path, void **namesp) |
626 | { |
690 | { |
627 | DIR *dirp = opendir (path); |
691 | DIR *dirp = opendir (path); |
628 | union |
692 | union |
629 | { |
693 | { |
630 | struct dirent d; |
694 | struct dirent d; |
… | |
… | |
684 | return res; |
748 | return res; |
685 | } |
749 | } |
686 | |
750 | |
687 | /*****************************************************************************/ |
751 | /*****************************************************************************/ |
688 | |
752 | |
689 | static void * |
|
|
690 | aio_proc (void *thr_arg) |
753 | static void *aio_proc (void *thr_arg) |
691 | { |
754 | { |
692 | aio_req req; |
755 | aio_req req; |
693 | int type; |
756 | int type; |
694 | |
757 | |
695 | do |
758 | do |
… | |
… | |
738 | case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; |
801 | case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; |
739 | |
802 | |
740 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
803 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
741 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
804 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
742 | case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break; |
805 | case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break; |
|
|
806 | |
|
|
807 | case REQ_SLEEP: |
|
|
808 | { |
|
|
809 | struct timeval tv; |
|
|
810 | |
|
|
811 | tv.tv_sec = req->fd; |
|
|
812 | tv.tv_usec = req->fd2; |
|
|
813 | |
|
|
814 | req->result = select (0, 0, 0, 0, &tv); |
|
|
815 | } |
743 | |
816 | |
744 | case REQ_QUIT: |
817 | case REQ_QUIT: |
745 | break; |
818 | break; |
746 | |
819 | |
747 | default: |
820 | default: |
… | |
… | |
846 | |
919 | |
847 | #define REQ_SEND \ |
920 | #define REQ_SEND \ |
848 | req_send (req); \ |
921 | req_send (req); \ |
849 | \ |
922 | \ |
850 | if (GIMME_V != G_VOID) \ |
923 | if (GIMME_V != G_VOID) \ |
851 | XPUSHs (req_sv (req)); |
924 | XPUSHs (req_sv (req, AIO_REQ_KLASS)); |
852 | |
925 | |
853 | MODULE = IO::AIO PACKAGE = IO::AIO |
926 | MODULE = IO::AIO PACKAGE = IO::AIO |
854 | |
927 | |
855 | PROTOTYPES: ENABLE |
928 | PROTOTYPES: ENABLE |
856 | |
929 | |
… | |
… | |
1103 | req->dataptr = SvPVbyte_nolen (req->data); |
1176 | req->dataptr = SvPVbyte_nolen (req->data); |
1104 | |
1177 | |
1105 | REQ_SEND; |
1178 | REQ_SEND; |
1106 | } |
1179 | } |
1107 | |
1180 | |
1108 | #if 0 |
|
|
1109 | |
|
|
1110 | # undocumented, because it does not cancel active requests |
|
|
1111 | void |
1181 | void |
1112 | cancel_most_requests () |
1182 | aio_sleep (delay,callback=&PL_sv_undef) |
|
|
1183 | double delay |
|
|
1184 | SV * callback |
|
|
1185 | PPCODE: |
|
|
1186 | { |
|
|
1187 | dREQ; |
|
|
1188 | |
|
|
1189 | req->type = REQ_SLEEP; |
|
|
1190 | req->fd = delay < 0. ? 0 : delay; |
|
|
1191 | req->fd2 = delay < 0. ? 0 : 1000. * (delay - req->fd); |
|
|
1192 | |
|
|
1193 | REQ_SEND; |
|
|
1194 | } |
|
|
1195 | |
|
|
1196 | void |
|
|
1197 | aio_group (callback=&PL_sv_undef) |
|
|
1198 | SV * callback |
1113 | PROTOTYPE: |
1199 | PROTOTYPE: ;$ |
1114 | CODE: |
1200 | PPCODE: |
1115 | { |
1201 | { |
1116 | aio_req *req; |
1202 | dREQ; |
1117 | |
1203 | req->type = REQ_GROUP; |
1118 | pthread_mutex_lock (&reqlock); |
1204 | req_send (req); |
1119 | for (req = reqs; req; req = req->next) |
1205 | 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 | } |
1206 | } |
1128 | |
|
|
1129 | #endif |
|
|
1130 | |
1207 | |
1131 | void |
1208 | void |
1132 | flush () |
1209 | flush () |
1133 | PROTOTYPE: |
1210 | PROTOTYPE: |
1134 | CODE: |
1211 | CODE: |
… | |
… | |
1177 | CODE: |
1254 | CODE: |
1178 | RETVAL = nreqs; |
1255 | RETVAL = nreqs; |
1179 | OUTPUT: |
1256 | OUTPUT: |
1180 | RETVAL |
1257 | RETVAL |
1181 | |
1258 | |
|
|
1259 | PROTOTYPES: DISABLE |
|
|
1260 | |
1182 | MODULE = IO::AIO PACKAGE = IO::AIO::CB |
1261 | MODULE = IO::AIO PACKAGE = IO::AIO::REQ |
1183 | |
1262 | |
1184 | void |
1263 | void |
1185 | cancel (aio_req_ornot req) |
1264 | cancel (aio_req_ornot req) |
1186 | PROTOTYPE: |
1265 | PROTOTYPE: |
1187 | CODE: |
1266 | CODE: |
1188 | req->cancelled = 1; |
1267 | req_cancel (req); |
1189 | |
1268 | |
|
|
1269 | MODULE = IO::AIO PACKAGE = IO::AIO::GRP |
|
|
1270 | |
|
|
1271 | void |
|
|
1272 | add (aio_req grp, ...) |
|
|
1273 | PPCODE: |
|
|
1274 | { |
|
|
1275 | int i; |
|
|
1276 | |
|
|
1277 | if (grp->fd == 2) |
|
|
1278 | croak ("cannot add requests to IO::AIO::GRP after the group finished"); |
|
|
1279 | |
|
|
1280 | for (i = 1; i < items; ++i ) |
|
|
1281 | { |
|
|
1282 | if (GIMME_V != G_VOID) |
|
|
1283 | XPUSHs (sv_2mortal (newSVsv (ST (i)))); |
|
|
1284 | |
|
|
1285 | aio_req req = SvAIO_REQ (ST (i)); |
|
|
1286 | |
|
|
1287 | if (req) |
|
|
1288 | { |
|
|
1289 | ++grp->length; |
|
|
1290 | req->grp = grp; |
|
|
1291 | |
|
|
1292 | req->grp_prev = 0; |
|
|
1293 | req->grp_next = grp->grp_first; |
|
|
1294 | |
|
|
1295 | if (grp->grp_first) |
|
|
1296 | grp->grp_first->grp_prev = req; |
|
|
1297 | |
|
|
1298 | grp->grp_first = req; |
|
|
1299 | } |
|
|
1300 | } |
|
|
1301 | } |
|
|
1302 | |
|
|
1303 | void |
|
|
1304 | result (aio_req grp, ...) |
|
|
1305 | CODE: |
|
|
1306 | { |
|
|
1307 | int i; |
|
|
1308 | AV *av = newAV (); |
|
|
1309 | |
|
|
1310 | for (i = 1; i < items; ++i ) |
|
|
1311 | av_push (av, newSVsv (ST (i))); |
|
|
1312 | |
|
|
1313 | SvREFCNT_dec (grp->data); |
|
|
1314 | grp->data = (SV *)av; |
|
|
1315 | } |
|
|
1316 | |
|
|
1317 | void |
|
|
1318 | feeder_limit (aio_req grp, int limit) |
|
|
1319 | CODE: |
|
|
1320 | grp->fd2 = limit; |
|
|
1321 | aio_grp_feed (grp); |
|
|
1322 | |
|
|
1323 | void |
|
|
1324 | set_feeder (aio_req grp, SV *callback=&PL_sv_undef) |
|
|
1325 | CODE: |
|
|
1326 | { |
|
|
1327 | SvREFCNT_dec (grp->fh2); |
|
|
1328 | grp->fh2 = newSVsv (callback); |
|
|
1329 | |
|
|
1330 | if (grp->fd2 <= 0) |
|
|
1331 | grp->fd2 = 2; |
|
|
1332 | |
|
|
1333 | aio_grp_feed (grp); |
|
|
1334 | } |
|
|
1335 | |