… | |
… | |
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 | // 128 seems to be enough most everywhere. alpha needs 256. |
19 | # define __NR_pread64 __NR_pread |
19 | #define STACKSIZE (256 * sizeof (long)) |
20 | #endif |
|
|
21 | #ifndef __NR_pwrite64 |
|
|
22 | # define __NR_pwrite64 __NR_pwrite |
|
|
23 | #endif |
|
|
24 | |
20 | |
25 | #define STACKSIZE 1024 /* yeah */ |
21 | enum { |
26 | |
22 | REQ_QUIT, |
27 | enum { REQ_QUIT, REQ_OPEN, REQ_CLOSE, REQ_READ, REQ_WRITE, REQ_STAT, REQ_LSTAT, REQ_FSTAT}; |
23 | REQ_OPEN, REQ_CLOSE, REQ_READ, REQ_WRITE, |
|
|
24 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_UNLINK |
|
|
25 | }; |
28 | |
26 | |
29 | typedef struct { |
27 | typedef struct { |
30 | char stack[STACKSIZE]; |
28 | char stack[STACKSIZE]; |
31 | } aio_thread; |
29 | } aio_thread; |
32 | |
30 | |
… | |
… | |
44 | int errorno; |
42 | int errorno; |
45 | SV *data, *callback; |
43 | SV *data, *callback; |
46 | void *dataptr; |
44 | void *dataptr; |
47 | STRLEN dataoffset; |
45 | STRLEN dataoffset; |
48 | |
46 | |
49 | struct stat64 *statdata; |
47 | Stat_t *statdata; |
50 | } aio_cb; |
48 | } aio_cb; |
51 | |
49 | |
52 | typedef aio_cb *aio_req; |
50 | typedef aio_cb *aio_req; |
53 | |
51 | |
54 | static int started; |
52 | static int started; |
… | |
… | |
65 | aio_thread *thr; |
63 | aio_thread *thr; |
66 | |
64 | |
67 | New (0, thr, 1, aio_thread); |
65 | New (0, thr, 1, aio_thread); |
68 | |
66 | |
69 | if (clone (aio_proc, |
67 | if (clone (aio_proc, |
70 | &(thr->stack[STACKSIZE]), |
68 | &(thr->stack[STACKSIZE - sizeof (long)]), |
71 | CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND, |
69 | CLONE_VM|CLONE_FS|CLONE_FILES, |
72 | thr) >= 0) |
70 | thr) >= 0) |
73 | started++; |
71 | started++; |
74 | else |
72 | else |
75 | Safefree (thr); |
73 | Safefree (thr); |
76 | } |
74 | } |
… | |
… | |
91 | { |
89 | { |
92 | nreqs++; |
90 | nreqs++; |
93 | req->next = 0; |
91 | req->next = 0; |
94 | |
92 | |
95 | if (qe) |
93 | if (qe) |
|
|
94 | { |
96 | qe->next = req; |
95 | qe->next = req; |
|
|
96 | qe = req; |
|
|
97 | } |
97 | else |
98 | else |
98 | qe = qs = req; |
99 | qe = qs = req; |
99 | |
100 | |
100 | send_reqs (); |
101 | send_reqs (); |
101 | } |
102 | } |
… | |
… | |
157 | req->callback = SvREFCNT_inc (callback); |
158 | req->callback = SvREFCNT_inc (callback); |
158 | |
159 | |
159 | send_req (req); |
160 | send_req (req); |
160 | } |
161 | } |
161 | |
162 | |
|
|
163 | static void |
|
|
164 | poll_wait () |
|
|
165 | { |
|
|
166 | fd_set rfd; |
|
|
167 | FD_ZERO(&rfd); |
|
|
168 | FD_SET(respipe[0], &rfd); |
|
|
169 | |
|
|
170 | select (respipe[0] + 1, &rfd, 0, 0, 0); |
|
|
171 | } |
|
|
172 | |
162 | static int |
173 | static int |
163 | poll_cb (pTHX) |
174 | poll_cb (pTHX) |
164 | { |
175 | { |
165 | dSP; |
176 | dSP; |
166 | int count = 0; |
177 | int count = 0; |
167 | aio_req req; |
178 | aio_req req; |
168 | |
179 | |
169 | while (read (respipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
180 | while (read (respipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
170 | { |
181 | { |
|
|
182 | nreqs--; |
|
|
183 | |
171 | if (req->type == REQ_QUIT) |
184 | if (req->type == REQ_QUIT) |
172 | { |
185 | { |
173 | Safefree (req->thread); |
186 | Safefree (req->thread); |
174 | started--; |
187 | started--; |
175 | } |
188 | } |
… | |
… | |
185 | if (req->data) |
198 | if (req->data) |
186 | SvREFCNT_dec (req->data); |
199 | SvREFCNT_dec (req->data); |
187 | |
200 | |
188 | if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) |
201 | if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) |
189 | { |
202 | { |
190 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
203 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
191 | PL_laststatval = req->result; |
204 | PL_laststatval = req->result; |
192 | PL_statcache.st_dev = req->statdata->st_dev; |
205 | 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 | |
206 | |
206 | Safefree (req->statdata); |
207 | Safefree (req->statdata); |
207 | } |
208 | } |
208 | |
209 | |
209 | PUSHMARK (SP); |
210 | PUSHMARK (SP); |
… | |
… | |
214 | |
215 | |
215 | if (req->callback) |
216 | if (req->callback) |
216 | SvREFCNT_dec (req->callback); |
217 | SvREFCNT_dec (req->callback); |
217 | |
218 | |
218 | errno = errorno; |
219 | errno = errorno; |
219 | nreqs--; |
|
|
220 | count++; |
220 | count++; |
221 | } |
221 | } |
222 | |
222 | |
223 | Safefree (req); |
223 | Safefree (req); |
224 | } |
224 | } |
225 | |
225 | |
226 | if (qs) |
226 | if (qs) |
227 | { |
|
|
228 | printf ("outstanding %p %d\n", qs, nreqs); |
|
|
229 | send_reqs (); |
227 | send_reqs (); |
230 | } |
|
|
231 | |
228 | |
232 | return count; |
229 | return count; |
233 | } |
230 | } |
234 | |
231 | |
235 | static sigset_t fullsigset; |
232 | static sigset_t fullsigset; |
236 | |
233 | |
237 | #undef errno |
234 | #undef errno |
238 | #include <asm/unistd.h> |
235 | #include <asm/unistd.h> |
|
|
236 | #include <sys/prctl.h> |
|
|
237 | |
|
|
238 | #if __alpha || __ia64 || __hppa || __sparc64__ || __v850__ |
|
|
239 | # define stat kernelstat |
|
|
240 | # define stat64 kernelstat64 |
|
|
241 | # include <asm/stat.h> |
|
|
242 | # undef stat |
|
|
243 | # undef stat64 |
|
|
244 | #else |
|
|
245 | # define kernelstat stat |
|
|
246 | # define kernelstat64 stat64 |
|
|
247 | #endif |
|
|
248 | |
|
|
249 | #define COPY_STATDATA \ |
|
|
250 | req->statdata->st_dev = statdata.st_dev; \ |
|
|
251 | req->statdata->st_ino = statdata.st_ino; \ |
|
|
252 | req->statdata->st_mode = statdata.st_mode; \ |
|
|
253 | req->statdata->st_nlink = statdata.st_nlink; \ |
|
|
254 | req->statdata->st_uid = statdata.st_uid; \ |
|
|
255 | req->statdata->st_gid = statdata.st_gid; \ |
|
|
256 | req->statdata->st_rdev = statdata.st_rdev; \ |
|
|
257 | req->statdata->st_size = statdata.st_size; \ |
|
|
258 | req->statdata->st_atime = statdata.st_atime; \ |
|
|
259 | req->statdata->st_mtime = statdata.st_mtime; \ |
|
|
260 | req->statdata->st_ctime = statdata.st_ctime; \ |
|
|
261 | req->statdata->st_blksize = statdata.st_blksize; \ |
|
|
262 | req->statdata->st_blocks = statdata.st_blocks; \ |
239 | |
263 | |
240 | static int |
264 | static int |
241 | aio_proc (void *thr_arg) |
265 | aio_proc (void *thr_arg) |
242 | { |
266 | { |
243 | aio_thread *thr = thr_arg; |
267 | aio_thread *thr = thr_arg; |
244 | aio_req req; |
268 | aio_req req; |
245 | int errno; |
269 | int errno; |
246 | |
270 | |
247 | /* this is very much x86 and kernel-specific :(:(:( */ |
271 | /* this is very much kernel-specific :(:(:( */ |
248 | /* we rely on gcc's ability to create closures. */ |
272 | /* we rely on gcc's ability to create closures. */ |
249 | _syscall3(int,read,int,fd,char *,buf,size_t,count) |
273 | _syscall3(int,read,int,fd,char *,buf,size_t,count) |
250 | _syscall3(int,write,int,fd,char *,buf,size_t,count) |
274 | _syscall3(int,write,int,fd,char *,buf,size_t,count) |
251 | |
275 | |
252 | _syscall3(int,open,char *,pathname,int,flags,mode_t,mode) |
276 | _syscall3(int,open,char *,pathname,int,flags,mode_t,mode) |
253 | _syscall1(int,close,int,fd) |
277 | _syscall1(int,close,int,fd) |
254 | |
278 | |
|
|
279 | #if __NR_pread64 |
255 | _syscall5(int,pread64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi) |
280 | _syscall5(int,pread64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi) |
256 | _syscall5(int,pwrite64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi) |
281 | _syscall5(int,pwrite64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi) |
|
|
282 | #elif __NR_pread |
|
|
283 | _syscall4(int,pread,int,fd,char *,buf,size_t,count,offset_t,offset) |
|
|
284 | _syscall4(int,pwrite,int,fd,char *,buf,size_t,count,offset_t,offset) |
|
|
285 | #else |
|
|
286 | # error "neither pread nor pread64 defined" |
|
|
287 | #endif |
257 | |
288 | |
|
|
289 | |
|
|
290 | #if __NR_stat64 |
258 | _syscall2(int,stat64, const char *, filename, struct stat64 *, buf) |
291 | _syscall2(int,stat64, const char *, filename, struct kernelstat64 *, buf) |
259 | _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf) |
292 | _syscall2(int,lstat64, const char *, filename, struct kernelstat64 *, buf) |
260 | _syscall2(int,fstat64, int, fd, struct stat64 *, buf) |
293 | _syscall2(int,fstat64, int, fd, struct kernelstat64 *, buf) |
|
|
294 | #elif __NR_stat |
|
|
295 | _syscall2(int,stat, const char *, filename, struct kernelstat *, buf) |
|
|
296 | _syscall2(int,lstat, const char *, filename, struct kernelstat *, buf) |
|
|
297 | _syscall2(int,fstat, int, fd, struct kernelstat *, buf) |
|
|
298 | #else |
|
|
299 | # error "neither stat64 nor stat defined" |
|
|
300 | #endif |
|
|
301 | |
|
|
302 | _syscall1(int,unlink, char *, filename); |
261 | |
303 | |
262 | sigprocmask (SIG_SETMASK, &fullsigset, 0); |
304 | sigprocmask (SIG_SETMASK, &fullsigset, 0); |
|
|
305 | prctl (PR_SET_PDEATHSIG, SIGKILL); |
263 | |
306 | |
264 | /* then loop */ |
307 | /* then loop */ |
265 | while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
308 | while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
266 | { |
309 | { |
267 | req->thread = thr; |
310 | req->thread = thr; |
268 | errno = 0; /* strictly unnecessary */ |
311 | errno = 0; /* strictly unnecessary */ |
269 | |
312 | |
270 | switch (req->type) |
313 | switch (req->type) |
271 | { |
314 | { |
|
|
315 | #if __NR_pread64 |
272 | case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); break; |
316 | case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); break; |
273 | case REQ_WRITE: req->result = pwrite64(req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); break; |
317 | case REQ_WRITE: req->result = pwrite64(req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); break; |
|
|
318 | #else |
|
|
319 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
|
|
320 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
|
|
321 | #endif |
|
|
322 | #if __NR_stat64 |
|
|
323 | struct kernelstat64 statdata; |
|
|
324 | case REQ_STAT: req->result = stat64 (req->dataptr, &statdata); COPY_STATDATA; break; |
|
|
325 | case REQ_LSTAT: req->result = lstat64 (req->dataptr, &statdata); COPY_STATDATA; break; |
|
|
326 | case REQ_FSTAT: req->result = fstat64 (req->fd, &statdata); COPY_STATDATA; break; |
|
|
327 | #else |
|
|
328 | struct kernelstat statdata; |
|
|
329 | case REQ_STAT: req->result = stat (req->dataptr, &statdata); COPY_STATDATA; break; |
|
|
330 | case REQ_LSTAT: req->result = lstat (req->dataptr, &statdata); COPY_STATDATA; break; |
|
|
331 | case REQ_FSTAT: req->result = fstat (req->fd, &statdata); COPY_STATDATA; break; |
|
|
332 | #endif |
274 | case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
333 | case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
275 | case REQ_CLOSE: req->result = close (req->fd); break; |
334 | case REQ_CLOSE: req->result = close (req->fd); break; |
276 | case REQ_STAT: req->result = stat64 (req->dataptr, req->statdata); break; |
|
|
277 | case REQ_LSTAT: req->result = lstat64 (req->dataptr, req->statdata); break; |
335 | case REQ_UNLINK: req->result = unlink (req->dataptr); break; |
278 | case REQ_FSTAT: req->result = fstat64 (req->fd, req->statdata); break; |
|
|
279 | |
336 | |
280 | case REQ_QUIT: |
337 | case REQ_QUIT: |
281 | default: |
338 | default: |
282 | write (respipe[1], (void *)&req, sizeof (req)); |
339 | write (respipe[1], (void *)&req, sizeof (req)); |
283 | return 0; |
340 | return 0; |
… | |
… | |
330 | cur--; |
387 | cur--; |
331 | } |
388 | } |
332 | |
389 | |
333 | while (started > nthreads) |
390 | while (started > nthreads) |
334 | { |
391 | { |
335 | fd_set rfd; |
392 | poll_wait (); |
336 | FD_ZERO(&rfd); |
|
|
337 | FD_SET(respipe[0], &rfd); |
|
|
338 | |
|
|
339 | select (respipe[0] + 1, &rfd, 0, 0, 0); |
|
|
340 | poll_cb (aTHX); |
393 | poll_cb (aTHX); |
341 | } |
394 | } |
342 | |
395 | |
343 | void |
396 | void |
344 | aio_open(pathname,flags,mode,callback) |
397 | aio_open(pathname,flags,mode,callback) |
… | |
… | |
420 | Newz (0, req, 1, aio_cb); |
473 | Newz (0, req, 1, aio_cb); |
421 | |
474 | |
422 | if (!req) |
475 | if (!req) |
423 | croak ("out of memory during aio_req allocation"); |
476 | croak ("out of memory during aio_req allocation"); |
424 | |
477 | |
425 | New (0, req->statdata, 1, struct stat64); |
478 | New (0, req->statdata, 1, Stat_t); |
426 | |
479 | |
427 | if (!req->statdata) |
480 | if (!req->statdata) |
428 | croak ("out of memory during aio_req->statdata allocation"); |
481 | croak ("out of memory during aio_req->statdata allocation"); |
429 | |
482 | |
430 | if (SvPOK (fh_or_path)) |
483 | if (SvPOK (fh_or_path)) |
… | |
… | |
441 | |
494 | |
442 | req->callback = SvREFCNT_inc (callback); |
495 | req->callback = SvREFCNT_inc (callback); |
443 | |
496 | |
444 | send_req (req); |
497 | send_req (req); |
445 | |
498 | |
|
|
499 | void |
|
|
500 | aio_unlink(pathname,callback) |
|
|
501 | SV * pathname |
|
|
502 | SV * callback |
|
|
503 | PROTOTYPE: $$ |
|
|
504 | CODE: |
|
|
505 | aio_req req; |
|
|
506 | |
|
|
507 | Newz (0, req, 1, aio_cb); |
|
|
508 | |
|
|
509 | if (!req) |
|
|
510 | croak ("out of memory during aio_req allocation"); |
|
|
511 | |
|
|
512 | req->type = REQ_UNLINK; |
|
|
513 | req->data = newSVsv (pathname); |
|
|
514 | req->dataptr = SvPV_nolen (req->data); |
|
|
515 | req->callback = SvREFCNT_inc (callback); |
|
|
516 | |
|
|
517 | send_req (req); |
|
|
518 | |
446 | int |
519 | int |
447 | poll_fileno() |
520 | poll_fileno() |
448 | PROTOTYPE: |
521 | PROTOTYPE: |
449 | CODE: |
522 | CODE: |
450 | RETVAL = respipe[0]; |
523 | RETVAL = respipe[0]; |
… | |
… | |
457 | CODE: |
530 | CODE: |
458 | RETVAL = poll_cb (aTHX); |
531 | RETVAL = poll_cb (aTHX); |
459 | OUTPUT: |
532 | OUTPUT: |
460 | RETVAL |
533 | RETVAL |
461 | |
534 | |
|
|
535 | void |
|
|
536 | poll_wait() |
|
|
537 | PROTOTYPE: |
|
|
538 | CODE: |
|
|
539 | poll_wait (); |
|
|
540 | |
462 | int |
541 | int |
463 | nreqs() |
542 | nreqs() |
464 | PROTOTYPE: |
543 | PROTOTYPE: |
465 | CODE: |
544 | CODE: |
466 | RETVAL = nreqs; |
545 | RETVAL = nreqs; |