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