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