… | |
… | |
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 | |
|
|
25 | #define STACKSIZE 1024 /* yeah */ |
|
|
26 | |
19 | |
27 | enum { |
20 | enum { |
28 | REQ_QUIT, |
21 | REQ_QUIT, |
29 | REQ_OPEN, REQ_CLOSE, REQ_READ, REQ_WRITE, |
22 | REQ_OPEN, REQ_CLOSE, REQ_READ, REQ_WRITE, |
30 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_UNLINK |
23 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_UNLINK |
… | |
… | |
48 | int errorno; |
41 | int errorno; |
49 | SV *data, *callback; |
42 | SV *data, *callback; |
50 | void *dataptr; |
43 | void *dataptr; |
51 | STRLEN dataoffset; |
44 | STRLEN dataoffset; |
52 | |
45 | |
53 | struct stat64 *statdata; |
46 | Stat_t *statdata; |
54 | } aio_cb; |
47 | } aio_cb; |
55 | |
48 | |
56 | typedef aio_cb *aio_req; |
49 | typedef aio_cb *aio_req; |
57 | |
50 | |
58 | static int started; |
51 | static int started; |
… | |
… | |
69 | aio_thread *thr; |
62 | aio_thread *thr; |
70 | |
63 | |
71 | New (0, thr, 1, aio_thread); |
64 | New (0, thr, 1, aio_thread); |
72 | |
65 | |
73 | if (clone (aio_proc, |
66 | if (clone (aio_proc, |
74 | &(thr->stack[STACKSIZE]), |
67 | &(thr->stack[STACKSIZE - sizeof (long)]), |
75 | CLONE_VM|CLONE_FS|CLONE_FILES, |
68 | CLONE_VM|CLONE_FS|CLONE_FILES, |
76 | thr) >= 0) |
69 | thr) >= 0) |
77 | started++; |
70 | started++; |
78 | else |
71 | else |
79 | Safefree (thr); |
72 | Safefree (thr); |
… | |
… | |
95 | { |
88 | { |
96 | nreqs++; |
89 | nreqs++; |
97 | req->next = 0; |
90 | req->next = 0; |
98 | |
91 | |
99 | if (qe) |
92 | if (qe) |
|
|
93 | { |
100 | qe->next = req; |
94 | qe->next = req; |
|
|
95 | qe = req; |
|
|
96 | } |
101 | else |
97 | else |
102 | qe = qs = req; |
98 | qe = qs = req; |
103 | |
99 | |
104 | send_reqs (); |
100 | send_reqs (); |
105 | } |
101 | } |
… | |
… | |
161 | req->callback = SvREFCNT_inc (callback); |
157 | req->callback = SvREFCNT_inc (callback); |
162 | |
158 | |
163 | send_req (req); |
159 | send_req (req); |
164 | } |
160 | } |
165 | |
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 | |
166 | static int |
172 | static int |
167 | poll_cb (pTHX) |
173 | poll_cb (pTHX) |
168 | { |
174 | { |
169 | dSP; |
175 | dSP; |
170 | int count = 0; |
176 | int count = 0; |
171 | aio_req req; |
177 | aio_req req; |
172 | |
178 | |
173 | while (read (respipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
179 | while (read (respipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
174 | { |
180 | { |
|
|
181 | nreqs--; |
|
|
182 | |
175 | if (req->type == REQ_QUIT) |
183 | if (req->type == REQ_QUIT) |
176 | { |
184 | { |
177 | Safefree (req->thread); |
185 | Safefree (req->thread); |
178 | started--; |
186 | started--; |
179 | } |
187 | } |
… | |
… | |
189 | if (req->data) |
197 | if (req->data) |
190 | SvREFCNT_dec (req->data); |
198 | SvREFCNT_dec (req->data); |
191 | |
199 | |
192 | 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) |
193 | { |
201 | { |
194 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
202 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
195 | PL_laststatval = req->result; |
203 | PL_laststatval = req->result; |
196 | PL_statcache.st_dev = req->statdata->st_dev; |
204 | PL_statcache = *(req->statdata); |
197 | PL_statcache.st_ino = req->statdata->st_ino; |
|
|
198 | PL_statcache.st_mode = req->statdata->st_mode; |
|
|
199 | PL_statcache.st_nlink = req->statdata->st_nlink; |
|
|
200 | PL_statcache.st_uid = req->statdata->st_uid; |
|
|
201 | PL_statcache.st_gid = req->statdata->st_gid; |
|
|
202 | PL_statcache.st_rdev = req->statdata->st_rdev; |
|
|
203 | PL_statcache.st_size = req->statdata->st_size; |
|
|
204 | PL_statcache.st_atime = req->statdata->st_atime; |
|
|
205 | PL_statcache.st_mtime = req->statdata->st_mtime; |
|
|
206 | PL_statcache.st_ctime = req->statdata->st_ctime; |
|
|
207 | PL_statcache.st_blksize = req->statdata->st_blksize; |
|
|
208 | PL_statcache.st_blocks = req->statdata->st_blocks; |
|
|
209 | |
205 | |
210 | Safefree (req->statdata); |
206 | Safefree (req->statdata); |
211 | } |
207 | } |
212 | |
208 | |
213 | PUSHMARK (SP); |
209 | PUSHMARK (SP); |
… | |
… | |
218 | |
214 | |
219 | if (req->callback) |
215 | if (req->callback) |
220 | SvREFCNT_dec (req->callback); |
216 | SvREFCNT_dec (req->callback); |
221 | |
217 | |
222 | errno = errorno; |
218 | errno = errorno; |
223 | nreqs--; |
|
|
224 | count++; |
219 | count++; |
225 | } |
220 | } |
226 | |
221 | |
227 | Safefree (req); |
222 | Safefree (req); |
228 | } |
223 | } |
… | |
… | |
237 | |
232 | |
238 | #undef errno |
233 | #undef errno |
239 | #include <asm/unistd.h> |
234 | #include <asm/unistd.h> |
240 | #include <sys/prctl.h> |
235 | #include <sys/prctl.h> |
241 | |
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; \ |
|
|
251 | |
242 | static int |
252 | static int |
243 | aio_proc (void *thr_arg) |
253 | aio_proc (void *thr_arg) |
244 | { |
254 | { |
245 | aio_thread *thr = thr_arg; |
255 | aio_thread *thr = thr_arg; |
246 | aio_req req; |
256 | aio_req req; |
247 | int errno; |
257 | int errno; |
248 | |
258 | |
249 | /* this is very much x86 and kernel-specific :(:(:( */ |
259 | /* this is very much kernel-specific :(:(:( */ |
250 | /* we rely on gcc's ability to create closures. */ |
260 | /* we rely on gcc's ability to create closures. */ |
251 | _syscall3(int,read,int,fd,char *,buf,size_t,count) |
261 | _syscall3(int,read,int,fd,char *,buf,size_t,count) |
252 | _syscall3(int,write,int,fd,char *,buf,size_t,count) |
262 | _syscall3(int,write,int,fd,char *,buf,size_t,count) |
253 | |
263 | |
254 | _syscall3(int,open,char *,pathname,int,flags,mode_t,mode) |
264 | _syscall3(int,open,char *,pathname,int,flags,mode_t,mode) |
255 | _syscall1(int,close,int,fd) |
265 | _syscall1(int,close,int,fd) |
256 | |
266 | |
|
|
267 | #ifdef __NR_pread64 |
257 | _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) |
258 | _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 |
259 | |
276 | |
|
|
277 | |
|
|
278 | #ifdef __NR_stat64 |
260 | _syscall2(int,stat64, const char *, filename, struct stat64 *, buf) |
279 | _syscall2(int,stat64, const char *, filename, struct stat64 *, buf) |
261 | _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf) |
280 | _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf) |
262 | _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 |
263 | |
289 | |
264 | _syscall1(int,unlink, char *, filename); |
290 | _syscall1(int,unlink, char *, filename); |
265 | |
291 | |
266 | sigprocmask (SIG_SETMASK, &fullsigset, 0); |
292 | sigprocmask (SIG_SETMASK, &fullsigset, 0); |
267 | prctl (PR_SET_PDEATHSIG, SIGKILL); |
293 | prctl (PR_SET_PDEATHSIG, SIGKILL); |
… | |
… | |
272 | req->thread = thr; |
298 | req->thread = thr; |
273 | errno = 0; /* strictly unnecessary */ |
299 | errno = 0; /* strictly unnecessary */ |
274 | |
300 | |
275 | switch (req->type) |
301 | switch (req->type) |
276 | { |
302 | { |
|
|
303 | #ifdef __NR_pread64 |
277 | 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; |
278 | 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 |
279 | 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; |
280 | case REQ_CLOSE: req->result = close (req->fd); break; |
322 | 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; |
323 | case REQ_UNLINK: req->result = unlink (req->dataptr); break; |
285 | |
324 | |
286 | case REQ_QUIT: |
325 | case REQ_QUIT: |
287 | default: |
326 | default: |
288 | write (respipe[1], (void *)&req, sizeof (req)); |
327 | write (respipe[1], (void *)&req, sizeof (req)); |
… | |
… | |
336 | cur--; |
375 | cur--; |
337 | } |
376 | } |
338 | |
377 | |
339 | while (started > nthreads) |
378 | while (started > nthreads) |
340 | { |
379 | { |
341 | fd_set rfd; |
380 | poll_wait (); |
342 | FD_ZERO(&rfd); |
|
|
343 | FD_SET(respipe[0], &rfd); |
|
|
344 | |
|
|
345 | select (respipe[0] + 1, &rfd, 0, 0, 0); |
|
|
346 | poll_cb (aTHX); |
381 | poll_cb (aTHX); |
347 | } |
382 | } |
348 | |
383 | |
349 | void |
384 | void |
350 | aio_open(pathname,flags,mode,callback) |
385 | aio_open(pathname,flags,mode,callback) |
… | |
… | |
426 | Newz (0, req, 1, aio_cb); |
461 | Newz (0, req, 1, aio_cb); |
427 | |
462 | |
428 | if (!req) |
463 | if (!req) |
429 | croak ("out of memory during aio_req allocation"); |
464 | croak ("out of memory during aio_req allocation"); |
430 | |
465 | |
431 | New (0, req->statdata, 1, struct stat64); |
466 | New (0, req->statdata, 1, Stat_t); |
432 | |
467 | |
433 | if (!req->statdata) |
468 | if (!req->statdata) |
434 | croak ("out of memory during aio_req->statdata allocation"); |
469 | croak ("out of memory during aio_req->statdata allocation"); |
435 | |
470 | |
436 | if (SvPOK (fh_or_path)) |
471 | if (SvPOK (fh_or_path)) |
… | |
… | |
483 | CODE: |
518 | CODE: |
484 | RETVAL = poll_cb (aTHX); |
519 | RETVAL = poll_cb (aTHX); |
485 | OUTPUT: |
520 | OUTPUT: |
486 | RETVAL |
521 | RETVAL |
487 | |
522 | |
|
|
523 | void |
|
|
524 | poll_wait() |
|
|
525 | PROTOTYPE: |
|
|
526 | CODE: |
|
|
527 | poll_wait (); |
|
|
528 | |
488 | int |
529 | int |
489 | nreqs() |
530 | nreqs() |
490 | PROTOTYPE: |
531 | PROTOTYPE: |
491 | CODE: |
532 | CODE: |
492 | RETVAL = nreqs; |
533 | RETVAL = nreqs; |