… | |
… | |
4 | #include "EXTERN.h" |
4 | #include "EXTERN.h" |
5 | #include "perl.h" |
5 | #include "perl.h" |
6 | #include "XSUB.h" |
6 | #include "XSUB.h" |
7 | |
7 | |
8 | #include "autoconf/config.h" |
8 | #include "autoconf/config.h" |
|
|
9 | |
|
|
10 | #include <pthread.h> |
9 | |
11 | |
10 | #include <sys/types.h> |
12 | #include <sys/types.h> |
11 | #include <sys/stat.h> |
13 | #include <sys/stat.h> |
12 | |
14 | |
13 | #include <unistd.h> |
15 | #include <unistd.h> |
14 | #include <fcntl.h> |
16 | #include <fcntl.h> |
15 | #include <signal.h> |
17 | #include <signal.h> |
16 | #include <sched.h> |
18 | #include <sched.h> |
17 | |
19 | |
18 | #include <pthread.h> |
20 | #if HAVE_SENDFILE |
19 | |
21 | # if __linux |
20 | typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
22 | # include <sys/sendfile.h> |
21 | typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
23 | # elif __freebsd |
22 | 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 | # endif |
|
|
29 | #endif |
23 | |
30 | |
24 | #if __ia64 |
31 | #if __ia64 |
25 | # define STACKSIZE 65536 |
32 | # define STACKSIZE 65536 |
26 | #else |
33 | #else |
27 | # define STACKSIZE 4096 |
34 | # define STACKSIZE 4096 |
… | |
… | |
29 | |
36 | |
30 | enum { |
37 | enum { |
31 | REQ_QUIT, |
38 | REQ_QUIT, |
32 | REQ_OPEN, REQ_CLOSE, |
39 | REQ_OPEN, REQ_CLOSE, |
33 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
40 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
|
|
41 | REQ_SENDFILE, |
34 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
42 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
35 | REQ_FSYNC, REQ_FDATASYNC, |
43 | REQ_FSYNC, REQ_FDATASYNC, |
36 | REQ_UNLINK, REQ_RMDIR, |
44 | REQ_UNLINK, REQ_RMDIR, |
37 | REQ_SYMLINK, |
45 | REQ_SYMLINK, |
38 | }; |
46 | }; |
… | |
… | |
40 | typedef struct aio_cb { |
48 | typedef struct aio_cb { |
41 | struct aio_cb *volatile next; |
49 | struct aio_cb *volatile next; |
42 | |
50 | |
43 | int type; |
51 | int type; |
44 | |
52 | |
45 | int fd; |
53 | int fd, fd2; |
46 | off_t offset; |
54 | off_t offset; |
47 | size_t length; |
55 | size_t length; |
48 | ssize_t result; |
56 | ssize_t result; |
49 | mode_t mode; /* open */ |
57 | mode_t mode; /* open */ |
50 | int errorno; |
58 | int errorno; |
51 | SV *data, *callback, *fh; |
59 | SV *data, *callback; |
|
|
60 | SV *fh, *fh2; |
52 | void *dataptr, *data2ptr; |
61 | void *dataptr, *data2ptr; |
53 | STRLEN dataoffset; |
62 | STRLEN dataoffset; |
54 | |
63 | |
55 | Stat_t *statdata; |
64 | Stat_t *statdata; |
56 | } aio_cb; |
65 | } aio_cb; |
57 | |
66 | |
58 | typedef aio_cb *aio_req; |
67 | typedef aio_cb *aio_req; |
59 | |
68 | |
60 | static int started; |
69 | static int started, wanted; |
61 | static volatile int nreqs; |
70 | static volatile int nreqs; |
62 | static int max_outstanding = 1<<30; |
71 | static int max_outstanding = 1<<30; |
63 | static int respipe [2]; |
72 | static int respipe [2]; |
64 | |
73 | |
65 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
74 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
66 | static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; |
75 | static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; |
67 | static pthread_mutex_t frklock = PTHREAD_MUTEX_INITIALIZER; |
|
|
68 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
76 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
69 | |
77 | |
70 | static volatile aio_req reqs, reqe; /* queue start, queue end */ |
78 | static volatile aio_req reqs, reqe; /* queue start, queue end */ |
71 | 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 | } |
72 | |
100 | |
73 | static void |
101 | static void |
74 | poll_wait () |
102 | poll_wait () |
75 | { |
103 | { |
76 | if (nreqs && !ress) |
104 | if (nreqs && !ress) |
… | |
… | |
87 | poll_cb () |
115 | poll_cb () |
88 | { |
116 | { |
89 | dSP; |
117 | dSP; |
90 | int count = 0; |
118 | int count = 0; |
91 | int do_croak = 0; |
119 | int do_croak = 0; |
92 | aio_req req, prv; |
120 | aio_req req; |
93 | |
121 | |
94 | pthread_mutex_lock (&reslock); |
122 | for (;;) |
95 | |
|
|
96 | { |
|
|
97 | /* read any signals sent by the worker threads */ |
|
|
98 | char buf [32]; |
|
|
99 | while (read (respipe [0], buf, 32) == 32) |
|
|
100 | ; |
|
|
101 | } |
|
|
102 | |
|
|
103 | req = ress; |
|
|
104 | ress = rese = 0; |
|
|
105 | |
|
|
106 | pthread_mutex_unlock (&reslock); |
|
|
107 | |
|
|
108 | while (req) |
|
|
109 | { |
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 | |
110 | nreqs--; |
147 | nreqs--; |
111 | |
148 | |
112 | if (req->type == REQ_QUIT) |
149 | if (req->type == REQ_QUIT) |
113 | started--; |
150 | started--; |
114 | else |
151 | else |
115 | { |
152 | { |
116 | int errorno = errno; |
153 | int errorno = errno; |
117 | errno = req->errorno; |
154 | errno = req->errorno; |
118 | |
155 | |
119 | if (req->type == REQ_READ) |
156 | if (req->type == REQ_READ) |
120 | SvCUR_set (req->data, req->dataoffset |
157 | SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); |
121 | + req->result > 0 ? req->result : 0); |
|
|
122 | |
158 | |
|
|
159 | if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE)) |
|
|
160 | SvREADONLY_off (req->data); |
|
|
161 | |
123 | if (req->data) |
162 | if (req->statdata) |
124 | SvREFCNT_dec (req->data); |
|
|
125 | |
|
|
126 | if (req->fh) |
|
|
127 | SvREFCNT_dec (req->fh); |
|
|
128 | |
|
|
129 | if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) |
|
|
130 | { |
163 | { |
131 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
164 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
132 | PL_laststatval = req->result; |
165 | PL_laststatval = req->result; |
133 | PL_statcache = *(req->statdata); |
166 | PL_statcache = *(req->statdata); |
134 | |
|
|
135 | Safefree (req->statdata); |
|
|
136 | } |
167 | } |
137 | |
168 | |
138 | ENTER; |
169 | ENTER; |
139 | PUSHMARK (SP); |
170 | PUSHMARK (SP); |
140 | XPUSHs (sv_2mortal (newSViv (req->result))); |
171 | XPUSHs (sv_2mortal (newSViv (req->result))); |
… | |
… | |
157 | if (SvOK (req->callback)) |
188 | if (SvOK (req->callback)) |
158 | { |
189 | { |
159 | PUTBACK; |
190 | PUTBACK; |
160 | call_sv (req->callback, G_VOID | G_EVAL); |
191 | call_sv (req->callback, G_VOID | G_EVAL); |
161 | SPAGAIN; |
192 | SPAGAIN; |
|
|
193 | |
|
|
194 | if (SvTRUE (ERRSV)) |
|
|
195 | { |
|
|
196 | free_req (req); |
|
|
197 | croak (0); |
|
|
198 | } |
162 | } |
199 | } |
163 | |
200 | |
164 | do_croak = SvTRUE (ERRSV); |
|
|
165 | |
|
|
166 | LEAVE; |
201 | LEAVE; |
167 | |
|
|
168 | if (req->callback) |
|
|
169 | SvREFCNT_dec (req->callback); |
|
|
170 | |
202 | |
171 | errno = errorno; |
203 | errno = errorno; |
172 | count++; |
204 | count++; |
173 | } |
205 | } |
174 | |
206 | |
175 | prv = req; |
207 | free_req (req); |
176 | req = req->next; |
|
|
177 | Safefree (prv); |
|
|
178 | |
|
|
179 | if (do_croak) |
|
|
180 | croak (0); |
|
|
181 | } |
208 | } |
182 | |
209 | |
183 | return count; |
210 | return count; |
184 | } |
211 | } |
185 | |
212 | |
… | |
… | |
206 | } |
233 | } |
207 | |
234 | |
208 | static void |
235 | static void |
209 | send_req (aio_req req) |
236 | send_req (aio_req req) |
210 | { |
237 | { |
|
|
238 | while (started < wanted && nreqs >= started) |
|
|
239 | start_thread (); |
|
|
240 | |
211 | nreqs++; |
241 | nreqs++; |
212 | |
242 | |
213 | pthread_mutex_lock (&reqlock); |
243 | pthread_mutex_lock (&reqlock); |
214 | |
244 | |
215 | req->next = 0; |
245 | req->next = 0; |
… | |
… | |
223 | reqe = reqs = req; |
253 | reqe = reqs = req; |
224 | |
254 | |
225 | pthread_cond_signal (&reqwait); |
255 | pthread_cond_signal (&reqwait); |
226 | pthread_mutex_unlock (&reqlock); |
256 | pthread_mutex_unlock (&reqlock); |
227 | |
257 | |
228 | 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) |
229 | { |
300 | { |
230 | poll_wait (); |
301 | poll_wait (); |
231 | poll_cb (); |
302 | poll_cb (); |
232 | } |
303 | } |
233 | } |
304 | } |
234 | |
305 | |
235 | static void |
306 | static void create_pipe () |
236 | end_thread (void) |
|
|
237 | { |
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 | { |
238 | aio_req req; |
332 | aio_req prv; |
239 | New (0, req, 1, aio_cb); |
|
|
240 | req->type = REQ_QUIT; |
|
|
241 | |
333 | |
242 | send_req (req); |
334 | started = 0; |
243 | } |
|
|
244 | |
335 | |
245 | |
336 | while (reqs) |
246 | static void min_parallel (int nthreads) |
337 | { |
247 | { |
338 | prv = reqs; |
248 | while (nthreads > started) |
339 | reqs = prv->next; |
249 | start_thread (); |
340 | free_req (prv); |
250 | } |
|
|
251 | |
|
|
252 | static void max_parallel (int nthreads) |
|
|
253 | { |
|
|
254 | int cur = started; |
|
|
255 | while (cur > nthreads) |
|
|
256 | { |
|
|
257 | end_thread (); |
|
|
258 | cur--; |
|
|
259 | } |
341 | } |
260 | |
342 | |
261 | while (started > nthreads) |
343 | reqs = reqe = 0; |
|
|
344 | |
|
|
345 | while (ress) |
262 | { |
346 | { |
263 | poll_wait (); |
347 | prv = ress; |
264 | poll_cb (); |
348 | ress = prv->next; |
|
|
349 | free_req (prv); |
265 | } |
350 | } |
266 | } |
|
|
267 | |
|
|
268 | static int fork_started; |
|
|
269 | |
|
|
270 | static void atfork_prepare (void) |
|
|
271 | { |
|
|
272 | int nstarted; |
|
|
273 | |
|
|
274 | for (;;) { |
|
|
275 | while (nreqs) |
|
|
276 | { |
351 | |
277 | poll_wait (); |
352 | ress = rese = 0; |
278 | poll_cb (); |
|
|
279 | } |
|
|
280 | |
353 | |
281 | nstarted = started; |
354 | close (respipe [0]); |
282 | max_parallel (0); |
355 | close (respipe [1]); |
283 | |
356 | create_pipe (); |
284 | pthread_mutex_lock (&reqlock); |
|
|
285 | |
357 | |
286 | if (!nreqs && !started) |
358 | atfork_parent (); |
287 | break; |
|
|
288 | |
|
|
289 | pthread_mutex_unlock (&reqlock); |
|
|
290 | |
|
|
291 | min_parallel (fork_started); |
|
|
292 | } |
|
|
293 | |
|
|
294 | pthread_mutex_lock (&frklock); |
|
|
295 | fork_started = nstarted; |
|
|
296 | pthread_mutex_lock (&reslock); |
|
|
297 | |
|
|
298 | assert (!started); |
|
|
299 | assert (!nreqs); |
|
|
300 | assert (!reqs && !reqe); |
|
|
301 | assert (!ress && !rese); |
|
|
302 | } |
359 | } |
303 | |
|
|
304 | static void atfork_parent (void) |
|
|
305 | { |
|
|
306 | pthread_mutex_unlock (&reslock); |
|
|
307 | pthread_mutex_unlock (&frklock); |
|
|
308 | pthread_mutex_unlock (&reqlock); |
|
|
309 | |
|
|
310 | min_parallel (fork_started); |
|
|
311 | } |
|
|
312 | |
|
|
313 | #define atfork_child atfork_parent |
|
|
314 | |
360 | |
315 | /*****************************************************************************/ |
361 | /*****************************************************************************/ |
316 | /* work around various missing functions */ |
362 | /* work around various missing functions */ |
317 | |
363 | |
318 | #if !HAVE_PREADWRITE |
364 | #if !HAVE_PREADWRITE |
… | |
… | |
382 | |
428 | |
383 | errno = 0; |
429 | errno = 0; |
384 | } |
430 | } |
385 | #endif |
431 | #endif |
386 | |
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 | |
387 | /*****************************************************************************/ |
507 | /*****************************************************************************/ |
388 | |
508 | |
389 | static void * |
509 | static void * |
390 | aio_proc (void *thr_arg) |
510 | aio_proc (void *thr_arg) |
391 | { |
511 | { |
… | |
… | |
422 | { |
542 | { |
423 | 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; |
424 | 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; |
425 | |
545 | |
426 | 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; |
|
|
547 | case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length); break; |
427 | |
548 | |
428 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
549 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
429 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
550 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
430 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
551 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
431 | |
552 | |
… | |
… | |
480 | \ |
601 | \ |
481 | Newz (0, req, 1, aio_cb); \ |
602 | Newz (0, req, 1, aio_cb); \ |
482 | if (!req) \ |
603 | if (!req) \ |
483 | croak ("out of memory during aio_req allocation"); \ |
604 | croak ("out of memory during aio_req allocation"); \ |
484 | \ |
605 | \ |
485 | req->callback = SvREFCNT_inc (callback); |
606 | req->callback = newSVsv (callback); |
486 | |
607 | |
487 | MODULE = IO::AIO PACKAGE = IO::AIO |
608 | MODULE = IO::AIO PACKAGE = IO::AIO |
488 | |
609 | |
489 | PROTOTYPES: ENABLE |
610 | PROTOTYPES: ENABLE |
490 | |
611 | |
491 | BOOT: |
612 | BOOT: |
492 | { |
613 | { |
493 | if (pipe (respipe)) |
614 | create_pipe (); |
494 | croak ("unable to initialize result pipe"); |
|
|
495 | |
|
|
496 | if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)) |
|
|
497 | croak ("cannot set result pipe to nonblocking mode"); |
|
|
498 | |
|
|
499 | if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) |
|
|
500 | croak ("cannot set result pipe to nonblocking mode"); |
|
|
501 | |
|
|
502 | pthread_atfork (atfork_prepare, atfork_parent, atfork_child); |
615 | pthread_atfork (atfork_prepare, atfork_parent, atfork_child); |
503 | } |
616 | } |
504 | |
617 | |
505 | void |
618 | void |
506 | min_parallel(nthreads) |
619 | min_parallel(nthreads) |
… | |
… | |
562 | |
675 | |
563 | void |
676 | void |
564 | aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
677 | aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
565 | SV * fh |
678 | SV * fh |
566 | UV offset |
679 | UV offset |
567 | IV length |
680 | UV length |
568 | SV * data |
681 | SV * data |
569 | IV dataoffset |
682 | UV dataoffset |
570 | SV * callback |
683 | SV * callback |
571 | ALIAS: |
684 | ALIAS: |
572 | aio_read = REQ_READ |
685 | aio_read = REQ_READ |
573 | aio_write = REQ_WRITE |
686 | aio_write = REQ_WRITE |
574 | PROTOTYPE: $$$$$;$ |
687 | PROTOTYPE: $$$$$;$ |
… | |
… | |
611 | : IoOFP (sv_2io (fh))); |
724 | : IoOFP (sv_2io (fh))); |
612 | req->offset = offset; |
725 | req->offset = offset; |
613 | req->length = length; |
726 | req->length = length; |
614 | req->data = SvREFCNT_inc (data); |
727 | req->data = SvREFCNT_inc (data); |
615 | req->dataptr = (char *)svptr + dataoffset; |
728 | req->dataptr = (char *)svptr + dataoffset; |
616 | req->callback = SvREFCNT_inc (callback); |
729 | |
|
|
730 | if (!SvREADONLY (data)) |
|
|
731 | { |
|
|
732 | SvREADONLY_on (data); |
|
|
733 | req->data2ptr = (void *)data; |
|
|
734 | } |
617 | |
735 | |
618 | send_req (req); |
736 | send_req (req); |
619 | } |
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; |
|
|
758 | req->length = length; |
|
|
759 | |
|
|
760 | send_req (req); |
620 | } |
761 | } |
621 | |
762 | |
622 | void |
763 | void |
623 | aio_readahead(fh,offset,length,callback=&PL_sv_undef) |
764 | aio_readahead(fh,offset,length,callback=&PL_sv_undef) |
624 | SV * fh |
765 | SV * fh |
… | |
… | |
650 | { |
791 | { |
651 | dREQ; |
792 | dREQ; |
652 | |
793 | |
653 | New (0, req->statdata, 1, Stat_t); |
794 | New (0, req->statdata, 1, Stat_t); |
654 | if (!req->statdata) |
795 | if (!req->statdata) |
|
|
796 | { |
|
|
797 | free_req (req); |
655 | croak ("out of memory during aio_req->statdata allocation (sorry, i just leaked memory, too)"); |
798 | croak ("out of memory during aio_req->statdata allocation"); |
|
|
799 | } |
656 | |
800 | |
657 | if (SvPOK (fh_or_path)) |
801 | if (SvPOK (fh_or_path)) |
658 | { |
802 | { |
659 | req->type = ix; |
803 | req->type = ix; |
660 | req->data = newSVsv (fh_or_path); |
804 | req->data = newSVsv (fh_or_path); |