|
|
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 | |
38 | |
28 | int fd; |
39 | int fd; |
29 | off_t offset; |
40 | off_t offset; |
… | |
… | |
42 | |
53 | |
43 | static int started; |
54 | static int started; |
44 | static int nreqs; |
55 | static int nreqs; |
45 | static int reqpipe[2], respipe[2]; |
56 | static int reqpipe[2], respipe[2]; |
46 | |
57 | |
|
|
58 | static aio_req qs, qe; /* queue start, queue end */ |
|
|
59 | |
47 | static int aio_proc(void *arg); |
60 | static int aio_proc(void *arg); |
48 | |
61 | |
49 | static void |
62 | static void |
50 | start_thread(void) |
63 | start_thread (void) |
51 | { |
64 | { |
52 | aio_thread *thr; |
65 | aio_thread *thr; |
53 | |
66 | |
54 | New (0, thr, 1, aio_thread); |
67 | New (0, thr, 1, aio_thread); |
55 | |
68 | |
… | |
… | |
61 | else |
74 | else |
62 | Safefree (thr); |
75 | Safefree (thr); |
63 | } |
76 | } |
64 | |
77 | |
65 | 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 |
66 | end_thread(void) |
104 | end_thread (void) |
67 | { |
105 | { |
68 | aio_req req; |
106 | aio_req req; |
69 | New (0, req, 1, aio_cb); |
107 | New (0, req, 1, aio_cb); |
70 | req->type = REQ_QUIT; |
108 | req->type = REQ_QUIT; |
71 | write (reqpipe[1], &req, sizeof (aio_req)); |
109 | |
|
|
110 | send_req (req); |
72 | } |
111 | } |
73 | |
112 | |
74 | static void |
113 | static void |
75 | send_req (aio_req req) |
114 | read_write (pTHX_ |
76 | { |
|
|
77 | nreqs++; |
|
|
78 | write (reqpipe[1], &req, sizeof (aio_req)); |
|
|
79 | } |
|
|
80 | |
|
|
81 | static void |
|
|
82 | read_write (pTHX_ int dowrite, int fd, off_t offset, size_t length, |
115 | int dowrite, int fd, off_t offset, size_t length, |
83 | SV *data, STRLEN dataoffset, SV*callback) |
116 | SV *data, STRLEN dataoffset, SV *callback) |
84 | { |
117 | { |
85 | aio_req req; |
118 | aio_req req; |
86 | STRLEN svlen; |
119 | STRLEN svlen; |
87 | char *svptr = SvPV (data, svlen); |
120 | char *svptr = SvPV (data, svlen); |
88 | |
121 | |
… | |
… | |
188 | } |
221 | } |
189 | |
222 | |
190 | Safefree (req); |
223 | Safefree (req); |
191 | } |
224 | } |
192 | |
225 | |
|
|
226 | if (qs) |
|
|
227 | send_reqs (); |
|
|
228 | |
193 | return count; |
229 | return count; |
194 | } |
230 | } |
195 | |
231 | |
196 | static sigset_t fullsigset; |
232 | static sigset_t fullsigset; |
197 | |
233 | |
198 | #undef errno |
234 | #undef errno |
199 | #include <asm/unistd.h> |
235 | #include <asm/unistd.h> |
200 | |
236 | |
201 | static int |
237 | static int |
202 | aio_proc(void *thr_arg) |
238 | aio_proc (void *thr_arg) |
203 | { |
239 | { |
204 | aio_thread *thr = thr_arg; |
240 | aio_thread *thr = thr_arg; |
205 | aio_req req; |
241 | aio_req req; |
206 | int errno; |
242 | int errno; |
207 | |
243 | |
|
|
244 | /* this is very much x86 and kernel-specific :(:(:( */ |
208 | /* we rely on gcc's ability to create closures. */ |
245 | /* we rely on gcc's ability to create closures. */ |
209 | _syscall3(int,read,int,fd,char *,buf,size_t,count) |
246 | _syscall3(int,read,int,fd,char *,buf,size_t,count) |
210 | _syscall3(int,write,int,fd,char *,buf,size_t,count) |
247 | _syscall3(int,write,int,fd,char *,buf,size_t,count) |
211 | |
248 | |
212 | _syscall3(int,open,char *,pathname,int,flags,mode_t,mode) |
249 | _syscall3(int,open,char *,pathname,int,flags,mode_t,mode) |
213 | _syscall1(int,close,int,fd) |
250 | _syscall1(int,close,int,fd) |
214 | |
251 | |
215 | _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) |
216 | _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) |
217 | |
254 | |
218 | _syscall2(int,stat64, const char *, filename, struct stat64 *, buf) |
255 | _syscall2(int,stat64, const char *, filename, struct stat64 *, buf) |
219 | _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf) |
256 | _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf) |
220 | _syscall2(int,fstat64, int, fd, struct stat64 *, buf) |
257 | _syscall2(int,fstat64, int, fd, struct stat64 *, buf) |
221 | |
258 | |
… | |
… | |
225 | while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
262 | while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
226 | { |
263 | { |
227 | req->thread = thr; |
264 | req->thread = thr; |
228 | errno = 0; /* strictly unnecessary */ |
265 | errno = 0; /* strictly unnecessary */ |
229 | |
266 | |
230 | if (req->type == REQ_READ) |
267 | switch (req->type) |
231 | req->result = pread (req->fd, req->dataptr, req->length, req->offset); |
|
|
232 | else if (req->type == REQ_WRITE) |
|
|
233 | req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); |
|
|
234 | else if (req->type == REQ_OPEN) |
|
|
235 | req->result = open (req->dataptr, req->fd, req->mode); |
|
|
236 | else if (req->type == REQ_CLOSE) |
|
|
237 | req->result = close (req->fd); |
|
|
238 | else if (req->type == REQ_STAT) |
|
|
239 | req->result = stat64 (req->dataptr, req->statdata); |
|
|
240 | else if (req->type == REQ_LSTAT) |
|
|
241 | req->result = lstat64 (req->dataptr, req->statdata); |
|
|
242 | else if (req->type == REQ_FSTAT) |
|
|
243 | req->result = fstat64 (req->fd, req->statdata); |
|
|
244 | else |
|
|
245 | { |
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: |
246 | write (respipe[1], (void *)&req, sizeof (req)); |
279 | write (respipe[1], (void *)&req, sizeof (req)); |
247 | break; |
280 | return 0; |
248 | } |
281 | } |
249 | |
282 | |
250 | req->errorno = errno; |
283 | req->errorno = errno; |
251 | write (respipe[1], (void *)&req, sizeof (req)); |
284 | write (respipe[1], (void *)&req, sizeof (req)); |
252 | } |
285 | } |
… | |
… | |
264 | sigdelset (&fullsigset, SIGABRT); |
297 | sigdelset (&fullsigset, SIGABRT); |
265 | sigdelset (&fullsigset, SIGINT); |
298 | sigdelset (&fullsigset, SIGINT); |
266 | |
299 | |
267 | if (pipe (reqpipe) || pipe (respipe)) |
300 | if (pipe (reqpipe) || pipe (respipe)) |
268 | 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"); |
269 | |
305 | |
270 | if (fcntl (respipe[0], F_SETFL, O_NONBLOCK)) |
306 | if (fcntl (respipe[0], F_SETFL, O_NONBLOCK)) |
271 | croak ("cannot set result pipe to nonblocking mode"); |
307 | croak ("cannot set result pipe to nonblocking mode"); |
272 | } |
308 | } |
273 | |
309 | |
… | |
… | |
296 | fd_set rfd; |
332 | fd_set rfd; |
297 | FD_ZERO(&rfd); |
333 | FD_ZERO(&rfd); |
298 | FD_SET(respipe[0], &rfd); |
334 | FD_SET(respipe[0], &rfd); |
299 | |
335 | |
300 | select (respipe[0] + 1, &rfd, 0, 0, 0); |
336 | select (respipe[0] + 1, &rfd, 0, 0, 0); |
301 | poll_cb (); |
337 | poll_cb (aTHX); |
302 | } |
338 | } |
303 | |
339 | |
304 | void |
340 | void |
305 | aio_open(pathname,flags,mode,callback) |
341 | aio_open(pathname,flags,mode,callback) |
306 | SV * pathname |
342 | SV * pathname |