… | |
… | |
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; |
69 | struct aio_cb *grp, *grp_prev, *grp_next; |
65 | struct aio_grp *grp; |
|
|
66 | |
70 | |
67 | struct aio_cb *volatile next; |
71 | struct aio_cb *volatile next; |
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 | |
… | |
… | |
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 | { |
138 | req->self = (SV *)newHV (); |
111 | req->self = (SV *)newHV (); |
139 | sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0); |
112 | sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0); |
140 | |
113 | |
141 | return sv_bless (newRV_noinc (req->self), gv_stashpv (AIO_CB_KLASS, 1)); |
114 | return sv_2mortal (sv_bless (newRV_inc (req->self), gv_stashpv (klass, 1))); |
142 | } |
115 | } |
143 | |
116 | |
144 | static aio_req SvAIO_REQ (SV *sv) |
117 | static aio_req SvAIO_REQ (SV *sv) |
145 | { |
118 | { |
146 | if (!sv_derived_from (sv, AIO_CB_KLASS) || !SvROK (sv)) |
119 | if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv)) |
147 | croak ("object of class " AIO_CB_KLASS " expected"); |
120 | croak ("object of class " AIO_REQ_KLASS " expected"); |
148 | |
121 | |
149 | MAGIC *mg = mg_find (SvRV (sv), PERL_MAGIC_ext); |
122 | MAGIC *mg = mg_find (SvRV (sv), PERL_MAGIC_ext); |
150 | |
123 | |
151 | return mg ? (aio_req)mg->mg_ptr : 0; |
124 | return mg ? (aio_req)mg->mg_ptr : 0; |
152 | } |
125 | } |
153 | |
126 | |
154 | static void req_free (aio_req req) |
127 | static void poll_wait () |
155 | { |
|
|
156 | if (req->self) |
|
|
157 | { |
|
|
158 | sv_unmagic (req->self, PERL_MAGIC_ext); |
|
|
159 | SvREFCNT_dec (req->self); |
|
|
160 | } |
|
|
161 | |
|
|
162 | if (req->data) |
|
|
163 | SvREFCNT_dec (req->data); |
|
|
164 | |
|
|
165 | if (req->fh) |
|
|
166 | SvREFCNT_dec (req->fh); |
|
|
167 | |
|
|
168 | if (req->fh2) |
|
|
169 | SvREFCNT_dec (req->fh2); |
|
|
170 | |
|
|
171 | if (req->statdata) |
|
|
172 | Safefree (req->statdata); |
|
|
173 | |
|
|
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 | } |
|
|
182 | |
|
|
183 | static void |
|
|
184 | poll_wait () |
|
|
185 | { |
128 | { |
186 | if (nreqs && !ress) |
129 | if (nreqs && !ress) |
187 | { |
130 | { |
188 | fd_set rfd; |
131 | fd_set rfd; |
189 | FD_ZERO(&rfd); |
132 | FD_ZERO(&rfd); |
… | |
… | |
191 | |
134 | |
192 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
135 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
193 | } |
136 | } |
194 | } |
137 | } |
195 | |
138 | |
196 | static int |
139 | static void req_invoke (aio_req req) |
197 | poll_cb () |
140 | { |
|
|
141 | dSP; |
|
|
142 | int errorno = errno; |
|
|
143 | |
|
|
144 | if (req->cancelled || !SvOK (req->callback)) |
|
|
145 | return; |
|
|
146 | |
|
|
147 | errno = req->errorno; |
|
|
148 | |
|
|
149 | ENTER; |
|
|
150 | PUSHMARK (SP); |
|
|
151 | EXTEND (SP, 1); |
|
|
152 | |
|
|
153 | switch (req->type) |
|
|
154 | { |
|
|
155 | case REQ_READDIR: |
|
|
156 | { |
|
|
157 | SV *rv = &PL_sv_undef; |
|
|
158 | |
|
|
159 | if (req->result >= 0) |
|
|
160 | { |
|
|
161 | char *buf = req->data2ptr; |
|
|
162 | AV *av = newAV (); |
|
|
163 | |
|
|
164 | while (req->result) |
|
|
165 | { |
|
|
166 | SV *sv = newSVpv (buf, 0); |
|
|
167 | |
|
|
168 | av_push (av, sv); |
|
|
169 | buf += SvCUR (sv) + 1; |
|
|
170 | req->result--; |
|
|
171 | } |
|
|
172 | |
|
|
173 | rv = sv_2mortal (newRV_noinc ((SV *)av)); |
|
|
174 | } |
|
|
175 | |
|
|
176 | PUSHs (rv); |
|
|
177 | } |
|
|
178 | break; |
|
|
179 | |
|
|
180 | case REQ_OPEN: |
|
|
181 | { |
|
|
182 | /* convert fd to fh */ |
|
|
183 | SV *fh; |
|
|
184 | |
|
|
185 | PUSHs (sv_2mortal (newSViv (req->result))); |
|
|
186 | 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_GROUP: |
|
|
198 | if (req->data) |
|
|
199 | { |
|
|
200 | int i; |
|
|
201 | AV *av = (AV *)req->data; |
|
|
202 | |
|
|
203 | EXTEND (SP, AvFILL (av) + 1); |
|
|
204 | for (i = 0; i <= AvFILL (av); ++i) |
|
|
205 | PUSHs (*av_fetch (av, i, 0)); |
|
|
206 | } |
|
|
207 | break; |
|
|
208 | |
|
|
209 | case REQ_SLEEP: |
|
|
210 | break; |
|
|
211 | |
|
|
212 | default: |
|
|
213 | PUSHs (sv_2mortal (newSViv (req->result))); |
|
|
214 | break; |
|
|
215 | } |
|
|
216 | |
|
|
217 | |
|
|
218 | PUTBACK; |
|
|
219 | call_sv (req->callback, G_VOID | G_EVAL); |
|
|
220 | SPAGAIN; |
|
|
221 | |
|
|
222 | if (SvTRUE (ERRSV)) |
|
|
223 | { |
|
|
224 | req_free (req); |
|
|
225 | croak (0); |
|
|
226 | } |
|
|
227 | |
|
|
228 | LEAVE; |
|
|
229 | |
|
|
230 | errno = errorno; |
|
|
231 | } |
|
|
232 | |
|
|
233 | static void req_free (aio_req req) |
|
|
234 | { |
|
|
235 | if (req->grp) |
|
|
236 | { |
|
|
237 | aio_req grp = req->grp; |
|
|
238 | |
|
|
239 | /* unlink request */ |
|
|
240 | req->grp_next->grp_prev = req->grp_prev; |
|
|
241 | req->grp_prev->grp_next = req->grp_next; |
|
|
242 | |
|
|
243 | if (grp->grp_next == grp && grp->fd) |
|
|
244 | { |
|
|
245 | req_invoke (grp); |
|
|
246 | req_free (grp); |
|
|
247 | } |
|
|
248 | } |
|
|
249 | |
|
|
250 | if (req->self) |
|
|
251 | { |
|
|
252 | sv_unmagic (req->self, PERL_MAGIC_ext); |
|
|
253 | SvREFCNT_dec (req->self); |
|
|
254 | } |
|
|
255 | |
|
|
256 | if (req->data) |
|
|
257 | SvREFCNT_dec (req->data); |
|
|
258 | |
|
|
259 | if (req->fh) |
|
|
260 | SvREFCNT_dec (req->fh); |
|
|
261 | |
|
|
262 | if (req->fh2) |
|
|
263 | SvREFCNT_dec (req->fh2); |
|
|
264 | |
|
|
265 | if (req->statdata) |
|
|
266 | Safefree (req->statdata); |
|
|
267 | |
|
|
268 | if (req->callback) |
|
|
269 | SvREFCNT_dec (req->callback); |
|
|
270 | |
|
|
271 | if (req->type == REQ_READDIR && req->result >= 0) |
|
|
272 | free (req->data2ptr); |
|
|
273 | |
|
|
274 | Safefree (req); |
|
|
275 | } |
|
|
276 | |
|
|
277 | static void req_cancel (aio_req req) |
|
|
278 | { |
|
|
279 | req->cancelled = 1; |
|
|
280 | |
|
|
281 | if (req->type == REQ_GROUP) |
|
|
282 | { |
|
|
283 | aio_req sub; |
|
|
284 | |
|
|
285 | for (sub = req->grp_next; sub != req; sub = sub->grp_next) |
|
|
286 | req_cancel (sub); |
|
|
287 | } |
|
|
288 | } |
|
|
289 | |
|
|
290 | static int poll_cb () |
198 | { |
291 | { |
199 | dSP; |
292 | dSP; |
200 | int count = 0; |
293 | int count = 0; |
201 | int do_croak = 0; |
294 | int do_croak = 0; |
202 | aio_req req; |
295 | aio_req req; |
… | |
… | |
228 | |
321 | |
229 | nreqs--; |
322 | nreqs--; |
230 | |
323 | |
231 | if (req->type == REQ_QUIT) |
324 | if (req->type == REQ_QUIT) |
232 | started--; |
325 | started--; |
|
|
326 | else if (req->type == REQ_GROUP && req->grp_next != req) |
|
|
327 | { |
|
|
328 | req->fd = 1; /* mark request as delayed */ |
|
|
329 | continue; |
|
|
330 | } |
233 | else |
331 | else |
234 | { |
332 | { |
235 | int errorno = errno; |
|
|
236 | errno = req->errorno; |
|
|
237 | |
|
|
238 | if (req->type == REQ_READ) |
333 | if (req->type == REQ_READ) |
239 | SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); |
334 | SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); |
240 | |
335 | |
241 | if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE)) |
336 | if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE)) |
242 | SvREADONLY_off (req->data); |
337 | SvREADONLY_off (req->data); |
… | |
… | |
246 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
341 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
247 | PL_laststatval = req->result; |
342 | PL_laststatval = req->result; |
248 | PL_statcache = *(req->statdata); |
343 | PL_statcache = *(req->statdata); |
249 | } |
344 | } |
250 | |
345 | |
251 | ENTER; |
346 | req_invoke (req); |
252 | PUSHMARK (SP); |
|
|
253 | |
347 | |
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++; |
348 | count++; |
314 | } |
349 | } |
315 | |
350 | |
316 | req_free (req); |
351 | req_free (req); |
317 | } |
352 | } |
… | |
… | |
319 | return count; |
354 | return count; |
320 | } |
355 | } |
321 | |
356 | |
322 | static void *aio_proc(void *arg); |
357 | static void *aio_proc(void *arg); |
323 | |
358 | |
324 | static void |
|
|
325 | start_thread (void) |
359 | static void start_thread (void) |
326 | { |
360 | { |
327 | sigset_t fullsigset, oldsigset; |
361 | sigset_t fullsigset, oldsigset; |
328 | pthread_t tid; |
362 | pthread_t tid; |
329 | pthread_attr_t attr; |
363 | pthread_attr_t attr; |
330 | |
364 | |
… | |
… | |
339 | started++; |
373 | started++; |
340 | |
374 | |
341 | sigprocmask (SIG_SETMASK, &oldsigset, 0); |
375 | sigprocmask (SIG_SETMASK, &oldsigset, 0); |
342 | } |
376 | } |
343 | |
377 | |
344 | static void |
|
|
345 | req_send (aio_req req) |
378 | static void req_send (aio_req req) |
346 | { |
379 | { |
347 | while (started < wanted && nreqs >= started) |
380 | while (started < wanted && nreqs >= started) |
348 | start_thread (); |
381 | start_thread (); |
349 | |
382 | |
350 | nreqs++; |
383 | nreqs++; |
… | |
… | |
374 | |
407 | |
375 | poll_wait (); |
408 | poll_wait (); |
376 | } |
409 | } |
377 | } |
410 | } |
378 | |
411 | |
379 | static void |
412 | static void end_thread (void) |
380 | end_thread (void) |
|
|
381 | { |
413 | { |
382 | aio_req req; |
414 | aio_req req; |
383 | Newz (0, req, 1, aio_cb); |
415 | Newz (0, req, 1, aio_cb); |
384 | req->type = REQ_QUIT; |
416 | req->type = REQ_QUIT; |
385 | |
417 | |
… | |
… | |
436 | * normal read/write by using a mutex. slows down execution a lot, |
468 | * normal read/write by using a mutex. slows down execution a lot, |
437 | * but that's your problem, not mine. |
469 | * but that's your problem, not mine. |
438 | */ |
470 | */ |
439 | static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER; |
471 | static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER; |
440 | |
472 | |
441 | static ssize_t |
473 | 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 | { |
474 | { |
444 | ssize_t res; |
475 | ssize_t res; |
445 | off_t ooffset; |
476 | off_t ooffset; |
446 | |
477 | |
447 | pthread_mutex_lock (&preadwritelock); |
478 | pthread_mutex_lock (&preadwritelock); |
… | |
… | |
452 | pthread_mutex_unlock (&preadwritelock); |
483 | pthread_mutex_unlock (&preadwritelock); |
453 | |
484 | |
454 | return res; |
485 | return res; |
455 | } |
486 | } |
456 | |
487 | |
457 | static ssize_t |
|
|
458 | pwrite (int fd, void *buf, size_t count, off_t offset) |
488 | static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset) |
459 | { |
489 | { |
460 | ssize_t res; |
490 | ssize_t res; |
461 | off_t ooffset; |
491 | off_t ooffset; |
462 | |
492 | |
463 | pthread_mutex_lock (&preadwritelock); |
493 | pthread_mutex_lock (&preadwritelock); |
… | |
… | |
476 | #endif |
506 | #endif |
477 | |
507 | |
478 | #if !HAVE_READAHEAD |
508 | #if !HAVE_READAHEAD |
479 | # define readahead aio_readahead |
509 | # define readahead aio_readahead |
480 | |
510 | |
481 | static ssize_t |
|
|
482 | readahead (int fd, off_t offset, size_t count) |
511 | static ssize_t readahead (int fd, off_t offset, size_t count) |
483 | { |
512 | { |
484 | char readahead_buf[4096]; |
513 | char readahead_buf[4096]; |
485 | |
514 | |
486 | while (count > 0) |
515 | while (count > 0) |
487 | { |
516 | { |
… | |
… | |
499 | #if !HAVE_READDIR_R |
528 | #if !HAVE_READDIR_R |
500 | # define readdir_r aio_readdir_r |
529 | # define readdir_r aio_readdir_r |
501 | |
530 | |
502 | static pthread_mutex_t readdirlock = PTHREAD_MUTEX_INITIALIZER; |
531 | static pthread_mutex_t readdirlock = PTHREAD_MUTEX_INITIALIZER; |
503 | |
532 | |
504 | static int |
|
|
505 | readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) |
533 | static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) |
506 | { |
534 | { |
507 | struct dirent *e; |
535 | struct dirent *e; |
508 | int errorno; |
536 | int errorno; |
509 | |
537 | |
510 | pthread_mutex_lock (&readdirlock); |
538 | pthread_mutex_lock (&readdirlock); |
… | |
… | |
526 | return e ? 0 : -1; |
554 | return e ? 0 : -1; |
527 | } |
555 | } |
528 | #endif |
556 | #endif |
529 | |
557 | |
530 | /* sendfile always needs emulation */ |
558 | /* sendfile always needs emulation */ |
531 | static ssize_t |
|
|
532 | sendfile_ (int ofd, int ifd, off_t offset, size_t count) |
559 | static ssize_t sendfile_ (int ofd, int ifd, off_t offset, size_t count) |
533 | { |
560 | { |
534 | ssize_t res; |
561 | ssize_t res; |
535 | |
562 | |
536 | if (!count) |
563 | if (!count) |
537 | return 0; |
564 | return 0; |
… | |
… | |
619 | |
646 | |
620 | return res; |
647 | return res; |
621 | } |
648 | } |
622 | |
649 | |
623 | /* read a full directory */ |
650 | /* read a full directory */ |
624 | static int |
|
|
625 | scandir_ (const char *path, void **namesp) |
651 | static int scandir_ (const char *path, void **namesp) |
626 | { |
652 | { |
627 | DIR *dirp = opendir (path); |
653 | DIR *dirp = opendir (path); |
628 | union |
654 | union |
629 | { |
655 | { |
630 | struct dirent d; |
656 | struct dirent d; |
… | |
… | |
684 | return res; |
710 | return res; |
685 | } |
711 | } |
686 | |
712 | |
687 | /*****************************************************************************/ |
713 | /*****************************************************************************/ |
688 | |
714 | |
689 | static void * |
|
|
690 | aio_proc (void *thr_arg) |
715 | static void *aio_proc (void *thr_arg) |
691 | { |
716 | { |
692 | aio_req req; |
717 | aio_req req; |
693 | int type; |
718 | int type; |
694 | |
719 | |
695 | do |
720 | do |
… | |
… | |
738 | case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; |
763 | case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; |
739 | |
764 | |
740 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
765 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
741 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
766 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
742 | case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break; |
767 | case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break; |
|
|
768 | |
|
|
769 | case REQ_SLEEP: |
|
|
770 | { |
|
|
771 | struct timeval tv; |
|
|
772 | |
|
|
773 | tv.tv_sec = req->fd; |
|
|
774 | tv.tv_usec = req->fd2; |
|
|
775 | |
|
|
776 | req->result = select (0, 0, 0, 0, &tv); |
|
|
777 | } |
743 | |
778 | |
744 | case REQ_QUIT: |
779 | case REQ_QUIT: |
745 | break; |
780 | break; |
746 | |
781 | |
747 | default: |
782 | default: |
… | |
… | |
846 | |
881 | |
847 | #define REQ_SEND \ |
882 | #define REQ_SEND \ |
848 | req_send (req); \ |
883 | req_send (req); \ |
849 | \ |
884 | \ |
850 | if (GIMME_V != G_VOID) \ |
885 | if (GIMME_V != G_VOID) \ |
851 | XPUSHs (req_sv (req)); |
886 | XPUSHs (req_sv (req, AIO_REQ_KLASS)); |
852 | |
887 | |
853 | MODULE = IO::AIO PACKAGE = IO::AIO |
888 | MODULE = IO::AIO PACKAGE = IO::AIO |
854 | |
889 | |
855 | PROTOTYPES: ENABLE |
890 | PROTOTYPES: ENABLE |
856 | |
891 | |
… | |
… | |
1103 | req->dataptr = SvPVbyte_nolen (req->data); |
1138 | req->dataptr = SvPVbyte_nolen (req->data); |
1104 | |
1139 | |
1105 | REQ_SEND; |
1140 | REQ_SEND; |
1106 | } |
1141 | } |
1107 | |
1142 | |
1108 | #if 0 |
|
|
1109 | |
|
|
1110 | # undocumented, because it does not cancel active requests |
|
|
1111 | void |
1143 | void |
1112 | cancel_most_requests () |
1144 | aio_sleep (delay,callback=&PL_sv_undef) |
|
|
1145 | double delay |
|
|
1146 | SV * callback |
|
|
1147 | PPCODE: |
|
|
1148 | { |
|
|
1149 | dREQ; |
|
|
1150 | |
|
|
1151 | req->type = REQ_SLEEP; |
|
|
1152 | req->fd = delay < 0. ? 0 : delay; |
|
|
1153 | req->fd2 = delay < 0. ? 0 : 1000. * (delay - req->fd); |
|
|
1154 | |
|
|
1155 | REQ_SEND; |
|
|
1156 | } |
|
|
1157 | |
|
|
1158 | void |
|
|
1159 | aio_group (callback=&PL_sv_undef) |
|
|
1160 | SV * callback |
1113 | PROTOTYPE: |
1161 | PROTOTYPE: ;$ |
1114 | CODE: |
1162 | PPCODE: |
1115 | { |
1163 | { |
1116 | aio_req *req; |
1164 | dREQ; |
|
|
1165 | req->type = REQ_GROUP; |
|
|
1166 | req->grp_next = req; |
|
|
1167 | req->grp_prev = req; |
1117 | |
1168 | |
1118 | pthread_mutex_lock (&reqlock); |
1169 | req_send (req); |
1119 | for (req = reqs; req; req = req->next) |
1170 | 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 | } |
1171 | } |
1128 | |
|
|
1129 | #endif |
|
|
1130 | |
1172 | |
1131 | void |
1173 | void |
1132 | flush () |
1174 | flush () |
1133 | PROTOTYPE: |
1175 | PROTOTYPE: |
1134 | CODE: |
1176 | CODE: |
… | |
… | |
1177 | CODE: |
1219 | CODE: |
1178 | RETVAL = nreqs; |
1220 | RETVAL = nreqs; |
1179 | OUTPUT: |
1221 | OUTPUT: |
1180 | RETVAL |
1222 | RETVAL |
1181 | |
1223 | |
|
|
1224 | PROTOTYPES: DISABLE |
|
|
1225 | |
1182 | MODULE = IO::AIO PACKAGE = IO::AIO::CB |
1226 | MODULE = IO::AIO PACKAGE = IO::AIO::REQ |
1183 | |
1227 | |
1184 | void |
1228 | void |
1185 | cancel (aio_req_ornot req) |
1229 | cancel (aio_req_ornot req) |
1186 | PROTOTYPE: |
1230 | PROTOTYPE: |
1187 | CODE: |
1231 | CODE: |
1188 | req->cancelled = 1; |
1232 | req_cancel (req); |
1189 | |
1233 | |
|
|
1234 | MODULE = IO::AIO PACKAGE = IO::AIO::GRP |
|
|
1235 | |
|
|
1236 | void |
|
|
1237 | add (aio_req grp, ...) |
|
|
1238 | PPCODE: |
|
|
1239 | { |
|
|
1240 | int i; |
|
|
1241 | |
|
|
1242 | for (i = 1; i < items; ++i ) |
|
|
1243 | { |
|
|
1244 | if (GIMME_V != G_VOID) |
|
|
1245 | XPUSHs (sv_2mortal (newSVsv (ST (i)))); |
|
|
1246 | |
|
|
1247 | aio_req req = SvAIO_REQ (ST (i)); |
|
|
1248 | |
|
|
1249 | if (req) |
|
|
1250 | { |
|
|
1251 | req->grp_prev = grp; |
|
|
1252 | req->grp_next = grp->grp_next; |
|
|
1253 | grp->grp_next->grp_prev = req; |
|
|
1254 | grp->grp_next = req; |
|
|
1255 | |
|
|
1256 | req->grp = grp; |
|
|
1257 | } |
|
|
1258 | } |
|
|
1259 | } |
|
|
1260 | |
|
|
1261 | void |
|
|
1262 | result (aio_req grp, ...) |
|
|
1263 | CODE: |
|
|
1264 | { |
|
|
1265 | int i; |
|
|
1266 | AV *av = newAV (); |
|
|
1267 | |
|
|
1268 | if (grp->data) |
|
|
1269 | SvREFCNT_dec (grp->data); |
|
|
1270 | |
|
|
1271 | for (i = 1; i < items; ++i ) |
|
|
1272 | av_push (av, newSVsv (ST (i))); |
|
|
1273 | |
|
|
1274 | grp->data = (SV *)av; |
|
|
1275 | } |
|
|
1276 | |