|
|
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> |
… | |
… | |
20 | # define __NR_pwrite64 __NR_pwrite |
22 | # define __NR_pwrite64 __NR_pwrite |
21 | #endif |
23 | #endif |
22 | |
24 | |
23 | #define STACKSIZE 1024 /* yeah */ |
25 | #define STACKSIZE 1024 /* yeah */ |
24 | |
26 | |
25 | enum { REQ_QUIT, REQ_OPEN, REQ_CLOSE, REQ_READ, REQ_WRITE, REQ_STAT, REQ_LSTAT, REQ_FSTAT}; |
27 | enum { |
|
|
28 | REQ_QUIT, |
|
|
29 | REQ_OPEN, REQ_CLOSE, REQ_READ, REQ_WRITE, |
|
|
30 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_UNLINK |
|
|
31 | }; |
26 | |
32 | |
27 | typedef struct { |
33 | typedef struct { |
28 | char stack[STACKSIZE]; |
34 | char stack[STACKSIZE]; |
29 | } aio_thread; |
35 | } aio_thread; |
30 | |
36 | |
31 | typedef struct { |
37 | typedef struct aio_cb { |
|
|
38 | struct aio_cb *next; |
|
|
39 | |
32 | int type; |
40 | int type; |
33 | aio_thread *thread; |
41 | aio_thread *thread; |
34 | |
42 | |
35 | int fd; |
43 | int fd; |
36 | off_t offset; |
44 | off_t offset; |
… | |
… | |
49 | |
57 | |
50 | static int started; |
58 | static int started; |
51 | static int nreqs; |
59 | static int nreqs; |
52 | static int reqpipe[2], respipe[2]; |
60 | static int reqpipe[2], respipe[2]; |
53 | |
61 | |
|
|
62 | static aio_req qs, qe; /* queue start, queue end */ |
|
|
63 | |
54 | static int aio_proc(void *arg); |
64 | static int aio_proc(void *arg); |
55 | |
65 | |
56 | static void |
66 | static void |
57 | start_thread(void) |
67 | start_thread (void) |
58 | { |
68 | { |
59 | aio_thread *thr; |
69 | aio_thread *thr; |
60 | |
70 | |
61 | New (0, thr, 1, aio_thread); |
71 | New (0, thr, 1, aio_thread); |
62 | |
72 | |
63 | if (clone (aio_proc, |
73 | if (clone (aio_proc, |
64 | &(thr->stack[STACKSIZE]), |
74 | &(thr->stack[STACKSIZE]), |
65 | CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND, |
75 | CLONE_VM|CLONE_FS|CLONE_FILES, |
66 | thr) >= 0) |
76 | thr) >= 0) |
67 | started++; |
77 | started++; |
68 | else |
78 | else |
69 | Safefree (thr); |
79 | Safefree (thr); |
70 | } |
80 | } |
71 | |
81 | |
72 | static void |
82 | static void |
|
|
83 | send_reqs (void) |
|
|
84 | { |
|
|
85 | /* this write is atomic */ |
|
|
86 | while (qs && write (reqpipe[1], &qs, sizeof qs) == sizeof qs) |
|
|
87 | { |
|
|
88 | qs = qs->next; |
|
|
89 | if (!qs) qe = 0; |
|
|
90 | } |
|
|
91 | } |
|
|
92 | |
|
|
93 | static void |
|
|
94 | send_req (aio_req req) |
|
|
95 | { |
|
|
96 | nreqs++; |
|
|
97 | req->next = 0; |
|
|
98 | |
|
|
99 | if (qe) |
|
|
100 | qe->next = req; |
|
|
101 | else |
|
|
102 | qe = qs = req; |
|
|
103 | |
|
|
104 | send_reqs (); |
|
|
105 | } |
|
|
106 | |
|
|
107 | static void |
73 | end_thread(void) |
108 | end_thread (void) |
74 | { |
109 | { |
75 | aio_req req; |
110 | aio_req req; |
76 | New (0, req, 1, aio_cb); |
111 | New (0, req, 1, aio_cb); |
77 | req->type = REQ_QUIT; |
112 | req->type = REQ_QUIT; |
78 | write (reqpipe[1], &req, sizeof (aio_req)); |
113 | |
|
|
114 | send_req (req); |
79 | } |
115 | } |
80 | |
116 | |
81 | static void |
117 | static void |
82 | send_req (aio_req req) |
118 | 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, |
119 | int dowrite, int fd, off_t offset, size_t length, |
90 | SV *data, STRLEN dataoffset, SV*callback) |
120 | SV *data, STRLEN dataoffset, SV *callback) |
91 | { |
121 | { |
92 | aio_req req; |
122 | aio_req req; |
93 | STRLEN svlen; |
123 | STRLEN svlen; |
94 | char *svptr = SvPV (data, svlen); |
124 | char *svptr = SvPV (data, svlen); |
95 | |
125 | |
… | |
… | |
195 | } |
225 | } |
196 | |
226 | |
197 | Safefree (req); |
227 | Safefree (req); |
198 | } |
228 | } |
199 | |
229 | |
|
|
230 | if (qs) |
|
|
231 | send_reqs (); |
|
|
232 | |
200 | return count; |
233 | return count; |
201 | } |
234 | } |
202 | |
235 | |
203 | static sigset_t fullsigset; |
236 | static sigset_t fullsigset; |
204 | |
237 | |
205 | #undef errno |
238 | #undef errno |
206 | #include <asm/unistd.h> |
239 | #include <asm/unistd.h> |
|
|
240 | #include <sys/prctl.h> |
207 | |
241 | |
208 | static int |
242 | static int |
209 | aio_proc(void *thr_arg) |
243 | aio_proc (void *thr_arg) |
210 | { |
244 | { |
211 | aio_thread *thr = thr_arg; |
245 | aio_thread *thr = thr_arg; |
212 | aio_req req; |
246 | aio_req req; |
213 | int errno; |
247 | int errno; |
214 | |
248 | |
… | |
… | |
225 | |
259 | |
226 | _syscall2(int,stat64, const char *, filename, struct stat64 *, buf) |
260 | _syscall2(int,stat64, const char *, filename, struct stat64 *, buf) |
227 | _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf) |
261 | _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf) |
228 | _syscall2(int,fstat64, int, fd, struct stat64 *, buf) |
262 | _syscall2(int,fstat64, int, fd, struct stat64 *, buf) |
229 | |
263 | |
|
|
264 | _syscall1(int,unlink, char *, filename); |
|
|
265 | |
230 | sigprocmask (SIG_SETMASK, &fullsigset, 0); |
266 | sigprocmask (SIG_SETMASK, &fullsigset, 0); |
|
|
267 | prctl (PR_SET_PDEATHSIG, SIGKILL); |
231 | |
268 | |
232 | /* then loop */ |
269 | /* then loop */ |
233 | while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
270 | while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
234 | { |
271 | { |
235 | req->thread = thr; |
272 | req->thread = thr; |
236 | errno = 0; /* strictly unnecessary */ |
273 | errno = 0; /* strictly unnecessary */ |
237 | |
274 | |
238 | if (req->type == REQ_READ) |
275 | 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 | { |
276 | { |
|
|
277 | case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); break; |
|
|
278 | case REQ_WRITE: req->result = pwrite64(req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); break; |
|
|
279 | case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
|
|
280 | case REQ_CLOSE: req->result = close (req->fd); break; |
|
|
281 | case REQ_STAT: req->result = stat64 (req->dataptr, req->statdata); break; |
|
|
282 | case REQ_LSTAT: req->result = lstat64 (req->dataptr, req->statdata); break; |
|
|
283 | case REQ_FSTAT: req->result = fstat64 (req->fd, req->statdata); break; |
|
|
284 | case REQ_UNLINK: req->result = unlink (req->dataptr); break; |
|
|
285 | |
|
|
286 | case REQ_QUIT: |
|
|
287 | default: |
254 | write (respipe[1], (void *)&req, sizeof (req)); |
288 | write (respipe[1], (void *)&req, sizeof (req)); |
255 | break; |
289 | return 0; |
256 | } |
290 | } |
257 | |
291 | |
258 | req->errorno = errno; |
292 | req->errorno = errno; |
259 | write (respipe[1], (void *)&req, sizeof (req)); |
293 | write (respipe[1], (void *)&req, sizeof (req)); |
260 | } |
294 | } |
… | |
… | |
272 | sigdelset (&fullsigset, SIGABRT); |
306 | sigdelset (&fullsigset, SIGABRT); |
273 | sigdelset (&fullsigset, SIGINT); |
307 | sigdelset (&fullsigset, SIGINT); |
274 | |
308 | |
275 | if (pipe (reqpipe) || pipe (respipe)) |
309 | if (pipe (reqpipe) || pipe (respipe)) |
276 | croak ("unable to initialize request or result pipe"); |
310 | croak ("unable to initialize request or result pipe"); |
|
|
311 | |
|
|
312 | if (fcntl (reqpipe[1], F_SETFL, O_NONBLOCK)) |
|
|
313 | croak ("cannot set result pipe to nonblocking mode"); |
277 | |
314 | |
278 | if (fcntl (respipe[0], F_SETFL, O_NONBLOCK)) |
315 | if (fcntl (respipe[0], F_SETFL, O_NONBLOCK)) |
279 | croak ("cannot set result pipe to nonblocking mode"); |
316 | croak ("cannot set result pipe to nonblocking mode"); |
280 | } |
317 | } |
281 | |
318 | |
… | |
… | |
304 | fd_set rfd; |
341 | fd_set rfd; |
305 | FD_ZERO(&rfd); |
342 | FD_ZERO(&rfd); |
306 | FD_SET(respipe[0], &rfd); |
343 | FD_SET(respipe[0], &rfd); |
307 | |
344 | |
308 | select (respipe[0] + 1, &rfd, 0, 0, 0); |
345 | select (respipe[0] + 1, &rfd, 0, 0, 0); |
309 | poll_cb (); |
346 | poll_cb (aTHX); |
310 | } |
347 | } |
311 | |
348 | |
312 | void |
349 | void |
313 | aio_open(pathname,flags,mode,callback) |
350 | aio_open(pathname,flags,mode,callback) |
314 | SV * pathname |
351 | SV * pathname |
… | |
… | |
410 | |
447 | |
411 | req->callback = SvREFCNT_inc (callback); |
448 | req->callback = SvREFCNT_inc (callback); |
412 | |
449 | |
413 | send_req (req); |
450 | send_req (req); |
414 | |
451 | |
|
|
452 | void |
|
|
453 | aio_unlink(pathname,callback) |
|
|
454 | SV * pathname |
|
|
455 | SV * callback |
|
|
456 | PROTOTYPE: $$ |
|
|
457 | CODE: |
|
|
458 | aio_req req; |
|
|
459 | |
|
|
460 | Newz (0, req, 1, aio_cb); |
|
|
461 | |
|
|
462 | if (!req) |
|
|
463 | croak ("out of memory during aio_req allocation"); |
|
|
464 | |
|
|
465 | req->type = REQ_UNLINK; |
|
|
466 | req->data = newSVsv (pathname); |
|
|
467 | req->dataptr = SvPV_nolen (req->data); |
|
|
468 | req->callback = SvREFCNT_inc (callback); |
|
|
469 | |
|
|
470 | send_req (req); |
|
|
471 | |
415 | int |
472 | int |
416 | poll_fileno() |
473 | poll_fileno() |
417 | PROTOTYPE: |
474 | PROTOTYPE: |
418 | CODE: |
475 | CODE: |
419 | RETVAL = respipe[0]; |
476 | RETVAL = respipe[0]; |