… | |
… | |
9 | #include <unistd.h> |
9 | #include <unistd.h> |
10 | #include <fcntl.h> |
10 | #include <fcntl.h> |
11 | #include <signal.h> |
11 | #include <signal.h> |
12 | #include <sched.h> |
12 | #include <sched.h> |
13 | |
13 | |
14 | 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 ;) */ |
15 | 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 ;) */ |
16 | 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 ;) */ |
17 | |
17 | |
18 | #ifndef __NR_pread64 |
18 | #define STACKSIZE (128 * sizeof (long)) /* yeah */ |
19 | # define __NR_pread64 __NR_pread |
|
|
20 | #endif |
|
|
21 | #ifndef __NR_pwrite64 |
|
|
22 | # define __NR_pwrite64 __NR_pwrite |
|
|
23 | #endif |
|
|
24 | |
19 | |
25 | #define STACKSIZE 1024 /* yeah */ |
20 | enum { |
26 | |
21 | REQ_QUIT, |
27 | enum { REQ_QUIT, REQ_OPEN, REQ_CLOSE, REQ_READ, REQ_WRITE, REQ_STAT, REQ_LSTAT, REQ_FSTAT}; |
22 | REQ_OPEN, REQ_CLOSE, REQ_READ, REQ_WRITE, |
|
|
23 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_UNLINK |
|
|
24 | }; |
28 | |
25 | |
29 | typedef struct { |
26 | typedef struct { |
30 | char stack[STACKSIZE]; |
27 | char stack[STACKSIZE]; |
31 | } aio_thread; |
28 | } aio_thread; |
32 | |
29 | |
… | |
… | |
44 | int errorno; |
41 | int errorno; |
45 | SV *data, *callback; |
42 | SV *data, *callback; |
46 | void *dataptr; |
43 | void *dataptr; |
47 | STRLEN dataoffset; |
44 | STRLEN dataoffset; |
48 | |
45 | |
49 | struct stat64 *statdata; |
46 | Stat_t *statdata; |
50 | } aio_cb; |
47 | } aio_cb; |
51 | |
48 | |
52 | typedef aio_cb *aio_req; |
49 | typedef aio_cb *aio_req; |
53 | |
50 | |
54 | static int started; |
51 | static int started; |
… | |
… | |
65 | aio_thread *thr; |
62 | aio_thread *thr; |
66 | |
63 | |
67 | New (0, thr, 1, aio_thread); |
64 | New (0, thr, 1, aio_thread); |
68 | |
65 | |
69 | if (clone (aio_proc, |
66 | if (clone (aio_proc, |
70 | &(thr->stack[STACKSIZE]), |
67 | &(thr->stack[STACKSIZE - sizeof (long)]), |
71 | CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND, |
68 | CLONE_VM|CLONE_FS|CLONE_FILES, |
72 | thr) >= 0) |
69 | thr) >= 0) |
73 | started++; |
70 | started++; |
74 | else |
71 | else |
75 | Safefree (thr); |
72 | Safefree (thr); |
76 | } |
73 | } |
… | |
… | |
91 | { |
88 | { |
92 | nreqs++; |
89 | nreqs++; |
93 | req->next = 0; |
90 | req->next = 0; |
94 | |
91 | |
95 | if (qe) |
92 | if (qe) |
|
|
93 | { |
96 | qe->next = req; |
94 | qe->next = req; |
|
|
95 | qe = req; |
|
|
96 | } |
97 | else |
97 | else |
98 | qe = qs = req; |
98 | qe = qs = req; |
99 | |
99 | |
100 | send_reqs (); |
100 | send_reqs (); |
101 | } |
101 | } |
… | |
… | |
157 | req->callback = SvREFCNT_inc (callback); |
157 | req->callback = SvREFCNT_inc (callback); |
158 | |
158 | |
159 | send_req (req); |
159 | send_req (req); |
160 | } |
160 | } |
161 | |
161 | |
|
|
162 | static void |
|
|
163 | poll_wait () |
|
|
164 | { |
|
|
165 | fd_set rfd; |
|
|
166 | FD_ZERO(&rfd); |
|
|
167 | FD_SET(respipe[0], &rfd); |
|
|
168 | |
|
|
169 | select (respipe[0] + 1, &rfd, 0, 0, 0); |
|
|
170 | } |
|
|
171 | |
162 | static int |
172 | static int |
163 | poll_cb (pTHX) |
173 | poll_cb (pTHX) |
164 | { |
174 | { |
165 | dSP; |
175 | dSP; |
166 | int count = 0; |
176 | int count = 0; |
167 | aio_req req; |
177 | aio_req req; |
168 | |
178 | |
169 | while (read (respipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
179 | while (read (respipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
170 | { |
180 | { |
|
|
181 | nreqs--; |
|
|
182 | |
171 | if (req->type == REQ_QUIT) |
183 | if (req->type == REQ_QUIT) |
172 | { |
184 | { |
173 | Safefree (req->thread); |
185 | Safefree (req->thread); |
174 | started--; |
186 | started--; |
175 | } |
187 | } |
… | |
… | |
185 | if (req->data) |
197 | if (req->data) |
186 | SvREFCNT_dec (req->data); |
198 | SvREFCNT_dec (req->data); |
187 | |
199 | |
188 | if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) |
200 | if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) |
189 | { |
201 | { |
190 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
202 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
191 | PL_laststatval = req->result; |
203 | PL_laststatval = req->result; |
192 | PL_statcache.st_dev = req->statdata->st_dev; |
204 | PL_statcache = *(req->statdata); |
193 | PL_statcache.st_ino = req->statdata->st_ino; |
|
|
194 | PL_statcache.st_mode = req->statdata->st_mode; |
|
|
195 | PL_statcache.st_nlink = req->statdata->st_nlink; |
|
|
196 | PL_statcache.st_uid = req->statdata->st_uid; |
|
|
197 | PL_statcache.st_gid = req->statdata->st_gid; |
|
|
198 | PL_statcache.st_rdev = req->statdata->st_rdev; |
|
|
199 | PL_statcache.st_size = req->statdata->st_size; |
|
|
200 | PL_statcache.st_atime = req->statdata->st_atime; |
|
|
201 | PL_statcache.st_mtime = req->statdata->st_mtime; |
|
|
202 | PL_statcache.st_ctime = req->statdata->st_ctime; |
|
|
203 | PL_statcache.st_blksize = req->statdata->st_blksize; |
|
|
204 | PL_statcache.st_blocks = req->statdata->st_blocks; |
|
|
205 | |
205 | |
206 | Safefree (req->statdata); |
206 | Safefree (req->statdata); |
207 | } |
207 | } |
208 | |
208 | |
209 | PUSHMARK (SP); |
209 | PUSHMARK (SP); |
… | |
… | |
214 | |
214 | |
215 | if (req->callback) |
215 | if (req->callback) |
216 | SvREFCNT_dec (req->callback); |
216 | SvREFCNT_dec (req->callback); |
217 | |
217 | |
218 | errno = errorno; |
218 | errno = errorno; |
219 | nreqs--; |
|
|
220 | count++; |
219 | count++; |
221 | } |
220 | } |
222 | |
221 | |
223 | Safefree (req); |
222 | Safefree (req); |
224 | } |
223 | } |
… | |
… | |
231 | |
230 | |
232 | static sigset_t fullsigset; |
231 | static sigset_t fullsigset; |
233 | |
232 | |
234 | #undef errno |
233 | #undef errno |
235 | #include <asm/unistd.h> |
234 | #include <asm/unistd.h> |
|
|
235 | #include <sys/prctl.h> |
|
|
236 | |
|
|
237 | #define COPY_STATDATA \ |
|
|
238 | req->statdata->st_dev = statdata.st_dev; \ |
|
|
239 | req->statdata->st_ino = statdata.st_ino; \ |
|
|
240 | req->statdata->st_mode = statdata.st_mode; \ |
|
|
241 | req->statdata->st_nlink = statdata.st_nlink; \ |
|
|
242 | req->statdata->st_uid = statdata.st_uid; \ |
|
|
243 | req->statdata->st_gid = statdata.st_gid; \ |
|
|
244 | req->statdata->st_rdev = statdata.st_rdev; \ |
|
|
245 | req->statdata->st_size = statdata.st_size; \ |
|
|
246 | req->statdata->st_atime = statdata.st_atime; \ |
|
|
247 | req->statdata->st_mtime = statdata.st_mtime; \ |
|
|
248 | req->statdata->st_ctime = statdata.st_ctime; \ |
|
|
249 | req->statdata->st_blksize = statdata.st_blksize; \ |
|
|
250 | req->statdata->st_blocks = statdata.st_blocks; \ |
236 | |
251 | |
237 | static int |
252 | static int |
238 | aio_proc (void *thr_arg) |
253 | aio_proc (void *thr_arg) |
239 | { |
254 | { |
240 | aio_thread *thr = thr_arg; |
255 | aio_thread *thr = thr_arg; |
241 | aio_req req; |
256 | aio_req req; |
242 | int errno; |
257 | int errno; |
243 | |
258 | |
244 | /* this is very much x86 and kernel-specific :(:(:( */ |
259 | /* this is very much kernel-specific :(:(:( */ |
245 | /* we rely on gcc's ability to create closures. */ |
260 | /* we rely on gcc's ability to create closures. */ |
246 | _syscall3(int,read,int,fd,char *,buf,size_t,count) |
261 | _syscall3(int,read,int,fd,char *,buf,size_t,count) |
247 | _syscall3(int,write,int,fd,char *,buf,size_t,count) |
262 | _syscall3(int,write,int,fd,char *,buf,size_t,count) |
248 | |
263 | |
249 | _syscall3(int,open,char *,pathname,int,flags,mode_t,mode) |
264 | _syscall3(int,open,char *,pathname,int,flags,mode_t,mode) |
250 | _syscall1(int,close,int,fd) |
265 | _syscall1(int,close,int,fd) |
251 | |
266 | |
|
|
267 | #ifdef __NR_pread64 |
252 | _syscall5(int,pread64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi) |
268 | _syscall5(int,pread64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi) |
253 | _syscall5(int,pwrite64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi) |
269 | _syscall5(int,pwrite64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi) |
|
|
270 | #elif __NR_pread |
|
|
271 | _syscall4(int,pread,int,fd,char *,buf,size_t,count,offset_t,offset) |
|
|
272 | _syscall4(int,pwrite,int,fd,char *,buf,size_t,count,offset_t,offset) |
|
|
273 | #else |
|
|
274 | # error "neither pread nor pread64 defined" |
|
|
275 | #endif |
254 | |
276 | |
|
|
277 | |
|
|
278 | #ifdef __NR_stat64 |
255 | _syscall2(int,stat64, const char *, filename, struct stat64 *, buf) |
279 | _syscall2(int,stat64, const char *, filename, struct stat64 *, buf) |
256 | _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf) |
280 | _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf) |
257 | _syscall2(int,fstat64, int, fd, struct stat64 *, buf) |
281 | _syscall2(int,fstat64, int, fd, struct stat64 *, buf) |
|
|
282 | #elif __NR_stat |
|
|
283 | _syscall2(int,stat, const char *, filename, struct stat *, buf) |
|
|
284 | _syscall2(int,lstat, const char *, filename, struct stat *, buf) |
|
|
285 | _syscall2(int,fstat, int, fd, struct stat *, buf) |
|
|
286 | #else |
|
|
287 | # error "neither stat64 nor stat defined" |
|
|
288 | #endif |
|
|
289 | |
|
|
290 | _syscall1(int,unlink, char *, filename); |
258 | |
291 | |
259 | sigprocmask (SIG_SETMASK, &fullsigset, 0); |
292 | sigprocmask (SIG_SETMASK, &fullsigset, 0); |
|
|
293 | prctl (PR_SET_PDEATHSIG, SIGKILL); |
260 | |
294 | |
261 | /* then loop */ |
295 | /* then loop */ |
262 | while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
296 | while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
263 | { |
297 | { |
264 | req->thread = thr; |
298 | req->thread = thr; |
265 | errno = 0; /* strictly unnecessary */ |
299 | errno = 0; /* strictly unnecessary */ |
266 | |
300 | |
267 | switch (req->type) |
301 | switch (req->type) |
268 | { |
302 | { |
|
|
303 | #ifdef __NR_pread64 |
269 | case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); break; |
304 | 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; |
305 | case REQ_WRITE: req->result = pwrite64(req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); break; |
|
|
306 | #else |
|
|
307 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
|
|
308 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
|
|
309 | #endif |
|
|
310 | #ifdef __NR_stat64 |
|
|
311 | struct stat64 statdata; |
|
|
312 | case REQ_STAT: req->result = stat64 (req->dataptr, &statdata); COPY_STATDATA; break; |
|
|
313 | case REQ_LSTAT: req->result = lstat64 (req->dataptr, &statdata); COPY_STATDATA; break; |
|
|
314 | case REQ_FSTAT: req->result = fstat64 (req->fd, &statdata); COPY_STATDATA; break; |
|
|
315 | #else |
|
|
316 | struct stat statdata; |
|
|
317 | case REQ_STAT: req->result = stat (req->dataptr, &statdata); COPY_STATDATA; break; |
|
|
318 | case REQ_LSTAT: req->result = lstat (req->dataptr, &statdata); COPY_STATDATA; break; |
|
|
319 | case REQ_FSTAT: req->result = fstat (req->fd, &statdata); COPY_STATDATA; break; |
|
|
320 | #endif |
271 | case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
321 | case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
272 | case REQ_CLOSE: req->result = close (req->fd); break; |
322 | 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; |
323 | case REQ_UNLINK: req->result = unlink (req->dataptr); break; |
275 | case REQ_FSTAT: req->result = fstat64 (req->fd, req->statdata); break; |
|
|
276 | |
324 | |
277 | case REQ_QUIT: |
325 | case REQ_QUIT: |
278 | default: |
326 | default: |
279 | write (respipe[1], (void *)&req, sizeof (req)); |
327 | write (respipe[1], (void *)&req, sizeof (req)); |
280 | return 0; |
328 | return 0; |
… | |
… | |
327 | cur--; |
375 | cur--; |
328 | } |
376 | } |
329 | |
377 | |
330 | while (started > nthreads) |
378 | while (started > nthreads) |
331 | { |
379 | { |
332 | fd_set rfd; |
380 | poll_wait (); |
333 | FD_ZERO(&rfd); |
|
|
334 | FD_SET(respipe[0], &rfd); |
|
|
335 | |
|
|
336 | select (respipe[0] + 1, &rfd, 0, 0, 0); |
|
|
337 | poll_cb (aTHX); |
381 | poll_cb (aTHX); |
338 | } |
382 | } |
339 | |
383 | |
340 | void |
384 | void |
341 | aio_open(pathname,flags,mode,callback) |
385 | aio_open(pathname,flags,mode,callback) |
… | |
… | |
417 | Newz (0, req, 1, aio_cb); |
461 | Newz (0, req, 1, aio_cb); |
418 | |
462 | |
419 | if (!req) |
463 | if (!req) |
420 | croak ("out of memory during aio_req allocation"); |
464 | croak ("out of memory during aio_req allocation"); |
421 | |
465 | |
422 | New (0, req->statdata, 1, struct stat64); |
466 | New (0, req->statdata, 1, Stat_t); |
423 | |
467 | |
424 | if (!req->statdata) |
468 | if (!req->statdata) |
425 | croak ("out of memory during aio_req->statdata allocation"); |
469 | croak ("out of memory during aio_req->statdata allocation"); |
426 | |
470 | |
427 | if (SvPOK (fh_or_path)) |
471 | if (SvPOK (fh_or_path)) |
… | |
… | |
438 | |
482 | |
439 | req->callback = SvREFCNT_inc (callback); |
483 | req->callback = SvREFCNT_inc (callback); |
440 | |
484 | |
441 | send_req (req); |
485 | send_req (req); |
442 | |
486 | |
|
|
487 | void |
|
|
488 | aio_unlink(pathname,callback) |
|
|
489 | SV * pathname |
|
|
490 | SV * callback |
|
|
491 | PROTOTYPE: $$ |
|
|
492 | CODE: |
|
|
493 | aio_req req; |
|
|
494 | |
|
|
495 | Newz (0, req, 1, aio_cb); |
|
|
496 | |
|
|
497 | if (!req) |
|
|
498 | croak ("out of memory during aio_req allocation"); |
|
|
499 | |
|
|
500 | req->type = REQ_UNLINK; |
|
|
501 | req->data = newSVsv (pathname); |
|
|
502 | req->dataptr = SvPV_nolen (req->data); |
|
|
503 | req->callback = SvREFCNT_inc (callback); |
|
|
504 | |
|
|
505 | send_req (req); |
|
|
506 | |
443 | int |
507 | int |
444 | poll_fileno() |
508 | poll_fileno() |
445 | PROTOTYPE: |
509 | PROTOTYPE: |
446 | CODE: |
510 | CODE: |
447 | RETVAL = respipe[0]; |
511 | RETVAL = respipe[0]; |
… | |
… | |
454 | CODE: |
518 | CODE: |
455 | RETVAL = poll_cb (aTHX); |
519 | RETVAL = poll_cb (aTHX); |
456 | OUTPUT: |
520 | OUTPUT: |
457 | RETVAL |
521 | RETVAL |
458 | |
522 | |
|
|
523 | void |
|
|
524 | poll_wait() |
|
|
525 | PROTOTYPE: |
|
|
526 | CODE: |
|
|
527 | poll_wait (); |
|
|
528 | |
459 | int |
529 | int |
460 | nreqs() |
530 | nreqs() |
461 | PROTOTYPE: |
531 | PROTOTYPE: |
462 | CODE: |
532 | CODE: |
463 | RETVAL = nreqs; |
533 | RETVAL = nreqs; |