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.33 by root, Wed Aug 17 16:57:53 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);
162} 169}
163 170
164static void 171static void
165poll_wait () 172poll_wait ()
166{ 173{
174 if (!nreqs)
175 return;
176
167 fd_set rfd; 177 fd_set rfd;
168 FD_ZERO(&rfd); 178 FD_ZERO(&rfd);
169 FD_SET(respipe[0], &rfd); 179 FD_SET(respipe[0], &rfd);
170 180
171 select (respipe[0] + 1, &rfd, 0, 0, 0); 181 select (respipe[0] + 1, &rfd, 0, 0, 0);
192 int errorno = errno; 202 int errorno = errno;
193 errno = req->errorno; 203 errno = req->errorno;
194 204
195 if (req->type == REQ_READ) 205 if (req->type == REQ_READ)
196 SvCUR_set (req->data, req->dataoffset 206 SvCUR_set (req->data, req->dataoffset
197 + req->result > 0 ? req->result : 0); 207 + (req->result > 0 ? req->result : 0));
198 208
199 if (req->data) 209 if (req->data)
200 SvREFCNT_dec (req->data); 210 SvREFCNT_dec (req->data);
201 211
202 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) 212 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT)
231} 241}
232 242
233static sigset_t fullsigset; 243static sigset_t fullsigset;
234 244
235#undef errno 245#undef errno
236#include <asm/unistd.h> 246#include <linux/unistd.h>
247#include <linux/types.h>
237#include <sys/prctl.h> 248#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 249
245#if __alpha || __ia64 || __hppa || __v850__ 250#if __alpha || __ia64 || __hppa || __v850__
246# define stat kernelstat 251# define stat kernelstat
247# define stat64 kernelstat64 252# define stat64 kernelstat64
248# include <asm/stat.h> 253# include <asm/stat.h>
275 aio_req req; 280 aio_req req;
276 int errno; 281 int errno;
277 282
278 /* this is very much kernel-specific :(:(:( */ 283 /* this is very much kernel-specific :(:(:( */
279 /* we rely on gcc's ability to create closures. */ 284 /* we rely on gcc's ability to create closures. */
280 _syscall3(int,read,int,fd,char *,buf,size_t,count) 285 _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) 286 _syscall3(__kernel_size_t, write, unsigned int, fd, char *, buf, __kernel_size_t, count)
282 287
283 _syscall3(int,open,char *,pathname,int,flags,mode_t,mode) 288 _syscall3(long, open, char *, pathname, int, flags, int, mode)
284 _syscall1(int,close,int,fd) 289 _syscall1(long, close, unsigned int, fd)
290 _syscall1(long, unlink, char *, filename);
291 _syscall1(long, fsync, unsigned int, fd);
285 292
286#if __NR_pread64 293#ifndef __NR_fdatasync
287 _syscall5(int,pread64,int,fd,char *,buf,size_t,count,unsigned int,offset_lh,unsigned int,offset_hl) 294# 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 295#endif
296 _syscall1(long, fdatasync, unsigned int, fd);
295 297
298#if BYTE_ORDER == LITTLE_ENDIAN
299# define LOFF_ARG(off) (off & 0xffffffff), (off >> 32)
300#elif BYTE_ORDER == BIG_ENDIAN
301# define LOFF_ARG(off) (off >> 32), (off & 0xffffffff)
302#endif
303
304#ifndef __NR_pread64
305# define __NR_pread64 __NR_pread
306# define __NR_pwrite64 __NR_write
307#endif
308 _syscall5(__kernel_ssize_t, pread64 , unsigned int, fd, char *, buf,
309 __kernel_size_t, count, unsigned int, offset_lh, unsigned int, offset_hl)
310 _syscall5(__kernel_ssize_t, pwrite64, unsigned int, fd, char *, buf,
311 __kernel_size_t, count, unsigned int, offset_lh, unsigned int, offset_hl)
312 _syscall4(long, readahead, unsigned int, fd, unsigned int, offset_lh, unsigned int, offset_hl, __kernel_size_t, count);
296 313
297#if __NR_stat64 314#if __NR_stat64
298 _syscall2(int,stat64, const char *, filename, struct kernelstat64 *, buf) 315 _syscall2(long, stat64 , const char *, filename, struct kernelstat64 *, buf)
299 _syscall2(int,lstat64, const char *, filename, struct kernelstat64 *, buf) 316 _syscall2(long, lstat64, const char *, filename, struct kernelstat64 *, buf)
300 _syscall2(int,fstat64, int, fd, struct kernelstat64 *, buf) 317 _syscall2(long, fstat64, int , fd , struct kernelstat64 *, buf)
301#elif __NR_stat 318#elif __NR_stat
302 _syscall2(int,stat, const char *, filename, struct kernelstat *, buf) 319 _syscall2(long, stat , const char *, filename, struct kernelstat *, buf)
303 _syscall2(int,lstat, const char *, filename, struct kernelstat *, buf) 320 _syscall2(long, lstat, const char *, filename, struct kernelstat *, buf)
304 _syscall2(int,fstat, int, fd, struct kernelstat *, buf) 321 _syscall2(long, fstat, int , fd , struct kernelstat *, buf)
305#else 322#else
306# error "neither stat64 nor stat defined" 323# error "neither stat64 nor stat defined"
307#endif 324#endif
308 325
309 _syscall1(int,unlink, char *, filename); 326 /* the following two calls might clobber errno */
310
311 sigprocmask (SIG_SETMASK, &fullsigset, 0); 327 sigprocmask (SIG_SETMASK, &fullsigset, 0);
312 prctl (PR_SET_PDEATHSIG, SIGKILL); 328 prctl (PR_SET_PDEATHSIG, SIGKILL);
313 329
314 /* then loop */ 330 /* then loop */
315 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) 331 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req))
317 req->thread = thr; 333 req->thread = thr;
318 errno = 0; /* strictly unnecessary */ 334 errno = 0; /* strictly unnecessary */
319 335
320 switch (req->type) 336 switch (req->type)
321 { 337 {
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; 338 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; 339 case REQ_WRITE: req->result = pwrite64 (req->fd, req->dataptr, req->length, LOFF_ARG (req->offset)); break;
330#endif 340 case REQ_READAHEAD: req->result = readahead (req->fd, LOFF_ARG (req->offset), req->length); break;
341
331#if __NR_stat64 342#if __NR_stat64
332 struct kernelstat64 statdata; 343 struct kernelstat64 statdata;
333 case REQ_STAT: req->result = stat64 (req->dataptr, &statdata); COPY_STATDATA; break; 344 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; 345 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; 346 case REQ_FSTAT: req->result = fstat64 (req->fd , &statdata); COPY_STATDATA; break;
336#else 347#else
337 struct kernelstat statdata; 348 struct kernelstat statdata;
338 case REQ_STAT: req->result = stat (req->dataptr, &statdata); COPY_STATDATA; break; 349 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; 350 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; 351 case REQ_FSTAT: req->result = fstat (req->fd , &statdata); COPY_STATDATA; break;
341#endif 352#endif
353
342 case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; 354 case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break;
343 case REQ_CLOSE: req->result = close (req->fd); break; 355 case REQ_CLOSE: req->result = close (req->fd); break;
344 case REQ_UNLINK: req->result = unlink (req->dataptr); break; 356 case REQ_UNLINK: req->result = unlink (req->dataptr); break;
357
358 case REQ_FSYNC: req->result = fsync (req->fd); break;
359 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break;
345 360
346 case REQ_QUIT: 361 case REQ_QUIT:
347 default:
348 write (respipe[1], (void *)&req, sizeof (req)); 362 write (respipe[1], (void *)&req, sizeof (req));
349 return 0; 363 return 0;
364
365 default:
366 req->result = ENOSYS;
367 break;
350 } 368 }
351 369
352 req->errorno = errno; 370 req->errorno = errno;
353 write (respipe[1], (void *)&req, sizeof (req)); 371 write (respipe[1], (void *)&req, sizeof (req));
354 } 372 }
387void 405void
388max_parallel(nthreads) 406max_parallel(nthreads)
389 int nthreads 407 int nthreads
390 PROTOTYPE: $ 408 PROTOTYPE: $
391 CODE: 409 CODE:
410{
392 int cur = started; 411 int cur = started;
393 while (cur > nthreads) 412 while (cur > nthreads)
394 { 413 {
395 end_thread (); 414 end_thread ();
396 cur--; 415 cur--;
399 while (started > nthreads) 418 while (started > nthreads)
400 { 419 {
401 poll_wait (); 420 poll_wait ();
402 poll_cb (aTHX); 421 poll_cb (aTHX);
403 } 422 }
423}
404 424
405void 425void
406aio_open(pathname,flags,mode,callback) 426aio_open(pathname,flags,mode,callback)
407 SV * pathname 427 SV * pathname
408 int flags 428 int flags
409 int mode 429 int mode
410 SV * callback 430 SV * callback
411 PROTOTYPE: $$$$ 431 PROTOTYPE: $$$$
412 CODE: 432 CODE:
433{
413 aio_req req; 434 aio_req req;
414 435
415 Newz (0, req, 1, aio_cb); 436 Newz (0, req, 1, aio_cb);
416 437
417 if (!req) 438 if (!req)
423 req->fd = flags; 444 req->fd = flags;
424 req->mode = mode; 445 req->mode = mode;
425 req->callback = SvREFCNT_inc (callback); 446 req->callback = SvREFCNT_inc (callback);
426 447
427 send_req (req); 448 send_req (req);
449}
428 450
429void 451void
430aio_close(fh,callback) 452aio_close(fh,callback)
431 InputStream fh 453 InputStream fh
432 SV * callback 454 SV * callback
433 PROTOTYPE: $$ 455 PROTOTYPE: $$
456 ALIAS:
457 aio_close = REQ_CLOSE
458 aio_fsync = REQ_FSYNC
459 aio_fdatasync = REQ_FDATASYNC
434 CODE: 460 CODE:
461{
435 aio_req req; 462 aio_req req;
436 463
437 Newz (0, req, 1, aio_cb); 464 Newz (0, req, 1, aio_cb);
438 465
439 if (!req) 466 if (!req)
440 croak ("out of memory during aio_req allocation"); 467 croak ("out of memory during aio_req allocation");
441 468
442 req->type = REQ_CLOSE; 469 req->type = ix;
443 req->fd = PerlIO_fileno (fh); 470 req->fd = PerlIO_fileno (fh);
444 req->callback = SvREFCNT_inc (callback); 471 req->callback = SvREFCNT_inc (callback);
445 472
446 send_req (req); 473 send_req (req);
474}
447 475
448void 476void
449aio_read(fh,offset,length,data,dataoffset,callback) 477aio_read(fh,offset,length,data,dataoffset,callback)
450 InputStream fh 478 InputStream fh
451 UV offset 479 UV offset
468 PROTOTYPE: $$$$$$ 496 PROTOTYPE: $$$$$$
469 CODE: 497 CODE:
470 read_write (aTHX_ 1, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); 498 read_write (aTHX_ 1, PerlIO_fileno (fh), offset, length, data, dataoffset, callback);
471 499
472void 500void
501aio_readahead(fh,offset,length,callback)
502 InputStream fh
503 UV offset
504 IV length
505 SV * callback
506 PROTOTYPE: $$$$
507 CODE:
508{
509 aio_req req;
510
511 if (length < 0)
512 croak ("length must not be negative");
513
514 Newz (0, req, 1, aio_cb);
515
516 if (!req)
517 croak ("out of memory during aio_req allocation");
518
519 req->type = REQ_READAHEAD;
520 req->fd = PerlIO_fileno (fh);
521 req->offset = offset;
522 req->length = length;
523 req->callback = SvREFCNT_inc (callback);
524
525 send_req (req);
526}
527
528void
473aio_stat(fh_or_path,callback) 529aio_stat(fh_or_path,callback)
474 SV * fh_or_path 530 SV * fh_or_path
475 SV * callback 531 SV * callback
476 PROTOTYPE: $$ 532 PROTOTYPE: $$
477 ALIAS: 533 ALIAS:
478 aio_lstat = 1 534 aio_lstat = 1
479 CODE: 535 CODE:
536{
480 aio_req req; 537 aio_req req;
481 538
482 Newz (0, req, 1, aio_cb); 539 Newz (0, req, 1, aio_cb);
483 540
484 if (!req) 541 if (!req)
502 } 559 }
503 560
504 req->callback = SvREFCNT_inc (callback); 561 req->callback = SvREFCNT_inc (callback);
505 562
506 send_req (req); 563 send_req (req);
564}
507 565
508void 566void
509aio_unlink(pathname,callback) 567aio_unlink(pathname,callback)
510 SV * pathname 568 SV * pathname
511 SV * callback 569 SV * callback
512 PROTOTYPE: $$ 570 PROTOTYPE: $$
513 CODE: 571 CODE:
572{
514 aio_req req; 573 aio_req req;
515 574
516 Newz (0, req, 1, aio_cb); 575 Newz (0, req, 1, aio_cb);
517 576
518 if (!req) 577 if (!req)
522 req->data = newSVsv (pathname); 581 req->data = newSVsv (pathname);
523 req->dataptr = SvPV_nolen (req->data); 582 req->dataptr = SvPV_nolen (req->data);
524 req->callback = SvREFCNT_inc (callback); 583 req->callback = SvREFCNT_inc (callback);
525 584
526 send_req (req); 585 send_req (req);
586}
527 587
528int 588int
529poll_fileno() 589poll_fileno()
530 PROTOTYPE: 590 PROTOTYPE:
531 CODE: 591 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines