… | |
… | |
8 | #include "autoconf/config.h" |
8 | #include "autoconf/config.h" |
9 | |
9 | |
10 | #include <pthread.h> |
10 | #include <pthread.h> |
11 | |
11 | |
12 | #include <stddef.h> |
12 | #include <stddef.h> |
|
|
13 | #include <errno.h> |
|
|
14 | #include <sys/time.h> |
|
|
15 | #include <sys/select.h> |
13 | #include <sys/types.h> |
16 | #include <sys/types.h> |
14 | #include <sys/stat.h> |
17 | #include <sys/stat.h> |
15 | #include <limits.h> |
18 | #include <limits.h> |
16 | #include <unistd.h> |
19 | #include <unistd.h> |
17 | #include <fcntl.h> |
20 | #include <fcntl.h> |
… | |
… | |
31 | # else |
34 | # else |
32 | # error sendfile support requested but not available |
35 | # error sendfile support requested but not available |
33 | # endif |
36 | # endif |
34 | #endif |
37 | #endif |
35 | |
38 | |
|
|
39 | /* used for struct dirent, AIX doesn't provide it */ |
|
|
40 | #ifndef NAME_MAX |
|
|
41 | # define NAME_MAX 4096 |
|
|
42 | #endif |
|
|
43 | |
36 | #if __ia64 |
44 | #if __ia64 |
37 | # define STACKSIZE 65536 |
45 | # define STACKSIZE 65536 |
38 | #else |
46 | #else |
39 | # define STACKSIZE 8192 |
47 | # define STACKSIZE 8192 |
40 | #endif |
48 | #endif |
… | |
… | |
44 | REQ_OPEN, REQ_CLOSE, |
52 | REQ_OPEN, REQ_CLOSE, |
45 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
53 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
46 | REQ_SENDFILE, |
54 | REQ_SENDFILE, |
47 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
55 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
48 | REQ_FSYNC, REQ_FDATASYNC, |
56 | REQ_FSYNC, REQ_FDATASYNC, |
49 | REQ_UNLINK, REQ_RMDIR, |
57 | REQ_UNLINK, REQ_RMDIR, REQ_RENAME, |
50 | REQ_READDIR, |
58 | REQ_READDIR, |
51 | REQ_SYMLINK, |
59 | REQ_LINK, REQ_SYMLINK, |
|
|
60 | REQ_GROUP, REQ_NOP, |
|
|
61 | REQ_SLEEP, |
52 | }; |
62 | }; |
53 | |
63 | |
|
|
64 | #define AIO_REQ_KLASS "IO::AIO::REQ" |
|
|
65 | #define AIO_GRP_KLASS "IO::AIO::GRP" |
|
|
66 | |
54 | typedef struct aio_cb { |
67 | typedef struct aio_cb |
|
|
68 | { |
55 | struct aio_cb *volatile next; |
69 | struct aio_cb *volatile next; |
56 | |
70 | |
57 | int type; |
71 | struct aio_cb *grp, *grp_prev, *grp_next, *grp_first; |
58 | |
72 | |
59 | /* should receive a cleanup, with unions */ |
73 | SV *self; /* the perl counterpart of this request, if any */ |
60 | int fd, fd2; |
74 | |
|
|
75 | SV *data, *callback; |
|
|
76 | SV *fh, *fh2; |
|
|
77 | void *dataptr, *data2ptr; |
|
|
78 | Stat_t *statdata; |
61 | off_t offset; |
79 | off_t offset; |
62 | size_t length; |
80 | size_t length; |
63 | ssize_t result; |
81 | ssize_t result; |
|
|
82 | |
|
|
83 | int type; |
|
|
84 | int fd, fd2; |
|
|
85 | int errorno; |
|
|
86 | STRLEN dataoffset; |
64 | mode_t mode; /* open */ |
87 | mode_t mode; /* open */ |
65 | int errorno; |
88 | unsigned char pri; |
66 | SV *data, *callback; |
89 | unsigned char flags; |
67 | SV *fh, *fh2; |
|
|
68 | void *dataptr, *data2ptr; |
|
|
69 | STRLEN dataoffset; |
|
|
70 | |
|
|
71 | Stat_t *statdata; |
|
|
72 | } aio_cb; |
90 | } aio_cb; |
73 | |
91 | |
|
|
92 | enum { |
|
|
93 | FLAG_CANCELLED = 0x01, |
|
|
94 | }; |
|
|
95 | |
74 | typedef aio_cb *aio_req; |
96 | typedef aio_cb *aio_req; |
|
|
97 | typedef aio_cb *aio_req_ornot; |
75 | |
98 | |
76 | static int started, wanted; |
99 | static int started, wanted; |
77 | static volatile int nreqs; |
100 | static volatile int nreqs; |
78 | static int max_outstanding = 1<<30; |
101 | static int max_outstanding = 1<<30; |
79 | static int respipe [2]; |
102 | static int respipe [2]; |
… | |
… | |
83 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
106 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
84 | |
107 | |
85 | static volatile aio_req reqs, reqe; /* queue start, queue end */ |
108 | static volatile aio_req reqs, reqe; /* queue start, queue end */ |
86 | static volatile aio_req ress, rese; /* queue start, queue end */ |
109 | static volatile aio_req ress, rese; /* queue start, queue end */ |
87 | |
110 | |
|
|
111 | static void req_invoke (aio_req req); |
88 | static void free_req (aio_req req) |
112 | static void req_free (aio_req req); |
89 | { |
|
|
90 | if (req->data) |
|
|
91 | SvREFCNT_dec (req->data); |
|
|
92 | |
113 | |
|
|
114 | /* must be called at most once */ |
|
|
115 | static SV *req_sv (aio_req req, const char *klass) |
|
|
116 | { |
93 | if (req->fh) |
117 | if (!req->self) |
94 | SvREFCNT_dec (req->fh); |
118 | { |
|
|
119 | req->self = (SV *)newHV (); |
|
|
120 | sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0); |
|
|
121 | } |
95 | |
122 | |
96 | if (req->fh2) |
123 | return sv_2mortal (sv_bless (newRV_inc (req->self), gv_stashpv (klass, 1))); |
|
|
124 | } |
|
|
125 | |
|
|
126 | static aio_req SvAIO_REQ (SV *sv) |
|
|
127 | { |
|
|
128 | MAGIC *mg; |
|
|
129 | |
|
|
130 | if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv)) |
|
|
131 | croak ("object of class " AIO_REQ_KLASS " expected"); |
|
|
132 | |
|
|
133 | mg = mg_find (SvRV (sv), PERL_MAGIC_ext); |
|
|
134 | |
|
|
135 | return mg ? (aio_req)mg->mg_ptr : 0; |
|
|
136 | } |
|
|
137 | |
|
|
138 | static void aio_grp_feed (aio_req grp) |
|
|
139 | { |
|
|
140 | while (grp->length < grp->fd2 && !(grp->flags & FLAG_CANCELLED)) |
|
|
141 | { |
|
|
142 | int old_len = grp->length; |
|
|
143 | |
|
|
144 | if (grp->fh2 && SvOK (grp->fh2)) |
|
|
145 | { |
|
|
146 | dSP; |
|
|
147 | |
|
|
148 | ENTER; |
|
|
149 | SAVETMPS; |
|
|
150 | PUSHMARK (SP); |
|
|
151 | XPUSHs (req_sv (grp, AIO_GRP_KLASS)); |
|
|
152 | PUTBACK; |
|
|
153 | call_sv (grp->fh2, G_VOID | G_EVAL); |
|
|
154 | SPAGAIN; |
|
|
155 | FREETMPS; |
|
|
156 | LEAVE; |
|
|
157 | } |
|
|
158 | |
|
|
159 | /* stop if no progress has been made */ |
|
|
160 | if (old_len == grp->length) |
|
|
161 | { |
97 | SvREFCNT_dec (req->fh2); |
162 | SvREFCNT_dec (grp->fh2); |
98 | |
163 | grp->fh2 = 0; |
99 | if (req->statdata) |
164 | break; |
100 | Safefree (req->statdata); |
165 | } |
101 | |
166 | } |
102 | if (req->callback) |
|
|
103 | SvREFCNT_dec (req->callback); |
|
|
104 | |
|
|
105 | if (req->type == REQ_READDIR && req->result >= 0) |
|
|
106 | free (req->data2ptr); |
|
|
107 | |
|
|
108 | Safefree (req); |
|
|
109 | } |
167 | } |
110 | |
168 | |
111 | static void |
169 | static void aio_grp_dec (aio_req grp) |
112 | poll_wait () |
170 | { |
|
|
171 | --grp->length; |
|
|
172 | |
|
|
173 | /* call feeder, if applicable */ |
|
|
174 | aio_grp_feed (grp); |
|
|
175 | |
|
|
176 | /* finish, if done */ |
|
|
177 | if (!grp->length && grp->fd) |
|
|
178 | { |
|
|
179 | req_invoke (grp); |
|
|
180 | req_free (grp); |
|
|
181 | } |
|
|
182 | } |
|
|
183 | |
|
|
184 | static void poll_wait () |
113 | { |
185 | { |
114 | if (nreqs && !ress) |
186 | if (nreqs && !ress) |
115 | { |
187 | { |
116 | fd_set rfd; |
188 | fd_set rfd; |
117 | FD_ZERO(&rfd); |
189 | FD_ZERO(&rfd); |
… | |
… | |
119 | |
191 | |
120 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
192 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
121 | } |
193 | } |
122 | } |
194 | } |
123 | |
195 | |
124 | static int |
196 | static void req_invoke (aio_req req) |
125 | poll_cb () |
197 | { |
|
|
198 | dSP; |
|
|
199 | int errorno = errno; |
|
|
200 | |
|
|
201 | if (req->flags & FLAG_CANCELLED || !SvOK (req->callback)) |
|
|
202 | return; |
|
|
203 | |
|
|
204 | errno = req->errorno; |
|
|
205 | |
|
|
206 | ENTER; |
|
|
207 | SAVETMPS; |
|
|
208 | PUSHMARK (SP); |
|
|
209 | EXTEND (SP, 1); |
|
|
210 | |
|
|
211 | switch (req->type) |
|
|
212 | { |
|
|
213 | case REQ_READDIR: |
|
|
214 | { |
|
|
215 | SV *rv = &PL_sv_undef; |
|
|
216 | |
|
|
217 | if (req->result >= 0) |
|
|
218 | { |
|
|
219 | char *buf = req->data2ptr; |
|
|
220 | AV *av = newAV (); |
|
|
221 | |
|
|
222 | while (req->result) |
|
|
223 | { |
|
|
224 | SV *sv = newSVpv (buf, 0); |
|
|
225 | |
|
|
226 | av_push (av, sv); |
|
|
227 | buf += SvCUR (sv) + 1; |
|
|
228 | req->result--; |
|
|
229 | } |
|
|
230 | |
|
|
231 | rv = sv_2mortal (newRV_noinc ((SV *)av)); |
|
|
232 | } |
|
|
233 | |
|
|
234 | PUSHs (rv); |
|
|
235 | } |
|
|
236 | break; |
|
|
237 | |
|
|
238 | case REQ_OPEN: |
|
|
239 | { |
|
|
240 | /* convert fd to fh */ |
|
|
241 | SV *fh; |
|
|
242 | |
|
|
243 | PUSHs (sv_2mortal (newSViv (req->result))); |
|
|
244 | PUTBACK; |
|
|
245 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
|
|
246 | SPAGAIN; |
|
|
247 | |
|
|
248 | fh = SvREFCNT_inc (POPs); |
|
|
249 | |
|
|
250 | PUSHMARK (SP); |
|
|
251 | XPUSHs (sv_2mortal (fh)); |
|
|
252 | } |
|
|
253 | break; |
|
|
254 | |
|
|
255 | case REQ_GROUP: |
|
|
256 | req->fd = 2; /* mark group as finished */ |
|
|
257 | |
|
|
258 | if (req->data) |
|
|
259 | { |
|
|
260 | int i; |
|
|
261 | AV *av = (AV *)req->data; |
|
|
262 | |
|
|
263 | EXTEND (SP, AvFILL (av) + 1); |
|
|
264 | for (i = 0; i <= AvFILL (av); ++i) |
|
|
265 | PUSHs (*av_fetch (av, i, 0)); |
|
|
266 | } |
|
|
267 | break; |
|
|
268 | |
|
|
269 | case REQ_NOP: |
|
|
270 | case REQ_SLEEP: |
|
|
271 | break; |
|
|
272 | |
|
|
273 | default: |
|
|
274 | PUSHs (sv_2mortal (newSViv (req->result))); |
|
|
275 | break; |
|
|
276 | } |
|
|
277 | |
|
|
278 | |
|
|
279 | PUTBACK; |
|
|
280 | call_sv (req->callback, G_VOID | G_EVAL); |
|
|
281 | SPAGAIN; |
|
|
282 | |
|
|
283 | FREETMPS; |
|
|
284 | LEAVE; |
|
|
285 | |
|
|
286 | errno = errorno; |
|
|
287 | |
|
|
288 | if (SvTRUE (ERRSV)) |
|
|
289 | { |
|
|
290 | req_free (req); |
|
|
291 | croak (0); |
|
|
292 | } |
|
|
293 | } |
|
|
294 | |
|
|
295 | static void req_free (aio_req req) |
|
|
296 | { |
|
|
297 | if (req->grp) |
|
|
298 | { |
|
|
299 | aio_req grp = req->grp; |
|
|
300 | |
|
|
301 | /* unlink request */ |
|
|
302 | if (req->grp_next) req->grp_next->grp_prev = req->grp_prev; |
|
|
303 | if (req->grp_prev) req->grp_prev->grp_next = req->grp_next; |
|
|
304 | |
|
|
305 | if (grp->grp_first == req) |
|
|
306 | grp->grp_first = req->grp_next; |
|
|
307 | |
|
|
308 | aio_grp_dec (grp); |
|
|
309 | } |
|
|
310 | |
|
|
311 | if (req->self) |
|
|
312 | { |
|
|
313 | sv_unmagic (req->self, PERL_MAGIC_ext); |
|
|
314 | SvREFCNT_dec (req->self); |
|
|
315 | } |
|
|
316 | |
|
|
317 | SvREFCNT_dec (req->data); |
|
|
318 | SvREFCNT_dec (req->fh); |
|
|
319 | SvREFCNT_dec (req->fh2); |
|
|
320 | SvREFCNT_dec (req->callback); |
|
|
321 | Safefree (req->statdata); |
|
|
322 | |
|
|
323 | if (req->type == REQ_READDIR && req->result >= 0) |
|
|
324 | free (req->data2ptr); |
|
|
325 | |
|
|
326 | Safefree (req); |
|
|
327 | } |
|
|
328 | |
|
|
329 | static void req_cancel (aio_req req) |
|
|
330 | { |
|
|
331 | req->flags |= FLAG_CANCELLED; |
|
|
332 | |
|
|
333 | if (req->type == REQ_GROUP) |
|
|
334 | { |
|
|
335 | aio_req sub; |
|
|
336 | |
|
|
337 | for (sub = req->grp_first; sub; sub = sub->grp_next) |
|
|
338 | req_cancel (sub); |
|
|
339 | } |
|
|
340 | } |
|
|
341 | |
|
|
342 | static int poll_cb () |
126 | { |
343 | { |
127 | dSP; |
344 | dSP; |
128 | int count = 0; |
345 | int count = 0; |
129 | int do_croak = 0; |
346 | int do_croak = 0; |
130 | aio_req req; |
347 | aio_req req; |
… | |
… | |
152 | pthread_mutex_unlock (&reslock); |
369 | pthread_mutex_unlock (&reslock); |
153 | |
370 | |
154 | if (!req) |
371 | if (!req) |
155 | break; |
372 | break; |
156 | |
373 | |
157 | nreqs--; |
374 | --nreqs; |
158 | |
375 | |
159 | if (req->type == REQ_QUIT) |
376 | if (req->type == REQ_QUIT) |
160 | started--; |
377 | started--; |
|
|
378 | else if (req->type == REQ_GROUP && req->length) |
|
|
379 | { |
|
|
380 | req->fd = 1; /* mark request as delayed */ |
|
|
381 | continue; |
|
|
382 | } |
161 | else |
383 | else |
162 | { |
384 | { |
163 | int errorno = errno; |
|
|
164 | errno = req->errorno; |
|
|
165 | |
|
|
166 | if (req->type == REQ_READ) |
385 | if (req->type == REQ_READ) |
167 | SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); |
386 | SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); |
168 | |
387 | |
169 | if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE)) |
388 | if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE)) |
170 | SvREADONLY_off (req->data); |
389 | SvREADONLY_off (req->data); |
… | |
… | |
174 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
393 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
175 | PL_laststatval = req->result; |
394 | PL_laststatval = req->result; |
176 | PL_statcache = *(req->statdata); |
395 | PL_statcache = *(req->statdata); |
177 | } |
396 | } |
178 | |
397 | |
179 | ENTER; |
398 | req_invoke (req); |
180 | PUSHMARK (SP); |
|
|
181 | |
399 | |
182 | if (req->type == REQ_READDIR) |
|
|
183 | { |
|
|
184 | SV *rv = &PL_sv_undef; |
|
|
185 | |
|
|
186 | if (req->result >= 0) |
|
|
187 | { |
|
|
188 | char *buf = req->data2ptr; |
|
|
189 | AV *av = newAV (); |
|
|
190 | |
|
|
191 | while (req->result) |
|
|
192 | { |
|
|
193 | SV *sv = newSVpv (buf, 0); |
|
|
194 | |
|
|
195 | av_push (av, sv); |
|
|
196 | buf += SvCUR (sv) + 1; |
|
|
197 | req->result--; |
|
|
198 | } |
|
|
199 | |
|
|
200 | rv = sv_2mortal (newRV_noinc ((SV *)av)); |
|
|
201 | } |
|
|
202 | |
|
|
203 | XPUSHs (rv); |
|
|
204 | } |
|
|
205 | else |
|
|
206 | { |
|
|
207 | XPUSHs (sv_2mortal (newSViv (req->result))); |
|
|
208 | |
|
|
209 | if (req->type == REQ_OPEN) |
|
|
210 | { |
|
|
211 | /* convert fd to fh */ |
|
|
212 | SV *fh; |
|
|
213 | |
|
|
214 | PUTBACK; |
|
|
215 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
|
|
216 | SPAGAIN; |
|
|
217 | |
|
|
218 | fh = SvREFCNT_inc (POPs); |
|
|
219 | |
|
|
220 | PUSHMARK (SP); |
|
|
221 | XPUSHs (sv_2mortal (fh)); |
|
|
222 | } |
|
|
223 | } |
|
|
224 | |
|
|
225 | if (SvOK (req->callback)) |
|
|
226 | { |
|
|
227 | PUTBACK; |
|
|
228 | call_sv (req->callback, G_VOID | G_EVAL); |
|
|
229 | SPAGAIN; |
|
|
230 | |
|
|
231 | if (SvTRUE (ERRSV)) |
|
|
232 | { |
|
|
233 | free_req (req); |
|
|
234 | croak (0); |
|
|
235 | } |
|
|
236 | } |
|
|
237 | |
|
|
238 | LEAVE; |
|
|
239 | |
|
|
240 | errno = errorno; |
|
|
241 | count++; |
400 | count++; |
242 | } |
401 | } |
243 | |
402 | |
244 | free_req (req); |
403 | req_free (req); |
245 | } |
404 | } |
246 | |
405 | |
247 | return count; |
406 | return count; |
248 | } |
407 | } |
249 | |
408 | |
250 | static void *aio_proc(void *arg); |
409 | static void *aio_proc(void *arg); |
251 | |
410 | |
252 | static void |
|
|
253 | start_thread (void) |
411 | static void start_thread (void) |
254 | { |
412 | { |
255 | sigset_t fullsigset, oldsigset; |
413 | sigset_t fullsigset, oldsigset; |
256 | pthread_t tid; |
414 | pthread_t tid; |
257 | pthread_attr_t attr; |
415 | pthread_attr_t attr; |
258 | |
416 | |
… | |
… | |
267 | started++; |
425 | started++; |
268 | |
426 | |
269 | sigprocmask (SIG_SETMASK, &oldsigset, 0); |
427 | sigprocmask (SIG_SETMASK, &oldsigset, 0); |
270 | } |
428 | } |
271 | |
429 | |
272 | static void |
430 | static void req_send (aio_req req) |
273 | send_req (aio_req req) |
|
|
274 | { |
431 | { |
275 | while (started < wanted && nreqs >= started) |
432 | while (started < wanted && nreqs >= started) |
276 | start_thread (); |
433 | start_thread (); |
277 | |
434 | |
278 | nreqs++; |
435 | ++nreqs; |
279 | |
436 | |
280 | pthread_mutex_lock (&reqlock); |
437 | pthread_mutex_lock (&reqlock); |
281 | |
438 | |
282 | req->next = 0; |
439 | req->next = 0; |
283 | |
440 | |
… | |
… | |
302 | |
459 | |
303 | poll_wait (); |
460 | poll_wait (); |
304 | } |
461 | } |
305 | } |
462 | } |
306 | |
463 | |
307 | static void |
464 | static void end_thread (void) |
308 | end_thread (void) |
|
|
309 | { |
465 | { |
310 | aio_req req; |
466 | aio_req req; |
311 | Newz (0, req, 1, aio_cb); |
467 | Newz (0, req, 1, aio_cb); |
312 | req->type = REQ_QUIT; |
468 | req->type = REQ_QUIT; |
313 | |
469 | |
314 | send_req (req); |
470 | req_send (req); |
315 | } |
471 | } |
316 | |
472 | |
317 | static void min_parallel (int nthreads) |
473 | static void min_parallel (int nthreads) |
318 | { |
474 | { |
319 | if (wanted < nthreads) |
475 | if (wanted < nthreads) |
… | |
… | |
364 | * normal read/write by using a mutex. slows down execution a lot, |
520 | * normal read/write by using a mutex. slows down execution a lot, |
365 | * but that's your problem, not mine. |
521 | * but that's your problem, not mine. |
366 | */ |
522 | */ |
367 | static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER; |
523 | static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER; |
368 | |
524 | |
369 | static ssize_t |
525 | static ssize_t pread (int fd, void *buf, size_t count, off_t offset) |
370 | pread (int fd, void *buf, size_t count, off_t offset) |
|
|
371 | { |
526 | { |
372 | ssize_t res; |
527 | ssize_t res; |
373 | off_t ooffset; |
528 | off_t ooffset; |
374 | |
529 | |
375 | pthread_mutex_lock (&preadwritelock); |
530 | pthread_mutex_lock (&preadwritelock); |
… | |
… | |
380 | pthread_mutex_unlock (&preadwritelock); |
535 | pthread_mutex_unlock (&preadwritelock); |
381 | |
536 | |
382 | return res; |
537 | return res; |
383 | } |
538 | } |
384 | |
539 | |
385 | static ssize_t |
|
|
386 | pwrite (int fd, void *buf, size_t count, off_t offset) |
540 | static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset) |
387 | { |
541 | { |
388 | ssize_t res; |
542 | ssize_t res; |
389 | off_t ooffset; |
543 | off_t ooffset; |
390 | |
544 | |
391 | pthread_mutex_lock (&preadwritelock); |
545 | pthread_mutex_lock (&preadwritelock); |
… | |
… | |
404 | #endif |
558 | #endif |
405 | |
559 | |
406 | #if !HAVE_READAHEAD |
560 | #if !HAVE_READAHEAD |
407 | # define readahead aio_readahead |
561 | # define readahead aio_readahead |
408 | |
562 | |
409 | static ssize_t |
|
|
410 | readahead (int fd, off_t offset, size_t count) |
563 | static ssize_t readahead (int fd, off_t offset, size_t count) |
411 | { |
564 | { |
412 | char readahead_buf[4096]; |
565 | char readahead_buf[4096]; |
413 | |
566 | |
414 | while (count > 0) |
567 | while (count > 0) |
415 | { |
568 | { |
… | |
… | |
427 | #if !HAVE_READDIR_R |
580 | #if !HAVE_READDIR_R |
428 | # define readdir_r aio_readdir_r |
581 | # define readdir_r aio_readdir_r |
429 | |
582 | |
430 | static pthread_mutex_t readdirlock = PTHREAD_MUTEX_INITIALIZER; |
583 | static pthread_mutex_t readdirlock = PTHREAD_MUTEX_INITIALIZER; |
431 | |
584 | |
432 | static int |
|
|
433 | readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) |
585 | static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) |
434 | { |
586 | { |
435 | struct dirent *e; |
587 | struct dirent *e; |
436 | int errorno; |
588 | int errorno; |
437 | |
589 | |
438 | pthread_mutex_lock (&readdirlock); |
590 | pthread_mutex_lock (&readdirlock); |
… | |
… | |
454 | return e ? 0 : -1; |
606 | return e ? 0 : -1; |
455 | } |
607 | } |
456 | #endif |
608 | #endif |
457 | |
609 | |
458 | /* sendfile always needs emulation */ |
610 | /* sendfile always needs emulation */ |
459 | static ssize_t |
|
|
460 | sendfile_ (int ofd, int ifd, off_t offset, size_t count) |
611 | static ssize_t sendfile_ (int ofd, int ifd, off_t offset, size_t count) |
461 | { |
612 | { |
462 | ssize_t res; |
613 | ssize_t res; |
463 | |
614 | |
464 | if (!count) |
615 | if (!count) |
465 | return 0; |
616 | return 0; |
… | |
… | |
499 | |
650 | |
500 | if (res < 0 && sbytes) |
651 | if (res < 0 && sbytes) |
501 | res = sbytes; |
652 | res = sbytes; |
502 | } |
653 | } |
503 | |
654 | |
|
|
655 | # endif |
504 | # else |
656 | #else |
505 | res = -1; |
657 | res = -1; |
506 | errno = ENOSYS; |
658 | errno = ENOSYS; |
507 | # endif |
|
|
508 | #endif |
659 | #endif |
509 | |
660 | |
510 | if (res < 0 |
661 | if (res < 0 |
511 | && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK |
662 | && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK |
512 | #if __solaris |
663 | #if __solaris |
… | |
… | |
517 | { |
668 | { |
518 | /* emulate sendfile. this is a major pain in the ass */ |
669 | /* emulate sendfile. this is a major pain in the ass */ |
519 | char buf[4096]; |
670 | char buf[4096]; |
520 | res = 0; |
671 | res = 0; |
521 | |
672 | |
522 | for (;;) |
673 | while (count) |
523 | { |
674 | { |
524 | ssize_t cnt; |
675 | ssize_t cnt; |
525 | |
676 | |
526 | cnt = pread (ifd, buf, 4096, offset); |
677 | cnt = pread (ifd, buf, count > 4096 ? 4096 : count, offset); |
527 | |
678 | |
528 | if (cnt <= 0) |
679 | if (cnt <= 0) |
529 | { |
680 | { |
530 | if (cnt && !res) res = -1; |
681 | if (cnt && !res) res = -1; |
531 | break; |
682 | break; |
… | |
… | |
539 | break; |
690 | break; |
540 | } |
691 | } |
541 | |
692 | |
542 | offset += cnt; |
693 | offset += cnt; |
543 | res += cnt; |
694 | res += cnt; |
|
|
695 | count -= cnt; |
544 | } |
696 | } |
545 | } |
697 | } |
546 | |
698 | |
547 | return res; |
699 | return res; |
548 | } |
700 | } |
549 | |
701 | |
550 | /* read a full directory */ |
702 | /* read a full directory */ |
551 | static int |
|
|
552 | scandir_ (const char *path, void **namesp) |
703 | static int scandir_ (const char *path, void **namesp) |
553 | { |
704 | { |
554 | DIR *dirp = opendir (path); |
705 | DIR *dirp = opendir (path); |
555 | union |
706 | union |
556 | { |
707 | { |
557 | struct dirent d; |
708 | struct dirent d; |
… | |
… | |
611 | return res; |
762 | return res; |
612 | } |
763 | } |
613 | |
764 | |
614 | /*****************************************************************************/ |
765 | /*****************************************************************************/ |
615 | |
766 | |
616 | static void * |
|
|
617 | aio_proc (void *thr_arg) |
767 | static void *aio_proc (void *thr_arg) |
618 | { |
768 | { |
619 | aio_req req; |
769 | aio_req req; |
620 | int type; |
770 | int type; |
621 | |
771 | |
622 | do |
772 | do |
… | |
… | |
641 | |
791 | |
642 | pthread_mutex_unlock (&reqlock); |
792 | pthread_mutex_unlock (&reqlock); |
643 | |
793 | |
644 | errno = 0; /* strictly unnecessary */ |
794 | errno = 0; /* strictly unnecessary */ |
645 | |
795 | |
646 | type = req->type; |
796 | if (!(req->flags & FLAG_CANCELLED)) |
647 | |
797 | switch (type = req->type) /* remember type for QUIT check */ |
648 | switch (type) |
|
|
649 | { |
798 | { |
650 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
799 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
651 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
800 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
652 | |
801 | |
653 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
802 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
654 | case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length); break; |
803 | case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length); break; |
655 | |
804 | |
656 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
805 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
657 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
806 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
658 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
807 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
659 | |
808 | |
660 | case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
809 | case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
661 | case REQ_CLOSE: req->result = close (req->fd); break; |
810 | case REQ_CLOSE: req->result = close (req->fd); break; |
662 | case REQ_UNLINK: req->result = unlink (req->dataptr); break; |
811 | case REQ_UNLINK: req->result = unlink (req->dataptr); break; |
663 | case REQ_RMDIR: req->result = rmdir (req->dataptr); break; |
812 | case REQ_RMDIR: req->result = rmdir (req->dataptr); break; |
|
|
813 | case REQ_RENAME: req->result = rename (req->data2ptr, req->dataptr); break; |
|
|
814 | case REQ_LINK: req->result = link (req->data2ptr, req->dataptr); break; |
664 | case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; |
815 | case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; |
665 | |
816 | |
666 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
817 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
667 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
818 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
668 | case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break; |
819 | case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break; |
669 | |
820 | |
|
|
821 | case REQ_SLEEP: |
|
|
822 | { |
|
|
823 | struct timeval tv; |
|
|
824 | |
|
|
825 | tv.tv_sec = req->fd; |
|
|
826 | tv.tv_usec = req->fd2; |
|
|
827 | |
|
|
828 | req->result = select (0, 0, 0, 0, &tv); |
|
|
829 | } |
|
|
830 | |
|
|
831 | case REQ_GROUP: |
|
|
832 | case REQ_NOP: |
670 | case REQ_QUIT: |
833 | case REQ_QUIT: |
671 | break; |
834 | break; |
672 | |
835 | |
673 | default: |
836 | default: |
674 | req->result = ENOSYS; |
837 | req->result = ENOSYS; |
675 | break; |
838 | break; |
676 | } |
839 | } |
677 | |
840 | |
678 | req->errorno = errno; |
841 | req->errorno = errno; |
679 | |
842 | |
680 | pthread_mutex_lock (&reslock); |
843 | pthread_mutex_lock (&reslock); |
681 | |
844 | |
… | |
… | |
735 | |
898 | |
736 | while (reqs) |
899 | while (reqs) |
737 | { |
900 | { |
738 | prv = reqs; |
901 | prv = reqs; |
739 | reqs = prv->next; |
902 | reqs = prv->next; |
740 | free_req (prv); |
903 | req_free (prv); |
741 | } |
904 | } |
742 | |
905 | |
743 | reqs = reqe = 0; |
906 | reqs = reqe = 0; |
744 | |
907 | |
745 | while (ress) |
908 | while (ress) |
746 | { |
909 | { |
747 | prv = ress; |
910 | prv = ress; |
748 | ress = prv->next; |
911 | ress = prv->next; |
749 | free_req (prv); |
912 | req_free (prv); |
750 | } |
913 | } |
751 | |
914 | |
752 | ress = rese = 0; |
915 | ress = rese = 0; |
753 | |
916 | |
754 | close (respipe [0]); |
917 | close (respipe [0]); |
… | |
… | |
760 | |
923 | |
761 | #define dREQ \ |
924 | #define dREQ \ |
762 | aio_req req; \ |
925 | aio_req req; \ |
763 | \ |
926 | \ |
764 | if (SvOK (callback) && !SvROK (callback)) \ |
927 | if (SvOK (callback) && !SvROK (callback)) \ |
765 | croak ("clalback must be undef or of reference type"); \ |
928 | croak ("callback must be undef or of reference type"); \ |
766 | \ |
929 | \ |
767 | Newz (0, req, 1, aio_cb); \ |
930 | Newz (0, req, 1, aio_cb); \ |
768 | if (!req) \ |
931 | if (!req) \ |
769 | croak ("out of memory during aio_req allocation"); \ |
932 | croak ("out of memory during aio_req allocation"); \ |
770 | \ |
933 | \ |
771 | req->callback = newSVsv (callback); |
934 | req->callback = newSVsv (callback) |
|
|
935 | |
|
|
936 | #define REQ_SEND \ |
|
|
937 | req_send (req); \ |
|
|
938 | \ |
|
|
939 | if (GIMME_V != G_VOID) \ |
|
|
940 | XPUSHs (req_sv (req, AIO_REQ_KLASS)); |
772 | |
941 | |
773 | MODULE = IO::AIO PACKAGE = IO::AIO |
942 | MODULE = IO::AIO PACKAGE = IO::AIO |
774 | |
943 | |
775 | PROTOTYPES: ENABLE |
944 | PROTOTYPES: ENABLE |
776 | |
945 | |
777 | BOOT: |
946 | BOOT: |
778 | { |
947 | { |
|
|
948 | HV *stash = gv_stashpv ("IO::AIO", 1); |
|
|
949 | newCONSTSUB (stash, "EXDEV", newSViv (EXDEV)); |
|
|
950 | newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY)); |
|
|
951 | newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY)); |
|
|
952 | |
779 | create_pipe (); |
953 | create_pipe (); |
780 | pthread_atfork (atfork_prepare, atfork_parent, atfork_child); |
954 | pthread_atfork (atfork_prepare, atfork_parent, atfork_child); |
781 | } |
955 | } |
782 | |
956 | |
783 | void |
957 | void |
784 | min_parallel(nthreads) |
958 | min_parallel (nthreads) |
785 | int nthreads |
959 | int nthreads |
786 | PROTOTYPE: $ |
960 | PROTOTYPE: $ |
787 | |
961 | |
788 | void |
962 | void |
789 | max_parallel(nthreads) |
963 | max_parallel (nthreads) |
790 | int nthreads |
964 | int nthreads |
791 | PROTOTYPE: $ |
965 | PROTOTYPE: $ |
792 | |
966 | |
793 | int |
967 | int |
794 | max_outstanding(nreqs) |
968 | max_outstanding (nreqs) |
795 | int nreqs |
969 | int nreqs |
796 | PROTOTYPE: $ |
970 | PROTOTYPE: $ |
797 | CODE: |
971 | CODE: |
798 | RETVAL = max_outstanding; |
972 | RETVAL = max_outstanding; |
799 | max_outstanding = nreqs; |
973 | max_outstanding = nreqs; |
800 | |
974 | |
801 | void |
975 | void |
802 | aio_open(pathname,flags,mode,callback=&PL_sv_undef) |
976 | aio_open (pathname,flags,mode,callback=&PL_sv_undef) |
803 | SV * pathname |
977 | SV * pathname |
804 | int flags |
978 | int flags |
805 | int mode |
979 | int mode |
806 | SV * callback |
980 | SV * callback |
807 | PROTOTYPE: $$$;$ |
981 | PROTOTYPE: $$$;$ |
808 | CODE: |
982 | PPCODE: |
809 | { |
983 | { |
810 | dREQ; |
984 | dREQ; |
811 | |
985 | |
812 | req->type = REQ_OPEN; |
986 | req->type = REQ_OPEN; |
813 | req->data = newSVsv (pathname); |
987 | req->data = newSVsv (pathname); |
814 | req->dataptr = SvPVbyte_nolen (req->data); |
988 | req->dataptr = SvPVbyte_nolen (req->data); |
815 | req->fd = flags; |
989 | req->fd = flags; |
816 | req->mode = mode; |
990 | req->mode = mode; |
817 | |
991 | |
818 | send_req (req); |
992 | REQ_SEND; |
819 | } |
993 | } |
820 | |
994 | |
821 | void |
995 | void |
822 | aio_close(fh,callback=&PL_sv_undef) |
996 | aio_close (fh,callback=&PL_sv_undef) |
823 | SV * fh |
997 | SV * fh |
824 | SV * callback |
998 | SV * callback |
825 | PROTOTYPE: $;$ |
999 | PROTOTYPE: $;$ |
826 | ALIAS: |
1000 | ALIAS: |
827 | aio_close = REQ_CLOSE |
1001 | aio_close = REQ_CLOSE |
828 | aio_fsync = REQ_FSYNC |
1002 | aio_fsync = REQ_FSYNC |
829 | aio_fdatasync = REQ_FDATASYNC |
1003 | aio_fdatasync = REQ_FDATASYNC |
830 | CODE: |
1004 | PPCODE: |
831 | { |
1005 | { |
832 | dREQ; |
1006 | dREQ; |
833 | |
1007 | |
834 | req->type = ix; |
1008 | req->type = ix; |
835 | req->fh = newSVsv (fh); |
1009 | req->fh = newSVsv (fh); |
836 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
1010 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
837 | |
1011 | |
838 | send_req (req); |
1012 | REQ_SEND (req); |
839 | } |
1013 | } |
840 | |
1014 | |
841 | void |
1015 | void |
842 | aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
1016 | aio_read (fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
843 | SV * fh |
1017 | SV * fh |
844 | UV offset |
1018 | UV offset |
845 | UV length |
1019 | UV length |
846 | SV * data |
1020 | SV * data |
847 | UV dataoffset |
1021 | UV dataoffset |
848 | SV * callback |
1022 | SV * callback |
849 | ALIAS: |
1023 | ALIAS: |
850 | aio_read = REQ_READ |
1024 | aio_read = REQ_READ |
851 | aio_write = REQ_WRITE |
1025 | aio_write = REQ_WRITE |
852 | PROTOTYPE: $$$$$;$ |
1026 | PROTOTYPE: $$$$$;$ |
853 | CODE: |
1027 | PPCODE: |
854 | { |
1028 | { |
855 | aio_req req; |
1029 | aio_req req; |
856 | STRLEN svlen; |
1030 | STRLEN svlen; |
857 | char *svptr = SvPVbyte (data, svlen); |
1031 | char *svptr = SvPVbyte (data, svlen); |
858 | |
1032 | |
… | |
… | |
896 | { |
1070 | { |
897 | SvREADONLY_on (data); |
1071 | SvREADONLY_on (data); |
898 | req->data2ptr = (void *)data; |
1072 | req->data2ptr = (void *)data; |
899 | } |
1073 | } |
900 | |
1074 | |
901 | send_req (req); |
1075 | REQ_SEND; |
902 | } |
1076 | } |
903 | } |
1077 | } |
904 | |
1078 | |
905 | void |
1079 | void |
906 | aio_sendfile(out_fh,in_fh,in_offset,length,callback=&PL_sv_undef) |
1080 | aio_sendfile (out_fh,in_fh,in_offset,length,callback=&PL_sv_undef) |
907 | SV * out_fh |
1081 | SV * out_fh |
908 | SV * in_fh |
1082 | SV * in_fh |
909 | UV in_offset |
1083 | UV in_offset |
910 | UV length |
1084 | UV length |
911 | SV * callback |
1085 | SV * callback |
912 | PROTOTYPE: $$$$;$ |
1086 | PROTOTYPE: $$$$;$ |
913 | CODE: |
1087 | PPCODE: |
914 | { |
1088 | { |
915 | dREQ; |
1089 | dREQ; |
916 | |
1090 | |
917 | req->type = REQ_SENDFILE; |
1091 | req->type = REQ_SENDFILE; |
918 | req->fh = newSVsv (out_fh); |
1092 | req->fh = newSVsv (out_fh); |
… | |
… | |
920 | req->fh2 = newSVsv (in_fh); |
1094 | req->fh2 = newSVsv (in_fh); |
921 | req->fd2 = PerlIO_fileno (IoIFP (sv_2io (in_fh))); |
1095 | req->fd2 = PerlIO_fileno (IoIFP (sv_2io (in_fh))); |
922 | req->offset = in_offset; |
1096 | req->offset = in_offset; |
923 | req->length = length; |
1097 | req->length = length; |
924 | |
1098 | |
925 | send_req (req); |
1099 | REQ_SEND; |
926 | } |
1100 | } |
927 | |
1101 | |
928 | void |
1102 | void |
929 | aio_readahead(fh,offset,length,callback=&PL_sv_undef) |
1103 | aio_readahead (fh,offset,length,callback=&PL_sv_undef) |
930 | SV * fh |
1104 | SV * fh |
931 | UV offset |
1105 | UV offset |
932 | IV length |
1106 | IV length |
933 | SV * callback |
1107 | SV * callback |
934 | PROTOTYPE: $$$;$ |
1108 | PROTOTYPE: $$$;$ |
935 | CODE: |
1109 | PPCODE: |
936 | { |
1110 | { |
937 | dREQ; |
1111 | dREQ; |
938 | |
1112 | |
939 | req->type = REQ_READAHEAD; |
1113 | req->type = REQ_READAHEAD; |
940 | req->fh = newSVsv (fh); |
1114 | req->fh = newSVsv (fh); |
941 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
1115 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
942 | req->offset = offset; |
1116 | req->offset = offset; |
943 | req->length = length; |
1117 | req->length = length; |
944 | |
1118 | |
945 | send_req (req); |
1119 | REQ_SEND; |
946 | } |
1120 | } |
947 | |
1121 | |
948 | void |
1122 | void |
949 | aio_stat(fh_or_path,callback=&PL_sv_undef) |
1123 | aio_stat (fh_or_path,callback=&PL_sv_undef) |
950 | SV * fh_or_path |
1124 | SV * fh_or_path |
951 | SV * callback |
1125 | SV * callback |
952 | ALIAS: |
1126 | ALIAS: |
953 | aio_stat = REQ_STAT |
1127 | aio_stat = REQ_STAT |
954 | aio_lstat = REQ_LSTAT |
1128 | aio_lstat = REQ_LSTAT |
955 | CODE: |
1129 | PPCODE: |
956 | { |
1130 | { |
957 | dREQ; |
1131 | dREQ; |
958 | |
1132 | |
959 | New (0, req->statdata, 1, Stat_t); |
1133 | New (0, req->statdata, 1, Stat_t); |
960 | if (!req->statdata) |
1134 | if (!req->statdata) |
961 | { |
1135 | { |
962 | free_req (req); |
1136 | req_free (req); |
963 | croak ("out of memory during aio_req->statdata allocation"); |
1137 | croak ("out of memory during aio_req->statdata allocation"); |
964 | } |
1138 | } |
965 | |
1139 | |
966 | if (SvPOK (fh_or_path)) |
1140 | if (SvPOK (fh_or_path)) |
967 | { |
1141 | { |
… | |
… | |
974 | req->type = REQ_FSTAT; |
1148 | req->type = REQ_FSTAT; |
975 | req->fh = newSVsv (fh_or_path); |
1149 | req->fh = newSVsv (fh_or_path); |
976 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
1150 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
977 | } |
1151 | } |
978 | |
1152 | |
979 | send_req (req); |
1153 | REQ_SEND; |
980 | } |
1154 | } |
981 | |
1155 | |
982 | void |
1156 | void |
983 | aio_unlink(pathname,callback=&PL_sv_undef) |
1157 | aio_unlink (pathname,callback=&PL_sv_undef) |
984 | SV * pathname |
1158 | SV * pathname |
985 | SV * callback |
1159 | SV * callback |
986 | ALIAS: |
1160 | ALIAS: |
987 | aio_unlink = REQ_UNLINK |
1161 | aio_unlink = REQ_UNLINK |
988 | aio_rmdir = REQ_RMDIR |
1162 | aio_rmdir = REQ_RMDIR |
|
|
1163 | aio_readdir = REQ_READDIR |
989 | CODE: |
1164 | PPCODE: |
990 | { |
1165 | { |
991 | dREQ; |
1166 | dREQ; |
992 | |
1167 | |
993 | req->type = ix; |
1168 | req->type = ix; |
994 | req->data = newSVsv (pathname); |
1169 | req->data = newSVsv (pathname); |
995 | req->dataptr = SvPVbyte_nolen (req->data); |
1170 | req->dataptr = SvPVbyte_nolen (req->data); |
996 | |
1171 | |
997 | send_req (req); |
1172 | REQ_SEND; |
998 | } |
1173 | } |
999 | |
1174 | |
1000 | void |
1175 | void |
1001 | aio_symlink(oldpath,newpath,callback=&PL_sv_undef) |
1176 | aio_link (oldpath,newpath,callback=&PL_sv_undef) |
1002 | SV * oldpath |
1177 | SV * oldpath |
1003 | SV * newpath |
1178 | SV * newpath |
1004 | SV * callback |
1179 | SV * callback |
|
|
1180 | ALIAS: |
|
|
1181 | aio_link = REQ_LINK |
|
|
1182 | aio_symlink = REQ_SYMLINK |
|
|
1183 | aio_rename = REQ_RENAME |
1005 | CODE: |
1184 | PPCODE: |
1006 | { |
1185 | { |
1007 | dREQ; |
1186 | dREQ; |
1008 | |
1187 | |
1009 | req->type = REQ_SYMLINK; |
1188 | req->type = ix; |
1010 | req->fh = newSVsv (oldpath); |
1189 | req->fh = newSVsv (oldpath); |
1011 | req->data2ptr = SvPVbyte_nolen (req->fh); |
1190 | req->data2ptr = SvPVbyte_nolen (req->fh); |
1012 | req->data = newSVsv (newpath); |
1191 | req->data = newSVsv (newpath); |
1013 | req->dataptr = SvPVbyte_nolen (req->data); |
1192 | req->dataptr = SvPVbyte_nolen (req->data); |
1014 | |
1193 | |
1015 | send_req (req); |
1194 | REQ_SEND; |
1016 | } |
1195 | } |
1017 | |
1196 | |
1018 | void |
1197 | void |
1019 | aio_readdir(pathname,callback=&PL_sv_undef) |
1198 | aio_sleep (delay,callback=&PL_sv_undef) |
1020 | SV * pathname |
1199 | double delay |
1021 | SV * callback |
1200 | SV * callback |
1022 | CODE: |
1201 | PPCODE: |
1023 | { |
1202 | { |
1024 | dREQ; |
1203 | dREQ; |
1025 | |
1204 | |
1026 | req->type = REQ_READDIR; |
1205 | req->type = REQ_SLEEP; |
1027 | req->data = newSVsv (pathname); |
1206 | req->fd = delay < 0. ? 0 : delay; |
1028 | req->dataptr = SvPVbyte_nolen (req->data); |
1207 | req->fd2 = delay < 0. ? 0 : 1000. * (delay - req->fd); |
1029 | |
|
|
1030 | send_req (req); |
|
|
1031 | } |
|
|
1032 | |
1208 | |
|
|
1209 | REQ_SEND; |
|
|
1210 | } |
|
|
1211 | |
1033 | void |
1212 | void |
|
|
1213 | aio_group (callback=&PL_sv_undef) |
|
|
1214 | SV * callback |
|
|
1215 | PROTOTYPE: ;$ |
|
|
1216 | PPCODE: |
|
|
1217 | { |
|
|
1218 | dREQ; |
|
|
1219 | req->type = REQ_GROUP; |
|
|
1220 | req_send (req); |
|
|
1221 | XPUSHs (req_sv (req, AIO_GRP_KLASS)); |
|
|
1222 | } |
|
|
1223 | |
|
|
1224 | void |
|
|
1225 | aio_nop (callback=&PL_sv_undef) |
|
|
1226 | SV * callback |
|
|
1227 | PPCODE: |
|
|
1228 | { |
|
|
1229 | dREQ; |
|
|
1230 | |
|
|
1231 | req->type = REQ_NOP; |
|
|
1232 | |
|
|
1233 | REQ_SEND; |
|
|
1234 | } |
|
|
1235 | |
|
|
1236 | void |
1034 | flush() |
1237 | flush () |
1035 | PROTOTYPE: |
1238 | PROTOTYPE: |
1036 | CODE: |
1239 | CODE: |
1037 | while (nreqs) |
1240 | while (nreqs) |
1038 | { |
1241 | { |
1039 | poll_wait (); |
1242 | poll_wait (); |
… | |
… | |
1079 | CODE: |
1282 | CODE: |
1080 | RETVAL = nreqs; |
1283 | RETVAL = nreqs; |
1081 | OUTPUT: |
1284 | OUTPUT: |
1082 | RETVAL |
1285 | RETVAL |
1083 | |
1286 | |
|
|
1287 | PROTOTYPES: DISABLE |
|
|
1288 | |
|
|
1289 | MODULE = IO::AIO PACKAGE = IO::AIO::REQ |
|
|
1290 | |
|
|
1291 | void |
|
|
1292 | cancel (aio_req_ornot req) |
|
|
1293 | PROTOTYPE: |
|
|
1294 | CODE: |
|
|
1295 | req_cancel (req); |
|
|
1296 | |
|
|
1297 | void |
|
|
1298 | cb (aio_req req, SV *callback=&PL_sv_undef) |
|
|
1299 | CODE: |
|
|
1300 | SvREFCNT_dec (req->callback); |
|
|
1301 | req->callback = newSVsv (callback); |
|
|
1302 | |
|
|
1303 | MODULE = IO::AIO PACKAGE = IO::AIO::GRP |
|
|
1304 | |
|
|
1305 | void |
|
|
1306 | add (aio_req grp, ...) |
|
|
1307 | PPCODE: |
|
|
1308 | { |
|
|
1309 | int i; |
|
|
1310 | aio_req req; |
|
|
1311 | |
|
|
1312 | if (grp->fd == 2) |
|
|
1313 | croak ("cannot add requests to IO::AIO::GRP after the group finished"); |
|
|
1314 | |
|
|
1315 | for (i = 1; i < items; ++i ) |
|
|
1316 | { |
|
|
1317 | if (GIMME_V != G_VOID) |
|
|
1318 | XPUSHs (sv_2mortal (newSVsv (ST (i)))); |
|
|
1319 | |
|
|
1320 | req = SvAIO_REQ (ST (i)); |
|
|
1321 | |
|
|
1322 | if (req) |
|
|
1323 | { |
|
|
1324 | ++grp->length; |
|
|
1325 | req->grp = grp; |
|
|
1326 | |
|
|
1327 | req->grp_prev = 0; |
|
|
1328 | req->grp_next = grp->grp_first; |
|
|
1329 | |
|
|
1330 | if (grp->grp_first) |
|
|
1331 | grp->grp_first->grp_prev = req; |
|
|
1332 | |
|
|
1333 | grp->grp_first = req; |
|
|
1334 | } |
|
|
1335 | } |
|
|
1336 | } |
|
|
1337 | |
|
|
1338 | void |
|
|
1339 | result (aio_req grp, ...) |
|
|
1340 | CODE: |
|
|
1341 | { |
|
|
1342 | int i; |
|
|
1343 | AV *av = newAV (); |
|
|
1344 | |
|
|
1345 | for (i = 1; i < items; ++i ) |
|
|
1346 | av_push (av, newSVsv (ST (i))); |
|
|
1347 | |
|
|
1348 | SvREFCNT_dec (grp->data); |
|
|
1349 | grp->data = (SV *)av; |
|
|
1350 | } |
|
|
1351 | |
|
|
1352 | void |
|
|
1353 | feed_limit (aio_req grp, int limit) |
|
|
1354 | CODE: |
|
|
1355 | grp->fd2 = limit; |
|
|
1356 | aio_grp_feed (grp); |
|
|
1357 | |
|
|
1358 | void |
|
|
1359 | feed (aio_req grp, SV *callback=&PL_sv_undef) |
|
|
1360 | CODE: |
|
|
1361 | { |
|
|
1362 | SvREFCNT_dec (grp->fh2); |
|
|
1363 | grp->fh2 = newSVsv (callback); |
|
|
1364 | |
|
|
1365 | if (grp->fd2 <= 0) |
|
|
1366 | grp->fd2 = 2; |
|
|
1367 | |
|
|
1368 | aio_grp_feed (grp); |
|
|
1369 | } |
|
|
1370 | |