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