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