|
|
1 | #define _REENTRANT 1 |
|
|
2 | #include <errno.h> |
|
|
3 | |
1 | #include "EXTERN.h" |
4 | #include "EXTERN.h" |
2 | #include "perl.h" |
5 | #include "perl.h" |
3 | #include "XSUB.h" |
6 | #include "XSUB.h" |
4 | |
7 | |
|
|
8 | #include "autoconf/config.h" |
|
|
9 | |
5 | #include <sys/types.h> |
10 | #include <sys/types.h> |
6 | #include <sys/stat.h> |
11 | #include <sys/stat.h> |
|
|
12 | |
7 | #include <unistd.h> |
13 | #include <unistd.h> |
8 | #include <fcntl.h> |
14 | #include <fcntl.h> |
9 | #include <signal.h> |
15 | #include <signal.h> |
10 | #include <sched.h> |
16 | #include <sched.h> |
11 | |
17 | |
12 | #include <pthread.h> |
18 | #include <pthread.h> |
13 | #include <sys/syscall.h> |
|
|
14 | |
19 | |
15 | typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
20 | typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
16 | typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
21 | typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
17 | typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
22 | typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
18 | |
23 | |
… | |
… | |
24 | |
29 | |
25 | enum { |
30 | enum { |
26 | REQ_QUIT, |
31 | REQ_QUIT, |
27 | REQ_OPEN, REQ_CLOSE, |
32 | REQ_OPEN, REQ_CLOSE, |
28 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
33 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
29 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_UNLINK, |
34 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
30 | REQ_FSYNC, REQ_FDATASYNC, |
35 | REQ_FSYNC, REQ_FDATASYNC, |
|
|
36 | REQ_UNLINK, REQ_RMDIR, |
|
|
37 | REQ_SYMLINK, |
31 | }; |
38 | }; |
32 | |
39 | |
33 | typedef struct aio_cb { |
40 | typedef struct aio_cb { |
34 | struct aio_cb *volatile next; |
41 | struct aio_cb *volatile next; |
35 | |
42 | |
… | |
… | |
39 | off_t offset; |
46 | off_t offset; |
40 | size_t length; |
47 | size_t length; |
41 | ssize_t result; |
48 | ssize_t result; |
42 | mode_t mode; /* open */ |
49 | mode_t mode; /* open */ |
43 | int errorno; |
50 | int errorno; |
44 | SV *data, *callback; |
51 | SV *data, *callback, *fh; |
45 | void *dataptr; |
52 | void *dataptr, *data2ptr; |
46 | STRLEN dataoffset; |
53 | STRLEN dataoffset; |
47 | |
54 | |
48 | Stat_t *statdata; |
55 | Stat_t *statdata; |
49 | } aio_cb; |
56 | } aio_cb; |
50 | |
57 | |
51 | typedef aio_cb *aio_req; |
58 | typedef aio_cb *aio_req; |
52 | |
59 | |
53 | static int started; |
60 | static int started; |
54 | static int nreqs; |
61 | static volatile int nreqs; |
55 | static int max_outstanding = 1<<30; |
62 | static int max_outstanding = 1<<30; |
56 | static int respipe [2]; |
63 | static int respipe [2]; |
57 | |
64 | |
58 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
65 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
59 | static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; |
66 | static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; |
60 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
67 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
61 | |
68 | |
62 | static volatile aio_req reqs, reqe; /* queue start, queue end */ |
69 | static volatile aio_req reqs, reqe; /* queue start, queue end */ |
63 | static volatile aio_req ress, rese; /* queue start, queue end */ |
70 | static volatile aio_req ress, rese; /* queue start, queue end */ |
64 | |
71 | |
|
|
72 | static void free_req (aio_req req) |
|
|
73 | { |
|
|
74 | if (req->data) |
|
|
75 | SvREFCNT_dec (req->data); |
|
|
76 | |
|
|
77 | if (req->fh) |
|
|
78 | SvREFCNT_dec (req->fh); |
|
|
79 | |
|
|
80 | if (req->statdata) |
|
|
81 | Safefree (req->statdata); |
|
|
82 | |
|
|
83 | if (req->callback) |
|
|
84 | SvREFCNT_dec (req->callback); |
|
|
85 | |
|
|
86 | Safefree (req); |
|
|
87 | } |
|
|
88 | |
65 | static void |
89 | static void |
66 | poll_wait () |
90 | poll_wait () |
67 | { |
91 | { |
68 | if (!nreqs) |
92 | if (nreqs && !ress) |
69 | return; |
93 | { |
70 | |
|
|
71 | fd_set rfd; |
94 | fd_set rfd; |
72 | FD_ZERO(&rfd); |
95 | FD_ZERO(&rfd); |
73 | FD_SET(respipe [0], &rfd); |
96 | FD_SET(respipe [0], &rfd); |
74 | |
97 | |
75 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
98 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
|
|
99 | } |
76 | } |
100 | } |
77 | |
101 | |
78 | static int |
102 | static int |
79 | poll_cb () |
103 | poll_cb () |
80 | { |
104 | { |
81 | dSP; |
105 | dSP; |
82 | int count = 0; |
106 | int count = 0; |
|
|
107 | int do_croak = 0; |
83 | aio_req req; |
108 | aio_req req; |
84 | |
|
|
85 | { |
|
|
86 | /* read and signals sent by the worker threads */ |
|
|
87 | char buf [32]; |
|
|
88 | while (read (respipe [0], buf, 32) > 0) |
|
|
89 | ; |
|
|
90 | } |
|
|
91 | |
109 | |
92 | for (;;) |
110 | for (;;) |
93 | { |
111 | { |
94 | pthread_mutex_lock (&reslock); |
112 | pthread_mutex_lock (&reslock); |
95 | |
|
|
96 | req = ress; |
113 | req = ress; |
97 | |
114 | |
98 | if (ress) |
115 | if (req) |
99 | { |
116 | { |
100 | ress = ress->next; |
117 | ress = req->next; |
|
|
118 | |
101 | if (!ress) rese = 0; |
119 | if (!ress) |
|
|
120 | { |
|
|
121 | rese = 0; |
|
|
122 | |
|
|
123 | /* read any signals sent by the worker threads */ |
|
|
124 | char buf [32]; |
|
|
125 | while (read (respipe [0], buf, 32) == 32) |
|
|
126 | ; |
|
|
127 | } |
102 | } |
128 | } |
103 | |
129 | |
104 | pthread_mutex_unlock (&reslock); |
130 | pthread_mutex_unlock (&reslock); |
105 | |
131 | |
106 | if (!req) |
132 | if (!req) |
… | |
… | |
114 | { |
140 | { |
115 | int errorno = errno; |
141 | int errorno = errno; |
116 | errno = req->errorno; |
142 | errno = req->errorno; |
117 | |
143 | |
118 | if (req->type == REQ_READ) |
144 | if (req->type == REQ_READ) |
119 | SvCUR_set (req->data, req->dataoffset |
145 | SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); |
120 | + req->result > 0 ? req->result : 0); |
|
|
121 | |
146 | |
|
|
147 | if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE)) |
|
|
148 | SvREADONLY_off (req->data); |
|
|
149 | |
122 | if (req->data) |
150 | if (req->statdata) |
123 | SvREFCNT_dec (req->data); |
|
|
124 | |
|
|
125 | if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) |
|
|
126 | { |
151 | { |
127 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
152 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
128 | PL_laststatval = req->result; |
153 | PL_laststatval = req->result; |
129 | PL_statcache = *(req->statdata); |
154 | PL_statcache = *(req->statdata); |
130 | |
|
|
131 | Safefree (req->statdata); |
|
|
132 | } |
155 | } |
133 | |
156 | |
|
|
157 | ENTER; |
134 | PUSHMARK (SP); |
158 | PUSHMARK (SP); |
135 | XPUSHs (sv_2mortal (newSViv (req->result))); |
159 | XPUSHs (sv_2mortal (newSViv (req->result))); |
136 | |
160 | |
137 | if (req->type == REQ_OPEN) |
161 | if (req->type == REQ_OPEN) |
138 | { |
162 | { |
… | |
… | |
141 | |
165 | |
142 | PUTBACK; |
166 | PUTBACK; |
143 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
167 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
144 | SPAGAIN; |
168 | SPAGAIN; |
145 | |
169 | |
146 | fh = POPs; |
170 | fh = SvREFCNT_inc (POPs); |
147 | |
171 | |
148 | PUSHMARK (SP); |
172 | PUSHMARK (SP); |
149 | XPUSHs (fh); |
173 | XPUSHs (sv_2mortal (fh)); |
150 | } |
174 | } |
151 | |
175 | |
152 | if (SvOK (req->callback)) |
176 | if (SvOK (req->callback)) |
153 | { |
177 | { |
154 | PUTBACK; |
178 | PUTBACK; |
155 | call_sv (req->callback, G_VOID | G_EVAL); |
179 | call_sv (req->callback, G_VOID | G_EVAL); |
156 | SPAGAIN; |
180 | SPAGAIN; |
|
|
181 | |
|
|
182 | if (SvTRUE (ERRSV)) |
|
|
183 | { |
|
|
184 | free_req (req); |
|
|
185 | croak (0); |
|
|
186 | } |
157 | } |
187 | } |
|
|
188 | |
158 | |
189 | LEAVE; |
159 | if (req->callback) |
|
|
160 | SvREFCNT_dec (req->callback); |
|
|
161 | |
190 | |
162 | errno = errorno; |
191 | errno = errorno; |
163 | count++; |
192 | count++; |
164 | } |
193 | } |
165 | |
194 | |
166 | Safefree (req); |
195 | free_req (req); |
167 | } |
196 | } |
168 | |
197 | |
169 | return count; |
198 | return count; |
170 | } |
199 | } |
171 | |
200 | |
… | |
… | |
209 | reqe = reqs = req; |
238 | reqe = reqs = req; |
210 | |
239 | |
211 | pthread_cond_signal (&reqwait); |
240 | pthread_cond_signal (&reqwait); |
212 | pthread_mutex_unlock (&reqlock); |
241 | pthread_mutex_unlock (&reqlock); |
213 | |
242 | |
214 | while (nreqs > max_outstanding) |
243 | if (nreqs > max_outstanding) |
|
|
244 | for (;;) |
|
|
245 | { |
|
|
246 | poll_cb (); |
|
|
247 | |
|
|
248 | if (nreqs <= max_outstanding) |
|
|
249 | break; |
|
|
250 | |
|
|
251 | poll_wait (); |
|
|
252 | } |
|
|
253 | } |
|
|
254 | |
|
|
255 | static void |
|
|
256 | end_thread (void) |
|
|
257 | { |
|
|
258 | aio_req req; |
|
|
259 | Newz (0, req, 1, aio_cb); |
|
|
260 | req->type = REQ_QUIT; |
|
|
261 | |
|
|
262 | send_req (req); |
|
|
263 | } |
|
|
264 | |
|
|
265 | static void min_parallel (int nthreads) |
|
|
266 | { |
|
|
267 | while (nthreads > started) |
|
|
268 | start_thread (); |
|
|
269 | } |
|
|
270 | |
|
|
271 | static void max_parallel (int nthreads) |
|
|
272 | { |
|
|
273 | int cur = started; |
|
|
274 | |
|
|
275 | while (cur > nthreads) |
|
|
276 | { |
|
|
277 | end_thread (); |
|
|
278 | cur--; |
|
|
279 | } |
|
|
280 | |
|
|
281 | while (started > nthreads) |
215 | { |
282 | { |
216 | poll_wait (); |
283 | poll_wait (); |
217 | poll_cb (); |
284 | poll_cb (); |
218 | } |
285 | } |
219 | } |
286 | } |
220 | |
287 | |
221 | static void |
288 | static void create_pipe () |
222 | end_thread (void) |
|
|
223 | { |
289 | { |
|
|
290 | if (pipe (respipe)) |
|
|
291 | croak ("unable to initialize result pipe"); |
|
|
292 | |
|
|
293 | if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)) |
|
|
294 | croak ("cannot set result pipe to nonblocking mode"); |
|
|
295 | |
|
|
296 | if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) |
|
|
297 | croak ("cannot set result pipe to nonblocking mode"); |
|
|
298 | } |
|
|
299 | |
|
|
300 | static void atfork_prepare (void) |
|
|
301 | { |
|
|
302 | pthread_mutex_lock (&reqlock); |
|
|
303 | pthread_mutex_lock (&reslock); |
|
|
304 | } |
|
|
305 | |
|
|
306 | static void atfork_parent (void) |
|
|
307 | { |
|
|
308 | pthread_mutex_unlock (&reslock); |
|
|
309 | pthread_mutex_unlock (&reqlock); |
|
|
310 | } |
|
|
311 | |
|
|
312 | static void atfork_child (void) |
|
|
313 | { |
224 | aio_req req; |
314 | aio_req prv; |
225 | New (0, req, 1, aio_cb); |
|
|
226 | req->type = REQ_QUIT; |
|
|
227 | |
315 | |
228 | send_req (req); |
316 | int restart = started; |
229 | } |
317 | started = 0; |
230 | |
318 | |
231 | static void |
319 | while (reqs) |
232 | read_write (int dowrite, int fd, off_t offset, size_t length, |
|
|
233 | SV *data, STRLEN dataoffset, SV *callback) |
|
|
234 | { |
|
|
235 | aio_req req; |
|
|
236 | STRLEN svlen; |
|
|
237 | char *svptr = SvPV (data, svlen); |
|
|
238 | |
|
|
239 | SvUPGRADE (data, SVt_PV); |
|
|
240 | SvPOK_on (data); |
|
|
241 | |
|
|
242 | if (dataoffset < 0) |
|
|
243 | dataoffset += svlen; |
|
|
244 | |
|
|
245 | if (dataoffset < 0 || dataoffset > svlen) |
|
|
246 | croak ("data offset outside of string"); |
|
|
247 | |
|
|
248 | if (dowrite) |
|
|
249 | { |
320 | { |
250 | /* write: check length and adjust. */ |
321 | prv = reqs; |
251 | if (length < 0 || length + dataoffset > svlen) |
322 | reqs = prv->next; |
252 | length = svlen - dataoffset; |
323 | free_req (prv); |
253 | } |
324 | } |
254 | else |
325 | |
|
|
326 | reqs = reqe = 0; |
|
|
327 | |
|
|
328 | while (ress) |
255 | { |
329 | { |
256 | /* read: grow scalar as necessary */ |
330 | prv = ress; |
257 | svptr = SvGROW (data, length + dataoffset); |
331 | ress = prv->next; |
|
|
332 | free_req (prv); |
|
|
333 | } |
|
|
334 | |
|
|
335 | ress = rese = 0; |
|
|
336 | |
|
|
337 | atfork_parent (); |
|
|
338 | |
|
|
339 | min_parallel (restart); |
|
|
340 | } |
|
|
341 | |
|
|
342 | /*****************************************************************************/ |
|
|
343 | /* work around various missing functions */ |
|
|
344 | |
|
|
345 | #if !HAVE_PREADWRITE |
|
|
346 | # define pread aio_pread |
|
|
347 | # define pwrite aio_pwrite |
|
|
348 | |
|
|
349 | /* |
|
|
350 | * make our pread/pwrite safe against themselves, but not against |
|
|
351 | * normal read/write by using a mutex. slows down execution a lot, |
|
|
352 | * but that's your problem, not mine. |
|
|
353 | */ |
|
|
354 | static pthread_mutex_t iolock = PTHREAD_MUTEX_INITIALIZER; |
|
|
355 | |
|
|
356 | static ssize_t |
|
|
357 | pread (int fd, void *buf, size_t count, off_t offset) |
|
|
358 | { |
|
|
359 | ssize_t res; |
|
|
360 | off_t ooffset; |
|
|
361 | |
|
|
362 | pthread_mutex_lock (&iolock); |
|
|
363 | ooffset = lseek (fd, 0, SEEK_CUR); |
|
|
364 | lseek (fd, offset, SEEK_SET); |
|
|
365 | res = read (fd, buf, count); |
|
|
366 | lseek (fd, ooffset, SEEK_SET); |
|
|
367 | pthread_mutex_unlock (&iolock); |
|
|
368 | |
|
|
369 | return res; |
|
|
370 | } |
|
|
371 | |
|
|
372 | static ssize_t |
|
|
373 | pwrite (int fd, void *buf, size_t count, off_t offset) |
|
|
374 | { |
|
|
375 | ssize_t res; |
|
|
376 | off_t ooffset; |
|
|
377 | |
|
|
378 | pthread_mutex_lock (&iolock); |
|
|
379 | ooffset = lseek (fd, 0, SEEK_CUR); |
|
|
380 | lseek (fd, offset, SEEK_SET); |
|
|
381 | res = write (fd, buf, count); |
|
|
382 | lseek (fd, offset, SEEK_SET); |
|
|
383 | pthread_mutex_unlock (&iolock); |
|
|
384 | |
|
|
385 | return res; |
|
|
386 | } |
|
|
387 | #endif |
|
|
388 | |
|
|
389 | #if !HAVE_FDATASYNC |
|
|
390 | # define fdatasync fsync |
|
|
391 | #endif |
|
|
392 | |
|
|
393 | #if !HAVE_READAHEAD |
|
|
394 | # define readahead aio_readahead |
|
|
395 | |
|
|
396 | static char readahead_buf[4096]; |
|
|
397 | |
|
|
398 | static ssize_t |
|
|
399 | readahead (int fd, off_t offset, size_t count) |
|
|
400 | { |
|
|
401 | while (count > 0) |
258 | } |
402 | { |
|
|
403 | size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); |
259 | |
404 | |
260 | if (length < 0) |
405 | pread (fd, readahead_buf, len, offset); |
261 | croak ("length must not be negative"); |
406 | offset += len; |
|
|
407 | count -= len; |
|
|
408 | } |
262 | |
409 | |
263 | Newz (0, req, 1, aio_cb); |
410 | errno = 0; |
264 | |
|
|
265 | if (!req) |
|
|
266 | croak ("out of memory during aio_req allocation"); |
|
|
267 | |
|
|
268 | req->type = dowrite ? REQ_WRITE : REQ_READ; |
|
|
269 | req->fd = fd; |
|
|
270 | req->offset = offset; |
|
|
271 | req->length = length; |
|
|
272 | req->data = SvREFCNT_inc (data); |
|
|
273 | req->dataptr = (char *)svptr + dataoffset; |
|
|
274 | req->callback = SvREFCNT_inc (callback); |
|
|
275 | |
|
|
276 | send_req (req); |
|
|
277 | } |
411 | } |
|
|
412 | #endif |
|
|
413 | |
|
|
414 | /*****************************************************************************/ |
278 | |
415 | |
279 | static void * |
416 | static void * |
280 | aio_proc (void *thr_arg) |
417 | aio_proc (void *thr_arg) |
281 | { |
418 | { |
282 | aio_req req; |
419 | aio_req req; |
… | |
… | |
308 | |
445 | |
309 | type = req->type; |
446 | type = req->type; |
310 | |
447 | |
311 | switch (type) |
448 | switch (type) |
312 | { |
449 | { |
313 | case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, req->offset); break; |
450 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
314 | case REQ_WRITE: req->result = pwrite64 (req->fd, req->dataptr, req->length, req->offset); break; |
451 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
315 | #if SYS_readahead |
452 | |
316 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
453 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
317 | #else |
|
|
318 | case REQ_READAHEAD: req->result = -1; errno = ENOSYS; break; |
|
|
319 | #endif |
|
|
320 | |
454 | |
321 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
455 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
322 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
456 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
323 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
457 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
324 | |
458 | |
325 | case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
459 | case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
326 | case REQ_CLOSE: req->result = close (req->fd); break; |
460 | case REQ_CLOSE: req->result = close (req->fd); break; |
327 | case REQ_UNLINK: req->result = unlink (req->dataptr); break; |
461 | case REQ_UNLINK: req->result = unlink (req->dataptr); break; |
|
|
462 | case REQ_RMDIR: req->result = rmdir (req->dataptr); break; |
|
|
463 | case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; |
328 | |
464 | |
|
|
465 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
329 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
466 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
330 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
|
|
331 | |
467 | |
332 | case REQ_QUIT: |
468 | case REQ_QUIT: |
333 | break; |
469 | break; |
334 | |
470 | |
335 | default: |
471 | default: |
… | |
… | |
361 | while (type != REQ_QUIT); |
497 | while (type != REQ_QUIT); |
362 | |
498 | |
363 | return 0; |
499 | return 0; |
364 | } |
500 | } |
365 | |
501 | |
|
|
502 | #define dREQ \ |
|
|
503 | aio_req req; \ |
|
|
504 | \ |
|
|
505 | if (SvOK (callback) && !SvROK (callback)) \ |
|
|
506 | croak ("clalback must be undef or of reference type"); \ |
|
|
507 | \ |
|
|
508 | Newz (0, req, 1, aio_cb); \ |
|
|
509 | if (!req) \ |
|
|
510 | croak ("out of memory during aio_req allocation"); \ |
|
|
511 | \ |
|
|
512 | req->callback = newSVsv (callback); |
|
|
513 | |
366 | MODULE = IO::AIO PACKAGE = IO::AIO |
514 | MODULE = IO::AIO PACKAGE = IO::AIO |
367 | |
515 | |
368 | PROTOTYPES: ENABLE |
516 | PROTOTYPES: ENABLE |
369 | |
517 | |
370 | BOOT: |
518 | BOOT: |
371 | { |
519 | { |
372 | if (pipe (respipe)) |
520 | create_pipe (); |
373 | croak ("unable to initialize result pipe"); |
521 | pthread_atfork (atfork_prepare, atfork_parent, atfork_child); |
374 | |
|
|
375 | if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)) |
|
|
376 | croak ("cannot set result pipe to nonblocking mode"); |
|
|
377 | |
|
|
378 | if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) |
|
|
379 | croak ("cannot set result pipe to nonblocking mode"); |
|
|
380 | } |
522 | } |
381 | |
523 | |
382 | void |
524 | void |
383 | min_parallel(nthreads) |
525 | min_parallel(nthreads) |
384 | int nthreads |
526 | int nthreads |
385 | PROTOTYPE: $ |
527 | PROTOTYPE: $ |
386 | CODE: |
|
|
387 | while (nthreads > started) |
|
|
388 | start_thread (); |
|
|
389 | |
528 | |
390 | void |
529 | void |
391 | max_parallel(nthreads) |
530 | max_parallel(nthreads) |
392 | int nthreads |
531 | int nthreads |
393 | PROTOTYPE: $ |
532 | PROTOTYPE: $ |
394 | CODE: |
|
|
395 | { |
|
|
396 | int cur = started; |
|
|
397 | while (cur > nthreads) |
|
|
398 | { |
|
|
399 | end_thread (); |
|
|
400 | cur--; |
|
|
401 | } |
|
|
402 | |
|
|
403 | while (started > nthreads) |
|
|
404 | { |
|
|
405 | poll_wait (); |
|
|
406 | poll_cb (); |
|
|
407 | } |
|
|
408 | } |
|
|
409 | |
533 | |
410 | int |
534 | int |
411 | max_outstanding(nreqs) |
535 | max_outstanding(nreqs) |
412 | int nreqs |
536 | int nreqs |
413 | PROTOTYPE: $ |
537 | PROTOTYPE: $ |
… | |
… | |
422 | int mode |
546 | int mode |
423 | SV * callback |
547 | SV * callback |
424 | PROTOTYPE: $$$;$ |
548 | PROTOTYPE: $$$;$ |
425 | CODE: |
549 | CODE: |
426 | { |
550 | { |
427 | aio_req req; |
551 | dREQ; |
428 | |
|
|
429 | Newz (0, req, 1, aio_cb); |
|
|
430 | |
|
|
431 | if (!req) |
|
|
432 | croak ("out of memory during aio_req allocation"); |
|
|
433 | |
552 | |
434 | req->type = REQ_OPEN; |
553 | req->type = REQ_OPEN; |
435 | req->data = newSVsv (pathname); |
554 | req->data = newSVsv (pathname); |
436 | req->dataptr = SvPV_nolen (req->data); |
555 | req->dataptr = SvPVbyte_nolen (req->data); |
437 | req->fd = flags; |
556 | req->fd = flags; |
438 | req->mode = mode; |
557 | req->mode = mode; |
439 | req->callback = SvREFCNT_inc (callback); |
|
|
440 | |
558 | |
441 | send_req (req); |
559 | send_req (req); |
442 | } |
560 | } |
443 | |
561 | |
444 | void |
562 | void |
445 | aio_close(fh,callback=&PL_sv_undef) |
563 | aio_close(fh,callback=&PL_sv_undef) |
446 | InputStream fh |
564 | SV * fh |
447 | SV * callback |
565 | SV * callback |
448 | PROTOTYPE: $;$ |
566 | PROTOTYPE: $;$ |
449 | ALIAS: |
567 | ALIAS: |
450 | aio_close = REQ_CLOSE |
568 | aio_close = REQ_CLOSE |
451 | aio_fsync = REQ_FSYNC |
569 | aio_fsync = REQ_FSYNC |
452 | aio_fdatasync = REQ_FDATASYNC |
570 | aio_fdatasync = REQ_FDATASYNC |
453 | CODE: |
571 | CODE: |
454 | { |
572 | { |
455 | aio_req req; |
573 | dREQ; |
456 | |
|
|
457 | Newz (0, req, 1, aio_cb); |
|
|
458 | |
|
|
459 | if (!req) |
|
|
460 | croak ("out of memory during aio_req allocation"); |
|
|
461 | |
574 | |
462 | req->type = ix; |
575 | req->type = ix; |
|
|
576 | req->fh = newSVsv (fh); |
463 | req->fd = PerlIO_fileno (fh); |
577 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
464 | req->callback = SvREFCNT_inc (callback); |
|
|
465 | |
578 | |
466 | send_req (req); |
579 | send_req (req); |
467 | } |
580 | } |
468 | |
581 | |
469 | void |
582 | void |
470 | aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
583 | aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
471 | InputStream fh |
584 | SV * fh |
472 | UV offset |
585 | UV offset |
473 | IV length |
586 | IV length |
474 | SV * data |
587 | SV * data |
475 | IV dataoffset |
588 | IV dataoffset |
476 | SV * callback |
589 | SV * callback |
|
|
590 | ALIAS: |
|
|
591 | aio_read = REQ_READ |
|
|
592 | aio_write = REQ_WRITE |
477 | PROTOTYPE: $$$$$;$ |
593 | PROTOTYPE: $$$$$;$ |
478 | CODE: |
594 | CODE: |
479 | read_write (0, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
595 | { |
|
|
596 | aio_req req; |
|
|
597 | STRLEN svlen; |
|
|
598 | char *svptr = SvPVbyte (data, svlen); |
480 | |
599 | |
481 | void |
600 | SvUPGRADE (data, SVt_PV); |
482 | aio_write(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
601 | SvPOK_on (data); |
483 | OutputStream fh |
602 | |
484 | UV offset |
603 | if (dataoffset < 0) |
485 | IV length |
604 | dataoffset += svlen; |
486 | SV * data |
605 | |
487 | IV dataoffset |
606 | if (dataoffset < 0 || dataoffset > svlen) |
488 | SV * callback |
607 | croak ("data offset outside of string"); |
489 | PROTOTYPE: $$$$$;$ |
608 | |
490 | CODE: |
609 | if (ix == REQ_WRITE) |
491 | read_write (1, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
610 | { |
|
|
611 | /* write: check length and adjust. */ |
|
|
612 | if (length < 0 || length + dataoffset > svlen) |
|
|
613 | length = svlen - dataoffset; |
|
|
614 | } |
|
|
615 | else |
|
|
616 | { |
|
|
617 | /* read: grow scalar as necessary */ |
|
|
618 | svptr = SvGROW (data, length + dataoffset); |
|
|
619 | } |
|
|
620 | |
|
|
621 | if (length < 0) |
|
|
622 | croak ("length must not be negative"); |
|
|
623 | |
|
|
624 | { |
|
|
625 | dREQ; |
|
|
626 | |
|
|
627 | req->type = ix; |
|
|
628 | req->fh = newSVsv (fh); |
|
|
629 | req->fd = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh)) |
|
|
630 | : IoOFP (sv_2io (fh))); |
|
|
631 | req->offset = offset; |
|
|
632 | req->length = length; |
|
|
633 | req->data = SvREFCNT_inc (data); |
|
|
634 | req->dataptr = (char *)svptr + dataoffset; |
|
|
635 | |
|
|
636 | if (!SvREADONLY (data)) |
|
|
637 | { |
|
|
638 | SvREADONLY_on (data); |
|
|
639 | req->data2ptr = (void *)data; |
|
|
640 | } |
|
|
641 | |
|
|
642 | send_req (req); |
|
|
643 | } |
|
|
644 | } |
492 | |
645 | |
493 | void |
646 | void |
494 | aio_readahead(fh,offset,length,callback=&PL_sv_undef) |
647 | aio_readahead(fh,offset,length,callback=&PL_sv_undef) |
495 | InputStream fh |
648 | SV * fh |
496 | UV offset |
649 | UV offset |
497 | IV length |
650 | IV length |
498 | SV * callback |
651 | SV * callback |
499 | PROTOTYPE: $$$;$ |
652 | PROTOTYPE: $$$;$ |
500 | CODE: |
653 | CODE: |
501 | { |
654 | { |
502 | aio_req req; |
655 | dREQ; |
503 | |
|
|
504 | if (length < 0) |
|
|
505 | croak ("length must not be negative"); |
|
|
506 | |
|
|
507 | Newz (0, req, 1, aio_cb); |
|
|
508 | |
|
|
509 | if (!req) |
|
|
510 | croak ("out of memory during aio_req allocation"); |
|
|
511 | |
656 | |
512 | req->type = REQ_READAHEAD; |
657 | req->type = REQ_READAHEAD; |
|
|
658 | req->fh = newSVsv (fh); |
513 | req->fd = PerlIO_fileno (fh); |
659 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
514 | req->offset = offset; |
660 | req->offset = offset; |
515 | req->length = length; |
661 | req->length = length; |
516 | req->callback = SvREFCNT_inc (callback); |
|
|
517 | |
662 | |
518 | send_req (req); |
663 | send_req (req); |
519 | } |
664 | } |
520 | |
665 | |
521 | void |
666 | void |
… | |
… | |
525 | ALIAS: |
670 | ALIAS: |
526 | aio_stat = REQ_STAT |
671 | aio_stat = REQ_STAT |
527 | aio_lstat = REQ_LSTAT |
672 | aio_lstat = REQ_LSTAT |
528 | CODE: |
673 | CODE: |
529 | { |
674 | { |
530 | aio_req req; |
675 | dREQ; |
531 | |
|
|
532 | Newz (0, req, 1, aio_cb); |
|
|
533 | |
|
|
534 | if (!req) |
|
|
535 | croak ("out of memory during aio_req allocation"); |
|
|
536 | |
676 | |
537 | New (0, req->statdata, 1, Stat_t); |
677 | New (0, req->statdata, 1, Stat_t); |
538 | |
|
|
539 | if (!req->statdata) |
678 | if (!req->statdata) |
|
|
679 | { |
|
|
680 | free_req (req); |
540 | croak ("out of memory during aio_req->statdata allocation"); |
681 | croak ("out of memory during aio_req->statdata allocation"); |
|
|
682 | } |
541 | |
683 | |
542 | if (SvPOK (fh_or_path)) |
684 | if (SvPOK (fh_or_path)) |
543 | { |
685 | { |
544 | req->type = ix; |
686 | req->type = ix; |
545 | req->data = newSVsv (fh_or_path); |
687 | req->data = newSVsv (fh_or_path); |
546 | req->dataptr = SvPV_nolen (req->data); |
688 | req->dataptr = SvPVbyte_nolen (req->data); |
547 | } |
689 | } |
548 | else |
690 | else |
549 | { |
691 | { |
550 | req->type = REQ_FSTAT; |
692 | req->type = REQ_FSTAT; |
|
|
693 | req->fh = newSVsv (fh_or_path); |
551 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
694 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
552 | } |
695 | } |
553 | |
|
|
554 | req->callback = SvREFCNT_inc (callback); |
|
|
555 | |
696 | |
556 | send_req (req); |
697 | send_req (req); |
557 | } |
698 | } |
558 | |
699 | |
559 | void |
700 | void |
560 | aio_unlink(pathname,callback=&PL_sv_undef) |
701 | aio_unlink(pathname,callback=&PL_sv_undef) |
561 | SV * pathname |
702 | SV * pathname |
562 | SV * callback |
703 | SV * callback |
|
|
704 | ALIAS: |
|
|
705 | aio_unlink = REQ_UNLINK |
|
|
706 | aio_rmdir = REQ_RMDIR |
563 | CODE: |
707 | CODE: |
564 | { |
708 | { |
565 | aio_req req; |
709 | dREQ; |
566 | |
710 | |
567 | Newz (0, req, 1, aio_cb); |
711 | req->type = ix; |
|
|
712 | req->data = newSVsv (pathname); |
|
|
713 | req->dataptr = SvPVbyte_nolen (req->data); |
568 | |
714 | |
569 | if (!req) |
715 | send_req (req); |
570 | croak ("out of memory during aio_req allocation"); |
716 | } |
|
|
717 | |
|
|
718 | void |
|
|
719 | aio_symlink(oldpath,newpath,callback=&PL_sv_undef) |
|
|
720 | SV * oldpath |
|
|
721 | SV * newpath |
|
|
722 | SV * callback |
|
|
723 | CODE: |
|
|
724 | { |
|
|
725 | dREQ; |
571 | |
726 | |
572 | req->type = REQ_UNLINK; |
727 | req->type = REQ_SYMLINK; |
|
|
728 | req->fh = newSVsv (oldpath); |
|
|
729 | req->data2ptr = SvPVbyte_nolen (req->fh); |
573 | req->data = newSVsv (pathname); |
730 | req->data = newSVsv (newpath); |
574 | req->dataptr = SvPV_nolen (req->data); |
731 | req->dataptr = SvPVbyte_nolen (req->data); |
575 | req->callback = SvREFCNT_inc (callback); |
|
|
576 | |
732 | |
577 | send_req (req); |
733 | send_req (req); |
578 | } |
734 | } |
579 | |
735 | |
580 | void |
736 | void |