… | |
… | |
8 | #include <sys/stat.h> |
8 | #include <sys/stat.h> |
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 | #include <endian.h> |
13 | |
14 | |
14 | typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
15 | 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 ;) */ |
16 | 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 ;) */ |
17 | typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
17 | |
18 | |
18 | #ifndef __NR_pread64 |
19 | // 128 seems to be enough most everywhere. alpha needs 256. |
19 | # define __NR_pread64 __NR_pread |
20 | #define STACKSIZE (256 * sizeof (long)) |
20 | #endif |
|
|
21 | #ifndef __NR_pwrite64 |
|
|
22 | # define __NR_pwrite64 __NR_pwrite |
|
|
23 | #endif |
|
|
24 | |
|
|
25 | #define STACKSIZE 1024 /* yeah */ |
|
|
26 | |
21 | |
27 | enum { |
22 | enum { |
28 | REQ_QUIT, |
23 | REQ_QUIT, |
29 | REQ_OPEN, REQ_CLOSE, REQ_READ, REQ_WRITE, |
24 | REQ_OPEN, REQ_CLOSE, REQ_READ, REQ_WRITE, |
30 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_UNLINK |
25 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_UNLINK |
… | |
… | |
48 | int errorno; |
43 | int errorno; |
49 | SV *data, *callback; |
44 | SV *data, *callback; |
50 | void *dataptr; |
45 | void *dataptr; |
51 | STRLEN dataoffset; |
46 | STRLEN dataoffset; |
52 | |
47 | |
53 | struct stat64 *statdata; |
48 | Stat_t *statdata; |
54 | } aio_cb; |
49 | } aio_cb; |
55 | |
50 | |
56 | typedef aio_cb *aio_req; |
51 | typedef aio_cb *aio_req; |
57 | |
52 | |
58 | static int started; |
53 | static int started; |
… | |
… | |
69 | aio_thread *thr; |
64 | aio_thread *thr; |
70 | |
65 | |
71 | New (0, thr, 1, aio_thread); |
66 | New (0, thr, 1, aio_thread); |
72 | |
67 | |
73 | if (clone (aio_proc, |
68 | if (clone (aio_proc, |
74 | &(thr->stack[STACKSIZE]), |
69 | &(thr->stack[STACKSIZE - sizeof (long)]), |
75 | CLONE_VM|CLONE_FS|CLONE_FILES, |
70 | CLONE_VM|CLONE_FS|CLONE_FILES, |
76 | thr) >= 0) |
71 | thr) >= 0) |
77 | started++; |
72 | started++; |
78 | else |
73 | else |
79 | Safefree (thr); |
74 | Safefree (thr); |
… | |
… | |
95 | { |
90 | { |
96 | nreqs++; |
91 | nreqs++; |
97 | req->next = 0; |
92 | req->next = 0; |
98 | |
93 | |
99 | if (qe) |
94 | if (qe) |
|
|
95 | { |
100 | qe->next = req; |
96 | qe->next = req; |
|
|
97 | qe = req; |
|
|
98 | } |
101 | else |
99 | else |
102 | qe = qs = req; |
100 | qe = qs = req; |
103 | |
101 | |
104 | send_reqs (); |
102 | send_reqs (); |
105 | } |
103 | } |
… | |
… | |
161 | req->callback = SvREFCNT_inc (callback); |
159 | req->callback = SvREFCNT_inc (callback); |
162 | |
160 | |
163 | send_req (req); |
161 | send_req (req); |
164 | } |
162 | } |
165 | |
163 | |
|
|
164 | static void |
|
|
165 | poll_wait () |
|
|
166 | { |
|
|
167 | fd_set rfd; |
|
|
168 | FD_ZERO(&rfd); |
|
|
169 | FD_SET(respipe[0], &rfd); |
|
|
170 | |
|
|
171 | select (respipe[0] + 1, &rfd, 0, 0, 0); |
|
|
172 | } |
|
|
173 | |
166 | static int |
174 | static int |
167 | poll_cb (pTHX) |
175 | poll_cb (pTHX) |
168 | { |
176 | { |
169 | dSP; |
177 | dSP; |
170 | int count = 0; |
178 | int count = 0; |
171 | aio_req req; |
179 | aio_req req; |
172 | |
180 | |
173 | while (read (respipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
181 | while (read (respipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
174 | { |
182 | { |
|
|
183 | nreqs--; |
|
|
184 | |
175 | if (req->type == REQ_QUIT) |
185 | if (req->type == REQ_QUIT) |
176 | { |
186 | { |
177 | Safefree (req->thread); |
187 | Safefree (req->thread); |
178 | started--; |
188 | started--; |
179 | } |
189 | } |
… | |
… | |
189 | if (req->data) |
199 | if (req->data) |
190 | SvREFCNT_dec (req->data); |
200 | SvREFCNT_dec (req->data); |
191 | |
201 | |
192 | if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) |
202 | if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) |
193 | { |
203 | { |
194 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
204 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
195 | PL_laststatval = req->result; |
205 | PL_laststatval = req->result; |
196 | PL_statcache.st_dev = req->statdata->st_dev; |
206 | 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 | |
207 | |
210 | Safefree (req->statdata); |
208 | Safefree (req->statdata); |
211 | } |
209 | } |
212 | |
210 | |
213 | PUSHMARK (SP); |
211 | PUSHMARK (SP); |
… | |
… | |
218 | |
216 | |
219 | if (req->callback) |
217 | if (req->callback) |
220 | SvREFCNT_dec (req->callback); |
218 | SvREFCNT_dec (req->callback); |
221 | |
219 | |
222 | errno = errorno; |
220 | errno = errorno; |
223 | nreqs--; |
|
|
224 | count++; |
221 | count++; |
225 | } |
222 | } |
226 | |
223 | |
227 | Safefree (req); |
224 | Safefree (req); |
228 | } |
225 | } |
… | |
… | |
237 | |
234 | |
238 | #undef errno |
235 | #undef errno |
239 | #include <asm/unistd.h> |
236 | #include <asm/unistd.h> |
240 | #include <sys/prctl.h> |
237 | #include <sys/prctl.h> |
241 | |
238 | |
|
|
239 | #if BYTE_ORDER == LITTLE_ENDIAN |
|
|
240 | # define LONG_LONG_PAIR(HI, LO) LO, HI |
|
|
241 | #elif BYTE_ORDER == BIG_ENDIAN |
|
|
242 | # define LONG_LONG_PAIR(HI, LO) HI, LO |
|
|
243 | #endif |
|
|
244 | |
|
|
245 | #if __alpha || __ia64 || __hppa || __v850__ |
|
|
246 | # define stat kernelstat |
|
|
247 | # define stat64 kernelstat64 |
|
|
248 | # include <asm/stat.h> |
|
|
249 | # undef stat |
|
|
250 | # undef stat64 |
|
|
251 | #else |
|
|
252 | # define kernelstat stat |
|
|
253 | # define kernelstat64 stat64 |
|
|
254 | #endif |
|
|
255 | |
|
|
256 | #define COPY_STATDATA \ |
|
|
257 | req->statdata->st_dev = statdata.st_dev; \ |
|
|
258 | req->statdata->st_ino = statdata.st_ino; \ |
|
|
259 | req->statdata->st_mode = statdata.st_mode; \ |
|
|
260 | req->statdata->st_nlink = statdata.st_nlink; \ |
|
|
261 | req->statdata->st_uid = statdata.st_uid; \ |
|
|
262 | req->statdata->st_gid = statdata.st_gid; \ |
|
|
263 | req->statdata->st_rdev = statdata.st_rdev; \ |
|
|
264 | req->statdata->st_size = statdata.st_size; \ |
|
|
265 | req->statdata->st_atime = statdata.st_atime; \ |
|
|
266 | req->statdata->st_mtime = statdata.st_mtime; \ |
|
|
267 | req->statdata->st_ctime = statdata.st_ctime; \ |
|
|
268 | req->statdata->st_blksize = statdata.st_blksize; \ |
|
|
269 | req->statdata->st_blocks = statdata.st_blocks; \ |
|
|
270 | |
242 | static int |
271 | static int |
243 | aio_proc (void *thr_arg) |
272 | aio_proc (void *thr_arg) |
244 | { |
273 | { |
245 | aio_thread *thr = thr_arg; |
274 | aio_thread *thr = thr_arg; |
246 | aio_req req; |
275 | aio_req req; |
247 | int errno; |
276 | int errno; |
248 | |
277 | |
249 | /* this is very much x86 and kernel-specific :(:(:( */ |
278 | /* this is very much kernel-specific :(:(:( */ |
250 | /* we rely on gcc's ability to create closures. */ |
279 | /* we rely on gcc's ability to create closures. */ |
251 | _syscall3(int,read,int,fd,char *,buf,size_t,count) |
280 | _syscall3(int,read,int,fd,char *,buf,size_t,count) |
252 | _syscall3(int,write,int,fd,char *,buf,size_t,count) |
281 | _syscall3(int,write,int,fd,char *,buf,size_t,count) |
253 | |
282 | |
254 | _syscall3(int,open,char *,pathname,int,flags,mode_t,mode) |
283 | _syscall3(int,open,char *,pathname,int,flags,mode_t,mode) |
255 | _syscall1(int,close,int,fd) |
284 | _syscall1(int,close,int,fd) |
256 | |
285 | |
|
|
286 | #if __NR_pread64 |
257 | _syscall5(int,pread64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi) |
287 | _syscall5(int,pread64,int,fd,char *,buf,size_t,count,unsigned int,offset_lh,unsigned int,offset_hl) |
258 | _syscall5(int,pwrite64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi) |
288 | _syscall5(int,pwrite64,int,fd,char *,buf,size_t,count,unsigned int,offset_lh,unsigned int,offset_hl) |
|
|
289 | #elif __NR_pread |
|
|
290 | _syscall4(int,pread,int,fd,char *,buf,size_t,count,offset_t,offset) |
|
|
291 | _syscall4(int,pwrite,int,fd,char *,buf,size_t,count,offset_t,offset) |
|
|
292 | #else |
|
|
293 | # error "neither pread nor pread64 defined" |
|
|
294 | #endif |
259 | |
295 | |
|
|
296 | |
|
|
297 | #if __NR_stat64 |
260 | _syscall2(int,stat64, const char *, filename, struct stat64 *, buf) |
298 | _syscall2(int,stat64, const char *, filename, struct kernelstat64 *, buf) |
261 | _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf) |
299 | _syscall2(int,lstat64, const char *, filename, struct kernelstat64 *, buf) |
262 | _syscall2(int,fstat64, int, fd, struct stat64 *, buf) |
300 | _syscall2(int,fstat64, int, fd, struct kernelstat64 *, buf) |
|
|
301 | #elif __NR_stat |
|
|
302 | _syscall2(int,stat, const char *, filename, struct kernelstat *, buf) |
|
|
303 | _syscall2(int,lstat, const char *, filename, struct kernelstat *, buf) |
|
|
304 | _syscall2(int,fstat, int, fd, struct kernelstat *, buf) |
|
|
305 | #else |
|
|
306 | # error "neither stat64 nor stat defined" |
|
|
307 | #endif |
263 | |
308 | |
264 | _syscall1(int,unlink, char *, filename); |
309 | _syscall1(int,unlink, char *, filename); |
265 | |
310 | |
266 | sigprocmask (SIG_SETMASK, &fullsigset, 0); |
311 | sigprocmask (SIG_SETMASK, &fullsigset, 0); |
267 | prctl (PR_SET_PDEATHSIG, SIGKILL); |
312 | prctl (PR_SET_PDEATHSIG, SIGKILL); |
… | |
… | |
272 | req->thread = thr; |
317 | req->thread = thr; |
273 | errno = 0; /* strictly unnecessary */ |
318 | errno = 0; /* strictly unnecessary */ |
274 | |
319 | |
275 | switch (req->type) |
320 | switch (req->type) |
276 | { |
321 | { |
|
|
322 | #if __NR_pread64 |
|
|
323 | case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, |
|
|
324 | LONG_LONG_PAIR (req->offset >> 32, req->offset & 0xffffffff)); break; |
|
|
325 | case REQ_WRITE: req->result = pwrite64(req->fd, req->dataptr, req->length, |
|
|
326 | LONG_LONG_PAIR (req->offset >> 32, req->offset & 0xffffffff)); break; |
|
|
327 | #else |
277 | case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); break; |
328 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
278 | case REQ_WRITE: req->result = pwrite64(req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); break; |
329 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
|
|
330 | #endif |
|
|
331 | #if __NR_stat64 |
|
|
332 | struct kernelstat64 statdata; |
|
|
333 | case REQ_STAT: req->result = stat64 (req->dataptr, &statdata); COPY_STATDATA; break; |
|
|
334 | case REQ_LSTAT: req->result = lstat64 (req->dataptr, &statdata); COPY_STATDATA; break; |
|
|
335 | case REQ_FSTAT: req->result = fstat64 (req->fd, &statdata); COPY_STATDATA; break; |
|
|
336 | #else |
|
|
337 | struct kernelstat statdata; |
|
|
338 | case REQ_STAT: req->result = stat (req->dataptr, &statdata); COPY_STATDATA; break; |
|
|
339 | case REQ_LSTAT: req->result = lstat (req->dataptr, &statdata); COPY_STATDATA; break; |
|
|
340 | case REQ_FSTAT: req->result = fstat (req->fd, &statdata); COPY_STATDATA; break; |
|
|
341 | #endif |
279 | case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
342 | case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
280 | case REQ_CLOSE: req->result = close (req->fd); break; |
343 | 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; |
344 | case REQ_UNLINK: req->result = unlink (req->dataptr); break; |
285 | |
345 | |
286 | case REQ_QUIT: |
346 | case REQ_QUIT: |
287 | default: |
347 | default: |
288 | write (respipe[1], (void *)&req, sizeof (req)); |
348 | write (respipe[1], (void *)&req, sizeof (req)); |
… | |
… | |
336 | cur--; |
396 | cur--; |
337 | } |
397 | } |
338 | |
398 | |
339 | while (started > nthreads) |
399 | while (started > nthreads) |
340 | { |
400 | { |
341 | fd_set rfd; |
401 | 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); |
402 | poll_cb (aTHX); |
347 | } |
403 | } |
348 | |
404 | |
349 | void |
405 | void |
350 | aio_open(pathname,flags,mode,callback) |
406 | aio_open(pathname,flags,mode,callback) |
… | |
… | |
426 | Newz (0, req, 1, aio_cb); |
482 | Newz (0, req, 1, aio_cb); |
427 | |
483 | |
428 | if (!req) |
484 | if (!req) |
429 | croak ("out of memory during aio_req allocation"); |
485 | croak ("out of memory during aio_req allocation"); |
430 | |
486 | |
431 | New (0, req->statdata, 1, struct stat64); |
487 | New (0, req->statdata, 1, Stat_t); |
432 | |
488 | |
433 | if (!req->statdata) |
489 | if (!req->statdata) |
434 | croak ("out of memory during aio_req->statdata allocation"); |
490 | croak ("out of memory during aio_req->statdata allocation"); |
435 | |
491 | |
436 | if (SvPOK (fh_or_path)) |
492 | if (SvPOK (fh_or_path)) |
… | |
… | |
483 | CODE: |
539 | CODE: |
484 | RETVAL = poll_cb (aTHX); |
540 | RETVAL = poll_cb (aTHX); |
485 | OUTPUT: |
541 | OUTPUT: |
486 | RETVAL |
542 | RETVAL |
487 | |
543 | |
|
|
544 | void |
|
|
545 | poll_wait() |
|
|
546 | PROTOTYPE: |
|
|
547 | CODE: |
|
|
548 | poll_wait (); |
|
|
549 | |
488 | int |
550 | int |
489 | nreqs() |
551 | nreqs() |
490 | PROTOTYPE: |
552 | PROTOTYPE: |
491 | CODE: |
553 | CODE: |
492 | RETVAL = nreqs; |
554 | RETVAL = nreqs; |