|
|
1 | #define PERL_NO_GET_CONTEXT |
|
|
2 | |
1 | #include "EXTERN.h" |
3 | #include "EXTERN.h" |
2 | #include "perl.h" |
4 | #include "perl.h" |
3 | #include "XSUB.h" |
5 | #include "XSUB.h" |
4 | |
6 | |
5 | #include <sys/types.h> |
7 | #include <sys/types.h> |
… | |
… | |
11 | |
13 | |
12 | typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
14 | typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
13 | typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
15 | typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
14 | typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
16 | typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
15 | |
17 | |
|
|
18 | #ifndef __NR_pread64 |
|
|
19 | # define __NR_pread64 __NR_pread |
|
|
20 | #endif |
|
|
21 | #ifndef __NR_pwrite64 |
|
|
22 | # define __NR_pwrite64 __NR_pwrite |
|
|
23 | #endif |
|
|
24 | |
16 | #define STACKSIZE 1024 /* yeah */ |
25 | #define STACKSIZE 1024 /* yeah */ |
17 | |
26 | |
18 | enum { REQ_QUIT, REQ_OPEN, REQ_CLOSE, REQ_READ, REQ_WRITE, REQ_STAT, REQ_LSTAT, REQ_FSTAT}; |
27 | enum { REQ_QUIT, REQ_OPEN, REQ_CLOSE, REQ_READ, REQ_WRITE, REQ_STAT, REQ_LSTAT, REQ_FSTAT}; |
19 | |
28 | |
20 | typedef struct { |
29 | typedef struct { |
21 | char stack[STACKSIZE]; |
30 | char stack[STACKSIZE]; |
22 | } aio_thread; |
31 | } aio_thread; |
23 | |
32 | |
24 | typedef struct { |
33 | typedef struct aio_cb { |
|
|
34 | struct aio_cb *next; |
|
|
35 | |
25 | int type; |
36 | int type; |
26 | aio_thread *thread; |
37 | aio_thread *thread; |
27 | |
|
|
28 | SV *savesv; |
|
|
29 | |
38 | |
30 | int fd; |
39 | int fd; |
31 | off_t offset; |
40 | off_t offset; |
32 | size_t length; |
41 | size_t length; |
33 | ssize_t result; |
42 | ssize_t result; |
… | |
… | |
44 | |
53 | |
45 | static int started; |
54 | static int started; |
46 | static int nreqs; |
55 | static int nreqs; |
47 | static int reqpipe[2], respipe[2]; |
56 | static int reqpipe[2], respipe[2]; |
48 | |
57 | |
|
|
58 | static aio_req qs, qe; /* queue start, queue end */ |
|
|
59 | |
49 | static int aio_proc(void *arg); |
60 | static int aio_proc(void *arg); |
50 | |
61 | |
51 | static void |
62 | static void |
52 | start_thread(void) |
63 | start_thread (void) |
53 | { |
64 | { |
54 | aio_thread *thr; |
65 | aio_thread *thr; |
55 | |
66 | |
56 | New (0, thr, 1, aio_thread); |
67 | New (0, thr, 1, aio_thread); |
57 | |
68 | |
… | |
… | |
63 | else |
74 | else |
64 | Safefree (thr); |
75 | Safefree (thr); |
65 | } |
76 | } |
66 | |
77 | |
67 | static void |
78 | static void |
|
|
79 | send_reqs (void) |
|
|
80 | { |
|
|
81 | /* this write is atomic */ |
|
|
82 | while (qs && write (reqpipe[1], &qs, sizeof qs) == sizeof qs) |
|
|
83 | { |
|
|
84 | qs = qs->next; |
|
|
85 | if (!qs) qe = 0; |
|
|
86 | } |
|
|
87 | } |
|
|
88 | |
|
|
89 | static void |
|
|
90 | send_req (aio_req req) |
|
|
91 | { |
|
|
92 | nreqs++; |
|
|
93 | req->next = 0; |
|
|
94 | |
|
|
95 | if (qe) |
|
|
96 | qe->next = req; |
|
|
97 | else |
|
|
98 | qe = qs = req; |
|
|
99 | |
|
|
100 | send_reqs (); |
|
|
101 | } |
|
|
102 | |
|
|
103 | static void |
68 | end_thread(void) |
104 | end_thread (void) |
69 | { |
105 | { |
70 | aio_req req; |
106 | aio_req req; |
71 | New (0, req, 1, aio_cb); |
107 | New (0, req, 1, aio_cb); |
72 | req->type = REQ_QUIT; |
108 | req->type = REQ_QUIT; |
73 | write (reqpipe[1], &req, sizeof (aio_req)); |
109 | |
|
|
110 | send_req (req); |
74 | } |
111 | } |
75 | |
112 | |
76 | static void |
113 | static void |
77 | send_req (aio_req req) |
114 | read_write (pTHX_ |
78 | { |
|
|
79 | nreqs++; |
|
|
80 | write (reqpipe[1], &req, sizeof (aio_req)); |
|
|
81 | } |
|
|
82 | |
|
|
83 | static void |
|
|
84 | read_write (pTHX_ int dowrite, int fd, off_t offset, size_t length, |
115 | int dowrite, int fd, off_t offset, size_t length, |
85 | SV *data, STRLEN dataoffset, SV*callback) |
116 | SV *data, STRLEN dataoffset, SV *callback) |
86 | { |
117 | { |
87 | aio_req req; |
118 | aio_req req; |
88 | STRLEN svlen; |
119 | STRLEN svlen; |
89 | char *svptr = SvPV (data, svlen); |
120 | char *svptr = SvPV (data, svlen); |
90 | |
121 | |
… | |
… | |
110 | } |
141 | } |
111 | |
142 | |
112 | if (length < 0) |
143 | if (length < 0) |
113 | croak ("length must not be negative"); |
144 | croak ("length must not be negative"); |
114 | |
145 | |
115 | New (0, req, 1, aio_cb); |
146 | Newz (0, req, 1, aio_cb); |
116 | |
147 | |
117 | if (!req) |
148 | if (!req) |
118 | croak ("out of memory during aio_req allocation"); |
149 | croak ("out of memory during aio_req allocation"); |
119 | |
150 | |
120 | req->type = dowrite ? REQ_WRITE : REQ_READ; |
151 | req->type = dowrite ? REQ_WRITE : REQ_READ; |
… | |
… | |
145 | else |
176 | else |
146 | { |
177 | { |
147 | int errorno = errno; |
178 | int errorno = errno; |
148 | errno = req->errorno; |
179 | errno = req->errorno; |
149 | |
180 | |
150 | if (req->savesv) |
|
|
151 | SvREFCNT_dec (req->savesv); |
|
|
152 | |
|
|
153 | if (req->type == REQ_READ) |
181 | if (req->type == REQ_READ) |
154 | SvCUR_set (req->data, req->dataoffset |
182 | SvCUR_set (req->data, req->dataoffset |
155 | + req->result > 0 ? req->result : 0); |
183 | + req->result > 0 ? req->result : 0); |
|
|
184 | |
|
|
185 | if (req->data) |
|
|
186 | SvREFCNT_dec (req->data); |
156 | |
187 | |
157 | if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) |
188 | if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) |
158 | { |
189 | { |
159 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
190 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
160 | PL_laststatval = req->result; |
191 | PL_laststatval = req->result; |
… | |
… | |
179 | XPUSHs (sv_2mortal (newSViv (req->result))); |
210 | XPUSHs (sv_2mortal (newSViv (req->result))); |
180 | PUTBACK; |
211 | PUTBACK; |
181 | call_sv (req->callback, G_VOID); |
212 | call_sv (req->callback, G_VOID); |
182 | SPAGAIN; |
213 | SPAGAIN; |
183 | |
214 | |
184 | SvREFCNT_dec (req->data); |
215 | if (req->callback) |
185 | SvREFCNT_dec (req->callback); |
216 | SvREFCNT_dec (req->callback); |
186 | |
217 | |
187 | errno = errorno; |
218 | errno = errorno; |
188 | nreqs--; |
219 | nreqs--; |
189 | count++; |
220 | count++; |
190 | } |
221 | } |
191 | |
222 | |
192 | Safefree (req); |
223 | Safefree (req); |
193 | } |
224 | } |
194 | |
225 | |
|
|
226 | if (qs) |
|
|
227 | send_reqs (); |
|
|
228 | |
195 | return count; |
229 | return count; |
196 | } |
230 | } |
197 | |
231 | |
198 | static sigset_t fullsigset; |
232 | static sigset_t fullsigset; |
199 | |
233 | |
200 | #undef errno |
234 | #undef errno |
201 | #include <asm/unistd.h> |
235 | #include <asm/unistd.h> |
202 | |
236 | |
203 | static int |
237 | static int |
204 | aio_proc(void *thr_arg) |
238 | aio_proc (void *thr_arg) |
205 | { |
239 | { |
206 | aio_thread *thr = thr_arg; |
240 | aio_thread *thr = thr_arg; |
207 | aio_req req; |
241 | aio_req req; |
208 | int errno; |
242 | int errno; |
209 | |
243 | |
|
|
244 | /* this is very much x86 and kernel-specific :(:(:( */ |
210 | /* we rely on gcc's ability to create closures. */ |
245 | /* we rely on gcc's ability to create closures. */ |
211 | _syscall3(int,read,int,fd,char *,buf,size_t,count) |
246 | _syscall3(int,read,int,fd,char *,buf,size_t,count) |
212 | _syscall3(int,write,int,fd,char *,buf,size_t,count) |
247 | _syscall3(int,write,int,fd,char *,buf,size_t,count) |
213 | |
248 | |
214 | _syscall3(int,open,char *,pathname,int,flags,mode_t,mode) |
249 | _syscall3(int,open,char *,pathname,int,flags,mode_t,mode) |
215 | _syscall1(int,close,int,fd) |
250 | _syscall1(int,close,int,fd) |
216 | |
251 | |
217 | _syscall4(int,pread,int,fd,char *,buf,size_t,count,off_t,offset) |
252 | _syscall5(int,pread64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi) |
218 | _syscall4(int,pwrite,int,fd,char *,buf,size_t,count,off_t,offset) |
253 | _syscall5(int,pwrite64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi) |
219 | |
254 | |
220 | _syscall2(int,stat64, const char *, filename, struct stat64 *, buf) |
255 | _syscall2(int,stat64, const char *, filename, struct stat64 *, buf) |
221 | _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf) |
256 | _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf) |
222 | _syscall2(int,fstat64, int, fd, struct stat64 *, buf) |
257 | _syscall2(int,fstat64, int, fd, struct stat64 *, buf) |
223 | |
258 | |
… | |
… | |
225 | |
260 | |
226 | /* then loop */ |
261 | /* then loop */ |
227 | while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
262 | while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
228 | { |
263 | { |
229 | req->thread = thr; |
264 | req->thread = thr; |
230 | errno = 0; |
265 | errno = 0; /* strictly unnecessary */ |
231 | |
266 | |
232 | if (req->type == REQ_READ) |
267 | switch (req->type) |
233 | req->result = pread (req->fd, req->dataptr, req->length, req->offset); |
|
|
234 | else if (req->type == REQ_WRITE) |
|
|
235 | req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); |
|
|
236 | else if (req->type == REQ_OPEN) |
|
|
237 | req->result = open (req->dataptr, req->fd, req->mode); |
|
|
238 | else if (req->type == REQ_CLOSE) |
|
|
239 | req->result = close (req->fd); |
|
|
240 | else if (req->type == REQ_STAT) |
|
|
241 | req->result = stat64 (req->dataptr, req->statdata); |
|
|
242 | else if (req->type == REQ_LSTAT) |
|
|
243 | req->result = lstat64 (req->dataptr, req->statdata); |
|
|
244 | else if (req->type == REQ_FSTAT) |
|
|
245 | req->result = fstat64 (req->fd, req->statdata); |
|
|
246 | else |
|
|
247 | { |
268 | { |
|
|
269 | case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); break; |
|
|
270 | case REQ_WRITE: req->result = pwrite64(req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); break; |
|
|
271 | case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
|
|
272 | case REQ_CLOSE: req->result = close (req->fd); break; |
|
|
273 | case REQ_STAT: req->result = stat64 (req->dataptr, req->statdata); break; |
|
|
274 | case REQ_LSTAT: req->result = lstat64 (req->dataptr, req->statdata); break; |
|
|
275 | case REQ_FSTAT: req->result = fstat64 (req->fd, req->statdata); break; |
|
|
276 | |
|
|
277 | case REQ_QUIT: |
|
|
278 | default: |
248 | write (respipe[1], (void *)&req, sizeof (req)); |
279 | write (respipe[1], (void *)&req, sizeof (req)); |
249 | break; |
280 | return 0; |
250 | } |
281 | } |
251 | |
282 | |
252 | req->errorno = errno; |
283 | req->errorno = errno; |
253 | write (respipe[1], (void *)&req, sizeof (req)); |
284 | write (respipe[1], (void *)&req, sizeof (req)); |
254 | } |
285 | } |
… | |
… | |
266 | sigdelset (&fullsigset, SIGABRT); |
297 | sigdelset (&fullsigset, SIGABRT); |
267 | sigdelset (&fullsigset, SIGINT); |
298 | sigdelset (&fullsigset, SIGINT); |
268 | |
299 | |
269 | if (pipe (reqpipe) || pipe (respipe)) |
300 | if (pipe (reqpipe) || pipe (respipe)) |
270 | croak ("unable to initialize request or result pipe"); |
301 | croak ("unable to initialize request or result pipe"); |
|
|
302 | |
|
|
303 | if (fcntl (reqpipe[1], F_SETFL, O_NONBLOCK)) |
|
|
304 | croak ("cannot set result pipe to nonblocking mode"); |
271 | |
305 | |
272 | if (fcntl (respipe[0], F_SETFL, O_NONBLOCK)) |
306 | if (fcntl (respipe[0], F_SETFL, O_NONBLOCK)) |
273 | croak ("cannot set result pipe to nonblocking mode"); |
307 | croak ("cannot set result pipe to nonblocking mode"); |
274 | } |
308 | } |
275 | |
309 | |
… | |
… | |
291 | { |
325 | { |
292 | end_thread (); |
326 | end_thread (); |
293 | cur--; |
327 | cur--; |
294 | } |
328 | } |
295 | |
329 | |
296 | poll_cb (); |
|
|
297 | while (started > nthreads) |
330 | while (started > nthreads) |
298 | { |
331 | { |
299 | sched_yield (); |
332 | fd_set rfd; |
300 | fcntl (respipe[0], F_SETFL, 0); |
333 | FD_ZERO(&rfd); |
|
|
334 | FD_SET(respipe[0], &rfd); |
|
|
335 | |
|
|
336 | select (respipe[0] + 1, &rfd, 0, 0, 0); |
301 | poll_cb (); |
337 | poll_cb (aTHX); |
302 | fcntl (respipe[0], F_SETFL, O_NONBLOCK); |
|
|
303 | } |
338 | } |
304 | |
339 | |
305 | void |
340 | void |
306 | aio_open(pathname,flags,mode,callback) |
341 | aio_open(pathname,flags,mode,callback) |
307 | SV * pathname |
342 | SV * pathname |
… | |
… | |
310 | SV * callback |
345 | SV * callback |
311 | PROTOTYPE: $$$$ |
346 | PROTOTYPE: $$$$ |
312 | CODE: |
347 | CODE: |
313 | aio_req req; |
348 | aio_req req; |
314 | |
349 | |
315 | New (0, req, 1, aio_cb); |
350 | Newz (0, req, 1, aio_cb); |
316 | |
351 | |
317 | if (!req) |
352 | if (!req) |
318 | croak ("out of memory during aio_req allocation"); |
353 | croak ("out of memory during aio_req allocation"); |
319 | |
354 | |
320 | req->type = REQ_OPEN; |
355 | req->type = REQ_OPEN; |
321 | req->savesv = newSVsv (pathname); |
356 | req->data = newSVsv (pathname); |
322 | req->dataptr = SvPV_nolen (req->savesv); |
357 | req->dataptr = SvPV_nolen (req->data); |
323 | req->fd = flags; |
358 | req->fd = flags; |
324 | req->mode = mode; |
359 | req->mode = mode; |
325 | req->callback = SvREFCNT_inc (callback); |
360 | req->callback = SvREFCNT_inc (callback); |
326 | |
361 | |
327 | send_req (req); |
362 | send_req (req); |
328 | |
363 | |
329 | void |
364 | void |
330 | aio_close(fh,callback) |
365 | aio_close(fh,callback) |
331 | InputStream fh |
366 | InputStream fh |
332 | SV * callback |
367 | SV * callback |
333 | PROTOTYPE: $ |
368 | PROTOTYPE: $$ |
334 | CODE: |
369 | CODE: |
335 | aio_req req; |
370 | aio_req req; |
336 | |
371 | |
337 | New (0, req, 1, aio_cb); |
372 | Newz (0, req, 1, aio_cb); |
338 | |
373 | |
339 | if (!req) |
374 | if (!req) |
340 | croak ("out of memory during aio_req allocation"); |
375 | croak ("out of memory during aio_req allocation"); |
341 | |
376 | |
342 | req->type = REQ_CLOSE; |
377 | req->type = REQ_CLOSE; |
… | |
… | |
377 | ALIAS: |
412 | ALIAS: |
378 | aio_lstat = 1 |
413 | aio_lstat = 1 |
379 | CODE: |
414 | CODE: |
380 | aio_req req; |
415 | aio_req req; |
381 | |
416 | |
382 | New (0, req, 1, aio_cb); |
417 | Newz (0, req, 1, aio_cb); |
383 | |
418 | |
384 | if (!req) |
419 | if (!req) |
385 | croak ("out of memory during aio_req allocation"); |
420 | croak ("out of memory during aio_req allocation"); |
386 | |
421 | |
387 | New (0, req->statdata, 1, struct stat64); |
422 | New (0, req->statdata, 1, struct stat64); |
… | |
… | |
390 | croak ("out of memory during aio_req->statdata allocation"); |
425 | croak ("out of memory during aio_req->statdata allocation"); |
391 | |
426 | |
392 | if (SvPOK (fh_or_path)) |
427 | if (SvPOK (fh_or_path)) |
393 | { |
428 | { |
394 | req->type = ix ? REQ_LSTAT : REQ_STAT; |
429 | req->type = ix ? REQ_LSTAT : REQ_STAT; |
395 | req->savesv = newSVsv (fh_or_path); |
430 | req->data = newSVsv (fh_or_path); |
396 | req->dataptr = SvPV_nolen (req->savesv); |
431 | req->dataptr = SvPV_nolen (req->data); |
397 | } |
432 | } |
398 | else |
433 | else |
399 | { |
434 | { |
400 | req->type = REQ_FSTAT; |
435 | req->type = REQ_FSTAT; |
401 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
436 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |