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.21 by root, Fri Aug 6 17:18:08 2004 UTC vs.
Revision 1.31 by root, Sun Jul 10 01:02:51 2005 UTC

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
14typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ 15typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */
15typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ 16typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */
16typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ 17typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */
17 18
18#define STACKSIZE 1024 /* yeah */ 19#if __i386 || __amd64
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
19 26
20enum { 27enum {
21 REQ_QUIT, 28 REQ_QUIT,
22 REQ_OPEN, REQ_CLOSE, REQ_READ, REQ_WRITE, 29 REQ_OPEN, REQ_CLOSE,
30 REQ_READ, REQ_WRITE, REQ_READAHEAD,
23 REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_UNLINK 31 REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_UNLINK,
32 REQ_FSYNC, REQ_FDATASYNC,
24}; 33};
25 34
26typedef struct { 35typedef struct {
27 char stack[STACKSIZE]; 36 char stack[STACKSIZE];
28} aio_thread; 37} aio_thread;
62 aio_thread *thr; 71 aio_thread *thr;
63 72
64 New (0, thr, 1, aio_thread); 73 New (0, thr, 1, aio_thread);
65 74
66 if (clone (aio_proc, 75 if (clone (aio_proc,
67 &(thr->stack[STACKSIZE]), 76 &(thr->stack[STACKSIZE - 16]),
68 CLONE_VM|CLONE_FS|CLONE_FILES, 77 CLONE_VM|CLONE_FS|CLONE_FILES,
69 thr) >= 0) 78 thr) >= 0)
70 started++; 79 started++;
71 else 80 else
72 Safefree (thr); 81 Safefree (thr);
88{ 97{
89 nreqs++; 98 nreqs++;
90 req->next = 0; 99 req->next = 0;
91 100
92 if (qe) 101 if (qe)
102 {
93 qe->next = req; 103 qe->next = req;
104 qe = req;
105 }
94 else 106 else
95 qe = qs = req; 107 qe = qs = req;
96 108
97 send_reqs (); 109 send_reqs ();
98} 110}
154 req->callback = SvREFCNT_inc (callback); 166 req->callback = SvREFCNT_inc (callback);
155 167
156 send_req (req); 168 send_req (req);
157} 169}
158 170
171static void
172poll_wait ()
173{
174 fd_set rfd;
175 FD_ZERO(&rfd);
176 FD_SET(respipe[0], &rfd);
177
178 select (respipe[0] + 1, &rfd, 0, 0, 0);
179}
180
159static int 181static int
160poll_cb (pTHX) 182poll_cb (pTHX)
161{ 183{
162 dSP; 184 dSP;
163 int count = 0; 185 int count = 0;
164 aio_req req; 186 aio_req req;
165 187
166 while (read (respipe[0], (void *)&req, sizeof (req)) == sizeof (req)) 188 while (read (respipe[0], (void *)&req, sizeof (req)) == sizeof (req))
167 { 189 {
190 nreqs--;
191
168 if (req->type == REQ_QUIT) 192 if (req->type == REQ_QUIT)
169 { 193 {
170 Safefree (req->thread); 194 Safefree (req->thread);
171 started--; 195 started--;
172 } 196 }
199 223
200 if (req->callback) 224 if (req->callback)
201 SvREFCNT_dec (req->callback); 225 SvREFCNT_dec (req->callback);
202 226
203 errno = errorno; 227 errno = errorno;
204 nreqs--;
205 count++; 228 count++;
206 } 229 }
207 230
208 Safefree (req); 231 Safefree (req);
209 } 232 }
216 239
217static sigset_t fullsigset; 240static sigset_t fullsigset;
218 241
219#undef errno 242#undef errno
220#include <asm/unistd.h> 243#include <asm/unistd.h>
244#include <linux/types.h>
221#include <sys/prctl.h> 245#include <sys/prctl.h>
246
247#if __alpha || __ia64 || __hppa || __v850__
248# define stat kernelstat
249# define stat64 kernelstat64
250# include <asm/stat.h>
251# undef stat
252# undef stat64
253#else
254# define kernelstat stat
255# define kernelstat64 stat64
256#endif
222 257
223#define COPY_STATDATA \ 258#define COPY_STATDATA \
224 req->statdata->st_dev = statdata.st_dev; \ 259 req->statdata->st_dev = statdata.st_dev; \
225 req->statdata->st_ino = statdata.st_ino; \ 260 req->statdata->st_ino = statdata.st_ino; \
226 req->statdata->st_mode = statdata.st_mode; \ 261 req->statdata->st_mode = statdata.st_mode; \
242 aio_req req; 277 aio_req req;
243 int errno; 278 int errno;
244 279
245 /* this is very much kernel-specific :(:(:( */ 280 /* this is very much kernel-specific :(:(:( */
246 /* we rely on gcc's ability to create closures. */ 281 /* we rely on gcc's ability to create closures. */
247 _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)
248 _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)
249 284
250 _syscall3(int,open,char *,pathname,int,flags,mode_t,mode) 285 _syscall3(long, open, char *, pathname, int, flags, int, mode)
251 _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);
252 289
253#ifdef __NR_pread64 290#ifndef __NR_fdatasync
254 _syscall5(int,pread64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi) 291# define __NR_fdatasync __NR_fsync
255 _syscall5(int,pwrite64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi)
256#elif __NR_pread
257 _syscall4(int,pread,int,fd,char *,buf,size_t,count,offset_t,offset)
258 _syscall4(int,pwrite,int,fd,char *,buf,size_t,count,offset_t,offset)
259#else
260# error "neither pread nor pread64 defined"
261#endif 292#endif
293 _syscall1(long, fdatasync, unsigned int, fd);
262 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
263 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);
310
264#ifdef __NR_stat64 311#if __NR_stat64
265 _syscall2(int,stat64, const char *, filename, struct stat64 *, buf) 312 _syscall2(long, stat64 , const char *, filename, struct kernelstat64 *, buf)
266 _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf) 313 _syscall2(long, lstat64, const char *, filename, struct kernelstat64 *, buf)
267 _syscall2(int,fstat64, int, fd, struct stat64 *, buf) 314 _syscall2(long, fstat64, int , fd , struct kernelstat64 *, buf)
268#elif __NR_stat 315#elif __NR_stat
269 _syscall2(int,stat, const char *, filename, struct stat *, buf) 316 _syscall2(long, stat , const char *, filename, struct kernelstat *, buf)
270 _syscall2(int,lstat, const char *, filename, struct stat *, buf) 317 _syscall2(long, lstat, const char *, filename, struct kernelstat *, buf)
271 _syscall2(int,fstat, int, fd, struct stat *, buf) 318 _syscall2(long, fstat, int , fd , struct kernelstat *, buf)
272#else 319#else
273# error "neither stat64 nor stat defined" 320# error "neither stat64 nor stat defined"
274#endif 321#endif
275 322
276 _syscall1(int,unlink, char *, filename); 323 /* the following two calls might clobber errno */
277
278 sigprocmask (SIG_SETMASK, &fullsigset, 0); 324 sigprocmask (SIG_SETMASK, &fullsigset, 0);
279 prctl (PR_SET_PDEATHSIG, SIGKILL); 325 prctl (PR_SET_PDEATHSIG, SIGKILL);
280 326
281 /* then loop */ 327 /* then loop */
282 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) 328 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req))
284 req->thread = thr; 330 req->thread = thr;
285 errno = 0; /* strictly unnecessary */ 331 errno = 0; /* strictly unnecessary */
286 332
287 switch (req->type) 333 switch (req->type)
288 { 334 {
289#ifdef __NR_pread64
290 case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); break; 335 case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, LOFF_ARG (req->offset)); break;
291 case REQ_WRITE: req->result = pwrite64(req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); break; 336 case REQ_WRITE: req->result = pwrite64 (req->fd, req->dataptr, req->length, LOFF_ARG (req->offset)); break;
337 case REQ_READAHEAD: req->result = readahead (req->fd, LOFF_ARG (req->offset), req->length); break;
338
339#if __NR_stat64
340 struct kernelstat64 statdata;
341 case REQ_STAT: req->result = stat64 (req->dataptr, &statdata); COPY_STATDATA; break;
342 case REQ_LSTAT: req->result = lstat64 (req->dataptr, &statdata); COPY_STATDATA; break;
343 case REQ_FSTAT: req->result = fstat64 (req->fd , &statdata); COPY_STATDATA; break;
292#else 344#else
293 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; 345 struct kernelstat statdata;
294 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; 346 case REQ_STAT: req->result = stat (req->dataptr, &statdata); COPY_STATDATA; break;
347 case REQ_LSTAT: req->result = lstat (req->dataptr, &statdata); COPY_STATDATA; break;
348 case REQ_FSTAT: req->result = fstat (req->fd , &statdata); COPY_STATDATA; break;
295#endif 349#endif
296#ifdef __NR_stat64 350
297 struct stat64 statdata;
298 case REQ_STAT: req->result = stat64 (req->dataptr, &statdata); COPY_STATDATA; break;
299 case REQ_LSTAT: req->result = lstat64 (req->dataptr, &statdata); COPY_STATDATA; break;
300 case REQ_FSTAT: req->result = fstat64 (req->fd, &statdata); COPY_STATDATA; break;
301#else
302 struct stat statdata;
303 case REQ_STAT: req->result = stat (req->dataptr, &statdata); COPY_STATDATA; break;
304 case REQ_LSTAT: req->result = lstat (req->dataptr, &statdata); COPY_STATDATA; break;
305 case REQ_FSTAT: req->result = fstat (req->fd, &statdata); COPY_STATDATA; break;
306#endif
307 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;
308 case REQ_CLOSE: req->result = close (req->fd); break; 352 case REQ_CLOSE: req->result = close (req->fd); break;
309 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;
310 357
311 case REQ_QUIT: 358 case REQ_QUIT:
312 default:
313 write (respipe[1], (void *)&req, sizeof (req)); 359 write (respipe[1], (void *)&req, sizeof (req));
314 return 0; 360 return 0;
361
362 default:
363 req->result = ENOSYS;
364 break;
315 } 365 }
316 366
317 req->errorno = errno; 367 req->errorno = errno;
318 write (respipe[1], (void *)&req, sizeof (req)); 368 write (respipe[1], (void *)&req, sizeof (req));
319 } 369 }
352void 402void
353max_parallel(nthreads) 403max_parallel(nthreads)
354 int nthreads 404 int nthreads
355 PROTOTYPE: $ 405 PROTOTYPE: $
356 CODE: 406 CODE:
407{
357 int cur = started; 408 int cur = started;
358 while (cur > nthreads) 409 while (cur > nthreads)
359 { 410 {
360 end_thread (); 411 end_thread ();
361 cur--; 412 cur--;
362 } 413 }
363 414
364 while (started > nthreads) 415 while (started > nthreads)
365 { 416 {
366 fd_set rfd; 417 poll_wait ();
367 FD_ZERO(&rfd);
368 FD_SET(respipe[0], &rfd);
369
370 select (respipe[0] + 1, &rfd, 0, 0, 0);
371 poll_cb (aTHX); 418 poll_cb (aTHX);
372 } 419 }
420}
373 421
374void 422void
375aio_open(pathname,flags,mode,callback) 423aio_open(pathname,flags,mode,callback)
376 SV * pathname 424 SV * pathname
377 int flags 425 int flags
378 int mode 426 int mode
379 SV * callback 427 SV * callback
380 PROTOTYPE: $$$$ 428 PROTOTYPE: $$$$
381 CODE: 429 CODE:
430{
382 aio_req req; 431 aio_req req;
383 432
384 Newz (0, req, 1, aio_cb); 433 Newz (0, req, 1, aio_cb);
385 434
386 if (!req) 435 if (!req)
392 req->fd = flags; 441 req->fd = flags;
393 req->mode = mode; 442 req->mode = mode;
394 req->callback = SvREFCNT_inc (callback); 443 req->callback = SvREFCNT_inc (callback);
395 444
396 send_req (req); 445 send_req (req);
446}
397 447
398void 448void
399aio_close(fh,callback) 449aio_close(fh,callback)
400 InputStream fh 450 InputStream fh
401 SV * callback 451 SV * callback
402 PROTOTYPE: $$ 452 PROTOTYPE: $$
453 ALIAS:
454 aio_close = REQ_CLOSE
455 aio_fsync = REQ_FSYNC
456 aio_fdatasync = REQ_FDATASYNC
403 CODE: 457 CODE:
458{
404 aio_req req; 459 aio_req req;
405 460
406 Newz (0, req, 1, aio_cb); 461 Newz (0, req, 1, aio_cb);
407 462
408 if (!req) 463 if (!req)
409 croak ("out of memory during aio_req allocation"); 464 croak ("out of memory during aio_req allocation");
410 465
411 req->type = REQ_CLOSE; 466 req->type = ix;
412 req->fd = PerlIO_fileno (fh); 467 req->fd = PerlIO_fileno (fh);
413 req->callback = SvREFCNT_inc (callback); 468 req->callback = SvREFCNT_inc (callback);
414 469
415 send_req (req); 470 send_req (req);
471}
416 472
417void 473void
418aio_read(fh,offset,length,data,dataoffset,callback) 474aio_read(fh,offset,length,data,dataoffset,callback)
419 InputStream fh 475 InputStream fh
420 UV offset 476 UV offset
437 PROTOTYPE: $$$$$$ 493 PROTOTYPE: $$$$$$
438 CODE: 494 CODE:
439 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);
440 496
441void 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
442aio_stat(fh_or_path,callback) 526aio_stat(fh_or_path,callback)
443 SV * fh_or_path 527 SV * fh_or_path
444 SV * callback 528 SV * callback
445 PROTOTYPE: $$ 529 PROTOTYPE: $$
446 ALIAS: 530 ALIAS:
447 aio_lstat = 1 531 aio_lstat = 1
448 CODE: 532 CODE:
533{
449 aio_req req; 534 aio_req req;
450 535
451 Newz (0, req, 1, aio_cb); 536 Newz (0, req, 1, aio_cb);
452 537
453 if (!req) 538 if (!req)
471 } 556 }
472 557
473 req->callback = SvREFCNT_inc (callback); 558 req->callback = SvREFCNT_inc (callback);
474 559
475 send_req (req); 560 send_req (req);
561}
476 562
477void 563void
478aio_unlink(pathname,callback) 564aio_unlink(pathname,callback)
479 SV * pathname 565 SV * pathname
480 SV * callback 566 SV * callback
481 PROTOTYPE: $$ 567 PROTOTYPE: $$
482 CODE: 568 CODE:
569{
483 aio_req req; 570 aio_req req;
484 571
485 Newz (0, req, 1, aio_cb); 572 Newz (0, req, 1, aio_cb);
486 573
487 if (!req) 574 if (!req)
491 req->data = newSVsv (pathname); 578 req->data = newSVsv (pathname);
492 req->dataptr = SvPV_nolen (req->data); 579 req->dataptr = SvPV_nolen (req->data);
493 req->callback = SvREFCNT_inc (callback); 580 req->callback = SvREFCNT_inc (callback);
494 581
495 send_req (req); 582 send_req (req);
583}
496 584
497int 585int
498poll_fileno() 586poll_fileno()
499 PROTOTYPE: 587 PROTOTYPE:
500 CODE: 588 CODE:
508 CODE: 596 CODE:
509 RETVAL = poll_cb (aTHX); 597 RETVAL = poll_cb (aTHX);
510 OUTPUT: 598 OUTPUT:
511 RETVAL 599 RETVAL
512 600
601void
602poll_wait()
603 PROTOTYPE:
604 CODE:
605 poll_wait ();
606
513int 607int
514nreqs() 608nreqs()
515 PROTOTYPE: 609 PROTOTYPE:
516 CODE: 610 CODE:
517 RETVAL = nreqs; 611 RETVAL = nreqs;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines