… | |
… | |
22 | # define __NR_pwrite64 __NR_pwrite |
22 | # define __NR_pwrite64 __NR_pwrite |
23 | #endif |
23 | #endif |
24 | |
24 | |
25 | #define STACKSIZE 1024 /* yeah */ |
25 | #define STACKSIZE 1024 /* yeah */ |
26 | |
26 | |
27 | 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 | }; |
28 | |
32 | |
29 | typedef struct { |
33 | typedef struct { |
30 | char stack[STACKSIZE]; |
34 | char stack[STACKSIZE]; |
31 | } aio_thread; |
35 | } aio_thread; |
32 | |
36 | |
33 | typedef struct { |
37 | typedef struct aio_cb { |
|
|
38 | struct aio_cb *next; |
|
|
39 | |
34 | int type; |
40 | int type; |
35 | aio_thread *thread; |
41 | aio_thread *thread; |
36 | |
42 | |
37 | int fd; |
43 | int fd; |
38 | off_t offset; |
44 | off_t offset; |
… | |
… | |
51 | |
57 | |
52 | static int started; |
58 | static int started; |
53 | static int nreqs; |
59 | static int nreqs; |
54 | static int reqpipe[2], respipe[2]; |
60 | static int reqpipe[2], respipe[2]; |
55 | |
61 | |
|
|
62 | static aio_req qs, qe; /* queue start, queue end */ |
|
|
63 | |
56 | static int aio_proc(void *arg); |
64 | static int aio_proc(void *arg); |
57 | |
65 | |
58 | static void |
66 | static void |
59 | start_thread(void) |
67 | start_thread (void) |
60 | { |
68 | { |
61 | aio_thread *thr; |
69 | aio_thread *thr; |
62 | |
70 | |
63 | New (0, thr, 1, aio_thread); |
71 | New (0, thr, 1, aio_thread); |
64 | |
72 | |
65 | if (clone (aio_proc, |
73 | if (clone (aio_proc, |
66 | &(thr->stack[STACKSIZE]), |
74 | &(thr->stack[STACKSIZE]), |
67 | CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND, |
75 | CLONE_VM|CLONE_FS|CLONE_FILES, |
68 | thr) >= 0) |
76 | thr) >= 0) |
69 | started++; |
77 | started++; |
70 | else |
78 | else |
71 | Safefree (thr); |
79 | Safefree (thr); |
72 | } |
80 | } |
73 | |
81 | |
74 | 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 |
75 | end_thread(void) |
108 | end_thread (void) |
76 | { |
109 | { |
77 | aio_req req; |
110 | aio_req req; |
78 | New (0, req, 1, aio_cb); |
111 | New (0, req, 1, aio_cb); |
79 | req->type = REQ_QUIT; |
112 | req->type = REQ_QUIT; |
80 | write (reqpipe[1], &req, sizeof (aio_req)); |
|
|
81 | } |
|
|
82 | |
113 | |
83 | static void |
114 | send_req (req); |
84 | send_req (aio_req req) |
|
|
85 | { |
|
|
86 | nreqs++; |
|
|
87 | write (reqpipe[1], &req, sizeof (aio_req)); |
|
|
88 | } |
115 | } |
89 | |
116 | |
90 | static void |
117 | static void |
91 | read_write (pTHX_ |
118 | read_write (pTHX_ |
92 | int dowrite, int fd, off_t offset, size_t length, |
119 | int dowrite, int fd, off_t offset, size_t length, |
93 | SV *data, STRLEN dataoffset, SV*callback) |
120 | SV *data, STRLEN dataoffset, SV *callback) |
94 | { |
121 | { |
95 | aio_req req; |
122 | aio_req req; |
96 | STRLEN svlen; |
123 | STRLEN svlen; |
97 | char *svptr = SvPV (data, svlen); |
124 | char *svptr = SvPV (data, svlen); |
98 | |
125 | |
… | |
… | |
198 | } |
225 | } |
199 | |
226 | |
200 | Safefree (req); |
227 | Safefree (req); |
201 | } |
228 | } |
202 | |
229 | |
|
|
230 | if (qs) |
|
|
231 | send_reqs (); |
|
|
232 | |
203 | return count; |
233 | return count; |
204 | } |
234 | } |
205 | |
235 | |
206 | static sigset_t fullsigset; |
236 | static sigset_t fullsigset; |
207 | |
237 | |
208 | #undef errno |
238 | #undef errno |
209 | #include <asm/unistd.h> |
239 | #include <asm/unistd.h> |
|
|
240 | #include <sys/prctl.h> |
210 | |
241 | |
211 | static int |
242 | static int |
212 | aio_proc(void *thr_arg) |
243 | aio_proc (void *thr_arg) |
213 | { |
244 | { |
214 | aio_thread *thr = thr_arg; |
245 | aio_thread *thr = thr_arg; |
215 | aio_req req; |
246 | aio_req req; |
216 | int errno; |
247 | int errno; |
217 | |
248 | |
… | |
… | |
228 | |
259 | |
229 | _syscall2(int,stat64, const char *, filename, struct stat64 *, buf) |
260 | _syscall2(int,stat64, const char *, filename, struct stat64 *, buf) |
230 | _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf) |
261 | _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf) |
231 | _syscall2(int,fstat64, int, fd, struct stat64 *, buf) |
262 | _syscall2(int,fstat64, int, fd, struct stat64 *, buf) |
232 | |
263 | |
|
|
264 | _syscall1(int,unlink, char *, filename); |
|
|
265 | |
233 | sigprocmask (SIG_SETMASK, &fullsigset, 0); |
266 | sigprocmask (SIG_SETMASK, &fullsigset, 0); |
|
|
267 | prctl (PR_SET_PDEATHSIG, SIGKILL); |
234 | |
268 | |
235 | /* then loop */ |
269 | /* then loop */ |
236 | while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
270 | while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
237 | { |
271 | { |
238 | req->thread = thr; |
272 | req->thread = thr; |
239 | errno = 0; /* strictly unnecessary */ |
273 | errno = 0; /* strictly unnecessary */ |
240 | |
274 | |
241 | if (req->type == REQ_READ) |
275 | switch (req->type) |
242 | req->result = pread64 (req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); |
|
|
243 | else if (req->type == REQ_WRITE) |
|
|
244 | req->result = pwrite64(req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); |
|
|
245 | else if (req->type == REQ_OPEN) |
|
|
246 | req->result = open (req->dataptr, req->fd, req->mode); |
|
|
247 | else if (req->type == REQ_CLOSE) |
|
|
248 | req->result = close (req->fd); |
|
|
249 | else if (req->type == REQ_STAT) |
|
|
250 | req->result = stat64 (req->dataptr, req->statdata); |
|
|
251 | else if (req->type == REQ_LSTAT) |
|
|
252 | req->result = lstat64 (req->dataptr, req->statdata); |
|
|
253 | else if (req->type == REQ_FSTAT) |
|
|
254 | req->result = fstat64 (req->fd, req->statdata); |
|
|
255 | else |
|
|
256 | { |
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: |
257 | write (respipe[1], (void *)&req, sizeof (req)); |
288 | write (respipe[1], (void *)&req, sizeof (req)); |
258 | break; |
289 | return 0; |
259 | } |
290 | } |
260 | |
291 | |
261 | req->errorno = errno; |
292 | req->errorno = errno; |
262 | write (respipe[1], (void *)&req, sizeof (req)); |
293 | write (respipe[1], (void *)&req, sizeof (req)); |
263 | } |
294 | } |
… | |
… | |
275 | sigdelset (&fullsigset, SIGABRT); |
306 | sigdelset (&fullsigset, SIGABRT); |
276 | sigdelset (&fullsigset, SIGINT); |
307 | sigdelset (&fullsigset, SIGINT); |
277 | |
308 | |
278 | if (pipe (reqpipe) || pipe (respipe)) |
309 | if (pipe (reqpipe) || pipe (respipe)) |
279 | 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"); |
280 | |
314 | |
281 | if (fcntl (respipe[0], F_SETFL, O_NONBLOCK)) |
315 | if (fcntl (respipe[0], F_SETFL, O_NONBLOCK)) |
282 | croak ("cannot set result pipe to nonblocking mode"); |
316 | croak ("cannot set result pipe to nonblocking mode"); |
283 | } |
317 | } |
284 | |
318 | |
… | |
… | |
413 | |
447 | |
414 | req->callback = SvREFCNT_inc (callback); |
448 | req->callback = SvREFCNT_inc (callback); |
415 | |
449 | |
416 | send_req (req); |
450 | send_req (req); |
417 | |
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 | |
418 | int |
472 | int |
419 | poll_fileno() |
473 | poll_fileno() |
420 | PROTOTYPE: |
474 | PROTOTYPE: |
421 | CODE: |
475 | CODE: |
422 | RETVAL = respipe[0]; |
476 | RETVAL = respipe[0]; |