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.28 by root, Sat Jul 9 04:02:54 2005 UTC vs.
Revision 1.31 by root, Sun Jul 10 01:02:51 2005 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines