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