|
|
1 | #define _REENTRANT 1 |
|
|
2 | #include <errno.h> |
|
|
3 | |
1 | #include "EXTERN.h" |
4 | #include "EXTERN.h" |
2 | #include "perl.h" |
5 | #include "perl.h" |
3 | #include "XSUB.h" |
6 | #include "XSUB.h" |
4 | |
7 | |
5 | #include "autoconf/config.h" |
8 | #include "autoconf/config.h" |
6 | |
9 | |
|
|
10 | #include <pthread.h> |
|
|
11 | |
|
|
12 | #include <stddef.h> |
|
|
13 | #include <errno.h> |
|
|
14 | #include <sys/time.h> |
|
|
15 | #include <sys/select.h> |
7 | #include <sys/types.h> |
16 | #include <sys/types.h> |
8 | #include <sys/stat.h> |
17 | #include <sys/stat.h> |
9 | |
18 | #include <limits.h> |
10 | #include <unistd.h> |
19 | #include <unistd.h> |
11 | #include <fcntl.h> |
20 | #include <fcntl.h> |
12 | #include <signal.h> |
21 | #include <signal.h> |
13 | #include <sched.h> |
22 | #include <sched.h> |
14 | |
23 | |
15 | #include <pthread.h> |
24 | #if HAVE_SENDFILE |
|
|
25 | # if __linux |
|
|
26 | # include <sys/sendfile.h> |
|
|
27 | # elif __freebsd |
|
|
28 | # include <sys/socket.h> |
|
|
29 | # include <sys/uio.h> |
|
|
30 | # elif __hpux |
|
|
31 | # include <sys/socket.h> |
|
|
32 | # elif __solaris /* not yet */ |
|
|
33 | # include <sys/sendfile.h> |
|
|
34 | # else |
|
|
35 | # error sendfile support requested but not available |
|
|
36 | # endif |
|
|
37 | #endif |
16 | |
38 | |
17 | typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
39 | /* used for struct dirent, AIX doesn't provide it */ |
18 | typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
40 | #ifndef NAME_MAX |
19 | typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
41 | # define NAME_MAX 4096 |
|
|
42 | #endif |
20 | |
43 | |
21 | #if __ia64 |
44 | #if __ia64 |
22 | # define STACKSIZE 65536 |
45 | # define STACKSIZE 65536 |
23 | #else |
46 | #else |
24 | # define STACKSIZE 4096 |
47 | # define STACKSIZE 8192 |
25 | #endif |
48 | #endif |
26 | |
49 | |
27 | enum { |
50 | enum { |
28 | REQ_QUIT, |
51 | REQ_QUIT, |
29 | REQ_OPEN, REQ_CLOSE, |
52 | REQ_OPEN, REQ_CLOSE, |
30 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
53 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
|
|
54 | REQ_SENDFILE, |
31 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_UNLINK, |
55 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
32 | REQ_FSYNC, REQ_FDATASYNC, |
56 | REQ_FSYNC, REQ_FDATASYNC, |
|
|
57 | REQ_UNLINK, REQ_RMDIR, REQ_RENAME, |
|
|
58 | REQ_READDIR, |
|
|
59 | REQ_LINK, REQ_SYMLINK, |
|
|
60 | REQ_SLEEP, |
|
|
61 | REQ_GROUP, |
33 | }; |
62 | }; |
34 | |
63 | |
|
|
64 | #define AIO_REQ_KLASS "IO::AIO::REQ" |
|
|
65 | #define AIO_GRP_KLASS "IO::AIO::GRP" |
|
|
66 | |
35 | typedef struct aio_cb { |
67 | typedef struct aio_cb |
|
|
68 | { |
|
|
69 | struct aio_cb *grp, *grp_prev, *grp_next; |
|
|
70 | |
36 | struct aio_cb *volatile next; |
71 | struct aio_cb *volatile next; |
37 | |
72 | |
38 | int type; |
73 | SV *self; /* the perl counterpart of this request, if any */ |
39 | |
74 | |
40 | int fd; |
75 | SV *data, *callback; |
|
|
76 | SV *fh, *fh2; |
|
|
77 | void *dataptr, *data2ptr; |
|
|
78 | Stat_t *statdata; |
41 | off_t offset; |
79 | off_t offset; |
42 | size_t length; |
80 | size_t length; |
43 | ssize_t result; |
81 | ssize_t result; |
|
|
82 | |
|
|
83 | int type; |
|
|
84 | int fd, fd2; |
|
|
85 | int errorno; |
|
|
86 | STRLEN dataoffset; |
44 | mode_t mode; /* open */ |
87 | mode_t mode; /* open */ |
45 | int errorno; |
88 | unsigned char cancelled; |
46 | SV *data, *callback, *fh; |
|
|
47 | void *dataptr; |
|
|
48 | STRLEN dataoffset; |
|
|
49 | |
|
|
50 | Stat_t *statdata; |
|
|
51 | } aio_cb; |
89 | } aio_cb; |
52 | |
90 | |
53 | typedef aio_cb *aio_req; |
91 | typedef aio_cb *aio_req; |
|
|
92 | typedef aio_cb *aio_req_ornot; |
54 | |
93 | |
55 | static int started; |
94 | static int started, wanted; |
56 | static volatile int nreqs; |
95 | static volatile int nreqs; |
57 | static int max_outstanding = 1<<30; |
96 | static int max_outstanding = 1<<30; |
58 | static int respipe [2]; |
97 | static int respipe [2]; |
59 | |
98 | |
60 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
99 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
… | |
… | |
62 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
101 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
63 | |
102 | |
64 | static volatile aio_req reqs, reqe; /* queue start, queue end */ |
103 | static volatile aio_req reqs, reqe; /* queue start, queue end */ |
65 | static volatile aio_req ress, rese; /* queue start, queue end */ |
104 | static volatile aio_req ress, rese; /* queue start, queue end */ |
66 | |
105 | |
67 | static void |
106 | static void req_free (aio_req req); |
68 | poll_wait () |
107 | |
|
|
108 | /* must be called at most once */ |
|
|
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); |
|
|
113 | |
|
|
114 | return sv_2mortal (sv_bless (newRV_inc (req->self), gv_stashpv (klass, 1))); |
|
|
115 | } |
|
|
116 | |
|
|
117 | static aio_req SvAIO_REQ (SV *sv) |
|
|
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 () |
69 | { |
128 | { |
70 | if (nreqs && !ress) |
129 | if (nreqs && !ress) |
71 | { |
130 | { |
72 | fd_set rfd; |
131 | fd_set rfd; |
73 | FD_ZERO(&rfd); |
132 | FD_ZERO(&rfd); |
… | |
… | |
75 | |
134 | |
76 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
135 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
77 | } |
136 | } |
78 | } |
137 | } |
79 | |
138 | |
80 | static int |
139 | static void req_invoke (aio_req req) |
81 | 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 () |
82 | { |
291 | { |
83 | dSP; |
292 | dSP; |
84 | int count = 0; |
293 | int count = 0; |
|
|
294 | int do_croak = 0; |
85 | aio_req req, prv; |
295 | aio_req req; |
86 | |
296 | |
|
|
297 | for (;;) |
|
|
298 | { |
87 | pthread_mutex_lock (&reslock); |
299 | pthread_mutex_lock (&reslock); |
|
|
300 | req = ress; |
88 | |
301 | |
89 | { |
302 | if (req) |
|
|
303 | { |
|
|
304 | ress = req->next; |
|
|
305 | |
|
|
306 | if (!ress) |
|
|
307 | { |
90 | /* read any signals sent by the worker threads */ |
308 | /* read any signals sent by the worker threads */ |
91 | char buf [32]; |
309 | char buf [32]; |
92 | while (read (respipe [0], buf, 32) > 0) |
310 | while (read (respipe [0], buf, 32) == 32) |
|
|
311 | ; |
|
|
312 | |
|
|
313 | rese = 0; |
|
|
314 | } |
93 | ; |
315 | } |
94 | } |
|
|
95 | |
316 | |
96 | req = ress; |
|
|
97 | ress = rese = 0; |
|
|
98 | |
|
|
99 | pthread_mutex_unlock (&reslock); |
317 | pthread_mutex_unlock (&reslock); |
100 | |
318 | |
101 | while (req) |
319 | if (!req) |
102 | { |
320 | break; |
|
|
321 | |
103 | nreqs--; |
322 | nreqs--; |
104 | |
323 | |
105 | if (req->type == REQ_QUIT) |
324 | if (req->type == REQ_QUIT) |
106 | 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 | } |
107 | else |
331 | else |
108 | { |
332 | { |
109 | int errorno = errno; |
|
|
110 | errno = req->errorno; |
|
|
111 | |
|
|
112 | if (req->type == REQ_READ) |
333 | if (req->type == REQ_READ) |
113 | SvCUR_set (req->data, req->dataoffset |
334 | SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); |
114 | + req->result > 0 ? req->result : 0); |
|
|
115 | |
335 | |
|
|
336 | if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE)) |
|
|
337 | SvREADONLY_off (req->data); |
|
|
338 | |
116 | if (req->data) |
339 | if (req->statdata) |
117 | SvREFCNT_dec (req->data); |
|
|
118 | |
|
|
119 | if (req->fh) |
|
|
120 | SvREFCNT_dec (req->fh); |
|
|
121 | |
|
|
122 | if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) |
|
|
123 | { |
340 | { |
124 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
341 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
125 | PL_laststatval = req->result; |
342 | PL_laststatval = req->result; |
126 | PL_statcache = *(req->statdata); |
343 | PL_statcache = *(req->statdata); |
127 | |
|
|
128 | Safefree (req->statdata); |
|
|
129 | } |
344 | } |
130 | |
345 | |
131 | ENTER; |
346 | req_invoke (req); |
132 | PUSHMARK (SP); |
|
|
133 | XPUSHs (sv_2mortal (newSViv (req->result))); |
|
|
134 | |
347 | |
135 | if (req->type == REQ_OPEN) |
|
|
136 | { |
|
|
137 | /* convert fd to fh */ |
|
|
138 | SV *fh; |
|
|
139 | |
|
|
140 | PUTBACK; |
|
|
141 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
|
|
142 | SPAGAIN; |
|
|
143 | |
|
|
144 | fh = SvREFCNT_inc (POPs); |
|
|
145 | |
|
|
146 | PUSHMARK (SP); |
|
|
147 | XPUSHs (sv_2mortal (fh)); |
|
|
148 | } |
|
|
149 | |
|
|
150 | if (SvOK (req->callback)) |
|
|
151 | { |
|
|
152 | PUTBACK; |
|
|
153 | call_sv (req->callback, G_VOID | G_EVAL); |
|
|
154 | SPAGAIN; |
|
|
155 | } |
|
|
156 | |
|
|
157 | LEAVE; |
|
|
158 | |
|
|
159 | if (req->callback) |
|
|
160 | SvREFCNT_dec (req->callback); |
|
|
161 | |
|
|
162 | errno = errorno; |
|
|
163 | count++; |
348 | count++; |
164 | } |
349 | } |
165 | |
350 | |
166 | prv = req; |
|
|
167 | req = req->next; |
|
|
168 | Safefree (prv); |
351 | req_free (req); |
169 | |
|
|
170 | /* TODO: croak on errors? */ |
|
|
171 | } |
352 | } |
172 | |
353 | |
173 | return count; |
354 | return count; |
174 | } |
355 | } |
175 | |
356 | |
176 | static void *aio_proc(void *arg); |
357 | static void *aio_proc(void *arg); |
177 | |
358 | |
178 | static void |
|
|
179 | start_thread (void) |
359 | static void start_thread (void) |
180 | { |
360 | { |
181 | sigset_t fullsigset, oldsigset; |
361 | sigset_t fullsigset, oldsigset; |
182 | pthread_t tid; |
362 | pthread_t tid; |
183 | pthread_attr_t attr; |
363 | pthread_attr_t attr; |
184 | |
364 | |
… | |
… | |
193 | started++; |
373 | started++; |
194 | |
374 | |
195 | sigprocmask (SIG_SETMASK, &oldsigset, 0); |
375 | sigprocmask (SIG_SETMASK, &oldsigset, 0); |
196 | } |
376 | } |
197 | |
377 | |
198 | static void |
378 | static void req_send (aio_req req) |
199 | send_req (aio_req req) |
|
|
200 | { |
379 | { |
|
|
380 | while (started < wanted && nreqs >= started) |
|
|
381 | start_thread (); |
|
|
382 | |
201 | nreqs++; |
383 | nreqs++; |
202 | |
384 | |
203 | pthread_mutex_lock (&reqlock); |
385 | pthread_mutex_lock (&reqlock); |
204 | |
386 | |
205 | req->next = 0; |
387 | req->next = 0; |
… | |
… | |
213 | reqe = reqs = req; |
395 | reqe = reqs = req; |
214 | |
396 | |
215 | pthread_cond_signal (&reqwait); |
397 | pthread_cond_signal (&reqwait); |
216 | pthread_mutex_unlock (&reqlock); |
398 | pthread_mutex_unlock (&reqlock); |
217 | |
399 | |
218 | while (nreqs > max_outstanding) |
400 | if (nreqs > max_outstanding) |
|
|
401 | for (;;) |
|
|
402 | { |
|
|
403 | poll_cb (); |
|
|
404 | |
|
|
405 | if (nreqs <= max_outstanding) |
|
|
406 | break; |
|
|
407 | |
|
|
408 | poll_wait (); |
|
|
409 | } |
|
|
410 | } |
|
|
411 | |
|
|
412 | static void end_thread (void) |
|
|
413 | { |
|
|
414 | aio_req req; |
|
|
415 | Newz (0, req, 1, aio_cb); |
|
|
416 | req->type = REQ_QUIT; |
|
|
417 | |
|
|
418 | req_send (req); |
|
|
419 | } |
|
|
420 | |
|
|
421 | static void min_parallel (int nthreads) |
|
|
422 | { |
|
|
423 | if (wanted < nthreads) |
|
|
424 | wanted = nthreads; |
|
|
425 | } |
|
|
426 | |
|
|
427 | static void max_parallel (int nthreads) |
|
|
428 | { |
|
|
429 | int cur = started; |
|
|
430 | |
|
|
431 | if (wanted > nthreads) |
|
|
432 | wanted = nthreads; |
|
|
433 | |
|
|
434 | while (cur > wanted) |
|
|
435 | { |
|
|
436 | end_thread (); |
|
|
437 | cur--; |
|
|
438 | } |
|
|
439 | |
|
|
440 | while (started > wanted) |
219 | { |
441 | { |
220 | poll_wait (); |
442 | poll_wait (); |
221 | poll_cb (); |
443 | poll_cb (); |
222 | } |
444 | } |
223 | } |
445 | } |
224 | |
446 | |
225 | static void |
447 | static void create_pipe () |
226 | end_thread (void) |
|
|
227 | { |
448 | { |
228 | aio_req req; |
449 | if (pipe (respipe)) |
229 | New (0, req, 1, aio_cb); |
450 | croak ("unable to initialize result pipe"); |
230 | req->type = REQ_QUIT; |
|
|
231 | |
451 | |
232 | send_req (req); |
452 | if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)) |
233 | } |
453 | croak ("cannot set result pipe to nonblocking mode"); |
234 | |
454 | |
|
|
455 | if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) |
|
|
456 | croak ("cannot set result pipe to nonblocking mode"); |
|
|
457 | } |
|
|
458 | |
|
|
459 | /*****************************************************************************/ |
235 | /* work around various missing functions */ |
460 | /* work around various missing functions */ |
236 | |
461 | |
237 | #if !HAVE_PREADWRITE |
462 | #if !HAVE_PREADWRITE |
238 | # define pread aio_pread |
463 | # define pread aio_pread |
239 | # define pwrite aio_pwrite |
464 | # define pwrite aio_pwrite |
… | |
… | |
241 | /* |
466 | /* |
242 | * make our pread/pwrite safe against themselves, but not against |
467 | * make our pread/pwrite safe against themselves, but not against |
243 | * 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, |
244 | * but that's your problem, not mine. |
469 | * but that's your problem, not mine. |
245 | */ |
470 | */ |
246 | static pthread_mutex_t iolock = PTHREAD_MUTEX_INITIALIZER; |
471 | static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER; |
247 | |
472 | |
248 | static ssize_t |
473 | static ssize_t pread (int fd, void *buf, size_t count, off_t offset) |
249 | pread (int fd, void *buf, size_t count, off_t offset) |
|
|
250 | { |
474 | { |
251 | ssize_t res; |
475 | ssize_t res; |
252 | off_t ooffset; |
476 | off_t ooffset; |
253 | |
477 | |
254 | pthread_mutex_lock (&iolock); |
478 | pthread_mutex_lock (&preadwritelock); |
255 | ooffset = lseek (fd, 0, SEEK_CUR); |
479 | ooffset = lseek (fd, 0, SEEK_CUR); |
256 | lseek (fd, offset, SEEK_SET); |
480 | lseek (fd, offset, SEEK_SET); |
257 | res = read (fd, buf, count); |
481 | res = read (fd, buf, count); |
258 | lseek (fd, ooffset, SEEK_SET); |
482 | lseek (fd, ooffset, SEEK_SET); |
259 | pthread_mutex_unlock (&iolock); |
483 | pthread_mutex_unlock (&preadwritelock); |
260 | |
484 | |
261 | return res; |
485 | return res; |
262 | } |
486 | } |
263 | |
487 | |
264 | static ssize_t |
|
|
265 | 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) |
266 | { |
489 | { |
267 | ssize_t res; |
490 | ssize_t res; |
268 | off_t ooffset; |
491 | off_t ooffset; |
269 | |
492 | |
270 | pthread_mutex_lock (&iolock); |
493 | pthread_mutex_lock (&preadwritelock); |
271 | ooffset = lseek (fd, 0, SEEK_CUR); |
494 | ooffset = lseek (fd, 0, SEEK_CUR); |
272 | lseek (fd, offset, SEEK_SET); |
495 | lseek (fd, offset, SEEK_SET); |
273 | res = write (fd, buf, count); |
496 | res = write (fd, buf, count); |
274 | lseek (fd, offset, SEEK_SET); |
497 | lseek (fd, offset, SEEK_SET); |
275 | pthread_mutex_unlock (&iolock); |
498 | pthread_mutex_unlock (&preadwritelock); |
276 | |
499 | |
277 | return res; |
500 | return res; |
278 | } |
501 | } |
279 | #endif |
502 | #endif |
280 | |
503 | |
… | |
… | |
283 | #endif |
506 | #endif |
284 | |
507 | |
285 | #if !HAVE_READAHEAD |
508 | #if !HAVE_READAHEAD |
286 | # define readahead aio_readahead |
509 | # define readahead aio_readahead |
287 | |
510 | |
|
|
511 | static ssize_t readahead (int fd, off_t offset, size_t count) |
|
|
512 | { |
288 | static char readahead_buf[4096]; |
513 | char readahead_buf[4096]; |
289 | |
514 | |
290 | static ssize_t |
|
|
291 | readahead (int fd, off_t offset, size_t count) |
|
|
292 | { |
|
|
293 | while (count > 0) |
515 | while (count > 0) |
294 | { |
516 | { |
295 | size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); |
517 | size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); |
296 | |
518 | |
297 | pread (fd, readahead_buf, len, offset); |
519 | pread (fd, readahead_buf, len, offset); |
… | |
… | |
301 | |
523 | |
302 | errno = 0; |
524 | errno = 0; |
303 | } |
525 | } |
304 | #endif |
526 | #endif |
305 | |
527 | |
306 | static void * |
528 | #if !HAVE_READDIR_R |
|
|
529 | # define readdir_r aio_readdir_r |
|
|
530 | |
|
|
531 | static pthread_mutex_t readdirlock = PTHREAD_MUTEX_INITIALIZER; |
|
|
532 | |
|
|
533 | static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) |
|
|
534 | { |
|
|
535 | struct dirent *e; |
|
|
536 | int errorno; |
|
|
537 | |
|
|
538 | pthread_mutex_lock (&readdirlock); |
|
|
539 | |
|
|
540 | e = readdir (dirp); |
|
|
541 | errorno = errno; |
|
|
542 | |
|
|
543 | if (e) |
|
|
544 | { |
|
|
545 | *res = ent; |
|
|
546 | strcpy (ent->d_name, e->d_name); |
|
|
547 | } |
|
|
548 | else |
|
|
549 | *res = 0; |
|
|
550 | |
|
|
551 | pthread_mutex_unlock (&readdirlock); |
|
|
552 | |
|
|
553 | errno = errorno; |
|
|
554 | return e ? 0 : -1; |
|
|
555 | } |
|
|
556 | #endif |
|
|
557 | |
|
|
558 | /* sendfile always needs emulation */ |
|
|
559 | static ssize_t sendfile_ (int ofd, int ifd, off_t offset, size_t count) |
|
|
560 | { |
|
|
561 | ssize_t res; |
|
|
562 | |
|
|
563 | if (!count) |
|
|
564 | return 0; |
|
|
565 | |
|
|
566 | #if HAVE_SENDFILE |
|
|
567 | # if __linux |
|
|
568 | res = sendfile (ofd, ifd, &offset, count); |
|
|
569 | |
|
|
570 | # elif __freebsd |
|
|
571 | /* |
|
|
572 | * Of course, the freebsd sendfile is a dire hack with no thoughts |
|
|
573 | * wasted on making it similar to other I/O functions. |
|
|
574 | */ |
|
|
575 | { |
|
|
576 | off_t sbytes; |
|
|
577 | res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); |
|
|
578 | |
|
|
579 | if (res < 0 && sbytes) |
|
|
580 | /* maybe only on EAGAIN only: as usual, the manpage leaves you guessing */ |
|
|
581 | res = sbytes; |
|
|
582 | } |
|
|
583 | |
|
|
584 | # elif __hpux |
|
|
585 | res = sendfile (ofd, ifd, offset, count, 0, 0); |
|
|
586 | |
|
|
587 | # elif __solaris |
|
|
588 | { |
|
|
589 | struct sendfilevec vec; |
|
|
590 | size_t sbytes; |
|
|
591 | |
|
|
592 | vec.sfv_fd = ifd; |
|
|
593 | vec.sfv_flag = 0; |
|
|
594 | vec.sfv_off = offset; |
|
|
595 | vec.sfv_len = count; |
|
|
596 | |
|
|
597 | res = sendfilev (ofd, &vec, 1, &sbytes); |
|
|
598 | |
|
|
599 | if (res < 0 && sbytes) |
|
|
600 | res = sbytes; |
|
|
601 | } |
|
|
602 | |
|
|
603 | # endif |
|
|
604 | #else |
|
|
605 | res = -1; |
|
|
606 | errno = ENOSYS; |
|
|
607 | #endif |
|
|
608 | |
|
|
609 | if (res < 0 |
|
|
610 | && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK |
|
|
611 | #if __solaris |
|
|
612 | || errno == EAFNOSUPPORT || errno == EPROTOTYPE |
|
|
613 | #endif |
|
|
614 | ) |
|
|
615 | ) |
|
|
616 | { |
|
|
617 | /* emulate sendfile. this is a major pain in the ass */ |
|
|
618 | char buf[4096]; |
|
|
619 | res = 0; |
|
|
620 | |
|
|
621 | while (count) |
|
|
622 | { |
|
|
623 | ssize_t cnt; |
|
|
624 | |
|
|
625 | cnt = pread (ifd, buf, count > 4096 ? 4096 : count, offset); |
|
|
626 | |
|
|
627 | if (cnt <= 0) |
|
|
628 | { |
|
|
629 | if (cnt && !res) res = -1; |
|
|
630 | break; |
|
|
631 | } |
|
|
632 | |
|
|
633 | cnt = write (ofd, buf, cnt); |
|
|
634 | |
|
|
635 | if (cnt <= 0) |
|
|
636 | { |
|
|
637 | if (cnt && !res) res = -1; |
|
|
638 | break; |
|
|
639 | } |
|
|
640 | |
|
|
641 | offset += cnt; |
|
|
642 | res += cnt; |
|
|
643 | count -= cnt; |
|
|
644 | } |
|
|
645 | } |
|
|
646 | |
|
|
647 | return res; |
|
|
648 | } |
|
|
649 | |
|
|
650 | /* read a full directory */ |
|
|
651 | static int scandir_ (const char *path, void **namesp) |
|
|
652 | { |
|
|
653 | DIR *dirp = opendir (path); |
|
|
654 | union |
|
|
655 | { |
|
|
656 | struct dirent d; |
|
|
657 | char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; |
|
|
658 | } u; |
|
|
659 | struct dirent *entp; |
|
|
660 | char *name, *names; |
|
|
661 | int memlen = 4096; |
|
|
662 | int memofs = 0; |
|
|
663 | int res = 0; |
|
|
664 | int errorno; |
|
|
665 | |
|
|
666 | if (!dirp) |
|
|
667 | return -1; |
|
|
668 | |
|
|
669 | names = malloc (memlen); |
|
|
670 | |
|
|
671 | for (;;) |
|
|
672 | { |
|
|
673 | errno = 0, readdir_r (dirp, &u.d, &entp); |
|
|
674 | |
|
|
675 | if (!entp) |
|
|
676 | break; |
|
|
677 | |
|
|
678 | name = entp->d_name; |
|
|
679 | |
|
|
680 | if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) |
|
|
681 | { |
|
|
682 | int len = strlen (name) + 1; |
|
|
683 | |
|
|
684 | res++; |
|
|
685 | |
|
|
686 | while (memofs + len > memlen) |
|
|
687 | { |
|
|
688 | memlen *= 2; |
|
|
689 | names = realloc (names, memlen); |
|
|
690 | if (!names) |
|
|
691 | break; |
|
|
692 | } |
|
|
693 | |
|
|
694 | memcpy (names + memofs, name, len); |
|
|
695 | memofs += len; |
|
|
696 | } |
|
|
697 | } |
|
|
698 | |
|
|
699 | errorno = errno; |
|
|
700 | closedir (dirp); |
|
|
701 | |
|
|
702 | if (errorno) |
|
|
703 | { |
|
|
704 | free (names); |
|
|
705 | errno = errorno; |
|
|
706 | res = -1; |
|
|
707 | } |
|
|
708 | |
|
|
709 | *namesp = (void *)names; |
|
|
710 | return res; |
|
|
711 | } |
|
|
712 | |
|
|
713 | /*****************************************************************************/ |
|
|
714 | |
307 | aio_proc (void *thr_arg) |
715 | static void *aio_proc (void *thr_arg) |
308 | { |
716 | { |
309 | aio_req req; |
717 | aio_req req; |
310 | int type; |
718 | int type; |
311 | |
719 | |
312 | do |
720 | do |
… | |
… | |
331 | |
739 | |
332 | pthread_mutex_unlock (&reqlock); |
740 | pthread_mutex_unlock (&reqlock); |
333 | |
741 | |
334 | errno = 0; /* strictly unnecessary */ |
742 | errno = 0; /* strictly unnecessary */ |
335 | |
743 | |
336 | type = req->type; |
744 | if (!req->cancelled) |
337 | |
|
|
338 | switch (type) |
745 | switch (req->type) |
339 | { |
746 | { |
340 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
747 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
341 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
748 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
342 | |
749 | |
343 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
750 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
|
|
751 | case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length); break; |
344 | |
752 | |
345 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
753 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
346 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
754 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
347 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
755 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
348 | |
756 | |
349 | case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
757 | case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
350 | case REQ_CLOSE: req->result = close (req->fd); break; |
758 | case REQ_CLOSE: req->result = close (req->fd); break; |
351 | case REQ_UNLINK: req->result = unlink (req->dataptr); break; |
759 | case REQ_UNLINK: req->result = unlink (req->dataptr); break; |
|
|
760 | case REQ_RMDIR: req->result = rmdir (req->dataptr); break; |
|
|
761 | case REQ_RENAME: req->result = rename (req->data2ptr, req->dataptr); break; |
|
|
762 | case REQ_LINK: req->result = link (req->data2ptr, req->dataptr); break; |
|
|
763 | case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; |
352 | |
764 | |
353 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
765 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
354 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
766 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
|
|
767 | case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break; |
355 | |
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 | } |
|
|
778 | |
356 | case REQ_QUIT: |
779 | case REQ_QUIT: |
357 | break; |
780 | break; |
358 | |
781 | |
359 | default: |
782 | default: |
360 | req->result = ENOSYS; |
783 | req->result = ENOSYS; |
361 | break; |
784 | break; |
362 | } |
785 | } |
363 | |
786 | |
364 | req->errorno = errno; |
787 | req->errorno = errno; |
365 | |
788 | |
366 | pthread_mutex_lock (&reslock); |
789 | pthread_mutex_lock (&reslock); |
367 | |
790 | |
… | |
… | |
385 | while (type != REQ_QUIT); |
808 | while (type != REQ_QUIT); |
386 | |
809 | |
387 | return 0; |
810 | return 0; |
388 | } |
811 | } |
389 | |
812 | |
|
|
813 | /*****************************************************************************/ |
|
|
814 | |
|
|
815 | static void atfork_prepare (void) |
|
|
816 | { |
|
|
817 | pthread_mutex_lock (&reqlock); |
|
|
818 | pthread_mutex_lock (&reslock); |
|
|
819 | #if !HAVE_PREADWRITE |
|
|
820 | pthread_mutex_lock (&preadwritelock); |
|
|
821 | #endif |
|
|
822 | #if !HAVE_READDIR_R |
|
|
823 | pthread_mutex_lock (&readdirlock); |
|
|
824 | #endif |
|
|
825 | } |
|
|
826 | |
|
|
827 | static void atfork_parent (void) |
|
|
828 | { |
|
|
829 | #if !HAVE_READDIR_R |
|
|
830 | pthread_mutex_unlock (&readdirlock); |
|
|
831 | #endif |
|
|
832 | #if !HAVE_PREADWRITE |
|
|
833 | pthread_mutex_unlock (&preadwritelock); |
|
|
834 | #endif |
|
|
835 | pthread_mutex_unlock (&reslock); |
|
|
836 | pthread_mutex_unlock (&reqlock); |
|
|
837 | } |
|
|
838 | |
|
|
839 | static void atfork_child (void) |
|
|
840 | { |
|
|
841 | aio_req prv; |
|
|
842 | |
|
|
843 | started = 0; |
|
|
844 | |
|
|
845 | while (reqs) |
|
|
846 | { |
|
|
847 | prv = reqs; |
|
|
848 | reqs = prv->next; |
|
|
849 | req_free (prv); |
|
|
850 | } |
|
|
851 | |
|
|
852 | reqs = reqe = 0; |
|
|
853 | |
|
|
854 | while (ress) |
|
|
855 | { |
|
|
856 | prv = ress; |
|
|
857 | ress = prv->next; |
|
|
858 | req_free (prv); |
|
|
859 | } |
|
|
860 | |
|
|
861 | ress = rese = 0; |
|
|
862 | |
|
|
863 | close (respipe [0]); |
|
|
864 | close (respipe [1]); |
|
|
865 | create_pipe (); |
|
|
866 | |
|
|
867 | atfork_parent (); |
|
|
868 | } |
|
|
869 | |
|
|
870 | #define dREQ \ |
|
|
871 | aio_req req; \ |
|
|
872 | \ |
|
|
873 | if (SvOK (callback) && !SvROK (callback)) \ |
|
|
874 | croak ("callback must be undef or of reference type"); \ |
|
|
875 | \ |
|
|
876 | Newz (0, req, 1, aio_cb); \ |
|
|
877 | if (!req) \ |
|
|
878 | croak ("out of memory during aio_req allocation"); \ |
|
|
879 | \ |
|
|
880 | req->callback = newSVsv (callback) |
|
|
881 | |
|
|
882 | #define REQ_SEND \ |
|
|
883 | req_send (req); \ |
|
|
884 | \ |
|
|
885 | if (GIMME_V != G_VOID) \ |
|
|
886 | XPUSHs (req_sv (req, AIO_REQ_KLASS)); |
|
|
887 | |
390 | MODULE = IO::AIO PACKAGE = IO::AIO |
888 | MODULE = IO::AIO PACKAGE = IO::AIO |
391 | |
889 | |
392 | PROTOTYPES: ENABLE |
890 | PROTOTYPES: ENABLE |
393 | |
891 | |
394 | BOOT: |
892 | BOOT: |
395 | { |
893 | { |
396 | if (pipe (respipe)) |
894 | HV *stash = gv_stashpv ("IO::AIO", 1); |
397 | croak ("unable to initialize result pipe"); |
895 | newCONSTSUB (stash, "EXDEV", newSViv (EXDEV)); |
|
|
896 | newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY)); |
|
|
897 | newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY)); |
398 | |
898 | |
399 | if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)) |
899 | create_pipe (); |
400 | croak ("cannot set result pipe to nonblocking mode"); |
900 | pthread_atfork (atfork_prepare, atfork_parent, atfork_child); |
401 | |
|
|
402 | if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) |
|
|
403 | croak ("cannot set result pipe to nonblocking mode"); |
|
|
404 | } |
901 | } |
405 | |
902 | |
406 | void |
903 | void |
407 | min_parallel(nthreads) |
904 | min_parallel (nthreads) |
408 | int nthreads |
905 | int nthreads |
409 | PROTOTYPE: $ |
906 | PROTOTYPE: $ |
410 | CODE: |
|
|
411 | while (nthreads > started) |
|
|
412 | start_thread (); |
|
|
413 | |
907 | |
414 | void |
908 | void |
415 | max_parallel(nthreads) |
909 | max_parallel (nthreads) |
416 | int nthreads |
910 | int nthreads |
417 | PROTOTYPE: $ |
911 | PROTOTYPE: $ |
418 | CODE: |
|
|
419 | { |
|
|
420 | int cur = started; |
|
|
421 | while (cur > nthreads) |
|
|
422 | { |
|
|
423 | end_thread (); |
|
|
424 | cur--; |
|
|
425 | } |
|
|
426 | |
|
|
427 | while (started > nthreads) |
|
|
428 | { |
|
|
429 | poll_wait (); |
|
|
430 | poll_cb (); |
|
|
431 | } |
|
|
432 | } |
|
|
433 | |
912 | |
434 | int |
913 | int |
435 | max_outstanding(nreqs) |
914 | max_outstanding (nreqs) |
436 | int nreqs |
915 | int nreqs |
437 | PROTOTYPE: $ |
916 | PROTOTYPE: $ |
438 | CODE: |
917 | CODE: |
439 | RETVAL = max_outstanding; |
918 | RETVAL = max_outstanding; |
440 | max_outstanding = nreqs; |
919 | max_outstanding = nreqs; |
441 | |
920 | |
442 | void |
921 | void |
443 | aio_open(pathname,flags,mode,callback=&PL_sv_undef) |
922 | aio_open (pathname,flags,mode,callback=&PL_sv_undef) |
444 | SV * pathname |
923 | SV * pathname |
445 | int flags |
924 | int flags |
446 | int mode |
925 | int mode |
447 | SV * callback |
926 | SV * callback |
448 | PROTOTYPE: $$$;$ |
927 | PROTOTYPE: $$$;$ |
449 | CODE: |
928 | PPCODE: |
450 | { |
929 | { |
451 | aio_req req; |
930 | dREQ; |
452 | |
|
|
453 | Newz (0, req, 1, aio_cb); |
|
|
454 | |
|
|
455 | if (!req) |
|
|
456 | croak ("out of memory during aio_req allocation"); |
|
|
457 | |
931 | |
458 | req->type = REQ_OPEN; |
932 | req->type = REQ_OPEN; |
459 | req->data = newSVsv (pathname); |
933 | req->data = newSVsv (pathname); |
460 | req->dataptr = SvPV_nolen (req->data); |
934 | req->dataptr = SvPVbyte_nolen (req->data); |
461 | req->fd = flags; |
935 | req->fd = flags; |
462 | req->mode = mode; |
936 | req->mode = mode; |
463 | req->callback = SvREFCNT_inc (callback); |
|
|
464 | |
937 | |
465 | send_req (req); |
938 | REQ_SEND; |
466 | } |
939 | } |
467 | |
940 | |
468 | void |
941 | void |
469 | aio_close(fh,callback=&PL_sv_undef) |
942 | aio_close (fh,callback=&PL_sv_undef) |
470 | SV * fh |
943 | SV * fh |
471 | SV * callback |
944 | SV * callback |
472 | PROTOTYPE: $;$ |
945 | PROTOTYPE: $;$ |
473 | ALIAS: |
946 | ALIAS: |
474 | aio_close = REQ_CLOSE |
947 | aio_close = REQ_CLOSE |
475 | aio_fsync = REQ_FSYNC |
948 | aio_fsync = REQ_FSYNC |
476 | aio_fdatasync = REQ_FDATASYNC |
949 | aio_fdatasync = REQ_FDATASYNC |
477 | CODE: |
950 | PPCODE: |
478 | { |
951 | { |
479 | aio_req req; |
952 | dREQ; |
480 | |
|
|
481 | Newz (0, req, 1, aio_cb); |
|
|
482 | |
|
|
483 | if (!req) |
|
|
484 | croak ("out of memory during aio_req allocation"); |
|
|
485 | |
953 | |
486 | req->type = ix; |
954 | req->type = ix; |
487 | req->fh = newSVsv (fh); |
955 | req->fh = newSVsv (fh); |
488 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
956 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
489 | req->callback = SvREFCNT_inc (callback); |
|
|
490 | |
957 | |
491 | send_req (req); |
958 | REQ_SEND (req); |
492 | } |
959 | } |
493 | |
960 | |
494 | void |
961 | void |
495 | aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
962 | aio_read (fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
496 | SV * fh |
963 | SV * fh |
497 | UV offset |
964 | UV offset |
498 | IV length |
965 | UV length |
499 | SV * data |
966 | SV * data |
500 | IV dataoffset |
967 | UV dataoffset |
501 | SV * callback |
968 | SV * callback |
502 | ALIAS: |
969 | ALIAS: |
503 | aio_read = REQ_READ |
970 | aio_read = REQ_READ |
504 | aio_write = REQ_WRITE |
971 | aio_write = REQ_WRITE |
505 | PROTOTYPE: $$$$$;$ |
972 | PROTOTYPE: $$$$$;$ |
506 | CODE: |
973 | PPCODE: |
507 | { |
974 | { |
508 | aio_req req; |
975 | aio_req req; |
509 | STRLEN svlen; |
976 | STRLEN svlen; |
510 | char *svptr = SvPV (data, svlen); |
977 | char *svptr = SvPVbyte (data, svlen); |
511 | |
978 | |
512 | SvUPGRADE (data, SVt_PV); |
979 | SvUPGRADE (data, SVt_PV); |
513 | SvPOK_on (data); |
980 | SvPOK_on (data); |
514 | |
981 | |
515 | if (dataoffset < 0) |
982 | if (dataoffset < 0) |
… | |
… | |
531 | } |
998 | } |
532 | |
999 | |
533 | if (length < 0) |
1000 | if (length < 0) |
534 | croak ("length must not be negative"); |
1001 | croak ("length must not be negative"); |
535 | |
1002 | |
536 | Newz (0, req, 1, aio_cb); |
1003 | { |
|
|
1004 | dREQ; |
537 | |
1005 | |
538 | if (!req) |
|
|
539 | croak ("out of memory during aio_req allocation"); |
|
|
540 | |
|
|
541 | req->type = ix; |
1006 | req->type = ix; |
542 | req->fh = newSVsv (fh); |
1007 | req->fh = newSVsv (fh); |
543 | req->fd = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh)) |
1008 | req->fd = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh)) |
544 | : IoOFP (sv_2io (fh))); |
1009 | : IoOFP (sv_2io (fh))); |
545 | req->offset = offset; |
1010 | req->offset = offset; |
|
|
1011 | req->length = length; |
|
|
1012 | req->data = SvREFCNT_inc (data); |
|
|
1013 | req->dataptr = (char *)svptr + dataoffset; |
|
|
1014 | |
|
|
1015 | if (!SvREADONLY (data)) |
|
|
1016 | { |
|
|
1017 | SvREADONLY_on (data); |
|
|
1018 | req->data2ptr = (void *)data; |
|
|
1019 | } |
|
|
1020 | |
|
|
1021 | REQ_SEND; |
|
|
1022 | } |
|
|
1023 | } |
|
|
1024 | |
|
|
1025 | void |
|
|
1026 | aio_sendfile (out_fh,in_fh,in_offset,length,callback=&PL_sv_undef) |
|
|
1027 | SV * out_fh |
|
|
1028 | SV * in_fh |
|
|
1029 | UV in_offset |
|
|
1030 | UV length |
|
|
1031 | SV * callback |
|
|
1032 | PROTOTYPE: $$$$;$ |
|
|
1033 | PPCODE: |
|
|
1034 | { |
|
|
1035 | dREQ; |
|
|
1036 | |
|
|
1037 | req->type = REQ_SENDFILE; |
|
|
1038 | req->fh = newSVsv (out_fh); |
|
|
1039 | req->fd = PerlIO_fileno (IoIFP (sv_2io (out_fh))); |
|
|
1040 | req->fh2 = newSVsv (in_fh); |
|
|
1041 | req->fd2 = PerlIO_fileno (IoIFP (sv_2io (in_fh))); |
|
|
1042 | req->offset = in_offset; |
546 | req->length = length; |
1043 | req->length = length; |
547 | req->data = SvREFCNT_inc (data); |
|
|
548 | req->dataptr = (char *)svptr + dataoffset; |
|
|
549 | req->callback = SvREFCNT_inc (callback); |
|
|
550 | |
1044 | |
551 | send_req (req); |
1045 | REQ_SEND; |
552 | } |
1046 | } |
553 | |
1047 | |
554 | void |
1048 | void |
555 | aio_readahead(fh,offset,length,callback=&PL_sv_undef) |
1049 | aio_readahead (fh,offset,length,callback=&PL_sv_undef) |
556 | SV * fh |
1050 | SV * fh |
557 | UV offset |
1051 | UV offset |
558 | IV length |
1052 | IV length |
559 | SV * callback |
1053 | SV * callback |
560 | PROTOTYPE: $$$;$ |
1054 | PROTOTYPE: $$$;$ |
561 | CODE: |
1055 | PPCODE: |
562 | { |
1056 | { |
563 | aio_req req; |
1057 | dREQ; |
564 | |
|
|
565 | if (length < 0) |
|
|
566 | croak ("length must not be negative"); |
|
|
567 | |
|
|
568 | Newz (0, req, 1, aio_cb); |
|
|
569 | |
|
|
570 | if (!req) |
|
|
571 | croak ("out of memory during aio_req allocation"); |
|
|
572 | |
1058 | |
573 | req->type = REQ_READAHEAD; |
1059 | req->type = REQ_READAHEAD; |
574 | req->fh = newSVsv (fh); |
1060 | req->fh = newSVsv (fh); |
575 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
1061 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
576 | req->offset = offset; |
1062 | req->offset = offset; |
577 | req->length = length; |
1063 | req->length = length; |
578 | req->callback = SvREFCNT_inc (callback); |
|
|
579 | |
1064 | |
580 | send_req (req); |
1065 | REQ_SEND; |
581 | } |
1066 | } |
582 | |
1067 | |
583 | void |
1068 | void |
584 | aio_stat(fh_or_path,callback=&PL_sv_undef) |
1069 | aio_stat (fh_or_path,callback=&PL_sv_undef) |
585 | SV * fh_or_path |
1070 | SV * fh_or_path |
586 | SV * callback |
1071 | SV * callback |
587 | ALIAS: |
1072 | ALIAS: |
588 | aio_stat = REQ_STAT |
1073 | aio_stat = REQ_STAT |
589 | aio_lstat = REQ_LSTAT |
1074 | aio_lstat = REQ_LSTAT |
590 | CODE: |
1075 | PPCODE: |
591 | { |
1076 | { |
592 | aio_req req; |
1077 | dREQ; |
593 | |
|
|
594 | Newz (0, req, 1, aio_cb); |
|
|
595 | |
|
|
596 | if (!req) |
|
|
597 | croak ("out of memory during aio_req allocation"); |
|
|
598 | |
1078 | |
599 | New (0, req->statdata, 1, Stat_t); |
1079 | New (0, req->statdata, 1, Stat_t); |
600 | |
|
|
601 | if (!req->statdata) |
1080 | if (!req->statdata) |
|
|
1081 | { |
|
|
1082 | req_free (req); |
602 | croak ("out of memory during aio_req->statdata allocation"); |
1083 | croak ("out of memory during aio_req->statdata allocation"); |
|
|
1084 | } |
603 | |
1085 | |
604 | if (SvPOK (fh_or_path)) |
1086 | if (SvPOK (fh_or_path)) |
605 | { |
1087 | { |
606 | req->type = ix; |
1088 | req->type = ix; |
607 | req->data = newSVsv (fh_or_path); |
1089 | req->data = newSVsv (fh_or_path); |
608 | req->dataptr = SvPV_nolen (req->data); |
1090 | req->dataptr = SvPVbyte_nolen (req->data); |
609 | } |
1091 | } |
610 | else |
1092 | else |
611 | { |
1093 | { |
612 | req->type = REQ_FSTAT; |
1094 | req->type = REQ_FSTAT; |
613 | req->fh = newSVsv (fh_or_path); |
1095 | req->fh = newSVsv (fh_or_path); |
614 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
1096 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
615 | } |
1097 | } |
616 | |
1098 | |
617 | req->callback = SvREFCNT_inc (callback); |
1099 | REQ_SEND; |
618 | |
|
|
619 | send_req (req); |
|
|
620 | } |
1100 | } |
621 | |
1101 | |
622 | void |
1102 | void |
623 | aio_unlink(pathname,callback=&PL_sv_undef) |
1103 | aio_unlink (pathname,callback=&PL_sv_undef) |
624 | SV * pathname |
1104 | SV * pathname |
625 | SV * callback |
1105 | SV * callback |
|
|
1106 | ALIAS: |
|
|
1107 | aio_unlink = REQ_UNLINK |
|
|
1108 | aio_rmdir = REQ_RMDIR |
|
|
1109 | aio_readdir = REQ_READDIR |
626 | CODE: |
1110 | PPCODE: |
627 | { |
1111 | { |
628 | aio_req req; |
1112 | dREQ; |
629 | |
1113 | |
630 | Newz (0, req, 1, aio_cb); |
1114 | req->type = ix; |
|
|
1115 | req->data = newSVsv (pathname); |
|
|
1116 | req->dataptr = SvPVbyte_nolen (req->data); |
631 | |
1117 | |
632 | if (!req) |
1118 | REQ_SEND; |
633 | croak ("out of memory during aio_req allocation"); |
1119 | } |
|
|
1120 | |
|
|
1121 | void |
|
|
1122 | aio_link (oldpath,newpath,callback=&PL_sv_undef) |
|
|
1123 | SV * oldpath |
|
|
1124 | SV * newpath |
|
|
1125 | SV * callback |
|
|
1126 | ALIAS: |
|
|
1127 | aio_link = REQ_LINK |
|
|
1128 | aio_symlink = REQ_SYMLINK |
|
|
1129 | aio_rename = REQ_RENAME |
|
|
1130 | PPCODE: |
|
|
1131 | { |
|
|
1132 | dREQ; |
634 | |
1133 | |
635 | req->type = REQ_UNLINK; |
1134 | req->type = ix; |
|
|
1135 | req->fh = newSVsv (oldpath); |
|
|
1136 | req->data2ptr = SvPVbyte_nolen (req->fh); |
636 | req->data = newSVsv (pathname); |
1137 | req->data = newSVsv (newpath); |
637 | req->dataptr = SvPV_nolen (req->data); |
1138 | req->dataptr = SvPVbyte_nolen (req->data); |
638 | req->callback = SvREFCNT_inc (callback); |
|
|
639 | |
1139 | |
640 | send_req (req); |
1140 | REQ_SEND; |
641 | } |
1141 | } |
642 | |
1142 | |
643 | void |
1143 | void |
|
|
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 |
|
|
1161 | PROTOTYPE: ;$ |
|
|
1162 | PPCODE: |
|
|
1163 | { |
|
|
1164 | dREQ; |
|
|
1165 | req->type = REQ_GROUP; |
|
|
1166 | req->grp_next = req; |
|
|
1167 | req->grp_prev = req; |
|
|
1168 | |
|
|
1169 | req_send (req); |
|
|
1170 | XPUSHs (req_sv (req, AIO_GRP_KLASS)); |
|
|
1171 | } |
|
|
1172 | |
|
|
1173 | void |
644 | flush() |
1174 | flush () |
645 | PROTOTYPE: |
1175 | PROTOTYPE: |
646 | CODE: |
1176 | CODE: |
647 | while (nreqs) |
1177 | while (nreqs) |
648 | { |
1178 | { |
649 | poll_wait (); |
1179 | poll_wait (); |
… | |
… | |
689 | CODE: |
1219 | CODE: |
690 | RETVAL = nreqs; |
1220 | RETVAL = nreqs; |
691 | OUTPUT: |
1221 | OUTPUT: |
692 | RETVAL |
1222 | RETVAL |
693 | |
1223 | |
|
|
1224 | PROTOTYPES: DISABLE |
|
|
1225 | |
|
|
1226 | MODULE = IO::AIO PACKAGE = IO::AIO::REQ |
|
|
1227 | |
|
|
1228 | void |
|
|
1229 | cancel (aio_req_ornot req) |
|
|
1230 | PROTOTYPE: |
|
|
1231 | CODE: |
|
|
1232 | req_cancel (req); |
|
|
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 | |