|
|
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> |
… | |
… | |
26 | |
28 | |
27 | typedef struct { |
29 | typedef struct { |
28 | char stack[STACKSIZE]; |
30 | char stack[STACKSIZE]; |
29 | } aio_thread; |
31 | } aio_thread; |
30 | |
32 | |
31 | typedef struct { |
33 | typedef struct aio_cb { |
|
|
34 | struct aio_cb *next; |
|
|
35 | |
32 | int type; |
36 | int type; |
33 | aio_thread *thread; |
37 | aio_thread *thread; |
34 | |
38 | |
35 | int fd; |
39 | int fd; |
36 | off_t offset; |
40 | off_t offset; |
… | |
… | |
49 | |
53 | |
50 | static int started; |
54 | static int started; |
51 | static int nreqs; |
55 | static int nreqs; |
52 | static int reqpipe[2], respipe[2]; |
56 | static int reqpipe[2], respipe[2]; |
53 | |
57 | |
|
|
58 | static aio_req qs, qe; /* queue start, queue end */ |
|
|
59 | |
54 | static int aio_proc(void *arg); |
60 | static int aio_proc(void *arg); |
55 | |
61 | |
56 | static void |
62 | static void |
57 | start_thread(void) |
63 | start_thread (void) |
58 | { |
64 | { |
59 | aio_thread *thr; |
65 | aio_thread *thr; |
60 | |
66 | |
61 | New (0, thr, 1, aio_thread); |
67 | New (0, thr, 1, aio_thread); |
62 | |
68 | |
… | |
… | |
68 | else |
74 | else |
69 | Safefree (thr); |
75 | Safefree (thr); |
70 | } |
76 | } |
71 | |
77 | |
72 | 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 |
73 | end_thread(void) |
104 | end_thread (void) |
74 | { |
105 | { |
75 | aio_req req; |
106 | aio_req req; |
76 | New (0, req, 1, aio_cb); |
107 | New (0, req, 1, aio_cb); |
77 | req->type = REQ_QUIT; |
108 | req->type = REQ_QUIT; |
78 | write (reqpipe[1], &req, sizeof (aio_req)); |
109 | |
|
|
110 | send_req (req); |
79 | } |
111 | } |
80 | |
112 | |
81 | static void |
113 | static void |
82 | send_req (aio_req req) |
114 | read_write (pTHX_ |
83 | { |
|
|
84 | nreqs++; |
|
|
85 | write (reqpipe[1], &req, sizeof (aio_req)); |
|
|
86 | } |
|
|
87 | |
|
|
88 | static void |
|
|
89 | read_write (pTHX_ int dowrite, int fd, off_t offset, size_t length, |
115 | int dowrite, int fd, off_t offset, size_t length, |
90 | SV *data, STRLEN dataoffset, SV*callback) |
116 | SV *data, STRLEN dataoffset, SV *callback) |
91 | { |
117 | { |
92 | aio_req req; |
118 | aio_req req; |
93 | STRLEN svlen; |
119 | STRLEN svlen; |
94 | char *svptr = SvPV (data, svlen); |
120 | char *svptr = SvPV (data, svlen); |
95 | |
121 | |
… | |
… | |
195 | } |
221 | } |
196 | |
222 | |
197 | Safefree (req); |
223 | Safefree (req); |
198 | } |
224 | } |
199 | |
225 | |
|
|
226 | if (qs) |
|
|
227 | send_reqs (); |
|
|
228 | |
200 | return count; |
229 | return count; |
201 | } |
230 | } |
202 | |
231 | |
203 | static sigset_t fullsigset; |
232 | static sigset_t fullsigset; |
204 | |
233 | |
205 | #undef errno |
234 | #undef errno |
206 | #include <asm/unistd.h> |
235 | #include <asm/unistd.h> |
207 | |
236 | |
208 | static int |
237 | static int |
209 | aio_proc(void *thr_arg) |
238 | aio_proc (void *thr_arg) |
210 | { |
239 | { |
211 | aio_thread *thr = thr_arg; |
240 | aio_thread *thr = thr_arg; |
212 | aio_req req; |
241 | aio_req req; |
213 | int errno; |
242 | int errno; |
214 | |
243 | |
… | |
… | |
233 | while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
262 | while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
234 | { |
263 | { |
235 | req->thread = thr; |
264 | req->thread = thr; |
236 | errno = 0; /* strictly unnecessary */ |
265 | errno = 0; /* strictly unnecessary */ |
237 | |
266 | |
238 | if (req->type == REQ_READ) |
267 | switch (req->type) |
239 | req->result = pread64 (req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); |
|
|
240 | else if (req->type == REQ_WRITE) |
|
|
241 | req->result = pwrite64(req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); |
|
|
242 | else if (req->type == REQ_OPEN) |
|
|
243 | req->result = open (req->dataptr, req->fd, req->mode); |
|
|
244 | else if (req->type == REQ_CLOSE) |
|
|
245 | req->result = close (req->fd); |
|
|
246 | else if (req->type == REQ_STAT) |
|
|
247 | req->result = stat64 (req->dataptr, req->statdata); |
|
|
248 | else if (req->type == REQ_LSTAT) |
|
|
249 | req->result = lstat64 (req->dataptr, req->statdata); |
|
|
250 | else if (req->type == REQ_FSTAT) |
|
|
251 | req->result = fstat64 (req->fd, req->statdata); |
|
|
252 | else |
|
|
253 | { |
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: |
254 | write (respipe[1], (void *)&req, sizeof (req)); |
279 | write (respipe[1], (void *)&req, sizeof (req)); |
255 | break; |
280 | return 0; |
256 | } |
281 | } |
257 | |
282 | |
258 | req->errorno = errno; |
283 | req->errorno = errno; |
259 | write (respipe[1], (void *)&req, sizeof (req)); |
284 | write (respipe[1], (void *)&req, sizeof (req)); |
260 | } |
285 | } |
… | |
… | |
272 | sigdelset (&fullsigset, SIGABRT); |
297 | sigdelset (&fullsigset, SIGABRT); |
273 | sigdelset (&fullsigset, SIGINT); |
298 | sigdelset (&fullsigset, SIGINT); |
274 | |
299 | |
275 | if (pipe (reqpipe) || pipe (respipe)) |
300 | if (pipe (reqpipe) || pipe (respipe)) |
276 | 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"); |
277 | |
305 | |
278 | if (fcntl (respipe[0], F_SETFL, O_NONBLOCK)) |
306 | if (fcntl (respipe[0], F_SETFL, O_NONBLOCK)) |
279 | croak ("cannot set result pipe to nonblocking mode"); |
307 | croak ("cannot set result pipe to nonblocking mode"); |
280 | } |
308 | } |
281 | |
309 | |
… | |
… | |
304 | fd_set rfd; |
332 | fd_set rfd; |
305 | FD_ZERO(&rfd); |
333 | FD_ZERO(&rfd); |
306 | FD_SET(respipe[0], &rfd); |
334 | FD_SET(respipe[0], &rfd); |
307 | |
335 | |
308 | select (respipe[0] + 1, &rfd, 0, 0, 0); |
336 | select (respipe[0] + 1, &rfd, 0, 0, 0); |
309 | poll_cb (); |
337 | poll_cb (aTHX); |
310 | } |
338 | } |
311 | |
339 | |
312 | void |
340 | void |
313 | aio_open(pathname,flags,mode,callback) |
341 | aio_open(pathname,flags,mode,callback) |
314 | SV * pathname |
342 | SV * pathname |