|
|
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" |
|
|
9 | |
|
|
10 | #include <pthread.h> |
6 | |
11 | |
7 | #include <sys/types.h> |
12 | #include <sys/types.h> |
8 | #include <sys/stat.h> |
13 | #include <sys/stat.h> |
9 | |
14 | |
10 | #include <unistd.h> |
15 | #include <unistd.h> |
11 | #include <fcntl.h> |
16 | #include <fcntl.h> |
12 | #include <signal.h> |
17 | #include <signal.h> |
13 | #include <sched.h> |
18 | #include <sched.h> |
14 | |
19 | |
15 | #ifndef _REENTRANT |
20 | #if HAVE_SENDFILE |
16 | # define _REENTRANT 1 |
21 | # if __linux |
|
|
22 | # include <sys/sendfile.h> |
|
|
23 | # elif __freebsd |
|
|
24 | # include <sys/socket.h> |
|
|
25 | # include <sys/uio.h> |
|
|
26 | # elif __hpux |
|
|
27 | # include <sys/socket.h> |
|
|
28 | # else |
|
|
29 | # error sendfile support requested but not available |
|
|
30 | # endif |
17 | #endif |
31 | #endif |
18 | #include <errno.h> |
|
|
19 | |
|
|
20 | #include <pthread.h> |
|
|
21 | |
|
|
22 | typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
|
|
23 | typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
|
|
24 | typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
|
|
25 | |
32 | |
26 | #if __ia64 |
33 | #if __ia64 |
27 | # define STACKSIZE 65536 |
34 | # define STACKSIZE 65536 |
28 | #else |
35 | #else |
29 | # define STACKSIZE 4096 |
36 | # define STACKSIZE 4096 |
… | |
… | |
31 | |
38 | |
32 | enum { |
39 | enum { |
33 | REQ_QUIT, |
40 | REQ_QUIT, |
34 | REQ_OPEN, REQ_CLOSE, |
41 | REQ_OPEN, REQ_CLOSE, |
35 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
42 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
|
|
43 | REQ_SENDFILE, |
36 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_UNLINK, |
44 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
37 | REQ_FSYNC, REQ_FDATASYNC, |
45 | REQ_FSYNC, REQ_FDATASYNC, |
|
|
46 | REQ_UNLINK, REQ_RMDIR, |
|
|
47 | REQ_SYMLINK, |
38 | }; |
48 | }; |
39 | |
49 | |
40 | typedef struct aio_cb { |
50 | typedef struct aio_cb { |
41 | struct aio_cb *volatile next; |
51 | struct aio_cb *volatile next; |
42 | |
52 | |
43 | int type; |
53 | int type; |
44 | |
54 | |
45 | int fd; |
55 | int fd, fd2; |
46 | off_t offset; |
56 | off_t offset; |
47 | size_t length; |
57 | size_t length; |
48 | ssize_t result; |
58 | ssize_t result; |
49 | mode_t mode; /* open */ |
59 | mode_t mode; /* open */ |
50 | int errorno; |
60 | int errorno; |
51 | SV *data, *callback, *fh; |
61 | SV *data, *callback; |
52 | void *dataptr; |
62 | SV *fh, *fh2; |
|
|
63 | void *dataptr, *data2ptr; |
53 | STRLEN dataoffset; |
64 | STRLEN dataoffset; |
54 | |
65 | |
55 | Stat_t *statdata; |
66 | Stat_t *statdata; |
56 | } aio_cb; |
67 | } aio_cb; |
57 | |
68 | |
58 | typedef aio_cb *aio_req; |
69 | typedef aio_cb *aio_req; |
59 | |
70 | |
60 | static int started; |
71 | static int started, wanted; |
61 | static volatile int nreqs; |
72 | static volatile int nreqs; |
62 | static int max_outstanding = 1<<30; |
73 | static int max_outstanding = 1<<30; |
63 | static int respipe [2]; |
74 | static int respipe [2]; |
64 | |
75 | |
65 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
76 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
66 | static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; |
77 | static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; |
67 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
78 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
68 | |
79 | |
69 | static volatile aio_req reqs, reqe; /* queue start, queue end */ |
80 | static volatile aio_req reqs, reqe; /* queue start, queue end */ |
70 | static volatile aio_req ress, rese; /* queue start, queue end */ |
81 | static volatile aio_req ress, rese; /* queue start, queue end */ |
|
|
82 | |
|
|
83 | static void free_req (aio_req req) |
|
|
84 | { |
|
|
85 | if (req->data) |
|
|
86 | SvREFCNT_dec (req->data); |
|
|
87 | |
|
|
88 | if (req->fh) |
|
|
89 | SvREFCNT_dec (req->fh); |
|
|
90 | |
|
|
91 | if (req->fh2) |
|
|
92 | SvREFCNT_dec (req->fh2); |
|
|
93 | |
|
|
94 | if (req->statdata) |
|
|
95 | Safefree (req->statdata); |
|
|
96 | |
|
|
97 | if (req->callback) |
|
|
98 | SvREFCNT_dec (req->callback); |
|
|
99 | |
|
|
100 | Safefree (req); |
|
|
101 | } |
71 | |
102 | |
72 | static void |
103 | static void |
73 | poll_wait () |
104 | poll_wait () |
74 | { |
105 | { |
75 | if (nreqs && !ress) |
106 | if (nreqs && !ress) |
… | |
… | |
85 | static int |
116 | static int |
86 | poll_cb () |
117 | poll_cb () |
87 | { |
118 | { |
88 | dSP; |
119 | dSP; |
89 | int count = 0; |
120 | int count = 0; |
|
|
121 | int do_croak = 0; |
90 | aio_req req, prv; |
122 | aio_req req; |
91 | |
123 | |
92 | pthread_mutex_lock (&reslock); |
124 | for (;;) |
93 | |
|
|
94 | { |
|
|
95 | /* read any signals sent by the worker threads */ |
|
|
96 | char buf [32]; |
|
|
97 | while (read (respipe [0], buf, 32) > 0) |
|
|
98 | ; |
|
|
99 | } |
|
|
100 | |
|
|
101 | req = ress; |
|
|
102 | ress = rese = 0; |
|
|
103 | |
|
|
104 | pthread_mutex_unlock (&reslock); |
|
|
105 | |
|
|
106 | while (req) |
|
|
107 | { |
125 | { |
|
|
126 | pthread_mutex_lock (&reslock); |
|
|
127 | req = ress; |
|
|
128 | |
|
|
129 | if (req) |
|
|
130 | { |
|
|
131 | ress = req->next; |
|
|
132 | |
|
|
133 | if (!ress) |
|
|
134 | { |
|
|
135 | /* read any signals sent by the worker threads */ |
|
|
136 | char buf [32]; |
|
|
137 | while (read (respipe [0], buf, 32) == 32) |
|
|
138 | ; |
|
|
139 | |
|
|
140 | rese = 0; |
|
|
141 | } |
|
|
142 | } |
|
|
143 | |
|
|
144 | pthread_mutex_unlock (&reslock); |
|
|
145 | |
|
|
146 | if (!req) |
|
|
147 | break; |
|
|
148 | |
108 | nreqs--; |
149 | nreqs--; |
109 | |
150 | |
110 | if (req->type == REQ_QUIT) |
151 | if (req->type == REQ_QUIT) |
111 | started--; |
152 | started--; |
112 | else |
153 | else |
113 | { |
154 | { |
114 | int errorno = errno; |
155 | int errorno = errno; |
115 | errno = req->errorno; |
156 | errno = req->errorno; |
116 | |
157 | |
117 | if (req->type == REQ_READ) |
158 | if (req->type == REQ_READ) |
118 | SvCUR_set (req->data, req->dataoffset |
159 | SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); |
119 | + req->result > 0 ? req->result : 0); |
|
|
120 | |
160 | |
|
|
161 | if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE)) |
|
|
162 | SvREADONLY_off (req->data); |
|
|
163 | |
121 | if (req->data) |
164 | if (req->statdata) |
122 | SvREFCNT_dec (req->data); |
|
|
123 | |
|
|
124 | if (req->fh) |
|
|
125 | SvREFCNT_dec (req->fh); |
|
|
126 | |
|
|
127 | if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) |
|
|
128 | { |
165 | { |
129 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
166 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
130 | PL_laststatval = req->result; |
167 | PL_laststatval = req->result; |
131 | PL_statcache = *(req->statdata); |
168 | PL_statcache = *(req->statdata); |
132 | |
|
|
133 | Safefree (req->statdata); |
|
|
134 | } |
169 | } |
135 | |
170 | |
136 | ENTER; |
171 | ENTER; |
137 | PUSHMARK (SP); |
172 | PUSHMARK (SP); |
138 | XPUSHs (sv_2mortal (newSViv (req->result))); |
173 | XPUSHs (sv_2mortal (newSViv (req->result))); |
… | |
… | |
155 | if (SvOK (req->callback)) |
190 | if (SvOK (req->callback)) |
156 | { |
191 | { |
157 | PUTBACK; |
192 | PUTBACK; |
158 | call_sv (req->callback, G_VOID | G_EVAL); |
193 | call_sv (req->callback, G_VOID | G_EVAL); |
159 | SPAGAIN; |
194 | SPAGAIN; |
|
|
195 | |
|
|
196 | if (SvTRUE (ERRSV)) |
|
|
197 | { |
|
|
198 | free_req (req); |
|
|
199 | croak (0); |
|
|
200 | } |
160 | } |
201 | } |
161 | |
202 | |
162 | LEAVE; |
203 | LEAVE; |
163 | |
|
|
164 | if (req->callback) |
|
|
165 | SvREFCNT_dec (req->callback); |
|
|
166 | |
204 | |
167 | errno = errorno; |
205 | errno = errorno; |
168 | count++; |
206 | count++; |
169 | } |
207 | } |
170 | |
208 | |
171 | prv = req; |
209 | free_req (req); |
172 | req = req->next; |
|
|
173 | Safefree (prv); |
|
|
174 | |
|
|
175 | /* TODO: croak on errors? */ |
|
|
176 | } |
210 | } |
177 | |
211 | |
178 | return count; |
212 | return count; |
179 | } |
213 | } |
180 | |
214 | |
… | |
… | |
201 | } |
235 | } |
202 | |
236 | |
203 | static void |
237 | static void |
204 | send_req (aio_req req) |
238 | send_req (aio_req req) |
205 | { |
239 | { |
|
|
240 | while (started < wanted && nreqs >= started) |
|
|
241 | start_thread (); |
|
|
242 | |
206 | nreqs++; |
243 | nreqs++; |
207 | |
244 | |
208 | pthread_mutex_lock (&reqlock); |
245 | pthread_mutex_lock (&reqlock); |
209 | |
246 | |
210 | req->next = 0; |
247 | req->next = 0; |
… | |
… | |
218 | reqe = reqs = req; |
255 | reqe = reqs = req; |
219 | |
256 | |
220 | pthread_cond_signal (&reqwait); |
257 | pthread_cond_signal (&reqwait); |
221 | pthread_mutex_unlock (&reqlock); |
258 | pthread_mutex_unlock (&reqlock); |
222 | |
259 | |
223 | while (nreqs > max_outstanding) |
260 | if (nreqs > max_outstanding) |
|
|
261 | for (;;) |
|
|
262 | { |
|
|
263 | poll_cb (); |
|
|
264 | |
|
|
265 | if (nreqs <= max_outstanding) |
|
|
266 | break; |
|
|
267 | |
|
|
268 | poll_wait (); |
|
|
269 | } |
|
|
270 | } |
|
|
271 | |
|
|
272 | static void |
|
|
273 | end_thread (void) |
|
|
274 | { |
|
|
275 | aio_req req; |
|
|
276 | Newz (0, req, 1, aio_cb); |
|
|
277 | req->type = REQ_QUIT; |
|
|
278 | |
|
|
279 | send_req (req); |
|
|
280 | } |
|
|
281 | |
|
|
282 | static void min_parallel (int nthreads) |
|
|
283 | { |
|
|
284 | if (wanted < nthreads) |
|
|
285 | wanted = nthreads; |
|
|
286 | } |
|
|
287 | |
|
|
288 | static void max_parallel (int nthreads) |
|
|
289 | { |
|
|
290 | int cur = started; |
|
|
291 | |
|
|
292 | if (wanted > nthreads) |
|
|
293 | wanted = nthreads; |
|
|
294 | |
|
|
295 | while (cur > wanted) |
|
|
296 | { |
|
|
297 | end_thread (); |
|
|
298 | cur--; |
|
|
299 | } |
|
|
300 | |
|
|
301 | while (started > wanted) |
224 | { |
302 | { |
225 | poll_wait (); |
303 | poll_wait (); |
226 | poll_cb (); |
304 | poll_cb (); |
227 | } |
305 | } |
228 | } |
306 | } |
229 | |
307 | |
230 | static void |
308 | static void create_pipe () |
231 | end_thread (void) |
|
|
232 | { |
309 | { |
|
|
310 | if (pipe (respipe)) |
|
|
311 | croak ("unable to initialize result pipe"); |
|
|
312 | |
|
|
313 | if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)) |
|
|
314 | croak ("cannot set result pipe to nonblocking mode"); |
|
|
315 | |
|
|
316 | if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) |
|
|
317 | croak ("cannot set result pipe to nonblocking mode"); |
|
|
318 | } |
|
|
319 | |
|
|
320 | static void atfork_prepare (void) |
|
|
321 | { |
|
|
322 | pthread_mutex_lock (&reqlock); |
|
|
323 | pthread_mutex_lock (&reslock); |
|
|
324 | } |
|
|
325 | |
|
|
326 | static void atfork_parent (void) |
|
|
327 | { |
|
|
328 | pthread_mutex_unlock (&reslock); |
|
|
329 | pthread_mutex_unlock (&reqlock); |
|
|
330 | } |
|
|
331 | |
|
|
332 | static void atfork_child (void) |
|
|
333 | { |
233 | aio_req req; |
334 | aio_req prv; |
234 | New (0, req, 1, aio_cb); |
|
|
235 | req->type = REQ_QUIT; |
|
|
236 | |
335 | |
237 | send_req (req); |
336 | started = 0; |
238 | } |
|
|
239 | |
337 | |
|
|
338 | while (reqs) |
|
|
339 | { |
|
|
340 | prv = reqs; |
|
|
341 | reqs = prv->next; |
|
|
342 | free_req (prv); |
|
|
343 | } |
|
|
344 | |
|
|
345 | reqs = reqe = 0; |
|
|
346 | |
|
|
347 | while (ress) |
|
|
348 | { |
|
|
349 | prv = ress; |
|
|
350 | ress = prv->next; |
|
|
351 | free_req (prv); |
|
|
352 | } |
|
|
353 | |
|
|
354 | ress = rese = 0; |
|
|
355 | |
|
|
356 | close (respipe [0]); |
|
|
357 | close (respipe [1]); |
|
|
358 | create_pipe (); |
|
|
359 | |
|
|
360 | atfork_parent (); |
|
|
361 | } |
|
|
362 | |
|
|
363 | /*****************************************************************************/ |
240 | /* work around various missing functions */ |
364 | /* work around various missing functions */ |
241 | |
365 | |
242 | #if !HAVE_PREADWRITE |
366 | #if !HAVE_PREADWRITE |
243 | # define pread aio_pread |
367 | # define pread aio_pread |
244 | # define pwrite aio_pwrite |
368 | # define pwrite aio_pwrite |
… | |
… | |
306 | |
430 | |
307 | errno = 0; |
431 | errno = 0; |
308 | } |
432 | } |
309 | #endif |
433 | #endif |
310 | |
434 | |
|
|
435 | /* sendfile always needs emulation */ |
|
|
436 | static ssize_t |
|
|
437 | sendfile_ (int ofd, int ifd, off_t offset, size_t count) |
|
|
438 | { |
|
|
439 | ssize_t res; |
|
|
440 | |
|
|
441 | if (!count) |
|
|
442 | return 0; |
|
|
443 | |
|
|
444 | #if __linux |
|
|
445 | res = sendfile (ofd, ifd, &offset, count); |
|
|
446 | |
|
|
447 | #elif __freebsd |
|
|
448 | /* |
|
|
449 | * Of course, the freebsd sendfile is a dire hack with no thoughts |
|
|
450 | * wasted on making it similar to other i/o functions. |
|
|
451 | */ |
|
|
452 | { |
|
|
453 | off_t sbytes; |
|
|
454 | res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); |
|
|
455 | |
|
|
456 | if (!res && errno == EAGAIN) |
|
|
457 | /* maybe on others, too, as usual, the manpage leaves you guessing */ |
|
|
458 | res = sbytes; |
|
|
459 | } |
|
|
460 | |
|
|
461 | #elif __hpux |
|
|
462 | res = sendfile (ofd, ifd, offset, count, 0, 0); |
|
|
463 | |
|
|
464 | #else |
|
|
465 | res = -1; |
|
|
466 | errno = ENOSYS; |
|
|
467 | #endif |
|
|
468 | |
|
|
469 | if (res < 0 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK)) |
|
|
470 | { |
|
|
471 | /* emulate sendfile. this is a major pain in the ass */ |
|
|
472 | char *buf = malloc (4096); |
|
|
473 | res = 0; |
|
|
474 | |
|
|
475 | for (;;) |
|
|
476 | { |
|
|
477 | ssize_t cnt; |
|
|
478 | |
|
|
479 | cnt = pread (ifd, buf, 4096, offset); |
|
|
480 | |
|
|
481 | if (cnt <= 0) |
|
|
482 | { |
|
|
483 | if (cnt && !res) res = -1; |
|
|
484 | break; |
|
|
485 | } |
|
|
486 | |
|
|
487 | cnt = write (ofd, buf, cnt); |
|
|
488 | |
|
|
489 | if (cnt <= 0) |
|
|
490 | { |
|
|
491 | if (cnt && !res) res = -1; |
|
|
492 | break; |
|
|
493 | } |
|
|
494 | |
|
|
495 | offset += cnt; |
|
|
496 | res += cnt; |
|
|
497 | } |
|
|
498 | |
|
|
499 | { |
|
|
500 | int errorno = errno; |
|
|
501 | free (buf); |
|
|
502 | errno = errorno; |
|
|
503 | } |
|
|
504 | } |
|
|
505 | |
|
|
506 | return res; |
|
|
507 | } |
|
|
508 | |
|
|
509 | /*****************************************************************************/ |
|
|
510 | |
311 | static void * |
511 | static void * |
312 | aio_proc (void *thr_arg) |
512 | aio_proc (void *thr_arg) |
313 | { |
513 | { |
314 | aio_req req; |
514 | aio_req req; |
315 | int type; |
515 | int type; |
… | |
… | |
344 | { |
544 | { |
345 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
545 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
346 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
546 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
347 | |
547 | |
348 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
548 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
|
|
549 | case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length); break; |
349 | |
550 | |
350 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
551 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
351 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
552 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
352 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
553 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
353 | |
554 | |
354 | case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
555 | case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
355 | case REQ_CLOSE: req->result = close (req->fd); break; |
556 | case REQ_CLOSE: req->result = close (req->fd); break; |
356 | case REQ_UNLINK: req->result = unlink (req->dataptr); break; |
557 | case REQ_UNLINK: req->result = unlink (req->dataptr); break; |
|
|
558 | case REQ_RMDIR: req->result = rmdir (req->dataptr); break; |
|
|
559 | case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; |
357 | |
560 | |
358 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
561 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
359 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
562 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
360 | |
563 | |
361 | case REQ_QUIT: |
564 | case REQ_QUIT: |
… | |
… | |
390 | while (type != REQ_QUIT); |
593 | while (type != REQ_QUIT); |
391 | |
594 | |
392 | return 0; |
595 | return 0; |
393 | } |
596 | } |
394 | |
597 | |
|
|
598 | #define dREQ \ |
|
|
599 | aio_req req; \ |
|
|
600 | \ |
|
|
601 | if (SvOK (callback) && !SvROK (callback)) \ |
|
|
602 | croak ("clalback must be undef or of reference type"); \ |
|
|
603 | \ |
|
|
604 | Newz (0, req, 1, aio_cb); \ |
|
|
605 | if (!req) \ |
|
|
606 | croak ("out of memory during aio_req allocation"); \ |
|
|
607 | \ |
|
|
608 | req->callback = newSVsv (callback); |
|
|
609 | |
395 | MODULE = IO::AIO PACKAGE = IO::AIO |
610 | MODULE = IO::AIO PACKAGE = IO::AIO |
396 | |
611 | |
397 | PROTOTYPES: ENABLE |
612 | PROTOTYPES: ENABLE |
398 | |
613 | |
399 | BOOT: |
614 | BOOT: |
400 | { |
615 | { |
401 | if (pipe (respipe)) |
616 | create_pipe (); |
402 | croak ("unable to initialize result pipe"); |
617 | pthread_atfork (atfork_prepare, atfork_parent, atfork_child); |
403 | |
|
|
404 | if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)) |
|
|
405 | croak ("cannot set result pipe to nonblocking mode"); |
|
|
406 | |
|
|
407 | if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) |
|
|
408 | croak ("cannot set result pipe to nonblocking mode"); |
|
|
409 | } |
618 | } |
410 | |
619 | |
411 | void |
620 | void |
412 | min_parallel(nthreads) |
621 | min_parallel(nthreads) |
413 | int nthreads |
622 | int nthreads |
414 | PROTOTYPE: $ |
623 | PROTOTYPE: $ |
415 | CODE: |
|
|
416 | while (nthreads > started) |
|
|
417 | start_thread (); |
|
|
418 | |
624 | |
419 | void |
625 | void |
420 | max_parallel(nthreads) |
626 | max_parallel(nthreads) |
421 | int nthreads |
627 | int nthreads |
422 | PROTOTYPE: $ |
628 | PROTOTYPE: $ |
423 | CODE: |
|
|
424 | { |
|
|
425 | int cur = started; |
|
|
426 | while (cur > nthreads) |
|
|
427 | { |
|
|
428 | end_thread (); |
|
|
429 | cur--; |
|
|
430 | } |
|
|
431 | |
|
|
432 | while (started > nthreads) |
|
|
433 | { |
|
|
434 | poll_wait (); |
|
|
435 | poll_cb (); |
|
|
436 | } |
|
|
437 | } |
|
|
438 | |
629 | |
439 | int |
630 | int |
440 | max_outstanding(nreqs) |
631 | max_outstanding(nreqs) |
441 | int nreqs |
632 | int nreqs |
442 | PROTOTYPE: $ |
633 | PROTOTYPE: $ |
… | |
… | |
451 | int mode |
642 | int mode |
452 | SV * callback |
643 | SV * callback |
453 | PROTOTYPE: $$$;$ |
644 | PROTOTYPE: $$$;$ |
454 | CODE: |
645 | CODE: |
455 | { |
646 | { |
456 | aio_req req; |
647 | dREQ; |
457 | |
|
|
458 | Newz (0, req, 1, aio_cb); |
|
|
459 | |
|
|
460 | if (!req) |
|
|
461 | croak ("out of memory during aio_req allocation"); |
|
|
462 | |
648 | |
463 | req->type = REQ_OPEN; |
649 | req->type = REQ_OPEN; |
464 | req->data = newSVsv (pathname); |
650 | req->data = newSVsv (pathname); |
465 | req->dataptr = SvPV_nolen (req->data); |
651 | req->dataptr = SvPVbyte_nolen (req->data); |
466 | req->fd = flags; |
652 | req->fd = flags; |
467 | req->mode = mode; |
653 | req->mode = mode; |
468 | req->callback = SvREFCNT_inc (callback); |
|
|
469 | |
654 | |
470 | send_req (req); |
655 | send_req (req); |
471 | } |
656 | } |
472 | |
657 | |
473 | void |
658 | void |
… | |
… | |
479 | aio_close = REQ_CLOSE |
664 | aio_close = REQ_CLOSE |
480 | aio_fsync = REQ_FSYNC |
665 | aio_fsync = REQ_FSYNC |
481 | aio_fdatasync = REQ_FDATASYNC |
666 | aio_fdatasync = REQ_FDATASYNC |
482 | CODE: |
667 | CODE: |
483 | { |
668 | { |
484 | aio_req req; |
669 | dREQ; |
485 | |
|
|
486 | Newz (0, req, 1, aio_cb); |
|
|
487 | |
|
|
488 | if (!req) |
|
|
489 | croak ("out of memory during aio_req allocation"); |
|
|
490 | |
670 | |
491 | req->type = ix; |
671 | req->type = ix; |
492 | req->fh = newSVsv (fh); |
672 | req->fh = newSVsv (fh); |
493 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
673 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
494 | req->callback = SvREFCNT_inc (callback); |
|
|
495 | |
674 | |
496 | send_req (req); |
675 | send_req (req); |
497 | } |
676 | } |
498 | |
677 | |
499 | void |
678 | void |
500 | aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
679 | aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
501 | SV * fh |
680 | SV * fh |
502 | UV offset |
681 | UV offset |
503 | IV length |
682 | UV length |
504 | SV * data |
683 | SV * data |
505 | IV dataoffset |
684 | UV dataoffset |
506 | SV * callback |
685 | SV * callback |
507 | ALIAS: |
686 | ALIAS: |
508 | aio_read = REQ_READ |
687 | aio_read = REQ_READ |
509 | aio_write = REQ_WRITE |
688 | aio_write = REQ_WRITE |
510 | PROTOTYPE: $$$$$;$ |
689 | PROTOTYPE: $$$$$;$ |
511 | CODE: |
690 | CODE: |
512 | { |
691 | { |
513 | aio_req req; |
692 | aio_req req; |
514 | STRLEN svlen; |
693 | STRLEN svlen; |
515 | char *svptr = SvPV (data, svlen); |
694 | char *svptr = SvPVbyte (data, svlen); |
516 | |
695 | |
517 | SvUPGRADE (data, SVt_PV); |
696 | SvUPGRADE (data, SVt_PV); |
518 | SvPOK_on (data); |
697 | SvPOK_on (data); |
519 | |
698 | |
520 | if (dataoffset < 0) |
699 | if (dataoffset < 0) |
… | |
… | |
536 | } |
715 | } |
537 | |
716 | |
538 | if (length < 0) |
717 | if (length < 0) |
539 | croak ("length must not be negative"); |
718 | croak ("length must not be negative"); |
540 | |
719 | |
541 | Newz (0, req, 1, aio_cb); |
720 | { |
|
|
721 | dREQ; |
542 | |
722 | |
543 | if (!req) |
|
|
544 | croak ("out of memory during aio_req allocation"); |
|
|
545 | |
|
|
546 | req->type = ix; |
723 | req->type = ix; |
547 | req->fh = newSVsv (fh); |
724 | req->fh = newSVsv (fh); |
548 | req->fd = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh)) |
725 | req->fd = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh)) |
549 | : IoOFP (sv_2io (fh))); |
726 | : IoOFP (sv_2io (fh))); |
550 | req->offset = offset; |
727 | req->offset = offset; |
|
|
728 | req->length = length; |
|
|
729 | req->data = SvREFCNT_inc (data); |
|
|
730 | req->dataptr = (char *)svptr + dataoffset; |
|
|
731 | |
|
|
732 | if (!SvREADONLY (data)) |
|
|
733 | { |
|
|
734 | SvREADONLY_on (data); |
|
|
735 | req->data2ptr = (void *)data; |
|
|
736 | } |
|
|
737 | |
|
|
738 | send_req (req); |
|
|
739 | } |
|
|
740 | } |
|
|
741 | |
|
|
742 | void |
|
|
743 | aio_sendfile(out_fh,in_fh,in_offset,length,callback=&PL_sv_undef) |
|
|
744 | SV * out_fh |
|
|
745 | SV * in_fh |
|
|
746 | UV in_offset |
|
|
747 | UV length |
|
|
748 | SV * callback |
|
|
749 | PROTOTYPE: $$$$;$ |
|
|
750 | CODE: |
|
|
751 | { |
|
|
752 | dREQ; |
|
|
753 | |
|
|
754 | req->type = REQ_SENDFILE; |
|
|
755 | req->fh = newSVsv (out_fh); |
|
|
756 | req->fd = PerlIO_fileno (IoIFP (sv_2io (out_fh))); |
|
|
757 | req->fh2 = newSVsv (in_fh); |
|
|
758 | req->fd2 = PerlIO_fileno (IoIFP (sv_2io (in_fh))); |
|
|
759 | req->offset = in_offset; |
551 | req->length = length; |
760 | req->length = length; |
552 | req->data = SvREFCNT_inc (data); |
|
|
553 | req->dataptr = (char *)svptr + dataoffset; |
|
|
554 | req->callback = SvREFCNT_inc (callback); |
|
|
555 | |
761 | |
556 | send_req (req); |
762 | send_req (req); |
557 | } |
763 | } |
558 | |
764 | |
559 | void |
765 | void |
… | |
… | |
563 | IV length |
769 | IV length |
564 | SV * callback |
770 | SV * callback |
565 | PROTOTYPE: $$$;$ |
771 | PROTOTYPE: $$$;$ |
566 | CODE: |
772 | CODE: |
567 | { |
773 | { |
568 | aio_req req; |
774 | dREQ; |
569 | |
|
|
570 | if (length < 0) |
|
|
571 | croak ("length must not be negative"); |
|
|
572 | |
|
|
573 | Newz (0, req, 1, aio_cb); |
|
|
574 | |
|
|
575 | if (!req) |
|
|
576 | croak ("out of memory during aio_req allocation"); |
|
|
577 | |
775 | |
578 | req->type = REQ_READAHEAD; |
776 | req->type = REQ_READAHEAD; |
579 | req->fh = newSVsv (fh); |
777 | req->fh = newSVsv (fh); |
580 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
778 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
581 | req->offset = offset; |
779 | req->offset = offset; |
582 | req->length = length; |
780 | req->length = length; |
583 | req->callback = SvREFCNT_inc (callback); |
|
|
584 | |
781 | |
585 | send_req (req); |
782 | send_req (req); |
586 | } |
783 | } |
587 | |
784 | |
588 | void |
785 | void |
… | |
… | |
592 | ALIAS: |
789 | ALIAS: |
593 | aio_stat = REQ_STAT |
790 | aio_stat = REQ_STAT |
594 | aio_lstat = REQ_LSTAT |
791 | aio_lstat = REQ_LSTAT |
595 | CODE: |
792 | CODE: |
596 | { |
793 | { |
597 | aio_req req; |
794 | dREQ; |
598 | |
|
|
599 | Newz (0, req, 1, aio_cb); |
|
|
600 | |
|
|
601 | if (!req) |
|
|
602 | croak ("out of memory during aio_req allocation"); |
|
|
603 | |
795 | |
604 | New (0, req->statdata, 1, Stat_t); |
796 | New (0, req->statdata, 1, Stat_t); |
605 | |
|
|
606 | if (!req->statdata) |
797 | if (!req->statdata) |
|
|
798 | { |
|
|
799 | free_req (req); |
607 | croak ("out of memory during aio_req->statdata allocation"); |
800 | croak ("out of memory during aio_req->statdata allocation"); |
|
|
801 | } |
608 | |
802 | |
609 | if (SvPOK (fh_or_path)) |
803 | if (SvPOK (fh_or_path)) |
610 | { |
804 | { |
611 | req->type = ix; |
805 | req->type = ix; |
612 | req->data = newSVsv (fh_or_path); |
806 | req->data = newSVsv (fh_or_path); |
613 | req->dataptr = SvPV_nolen (req->data); |
807 | req->dataptr = SvPVbyte_nolen (req->data); |
614 | } |
808 | } |
615 | else |
809 | else |
616 | { |
810 | { |
617 | req->type = REQ_FSTAT; |
811 | req->type = REQ_FSTAT; |
618 | req->fh = newSVsv (fh_or_path); |
812 | req->fh = newSVsv (fh_or_path); |
619 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
813 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
620 | } |
814 | } |
621 | |
815 | |
622 | req->callback = SvREFCNT_inc (callback); |
|
|
623 | |
|
|
624 | send_req (req); |
816 | send_req (req); |
625 | } |
817 | } |
626 | |
818 | |
627 | void |
819 | void |
628 | aio_unlink(pathname,callback=&PL_sv_undef) |
820 | aio_unlink(pathname,callback=&PL_sv_undef) |
629 | SV * pathname |
821 | SV * pathname |
630 | SV * callback |
822 | SV * callback |
|
|
823 | ALIAS: |
|
|
824 | aio_unlink = REQ_UNLINK |
|
|
825 | aio_rmdir = REQ_RMDIR |
631 | CODE: |
826 | CODE: |
632 | { |
827 | { |
633 | aio_req req; |
828 | dREQ; |
634 | |
829 | |
635 | Newz (0, req, 1, aio_cb); |
830 | req->type = ix; |
|
|
831 | req->data = newSVsv (pathname); |
|
|
832 | req->dataptr = SvPVbyte_nolen (req->data); |
636 | |
833 | |
637 | if (!req) |
834 | send_req (req); |
638 | croak ("out of memory during aio_req allocation"); |
835 | } |
|
|
836 | |
|
|
837 | void |
|
|
838 | aio_symlink(oldpath,newpath,callback=&PL_sv_undef) |
|
|
839 | SV * oldpath |
|
|
840 | SV * newpath |
|
|
841 | SV * callback |
|
|
842 | CODE: |
|
|
843 | { |
|
|
844 | dREQ; |
639 | |
845 | |
640 | req->type = REQ_UNLINK; |
846 | req->type = REQ_SYMLINK; |
|
|
847 | req->fh = newSVsv (oldpath); |
|
|
848 | req->data2ptr = SvPVbyte_nolen (req->fh); |
641 | req->data = newSVsv (pathname); |
849 | req->data = newSVsv (newpath); |
642 | req->dataptr = SvPV_nolen (req->data); |
850 | req->dataptr = SvPVbyte_nolen (req->data); |
643 | req->callback = SvREFCNT_inc (callback); |
|
|
644 | |
851 | |
645 | send_req (req); |
852 | send_req (req); |
646 | } |
853 | } |
647 | |
854 | |
648 | void |
855 | void |