… | |
… | |
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> |
… | |
… | |
52 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
55 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
53 | REQ_FSYNC, REQ_FDATASYNC, |
56 | REQ_FSYNC, REQ_FDATASYNC, |
54 | REQ_UNLINK, REQ_RMDIR, REQ_RENAME, |
57 | REQ_UNLINK, REQ_RMDIR, REQ_RENAME, |
55 | REQ_READDIR, |
58 | REQ_READDIR, |
56 | REQ_LINK, REQ_SYMLINK, |
59 | REQ_LINK, REQ_SYMLINK, |
|
|
60 | REQ_SLEEP, |
|
|
61 | REQ_GROUP, |
57 | }; |
62 | }; |
58 | |
63 | |
|
|
64 | #define AIO_REQ_KLASS "IO::AIO::REQ" |
|
|
65 | #define AIO_GRP_KLASS "IO::AIO::GRP" |
|
|
66 | |
59 | typedef struct aio_cb { |
67 | typedef struct aio_cb |
|
|
68 | { |
|
|
69 | struct aio_cb *grp, *grp_prev, *grp_next; |
|
|
70 | |
60 | struct aio_cb *volatile next; |
71 | struct aio_cb *volatile next; |
61 | |
72 | |
62 | int type; |
73 | SV *self; /* the perl counterpart of this request, if any */ |
63 | |
74 | |
64 | /* should receive a cleanup, with unions */ |
75 | SV *data, *callback; |
65 | int fd, fd2; |
76 | SV *fh, *fh2; |
|
|
77 | void *dataptr, *data2ptr; |
|
|
78 | Stat_t *statdata; |
66 | off_t offset; |
79 | off_t offset; |
67 | size_t length; |
80 | size_t length; |
68 | ssize_t result; |
81 | ssize_t result; |
|
|
82 | |
|
|
83 | int type; |
|
|
84 | int fd, fd2; |
|
|
85 | int errorno; |
|
|
86 | STRLEN dataoffset; |
69 | mode_t mode; /* open */ |
87 | mode_t mode; /* open */ |
70 | int errorno; |
88 | unsigned char cancelled; |
71 | SV *data, *callback; |
|
|
72 | SV *fh, *fh2; |
|
|
73 | void *dataptr, *data2ptr; |
|
|
74 | STRLEN dataoffset; |
|
|
75 | |
|
|
76 | Stat_t *statdata; |
|
|
77 | } aio_cb; |
89 | } aio_cb; |
78 | |
90 | |
79 | typedef aio_cb *aio_req; |
91 | typedef aio_cb *aio_req; |
|
|
92 | typedef aio_cb *aio_req_ornot; |
80 | |
93 | |
81 | static int started, wanted; |
94 | static int started, wanted; |
82 | static volatile int nreqs; |
95 | static volatile int nreqs; |
83 | static int max_outstanding = 1<<30; |
96 | static int max_outstanding = 1<<30; |
84 | static int respipe [2]; |
97 | static int respipe [2]; |
… | |
… | |
88 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
101 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
89 | |
102 | |
90 | static volatile aio_req reqs, reqe; /* queue start, queue end */ |
103 | static volatile aio_req reqs, reqe; /* queue start, queue end */ |
91 | static volatile aio_req ress, rese; /* queue start, queue end */ |
104 | static volatile aio_req ress, rese; /* queue start, queue end */ |
92 | |
105 | |
93 | static void free_req (aio_req req) |
106 | static void req_free (aio_req req); |
94 | { |
|
|
95 | if (req->data) |
|
|
96 | SvREFCNT_dec (req->data); |
|
|
97 | |
107 | |
98 | if (req->fh) |
108 | /* must be called at most once */ |
99 | SvREFCNT_dec (req->fh); |
109 | static SV *req_sv (aio_req req, const char *klass) |
|
|
110 | { |
|
|
111 | req->self = (SV *)newHV (); |
|
|
112 | sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0); |
100 | |
113 | |
101 | if (req->fh2) |
114 | return sv_2mortal (sv_bless (newRV_inc (req->self), gv_stashpv (klass, 1))); |
102 | SvREFCNT_dec (req->fh2); |
|
|
103 | |
|
|
104 | if (req->statdata) |
|
|
105 | Safefree (req->statdata); |
|
|
106 | |
|
|
107 | if (req->callback) |
|
|
108 | SvREFCNT_dec (req->callback); |
|
|
109 | |
|
|
110 | if (req->type == REQ_READDIR && req->result >= 0) |
|
|
111 | free (req->data2ptr); |
|
|
112 | |
|
|
113 | Safefree (req); |
|
|
114 | } |
115 | } |
115 | |
116 | |
116 | static void |
117 | static aio_req SvAIO_REQ (SV *sv) |
117 | poll_wait () |
118 | { |
|
|
119 | if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv)) |
|
|
120 | croak ("object of class " AIO_REQ_KLASS " expected"); |
|
|
121 | |
|
|
122 | MAGIC *mg = mg_find (SvRV (sv), PERL_MAGIC_ext); |
|
|
123 | |
|
|
124 | return mg ? (aio_req)mg->mg_ptr : 0; |
|
|
125 | } |
|
|
126 | |
|
|
127 | static void poll_wait () |
118 | { |
128 | { |
119 | if (nreqs && !ress) |
129 | if (nreqs && !ress) |
120 | { |
130 | { |
121 | fd_set rfd; |
131 | fd_set rfd; |
122 | FD_ZERO(&rfd); |
132 | FD_ZERO(&rfd); |
… | |
… | |
124 | |
134 | |
125 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
135 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
126 | } |
136 | } |
127 | } |
137 | } |
128 | |
138 | |
129 | static int |
139 | static void req_invoke (aio_req req) |
130 | 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 | |
|
|
152 | switch (req->type) |
|
|
153 | { |
|
|
154 | case REQ_READDIR: |
|
|
155 | { |
|
|
156 | SV *rv = &PL_sv_undef; |
|
|
157 | |
|
|
158 | if (req->result >= 0) |
|
|
159 | { |
|
|
160 | char *buf = req->data2ptr; |
|
|
161 | AV *av = newAV (); |
|
|
162 | |
|
|
163 | while (req->result) |
|
|
164 | { |
|
|
165 | SV *sv = newSVpv (buf, 0); |
|
|
166 | |
|
|
167 | av_push (av, sv); |
|
|
168 | buf += SvCUR (sv) + 1; |
|
|
169 | req->result--; |
|
|
170 | } |
|
|
171 | |
|
|
172 | rv = sv_2mortal (newRV_noinc ((SV *)av)); |
|
|
173 | } |
|
|
174 | |
|
|
175 | XPUSHs (rv); |
|
|
176 | } |
|
|
177 | break; |
|
|
178 | |
|
|
179 | case REQ_OPEN: |
|
|
180 | { |
|
|
181 | /* convert fd to fh */ |
|
|
182 | SV *fh; |
|
|
183 | |
|
|
184 | XPUSHs (sv_2mortal (newSViv (req->result))); |
|
|
185 | PUTBACK; |
|
|
186 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
|
|
187 | SPAGAIN; |
|
|
188 | |
|
|
189 | fh = SvREFCNT_inc (POPs); |
|
|
190 | |
|
|
191 | PUSHMARK (SP); |
|
|
192 | XPUSHs (sv_2mortal (fh)); |
|
|
193 | } |
|
|
194 | break; |
|
|
195 | |
|
|
196 | case REQ_SLEEP: |
|
|
197 | case REQ_GROUP: |
|
|
198 | break; |
|
|
199 | |
|
|
200 | default: |
|
|
201 | XPUSHs (sv_2mortal (newSViv (req->result))); |
|
|
202 | break; |
|
|
203 | } |
|
|
204 | |
|
|
205 | |
|
|
206 | PUTBACK; |
|
|
207 | call_sv (req->callback, G_VOID | G_EVAL); |
|
|
208 | SPAGAIN; |
|
|
209 | |
|
|
210 | if (SvTRUE (ERRSV)) |
|
|
211 | { |
|
|
212 | req_free (req); |
|
|
213 | croak (0); |
|
|
214 | } |
|
|
215 | |
|
|
216 | LEAVE; |
|
|
217 | |
|
|
218 | errno = errorno; |
|
|
219 | } |
|
|
220 | |
|
|
221 | static void req_free (aio_req req) |
|
|
222 | { |
|
|
223 | if (req->grp) |
|
|
224 | { |
|
|
225 | aio_req grp = req->grp; |
|
|
226 | |
|
|
227 | /* unlink request */ |
|
|
228 | req->grp_next->grp_prev = req->grp_prev; |
|
|
229 | req->grp_prev->grp_next = req->grp_next; |
|
|
230 | |
|
|
231 | if (grp->grp_next == grp && grp->fd) |
|
|
232 | { |
|
|
233 | req_invoke (grp); |
|
|
234 | req_free (grp); |
|
|
235 | } |
|
|
236 | } |
|
|
237 | |
|
|
238 | if (req->self) |
|
|
239 | { |
|
|
240 | sv_unmagic (req->self, PERL_MAGIC_ext); |
|
|
241 | SvREFCNT_dec (req->self); |
|
|
242 | } |
|
|
243 | |
|
|
244 | if (req->data) |
|
|
245 | SvREFCNT_dec (req->data); |
|
|
246 | |
|
|
247 | if (req->fh) |
|
|
248 | SvREFCNT_dec (req->fh); |
|
|
249 | |
|
|
250 | if (req->fh2) |
|
|
251 | SvREFCNT_dec (req->fh2); |
|
|
252 | |
|
|
253 | if (req->statdata) |
|
|
254 | Safefree (req->statdata); |
|
|
255 | |
|
|
256 | if (req->callback) |
|
|
257 | SvREFCNT_dec (req->callback); |
|
|
258 | |
|
|
259 | if (req->type == REQ_READDIR && req->result >= 0) |
|
|
260 | free (req->data2ptr); |
|
|
261 | |
|
|
262 | Safefree (req); |
|
|
263 | } |
|
|
264 | |
|
|
265 | static void req_cancel (aio_req req) |
|
|
266 | { |
|
|
267 | req->cancelled = 1; |
|
|
268 | |
|
|
269 | if (req->type == REQ_GROUP) |
|
|
270 | { |
|
|
271 | aio_req sub; |
|
|
272 | |
|
|
273 | for (sub = req->grp_next; sub != req; sub = sub->grp_next) |
|
|
274 | req_cancel (sub); |
|
|
275 | } |
|
|
276 | } |
|
|
277 | |
|
|
278 | static int poll_cb () |
131 | { |
279 | { |
132 | dSP; |
280 | dSP; |
133 | int count = 0; |
281 | int count = 0; |
134 | int do_croak = 0; |
282 | int do_croak = 0; |
135 | aio_req req; |
283 | aio_req req; |
… | |
… | |
161 | |
309 | |
162 | nreqs--; |
310 | nreqs--; |
163 | |
311 | |
164 | if (req->type == REQ_QUIT) |
312 | if (req->type == REQ_QUIT) |
165 | started--; |
313 | started--; |
|
|
314 | else if (req->type == REQ_GROUP && req->grp_next != req) |
|
|
315 | { |
|
|
316 | req->fd = 1; /* mark request as delayed */ |
|
|
317 | continue; |
|
|
318 | } |
166 | else |
319 | else |
167 | { |
320 | { |
168 | int errorno = errno; |
|
|
169 | errno = req->errorno; |
|
|
170 | |
|
|
171 | if (req->type == REQ_READ) |
321 | if (req->type == REQ_READ) |
172 | SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); |
322 | SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); |
173 | |
323 | |
174 | if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE)) |
324 | if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE)) |
175 | SvREADONLY_off (req->data); |
325 | SvREADONLY_off (req->data); |
… | |
… | |
179 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
329 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
180 | PL_laststatval = req->result; |
330 | PL_laststatval = req->result; |
181 | PL_statcache = *(req->statdata); |
331 | PL_statcache = *(req->statdata); |
182 | } |
332 | } |
183 | |
333 | |
184 | ENTER; |
334 | req_invoke (req); |
185 | PUSHMARK (SP); |
|
|
186 | |
335 | |
187 | if (req->type == REQ_READDIR) |
|
|
188 | { |
|
|
189 | SV *rv = &PL_sv_undef; |
|
|
190 | |
|
|
191 | if (req->result >= 0) |
|
|
192 | { |
|
|
193 | char *buf = req->data2ptr; |
|
|
194 | AV *av = newAV (); |
|
|
195 | |
|
|
196 | while (req->result) |
|
|
197 | { |
|
|
198 | SV *sv = newSVpv (buf, 0); |
|
|
199 | |
|
|
200 | av_push (av, sv); |
|
|
201 | buf += SvCUR (sv) + 1; |
|
|
202 | req->result--; |
|
|
203 | } |
|
|
204 | |
|
|
205 | rv = sv_2mortal (newRV_noinc ((SV *)av)); |
|
|
206 | } |
|
|
207 | |
|
|
208 | XPUSHs (rv); |
|
|
209 | } |
|
|
210 | else |
|
|
211 | { |
|
|
212 | XPUSHs (sv_2mortal (newSViv (req->result))); |
|
|
213 | |
|
|
214 | if (req->type == REQ_OPEN) |
|
|
215 | { |
|
|
216 | /* convert fd to fh */ |
|
|
217 | SV *fh; |
|
|
218 | |
|
|
219 | PUTBACK; |
|
|
220 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
|
|
221 | SPAGAIN; |
|
|
222 | |
|
|
223 | fh = SvREFCNT_inc (POPs); |
|
|
224 | |
|
|
225 | PUSHMARK (SP); |
|
|
226 | XPUSHs (sv_2mortal (fh)); |
|
|
227 | } |
|
|
228 | } |
|
|
229 | |
|
|
230 | if (SvOK (req->callback)) |
|
|
231 | { |
|
|
232 | PUTBACK; |
|
|
233 | call_sv (req->callback, G_VOID | G_EVAL); |
|
|
234 | SPAGAIN; |
|
|
235 | |
|
|
236 | if (SvTRUE (ERRSV)) |
|
|
237 | { |
|
|
238 | free_req (req); |
|
|
239 | croak (0); |
|
|
240 | } |
|
|
241 | } |
|
|
242 | |
|
|
243 | LEAVE; |
|
|
244 | |
|
|
245 | errno = errorno; |
|
|
246 | count++; |
336 | count++; |
247 | } |
337 | } |
248 | |
338 | |
249 | free_req (req); |
339 | req_free (req); |
250 | } |
340 | } |
251 | |
341 | |
252 | return count; |
342 | return count; |
253 | } |
343 | } |
254 | |
344 | |
255 | static void *aio_proc(void *arg); |
345 | static void *aio_proc(void *arg); |
256 | |
346 | |
257 | static void |
|
|
258 | start_thread (void) |
347 | static void start_thread (void) |
259 | { |
348 | { |
260 | sigset_t fullsigset, oldsigset; |
349 | sigset_t fullsigset, oldsigset; |
261 | pthread_t tid; |
350 | pthread_t tid; |
262 | pthread_attr_t attr; |
351 | pthread_attr_t attr; |
263 | |
352 | |
… | |
… | |
272 | started++; |
361 | started++; |
273 | |
362 | |
274 | sigprocmask (SIG_SETMASK, &oldsigset, 0); |
363 | sigprocmask (SIG_SETMASK, &oldsigset, 0); |
275 | } |
364 | } |
276 | |
365 | |
277 | static void |
366 | static void req_send (aio_req req) |
278 | send_req (aio_req req) |
|
|
279 | { |
367 | { |
280 | while (started < wanted && nreqs >= started) |
368 | while (started < wanted && nreqs >= started) |
281 | start_thread (); |
369 | start_thread (); |
282 | |
370 | |
283 | nreqs++; |
371 | nreqs++; |
… | |
… | |
307 | |
395 | |
308 | poll_wait (); |
396 | poll_wait (); |
309 | } |
397 | } |
310 | } |
398 | } |
311 | |
399 | |
312 | static void |
400 | static void end_thread (void) |
313 | end_thread (void) |
|
|
314 | { |
401 | { |
315 | aio_req req; |
402 | aio_req req; |
316 | Newz (0, req, 1, aio_cb); |
403 | Newz (0, req, 1, aio_cb); |
317 | req->type = REQ_QUIT; |
404 | req->type = REQ_QUIT; |
318 | |
405 | |
319 | send_req (req); |
406 | req_send (req); |
320 | } |
407 | } |
321 | |
408 | |
322 | static void min_parallel (int nthreads) |
409 | static void min_parallel (int nthreads) |
323 | { |
410 | { |
324 | if (wanted < nthreads) |
411 | if (wanted < nthreads) |
… | |
… | |
369 | * normal read/write by using a mutex. slows down execution a lot, |
456 | * normal read/write by using a mutex. slows down execution a lot, |
370 | * but that's your problem, not mine. |
457 | * but that's your problem, not mine. |
371 | */ |
458 | */ |
372 | static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER; |
459 | static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER; |
373 | |
460 | |
374 | static ssize_t |
461 | static ssize_t pread (int fd, void *buf, size_t count, off_t offset) |
375 | pread (int fd, void *buf, size_t count, off_t offset) |
|
|
376 | { |
462 | { |
377 | ssize_t res; |
463 | ssize_t res; |
378 | off_t ooffset; |
464 | off_t ooffset; |
379 | |
465 | |
380 | pthread_mutex_lock (&preadwritelock); |
466 | pthread_mutex_lock (&preadwritelock); |
… | |
… | |
385 | pthread_mutex_unlock (&preadwritelock); |
471 | pthread_mutex_unlock (&preadwritelock); |
386 | |
472 | |
387 | return res; |
473 | return res; |
388 | } |
474 | } |
389 | |
475 | |
390 | static ssize_t |
|
|
391 | pwrite (int fd, void *buf, size_t count, off_t offset) |
476 | static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset) |
392 | { |
477 | { |
393 | ssize_t res; |
478 | ssize_t res; |
394 | off_t ooffset; |
479 | off_t ooffset; |
395 | |
480 | |
396 | pthread_mutex_lock (&preadwritelock); |
481 | pthread_mutex_lock (&preadwritelock); |
… | |
… | |
409 | #endif |
494 | #endif |
410 | |
495 | |
411 | #if !HAVE_READAHEAD |
496 | #if !HAVE_READAHEAD |
412 | # define readahead aio_readahead |
497 | # define readahead aio_readahead |
413 | |
498 | |
414 | static ssize_t |
|
|
415 | readahead (int fd, off_t offset, size_t count) |
499 | static ssize_t readahead (int fd, off_t offset, size_t count) |
416 | { |
500 | { |
417 | char readahead_buf[4096]; |
501 | char readahead_buf[4096]; |
418 | |
502 | |
419 | while (count > 0) |
503 | while (count > 0) |
420 | { |
504 | { |
… | |
… | |
432 | #if !HAVE_READDIR_R |
516 | #if !HAVE_READDIR_R |
433 | # define readdir_r aio_readdir_r |
517 | # define readdir_r aio_readdir_r |
434 | |
518 | |
435 | static pthread_mutex_t readdirlock = PTHREAD_MUTEX_INITIALIZER; |
519 | static pthread_mutex_t readdirlock = PTHREAD_MUTEX_INITIALIZER; |
436 | |
520 | |
437 | static int |
|
|
438 | readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) |
521 | static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) |
439 | { |
522 | { |
440 | struct dirent *e; |
523 | struct dirent *e; |
441 | int errorno; |
524 | int errorno; |
442 | |
525 | |
443 | pthread_mutex_lock (&readdirlock); |
526 | pthread_mutex_lock (&readdirlock); |
… | |
… | |
459 | return e ? 0 : -1; |
542 | return e ? 0 : -1; |
460 | } |
543 | } |
461 | #endif |
544 | #endif |
462 | |
545 | |
463 | /* sendfile always needs emulation */ |
546 | /* sendfile always needs emulation */ |
464 | static ssize_t |
|
|
465 | sendfile_ (int ofd, int ifd, off_t offset, size_t count) |
547 | static ssize_t sendfile_ (int ofd, int ifd, off_t offset, size_t count) |
466 | { |
548 | { |
467 | ssize_t res; |
549 | ssize_t res; |
468 | |
550 | |
469 | if (!count) |
551 | if (!count) |
470 | return 0; |
552 | return 0; |
… | |
… | |
552 | |
634 | |
553 | return res; |
635 | return res; |
554 | } |
636 | } |
555 | |
637 | |
556 | /* read a full directory */ |
638 | /* read a full directory */ |
557 | static int |
|
|
558 | scandir_ (const char *path, void **namesp) |
639 | static int scandir_ (const char *path, void **namesp) |
559 | { |
640 | { |
560 | DIR *dirp = opendir (path); |
641 | DIR *dirp = opendir (path); |
561 | union |
642 | union |
562 | { |
643 | { |
563 | struct dirent d; |
644 | struct dirent d; |
… | |
… | |
617 | return res; |
698 | return res; |
618 | } |
699 | } |
619 | |
700 | |
620 | /*****************************************************************************/ |
701 | /*****************************************************************************/ |
621 | |
702 | |
622 | static void * |
|
|
623 | aio_proc (void *thr_arg) |
703 | static void *aio_proc (void *thr_arg) |
624 | { |
704 | { |
625 | aio_req req; |
705 | aio_req req; |
626 | int type; |
706 | int type; |
627 | |
707 | |
628 | do |
708 | do |
… | |
… | |
647 | |
727 | |
648 | pthread_mutex_unlock (&reqlock); |
728 | pthread_mutex_unlock (&reqlock); |
649 | |
729 | |
650 | errno = 0; /* strictly unnecessary */ |
730 | errno = 0; /* strictly unnecessary */ |
651 | |
731 | |
652 | type = req->type; |
732 | if (!req->cancelled) |
653 | |
|
|
654 | switch (type) |
733 | switch (req->type) |
655 | { |
734 | { |
656 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
735 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
657 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
736 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
658 | |
737 | |
659 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
738 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
660 | case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length); break; |
739 | case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length); break; |
661 | |
740 | |
662 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
741 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
663 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
742 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
664 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
743 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
665 | |
744 | |
666 | case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
745 | case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
667 | case REQ_CLOSE: req->result = close (req->fd); break; |
746 | case REQ_CLOSE: req->result = close (req->fd); break; |
668 | case REQ_UNLINK: req->result = unlink (req->dataptr); break; |
747 | case REQ_UNLINK: req->result = unlink (req->dataptr); break; |
669 | case REQ_RMDIR: req->result = rmdir (req->dataptr); break; |
748 | case REQ_RMDIR: req->result = rmdir (req->dataptr); break; |
670 | case REQ_RENAME: req->result = rename (req->data2ptr, req->dataptr); break; |
749 | case REQ_RENAME: req->result = rename (req->data2ptr, req->dataptr); break; |
671 | case REQ_LINK: req->result = link (req->data2ptr, req->dataptr); break; |
750 | case REQ_LINK: req->result = link (req->data2ptr, req->dataptr); break; |
672 | case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; |
751 | case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; |
673 | |
752 | |
674 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
753 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
675 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
754 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
676 | case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break; |
755 | case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break; |
677 | |
756 | |
|
|
757 | case REQ_SLEEP: |
|
|
758 | { |
|
|
759 | struct timeval tv; |
|
|
760 | |
|
|
761 | tv.tv_sec = req->fd; |
|
|
762 | tv.tv_usec = req->fd2; |
|
|
763 | |
|
|
764 | req->result = select (0, 0, 0, 0, &tv); |
|
|
765 | } |
|
|
766 | |
678 | case REQ_QUIT: |
767 | case REQ_QUIT: |
679 | break; |
768 | break; |
680 | |
769 | |
681 | default: |
770 | default: |
682 | req->result = ENOSYS; |
771 | req->result = ENOSYS; |
683 | break; |
772 | break; |
684 | } |
773 | } |
685 | |
774 | |
686 | req->errorno = errno; |
775 | req->errorno = errno; |
687 | |
776 | |
688 | pthread_mutex_lock (&reslock); |
777 | pthread_mutex_lock (&reslock); |
689 | |
778 | |
… | |
… | |
743 | |
832 | |
744 | while (reqs) |
833 | while (reqs) |
745 | { |
834 | { |
746 | prv = reqs; |
835 | prv = reqs; |
747 | reqs = prv->next; |
836 | reqs = prv->next; |
748 | free_req (prv); |
837 | req_free (prv); |
749 | } |
838 | } |
750 | |
839 | |
751 | reqs = reqe = 0; |
840 | reqs = reqe = 0; |
752 | |
841 | |
753 | while (ress) |
842 | while (ress) |
754 | { |
843 | { |
755 | prv = ress; |
844 | prv = ress; |
756 | ress = prv->next; |
845 | ress = prv->next; |
757 | free_req (prv); |
846 | req_free (prv); |
758 | } |
847 | } |
759 | |
848 | |
760 | ress = rese = 0; |
849 | ress = rese = 0; |
761 | |
850 | |
762 | close (respipe [0]); |
851 | close (respipe [0]); |
… | |
… | |
768 | |
857 | |
769 | #define dREQ \ |
858 | #define dREQ \ |
770 | aio_req req; \ |
859 | aio_req req; \ |
771 | \ |
860 | \ |
772 | if (SvOK (callback) && !SvROK (callback)) \ |
861 | if (SvOK (callback) && !SvROK (callback)) \ |
773 | croak ("clalback must be undef or of reference type"); \ |
862 | croak ("callback must be undef or of reference type"); \ |
774 | \ |
863 | \ |
775 | Newz (0, req, 1, aio_cb); \ |
864 | Newz (0, req, 1, aio_cb); \ |
776 | if (!req) \ |
865 | if (!req) \ |
777 | croak ("out of memory during aio_req allocation"); \ |
866 | croak ("out of memory during aio_req allocation"); \ |
778 | \ |
867 | \ |
779 | req->callback = newSVsv (callback); |
868 | req->callback = newSVsv (callback) |
|
|
869 | |
|
|
870 | #define REQ_SEND \ |
|
|
871 | req_send (req); \ |
|
|
872 | \ |
|
|
873 | if (GIMME_V != G_VOID) \ |
|
|
874 | XPUSHs (req_sv (req, AIO_REQ_KLASS)); |
780 | |
875 | |
781 | MODULE = IO::AIO PACKAGE = IO::AIO |
876 | MODULE = IO::AIO PACKAGE = IO::AIO |
782 | |
877 | |
783 | PROTOTYPES: ENABLE |
878 | PROTOTYPES: ENABLE |
784 | |
879 | |
785 | BOOT: |
880 | BOOT: |
786 | { |
881 | { |
|
|
882 | HV *stash = gv_stashpv ("IO::AIO", 1); |
|
|
883 | newCONSTSUB (stash, "EXDEV", newSViv (EXDEV)); |
|
|
884 | newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY)); |
|
|
885 | newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY)); |
|
|
886 | |
787 | create_pipe (); |
887 | create_pipe (); |
788 | pthread_atfork (atfork_prepare, atfork_parent, atfork_child); |
888 | pthread_atfork (atfork_prepare, atfork_parent, atfork_child); |
789 | } |
889 | } |
790 | |
890 | |
791 | void |
891 | void |
… | |
… | |
811 | SV * pathname |
911 | SV * pathname |
812 | int flags |
912 | int flags |
813 | int mode |
913 | int mode |
814 | SV * callback |
914 | SV * callback |
815 | PROTOTYPE: $$$;$ |
915 | PROTOTYPE: $$$;$ |
816 | CODE: |
916 | PPCODE: |
817 | { |
917 | { |
818 | dREQ; |
918 | dREQ; |
819 | |
919 | |
820 | req->type = REQ_OPEN; |
920 | req->type = REQ_OPEN; |
821 | req->data = newSVsv (pathname); |
921 | req->data = newSVsv (pathname); |
822 | req->dataptr = SvPVbyte_nolen (req->data); |
922 | req->dataptr = SvPVbyte_nolen (req->data); |
823 | req->fd = flags; |
923 | req->fd = flags; |
824 | req->mode = mode; |
924 | req->mode = mode; |
825 | |
925 | |
826 | send_req (req); |
926 | REQ_SEND; |
827 | } |
927 | } |
828 | |
928 | |
829 | void |
929 | void |
830 | aio_close (fh,callback=&PL_sv_undef) |
930 | aio_close (fh,callback=&PL_sv_undef) |
831 | SV * fh |
931 | SV * fh |
… | |
… | |
833 | PROTOTYPE: $;$ |
933 | PROTOTYPE: $;$ |
834 | ALIAS: |
934 | ALIAS: |
835 | aio_close = REQ_CLOSE |
935 | aio_close = REQ_CLOSE |
836 | aio_fsync = REQ_FSYNC |
936 | aio_fsync = REQ_FSYNC |
837 | aio_fdatasync = REQ_FDATASYNC |
937 | aio_fdatasync = REQ_FDATASYNC |
838 | CODE: |
938 | PPCODE: |
839 | { |
939 | { |
840 | dREQ; |
940 | dREQ; |
841 | |
941 | |
842 | req->type = ix; |
942 | req->type = ix; |
843 | req->fh = newSVsv (fh); |
943 | req->fh = newSVsv (fh); |
844 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
944 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
845 | |
945 | |
846 | send_req (req); |
946 | REQ_SEND (req); |
847 | } |
947 | } |
848 | |
948 | |
849 | void |
949 | void |
850 | aio_read (fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
950 | aio_read (fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
851 | SV * fh |
951 | SV * fh |
… | |
… | |
856 | SV * callback |
956 | SV * callback |
857 | ALIAS: |
957 | ALIAS: |
858 | aio_read = REQ_READ |
958 | aio_read = REQ_READ |
859 | aio_write = REQ_WRITE |
959 | aio_write = REQ_WRITE |
860 | PROTOTYPE: $$$$$;$ |
960 | PROTOTYPE: $$$$$;$ |
861 | CODE: |
961 | PPCODE: |
862 | { |
962 | { |
863 | aio_req req; |
963 | aio_req req; |
864 | STRLEN svlen; |
964 | STRLEN svlen; |
865 | char *svptr = SvPVbyte (data, svlen); |
965 | char *svptr = SvPVbyte (data, svlen); |
866 | |
966 | |
… | |
… | |
904 | { |
1004 | { |
905 | SvREADONLY_on (data); |
1005 | SvREADONLY_on (data); |
906 | req->data2ptr = (void *)data; |
1006 | req->data2ptr = (void *)data; |
907 | } |
1007 | } |
908 | |
1008 | |
909 | send_req (req); |
1009 | REQ_SEND; |
910 | } |
1010 | } |
911 | } |
1011 | } |
912 | |
1012 | |
913 | void |
1013 | void |
914 | aio_sendfile (out_fh,in_fh,in_offset,length,callback=&PL_sv_undef) |
1014 | aio_sendfile (out_fh,in_fh,in_offset,length,callback=&PL_sv_undef) |
… | |
… | |
916 | SV * in_fh |
1016 | SV * in_fh |
917 | UV in_offset |
1017 | UV in_offset |
918 | UV length |
1018 | UV length |
919 | SV * callback |
1019 | SV * callback |
920 | PROTOTYPE: $$$$;$ |
1020 | PROTOTYPE: $$$$;$ |
921 | CODE: |
1021 | PPCODE: |
922 | { |
1022 | { |
923 | dREQ; |
1023 | dREQ; |
924 | |
1024 | |
925 | req->type = REQ_SENDFILE; |
1025 | req->type = REQ_SENDFILE; |
926 | req->fh = newSVsv (out_fh); |
1026 | req->fh = newSVsv (out_fh); |
… | |
… | |
928 | req->fh2 = newSVsv (in_fh); |
1028 | req->fh2 = newSVsv (in_fh); |
929 | req->fd2 = PerlIO_fileno (IoIFP (sv_2io (in_fh))); |
1029 | req->fd2 = PerlIO_fileno (IoIFP (sv_2io (in_fh))); |
930 | req->offset = in_offset; |
1030 | req->offset = in_offset; |
931 | req->length = length; |
1031 | req->length = length; |
932 | |
1032 | |
933 | send_req (req); |
1033 | REQ_SEND; |
934 | } |
1034 | } |
935 | |
1035 | |
936 | void |
1036 | void |
937 | aio_readahead (fh,offset,length,callback=&PL_sv_undef) |
1037 | aio_readahead (fh,offset,length,callback=&PL_sv_undef) |
938 | SV * fh |
1038 | SV * fh |
939 | UV offset |
1039 | UV offset |
940 | IV length |
1040 | IV length |
941 | SV * callback |
1041 | SV * callback |
942 | PROTOTYPE: $$$;$ |
1042 | PROTOTYPE: $$$;$ |
943 | CODE: |
1043 | PPCODE: |
944 | { |
1044 | { |
945 | dREQ; |
1045 | dREQ; |
946 | |
1046 | |
947 | req->type = REQ_READAHEAD; |
1047 | req->type = REQ_READAHEAD; |
948 | req->fh = newSVsv (fh); |
1048 | req->fh = newSVsv (fh); |
949 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
1049 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
950 | req->offset = offset; |
1050 | req->offset = offset; |
951 | req->length = length; |
1051 | req->length = length; |
952 | |
1052 | |
953 | send_req (req); |
1053 | REQ_SEND; |
954 | } |
1054 | } |
955 | |
1055 | |
956 | void |
1056 | void |
957 | aio_stat (fh_or_path,callback=&PL_sv_undef) |
1057 | aio_stat (fh_or_path,callback=&PL_sv_undef) |
958 | SV * fh_or_path |
1058 | SV * fh_or_path |
959 | SV * callback |
1059 | SV * callback |
960 | ALIAS: |
1060 | ALIAS: |
961 | aio_stat = REQ_STAT |
1061 | aio_stat = REQ_STAT |
962 | aio_lstat = REQ_LSTAT |
1062 | aio_lstat = REQ_LSTAT |
963 | CODE: |
1063 | PPCODE: |
964 | { |
1064 | { |
965 | dREQ; |
1065 | dREQ; |
966 | |
1066 | |
967 | New (0, req->statdata, 1, Stat_t); |
1067 | New (0, req->statdata, 1, Stat_t); |
968 | if (!req->statdata) |
1068 | if (!req->statdata) |
969 | { |
1069 | { |
970 | free_req (req); |
1070 | req_free (req); |
971 | croak ("out of memory during aio_req->statdata allocation"); |
1071 | croak ("out of memory during aio_req->statdata allocation"); |
972 | } |
1072 | } |
973 | |
1073 | |
974 | if (SvPOK (fh_or_path)) |
1074 | if (SvPOK (fh_or_path)) |
975 | { |
1075 | { |
… | |
… | |
982 | req->type = REQ_FSTAT; |
1082 | req->type = REQ_FSTAT; |
983 | req->fh = newSVsv (fh_or_path); |
1083 | req->fh = newSVsv (fh_or_path); |
984 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
1084 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
985 | } |
1085 | } |
986 | |
1086 | |
987 | send_req (req); |
1087 | REQ_SEND; |
988 | } |
1088 | } |
989 | |
1089 | |
990 | void |
1090 | void |
991 | aio_unlink (pathname,callback=&PL_sv_undef) |
1091 | aio_unlink (pathname,callback=&PL_sv_undef) |
992 | SV * pathname |
1092 | SV * pathname |
993 | SV * callback |
1093 | SV * callback |
994 | ALIAS: |
1094 | ALIAS: |
995 | aio_unlink = REQ_UNLINK |
1095 | aio_unlink = REQ_UNLINK |
996 | aio_rmdir = REQ_RMDIR |
1096 | aio_rmdir = REQ_RMDIR |
997 | aio_readdir = REQ_READDIR |
1097 | aio_readdir = REQ_READDIR |
998 | CODE: |
1098 | PPCODE: |
999 | { |
1099 | { |
1000 | dREQ; |
1100 | dREQ; |
1001 | |
1101 | |
1002 | req->type = ix; |
1102 | req->type = ix; |
1003 | req->data = newSVsv (pathname); |
1103 | req->data = newSVsv (pathname); |
1004 | req->dataptr = SvPVbyte_nolen (req->data); |
1104 | req->dataptr = SvPVbyte_nolen (req->data); |
1005 | |
1105 | |
1006 | send_req (req); |
1106 | REQ_SEND; |
1007 | } |
1107 | } |
1008 | |
1108 | |
1009 | void |
1109 | void |
1010 | aio_link (oldpath,newpath,callback=&PL_sv_undef) |
1110 | aio_link (oldpath,newpath,callback=&PL_sv_undef) |
1011 | SV * oldpath |
1111 | SV * oldpath |
… | |
… | |
1013 | SV * callback |
1113 | SV * callback |
1014 | ALIAS: |
1114 | ALIAS: |
1015 | aio_link = REQ_LINK |
1115 | aio_link = REQ_LINK |
1016 | aio_symlink = REQ_SYMLINK |
1116 | aio_symlink = REQ_SYMLINK |
1017 | aio_rename = REQ_RENAME |
1117 | aio_rename = REQ_RENAME |
1018 | CODE: |
1118 | PPCODE: |
1019 | { |
1119 | { |
1020 | dREQ; |
1120 | dREQ; |
1021 | |
1121 | |
1022 | req->type = ix; |
1122 | req->type = ix; |
1023 | req->fh = newSVsv (oldpath); |
1123 | req->fh = newSVsv (oldpath); |
1024 | req->data2ptr = SvPVbyte_nolen (req->fh); |
1124 | req->data2ptr = SvPVbyte_nolen (req->fh); |
1025 | req->data = newSVsv (newpath); |
1125 | req->data = newSVsv (newpath); |
1026 | req->dataptr = SvPVbyte_nolen (req->data); |
1126 | req->dataptr = SvPVbyte_nolen (req->data); |
1027 | |
1127 | |
1028 | send_req (req); |
1128 | REQ_SEND; |
|
|
1129 | } |
|
|
1130 | |
|
|
1131 | void |
|
|
1132 | aio_sleep (delay,callback=&PL_sv_undef) |
|
|
1133 | double delay |
|
|
1134 | SV * callback |
|
|
1135 | PPCODE: |
|
|
1136 | { |
|
|
1137 | dREQ; |
|
|
1138 | |
|
|
1139 | req->type = REQ_SLEEP; |
|
|
1140 | req->fd = delay < 0. ? 0 : delay; |
|
|
1141 | req->fd2 = delay < 0. ? 0 : 1000. * (delay - req->fd); |
|
|
1142 | |
|
|
1143 | REQ_SEND; |
|
|
1144 | } |
|
|
1145 | |
|
|
1146 | void |
|
|
1147 | aio_group (callback=&PL_sv_undef) |
|
|
1148 | SV * callback |
|
|
1149 | PROTOTYPE: ;$ |
|
|
1150 | PPCODE: |
|
|
1151 | { |
|
|
1152 | dREQ; |
|
|
1153 | req->type = REQ_GROUP; |
|
|
1154 | req->grp_next = req; |
|
|
1155 | req->grp_prev = req; |
|
|
1156 | |
|
|
1157 | req_send (req); |
|
|
1158 | XPUSHs (req_sv (req, AIO_GRP_KLASS)); |
1029 | } |
1159 | } |
1030 | |
1160 | |
1031 | void |
1161 | void |
1032 | flush () |
1162 | flush () |
1033 | PROTOTYPE: |
1163 | PROTOTYPE: |
… | |
… | |
1077 | CODE: |
1207 | CODE: |
1078 | RETVAL = nreqs; |
1208 | RETVAL = nreqs; |
1079 | OUTPUT: |
1209 | OUTPUT: |
1080 | RETVAL |
1210 | RETVAL |
1081 | |
1211 | |
|
|
1212 | MODULE = IO::AIO PACKAGE = IO::AIO::REQ |
|
|
1213 | |
|
|
1214 | void |
|
|
1215 | cancel (aio_req_ornot req) |
|
|
1216 | PROTOTYPE: |
|
|
1217 | CODE: |
|
|
1218 | req_cancel (req); |
|
|
1219 | |
|
|
1220 | MODULE = IO::AIO PACKAGE = IO::AIO::GRP |
|
|
1221 | |
|
|
1222 | void |
|
|
1223 | add (aio_req grp, ...) |
|
|
1224 | PROTOTYPE: $;@ |
|
|
1225 | PPCODE: |
|
|
1226 | { |
|
|
1227 | int i; |
|
|
1228 | |
|
|
1229 | for (i = 1; i < items; ++i ) |
|
|
1230 | { |
|
|
1231 | if (GIMME_V != G_VOID) |
|
|
1232 | XPUSHs (sv_2mortal (newSVsv (ST (i)))); |
|
|
1233 | |
|
|
1234 | aio_req req = SvAIO_REQ (ST (i)); |
|
|
1235 | |
|
|
1236 | if (req) |
|
|
1237 | { |
|
|
1238 | req->grp_prev = grp; |
|
|
1239 | req->grp_next = grp->grp_next; |
|
|
1240 | grp->grp_next->grp_prev = req; |
|
|
1241 | grp->grp_next = req; |
|
|
1242 | |
|
|
1243 | req->grp = grp; |
|
|
1244 | } |
|
|
1245 | } |
|
|
1246 | } |
|
|
1247 | |