ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Linux-AIO/AIO.xs
(Generate patch)

Comparing Linux-AIO/AIO.xs (file contents):
Revision 1.26 by root, Thu Jul 7 23:17:23 2005 UTC vs.
Revision 1.31 by root, Sun Jul 10 01:02:51 2005 UTC

14 14
15typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ 15typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */
16typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ 16typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */
17typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ 17typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */
18 18
19// 128 seems to be enough most everywhere. alpha needs 256. 19#if __i386 || __amd64
20#define STACKSIZE (256 * sizeof (long)) 20# define STACKSIZE ( 256 * sizeof (long))
21#elif __ia64
22# define STACKSIZE (8192 * sizeof (long))
23#else
24# define STACKSIZE ( 512 * sizeof (long))
25#endif
21 26
22enum { 27enum {
23 REQ_QUIT, 28 REQ_QUIT,
24 REQ_OPEN, REQ_CLOSE, REQ_READ, REQ_WRITE, 29 REQ_OPEN, REQ_CLOSE,
30 REQ_READ, REQ_WRITE, REQ_READAHEAD,
25 REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_UNLINK 31 REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_UNLINK,
32 REQ_FSYNC, REQ_FDATASYNC,
26}; 33};
27 34
28typedef struct { 35typedef struct {
29 char stack[STACKSIZE]; 36 char stack[STACKSIZE];
30} aio_thread; 37} aio_thread;
64 aio_thread *thr; 71 aio_thread *thr;
65 72
66 New (0, thr, 1, aio_thread); 73 New (0, thr, 1, aio_thread);
67 74
68 if (clone (aio_proc, 75 if (clone (aio_proc,
69 &(thr->stack[STACKSIZE - sizeof (long)]), 76 &(thr->stack[STACKSIZE - 16]),
70 CLONE_VM|CLONE_FS|CLONE_FILES, 77 CLONE_VM|CLONE_FS|CLONE_FILES,
71 thr) >= 0) 78 thr) >= 0)
72 started++; 79 started++;
73 else 80 else
74 Safefree (thr); 81 Safefree (thr);
232 239
233static sigset_t fullsigset; 240static sigset_t fullsigset;
234 241
235#undef errno 242#undef errno
236#include <asm/unistd.h> 243#include <asm/unistd.h>
244#include <linux/types.h>
237#include <sys/prctl.h> 245#include <sys/prctl.h>
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 246
245#if __alpha || __ia64 || __hppa || __v850__ 247#if __alpha || __ia64 || __hppa || __v850__
246# define stat kernelstat 248# define stat kernelstat
247# define stat64 kernelstat64 249# define stat64 kernelstat64
248# include <asm/stat.h> 250# include <asm/stat.h>
275 aio_req req; 277 aio_req req;
276 int errno; 278 int errno;
277 279
278 /* this is very much kernel-specific :(:(:( */ 280 /* this is very much kernel-specific :(:(:( */
279 /* we rely on gcc's ability to create closures. */ 281 /* we rely on gcc's ability to create closures. */
280 _syscall3(int,read,int,fd,char *,buf,size_t,count) 282 _syscall3(__kernel_size_t, read , unsigned int, fd, char *, buf, __kernel_size_t, count)
281 _syscall3(int,write,int,fd,char *,buf,size_t,count) 283 _syscall3(__kernel_size_t, write, unsigned int, fd, char *, buf, __kernel_size_t, count)
282 284
283 _syscall3(int,open,char *,pathname,int,flags,mode_t,mode) 285 _syscall3(long, open, char *, pathname, int, flags, int, mode)
284 _syscall1(int,close,int,fd) 286 _syscall1(long, close, unsigned int, fd)
287 _syscall1(long, unlink, char *, filename);
288 _syscall1(long, fsync, unsigned int, fd);
285 289
286#if __NR_pread64 290#ifndef __NR_fdatasync
287 _syscall5(int,pread64,int,fd,char *,buf,size_t,count,unsigned int,offset_lh,unsigned int,offset_hl) 291# define __NR_fdatasync __NR_fsync
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 292#endif
293 _syscall1(long, fdatasync, unsigned int, fd);
295 294
295#if BYTE_ORDER == LITTLE_ENDIAN
296# define LOFF_ARG(off) (off & 0xffffffff), (off >> 32)
297#elif BYTE_ORDER == BIG_ENDIAN
298# define LOFF_ARG(off) (off >> 32), (off & 0xffffffff)
299#endif
300
301#ifndef __NR_pread64
302# define __NR_pread64 __NR_pread
303# define __NR_pwrite64 __NR_write
304#endif
305 _syscall5(__kernel_ssize_t, pread64 , unsigned int, fd, char *, buf,
306 __kernel_size_t, count, unsigned int, offset_lh, unsigned int, offset_hl)
307 _syscall5(__kernel_ssize_t, pwrite64, unsigned int, fd, char *, buf,
308 __kernel_size_t, count, unsigned int, offset_lh, unsigned int, offset_hl)
309 _syscall4(long, readahead, unsigned int, fd, unsigned int, offset_lh, unsigned int, offset_hl, __kernel_size_t, count);
296 310
297#if __NR_stat64 311#if __NR_stat64
298 _syscall2(int,stat64, const char *, filename, struct kernelstat64 *, buf) 312 _syscall2(long, stat64 , const char *, filename, struct kernelstat64 *, buf)
299 _syscall2(int,lstat64, const char *, filename, struct kernelstat64 *, buf) 313 _syscall2(long, lstat64, const char *, filename, struct kernelstat64 *, buf)
300 _syscall2(int,fstat64, int, fd, struct kernelstat64 *, buf) 314 _syscall2(long, fstat64, int , fd , struct kernelstat64 *, buf)
301#elif __NR_stat 315#elif __NR_stat
302 _syscall2(int,stat, const char *, filename, struct kernelstat *, buf) 316 _syscall2(long, stat , const char *, filename, struct kernelstat *, buf)
303 _syscall2(int,lstat, const char *, filename, struct kernelstat *, buf) 317 _syscall2(long, lstat, const char *, filename, struct kernelstat *, buf)
304 _syscall2(int,fstat, int, fd, struct kernelstat *, buf) 318 _syscall2(long, fstat, int , fd , struct kernelstat *, buf)
305#else 319#else
306# error "neither stat64 nor stat defined" 320# error "neither stat64 nor stat defined"
307#endif 321#endif
308 322
309 _syscall1(int,unlink, char *, filename); 323 /* the following two calls might clobber errno */
310
311 sigprocmask (SIG_SETMASK, &fullsigset, 0); 324 sigprocmask (SIG_SETMASK, &fullsigset, 0);
312 prctl (PR_SET_PDEATHSIG, SIGKILL); 325 prctl (PR_SET_PDEATHSIG, SIGKILL);
313 326
314 /* then loop */ 327 /* then loop */
315 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) 328 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req))
317 req->thread = thr; 330 req->thread = thr;
318 errno = 0; /* strictly unnecessary */ 331 errno = 0; /* strictly unnecessary */
319 332
320 switch (req->type) 333 switch (req->type)
321 { 334 {
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
328 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; 335 case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, LOFF_ARG (req->offset)); break;
329 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; 336 case REQ_WRITE: req->result = pwrite64 (req->fd, req->dataptr, req->length, LOFF_ARG (req->offset)); break;
330#endif 337 case REQ_READAHEAD: req->result = readahead (req->fd, LOFF_ARG (req->offset), req->length); break;
338
331#if __NR_stat64 339#if __NR_stat64
332 struct kernelstat64 statdata; 340 struct kernelstat64 statdata;
333 case REQ_STAT: req->result = stat64 (req->dataptr, &statdata); COPY_STATDATA; break; 341 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; 342 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; 343 case REQ_FSTAT: req->result = fstat64 (req->fd , &statdata); COPY_STATDATA; break;
336#else 344#else
337 struct kernelstat statdata; 345 struct kernelstat statdata;
338 case REQ_STAT: req->result = stat (req->dataptr, &statdata); COPY_STATDATA; break; 346 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; 347 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; 348 case REQ_FSTAT: req->result = fstat (req->fd , &statdata); COPY_STATDATA; break;
341#endif 349#endif
350
342 case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; 351 case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break;
343 case REQ_CLOSE: req->result = close (req->fd); break; 352 case REQ_CLOSE: req->result = close (req->fd); break;
344 case REQ_UNLINK: req->result = unlink (req->dataptr); break; 353 case REQ_UNLINK: req->result = unlink (req->dataptr); break;
354
355 case REQ_FSYNC: req->result = fsync (req->fd); break;
356 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break;
345 357
346 case REQ_QUIT: 358 case REQ_QUIT:
347 default:
348 write (respipe[1], (void *)&req, sizeof (req)); 359 write (respipe[1], (void *)&req, sizeof (req));
349 return 0; 360 return 0;
361
362 default:
363 req->result = ENOSYS;
364 break;
350 } 365 }
351 366
352 req->errorno = errno; 367 req->errorno = errno;
353 write (respipe[1], (void *)&req, sizeof (req)); 368 write (respipe[1], (void *)&req, sizeof (req));
354 } 369 }
387void 402void
388max_parallel(nthreads) 403max_parallel(nthreads)
389 int nthreads 404 int nthreads
390 PROTOTYPE: $ 405 PROTOTYPE: $
391 CODE: 406 CODE:
407{
392 int cur = started; 408 int cur = started;
393 while (cur > nthreads) 409 while (cur > nthreads)
394 { 410 {
395 end_thread (); 411 end_thread ();
396 cur--; 412 cur--;
399 while (started > nthreads) 415 while (started > nthreads)
400 { 416 {
401 poll_wait (); 417 poll_wait ();
402 poll_cb (aTHX); 418 poll_cb (aTHX);
403 } 419 }
420}
404 421
405void 422void
406aio_open(pathname,flags,mode,callback) 423aio_open(pathname,flags,mode,callback)
407 SV * pathname 424 SV * pathname
408 int flags 425 int flags
409 int mode 426 int mode
410 SV * callback 427 SV * callback
411 PROTOTYPE: $$$$ 428 PROTOTYPE: $$$$
412 CODE: 429 CODE:
430{
413 aio_req req; 431 aio_req req;
414 432
415 Newz (0, req, 1, aio_cb); 433 Newz (0, req, 1, aio_cb);
416 434
417 if (!req) 435 if (!req)
423 req->fd = flags; 441 req->fd = flags;
424 req->mode = mode; 442 req->mode = mode;
425 req->callback = SvREFCNT_inc (callback); 443 req->callback = SvREFCNT_inc (callback);
426 444
427 send_req (req); 445 send_req (req);
446}
428 447
429void 448void
430aio_close(fh,callback) 449aio_close(fh,callback)
431 InputStream fh 450 InputStream fh
432 SV * callback 451 SV * callback
433 PROTOTYPE: $$ 452 PROTOTYPE: $$
453 ALIAS:
454 aio_close = REQ_CLOSE
455 aio_fsync = REQ_FSYNC
456 aio_fdatasync = REQ_FDATASYNC
434 CODE: 457 CODE:
458{
435 aio_req req; 459 aio_req req;
436 460
437 Newz (0, req, 1, aio_cb); 461 Newz (0, req, 1, aio_cb);
438 462
439 if (!req) 463 if (!req)
440 croak ("out of memory during aio_req allocation"); 464 croak ("out of memory during aio_req allocation");
441 465
442 req->type = REQ_CLOSE; 466 req->type = ix;
443 req->fd = PerlIO_fileno (fh); 467 req->fd = PerlIO_fileno (fh);
444 req->callback = SvREFCNT_inc (callback); 468 req->callback = SvREFCNT_inc (callback);
445 469
446 send_req (req); 470 send_req (req);
471}
447 472
448void 473void
449aio_read(fh,offset,length,data,dataoffset,callback) 474aio_read(fh,offset,length,data,dataoffset,callback)
450 InputStream fh 475 InputStream fh
451 UV offset 476 UV offset
468 PROTOTYPE: $$$$$$ 493 PROTOTYPE: $$$$$$
469 CODE: 494 CODE:
470 read_write (aTHX_ 1, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); 495 read_write (aTHX_ 1, PerlIO_fileno (fh), offset, length, data, dataoffset, callback);
471 496
472void 497void
498aio_readahead(fh,offset,length,callback)
499 InputStream fh
500 UV offset
501 IV length
502 SV * callback
503 PROTOTYPE: $$$$
504 CODE:
505{
506 aio_req req;
507
508 if (length < 0)
509 croak ("length must not be negative");
510
511 Newz (0, req, 1, aio_cb);
512
513 if (!req)
514 croak ("out of memory during aio_req allocation");
515
516 req->type = REQ_READAHEAD;
517 req->fd = PerlIO_fileno (fh);
518 req->offset = offset;
519 req->length = length;
520 req->callback = SvREFCNT_inc (callback);
521
522 send_req (req);
523}
524
525void
473aio_stat(fh_or_path,callback) 526aio_stat(fh_or_path,callback)
474 SV * fh_or_path 527 SV * fh_or_path
475 SV * callback 528 SV * callback
476 PROTOTYPE: $$ 529 PROTOTYPE: $$
477 ALIAS: 530 ALIAS:
478 aio_lstat = 1 531 aio_lstat = 1
479 CODE: 532 CODE:
533{
480 aio_req req; 534 aio_req req;
481 535
482 Newz (0, req, 1, aio_cb); 536 Newz (0, req, 1, aio_cb);
483 537
484 if (!req) 538 if (!req)
502 } 556 }
503 557
504 req->callback = SvREFCNT_inc (callback); 558 req->callback = SvREFCNT_inc (callback);
505 559
506 send_req (req); 560 send_req (req);
561}
507 562
508void 563void
509aio_unlink(pathname,callback) 564aio_unlink(pathname,callback)
510 SV * pathname 565 SV * pathname
511 SV * callback 566 SV * callback
512 PROTOTYPE: $$ 567 PROTOTYPE: $$
513 CODE: 568 CODE:
569{
514 aio_req req; 570 aio_req req;
515 571
516 Newz (0, req, 1, aio_cb); 572 Newz (0, req, 1, aio_cb);
517 573
518 if (!req) 574 if (!req)
522 req->data = newSVsv (pathname); 578 req->data = newSVsv (pathname);
523 req->dataptr = SvPV_nolen (req->data); 579 req->dataptr = SvPV_nolen (req->data);
524 req->callback = SvREFCNT_inc (callback); 580 req->callback = SvREFCNT_inc (callback);
525 581
526 send_req (req); 582 send_req (req);
583}
527 584
528int 585int
529poll_fileno() 586poll_fileno()
530 PROTOTYPE: 587 PROTOTYPE:
531 CODE: 588 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines