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