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

Comparing IO-AIO/AIO.xs (file contents):
Revision 1.29 by root, Wed Aug 17 06:12:10 2005 UTC vs.
Revision 1.34 by root, Tue Aug 23 00:03:14 2005 UTC

4#include "EXTERN.h" 4#include "EXTERN.h"
5#include "perl.h" 5#include "perl.h"
6#include "XSUB.h" 6#include "XSUB.h"
7 7
8#include "autoconf/config.h" 8#include "autoconf/config.h"
9
10#include <pthread.h>
9 11
10#include <sys/types.h> 12#include <sys/types.h>
11#include <sys/stat.h> 13#include <sys/stat.h>
12 14
13#include <unistd.h> 15#include <unistd.h>
14#include <fcntl.h> 16#include <fcntl.h>
15#include <signal.h> 17#include <signal.h>
16#include <sched.h> 18#include <sched.h>
17 19
18#include <pthread.h> 20#if HAVE_SENDFILE
19 21# if __linux
20typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ 22# include <sys/sendfile.h>
21typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ 23# elif __freebsd
22typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ 24# include <sys/socket.h>
25# include <sys/uio.h>
26# elif __hpux
27# include <sys/socket.h>
28# else
29# error sendfile support requested but not available
30# endif
31#endif
23 32
24#if __ia64 33#if __ia64
25# define STACKSIZE 65536 34# define STACKSIZE 65536
26#else 35#else
27# define STACKSIZE 4096 36# define STACKSIZE 4096
29 38
30enum { 39enum {
31 REQ_QUIT, 40 REQ_QUIT,
32 REQ_OPEN, REQ_CLOSE, 41 REQ_OPEN, REQ_CLOSE,
33 REQ_READ, REQ_WRITE, REQ_READAHEAD, 42 REQ_READ, REQ_WRITE, REQ_READAHEAD,
43 REQ_SENDFILE,
34 REQ_STAT, REQ_LSTAT, REQ_FSTAT, 44 REQ_STAT, REQ_LSTAT, REQ_FSTAT,
35 REQ_FSYNC, REQ_FDATASYNC, 45 REQ_FSYNC, REQ_FDATASYNC,
36 REQ_UNLINK, REQ_RMDIR, 46 REQ_UNLINK, REQ_RMDIR,
37 REQ_SYMLINK, 47 REQ_SYMLINK,
38}; 48};
40typedef struct aio_cb { 50typedef struct aio_cb {
41 struct aio_cb *volatile next; 51 struct aio_cb *volatile next;
42 52
43 int type; 53 int type;
44 54
45 int fd; 55 int fd, fd2;
46 off_t offset; 56 off_t offset;
47 size_t length; 57 size_t length;
48 ssize_t result; 58 ssize_t result;
49 mode_t mode; /* open */ 59 mode_t mode; /* open */
50 int errorno; 60 int errorno;
51 SV *data, *callback, *fh; 61 SV *data, *callback;
62 SV *fh, *fh2;
52 void *dataptr, *data2ptr; 63 void *dataptr, *data2ptr;
53 STRLEN dataoffset; 64 STRLEN dataoffset;
54 65
55 Stat_t *statdata; 66 Stat_t *statdata;
56} aio_cb; 67} aio_cb;
57 68
58typedef aio_cb *aio_req; 69typedef aio_cb *aio_req;
59 70
60static int started; 71static int started, wanted;
61static volatile int nreqs; 72static volatile int nreqs;
62static int max_outstanding = 1<<30; 73static int max_outstanding = 1<<30;
63static int respipe [2]; 74static int respipe [2];
64 75
65static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; 76static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER;
74 if (req->data) 85 if (req->data)
75 SvREFCNT_dec (req->data); 86 SvREFCNT_dec (req->data);
76 87
77 if (req->fh) 88 if (req->fh)
78 SvREFCNT_dec (req->fh); 89 SvREFCNT_dec (req->fh);
90
91 if (req->fh2)
92 SvREFCNT_dec (req->fh2);
79 93
80 if (req->statdata) 94 if (req->statdata)
81 Safefree (req->statdata); 95 Safefree (req->statdata);
82 96
83 if (req->callback) 97 if (req->callback)
116 { 130 {
117 ress = req->next; 131 ress = req->next;
118 132
119 if (!ress) 133 if (!ress)
120 { 134 {
121 rese = 0;
122
123 /* read any signals sent by the worker threads */ 135 /* read any signals sent by the worker threads */
124 char buf [32]; 136 char buf [32];
125 while (read (respipe [0], buf, 32) == 32) 137 while (read (respipe [0], buf, 32) == 32)
126 ; 138 ;
139
140 rese = 0;
127 } 141 }
128 } 142 }
129 143
130 pthread_mutex_unlock (&reslock); 144 pthread_mutex_unlock (&reslock);
131 145
221} 235}
222 236
223static void 237static void
224send_req (aio_req req) 238send_req (aio_req req)
225{ 239{
240 while (started < wanted && nreqs >= started)
241 start_thread ();
242
226 nreqs++; 243 nreqs++;
227 244
228 pthread_mutex_lock (&reqlock); 245 pthread_mutex_lock (&reqlock);
229 246
230 req->next = 0; 247 req->next = 0;
262 send_req (req); 279 send_req (req);
263} 280}
264 281
265static void min_parallel (int nthreads) 282static void min_parallel (int nthreads)
266{ 283{
267 while (nthreads > started) 284 if (wanted < nthreads)
268 start_thread (); 285 wanted = nthreads;
269} 286}
270 287
271static void max_parallel (int nthreads) 288static void max_parallel (int nthreads)
272{ 289{
273 int cur = started; 290 int cur = started;
274 291
292 if (wanted > nthreads)
293 wanted = nthreads;
294
275 while (cur > nthreads) 295 while (cur > wanted)
276 { 296 {
277 end_thread (); 297 end_thread ();
278 cur--; 298 cur--;
279 } 299 }
280 300
281 while (started > nthreads) 301 while (started > wanted)
282 { 302 {
283 poll_wait (); 303 poll_wait ();
284 poll_cb (); 304 poll_cb ();
285 } 305 }
286} 306}
311 331
312static void atfork_child (void) 332static void atfork_child (void)
313{ 333{
314 aio_req prv; 334 aio_req prv;
315 335
316 int restart = started;
317 started = 0; 336 started = 0;
318 337
319 while (reqs) 338 while (reqs)
320 { 339 {
321 prv = reqs; 340 prv = reqs;
337 close (respipe [0]); 356 close (respipe [0]);
338 close (respipe [1]); 357 close (respipe [1]);
339 create_pipe (); 358 create_pipe ();
340 359
341 atfork_parent (); 360 atfork_parent ();
342
343 min_parallel (restart);
344} 361}
345 362
346/*****************************************************************************/ 363/*****************************************************************************/
347/* work around various missing functions */ 364/* work around various missing functions */
348 365
413 430
414 errno = 0; 431 errno = 0;
415} 432}
416#endif 433#endif
417 434
435/* sendfile always needs emulation */
436static ssize_t
437sendfile_ (int ofd, int ifd, off_t offset, size_t count)
438{
439 ssize_t res;
440
441 if (!count)
442 return 0;
443
444#if __linux
445 res = sendfile (ofd, ifd, &offset, count);
446
447#elif __freebsd
448 /*
449 * Of course, the freebsd sendfile is a dire hack with no thoughts
450 * wasted on making it similar to other i/o functions.
451 */
452 {
453 off_t sbytes;
454 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0);
455
456 if (!res && errno == EAGAIN)
457 /* maybe on others, too, as usual, the manpage leaves you guessing */
458 res = sbytes;
459 }
460
461#elif __hpux
462 res = sendfile (ofd, ifd, offset, count, 0, 0);
463
464#else
465 res = -1;
466 errno = ENOSYS;
467#endif
468
469 if (res < 0 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK))
470 {
471 /* emulate sendfile. this is a major pain in the ass */
472 char *buf = malloc (4096);
473 res = 0;
474
475 for (;;)
476 {
477 ssize_t cnt;
478
479 cnt = pread (ifd, buf, 4096, offset);
480
481 if (cnt <= 0)
482 {
483 if (cnt && !res) res = -1;
484 break;
485 }
486
487 cnt = write (ofd, buf, cnt);
488
489 if (cnt <= 0)
490 {
491 if (cnt && !res) res = -1;
492 break;
493 }
494
495 offset += cnt;
496 res += cnt;
497 }
498
499 {
500 int errorno = errno;
501 free (buf);
502 errno = errorno;
503 }
504 }
505
506 return res;
507}
508
418/*****************************************************************************/ 509/*****************************************************************************/
419 510
420static void * 511static void *
421aio_proc (void *thr_arg) 512aio_proc (void *thr_arg)
422{ 513{
453 { 544 {
454 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; 545 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break;
455 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; 546 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break;
456 547
457 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; 548 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break;
549 case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length); break;
458 550
459 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; 551 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break;
460 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; 552 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break;
461 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; 553 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break;
462 554
585 677
586void 678void
587aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) 679aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef)
588 SV * fh 680 SV * fh
589 UV offset 681 UV offset
590 IV length 682 UV length
591 SV * data 683 SV * data
592 IV dataoffset 684 UV dataoffset
593 SV * callback 685 SV * callback
594 ALIAS: 686 ALIAS:
595 aio_read = REQ_READ 687 aio_read = REQ_READ
596 aio_write = REQ_WRITE 688 aio_write = REQ_WRITE
597 PROTOTYPE: $$$$$;$ 689 PROTOTYPE: $$$$$;$
643 req->data2ptr = (void *)data; 735 req->data2ptr = (void *)data;
644 } 736 }
645 737
646 send_req (req); 738 send_req (req);
647 } 739 }
740}
741
742void
743aio_sendfile(out_fh,in_fh,in_offset,length,callback=&PL_sv_undef)
744 SV * out_fh
745 SV * in_fh
746 UV in_offset
747 UV length
748 SV * callback
749 PROTOTYPE: $$$$;$
750 CODE:
751{
752 dREQ;
753
754 req->type = REQ_SENDFILE;
755 req->fh = newSVsv (out_fh);
756 req->fd = PerlIO_fileno (IoIFP (sv_2io (out_fh)));
757 req->fh2 = newSVsv (in_fh);
758 req->fd2 = PerlIO_fileno (IoIFP (sv_2io (in_fh)));
759 req->offset = in_offset;
760 req->length = length;
761
762 send_req (req);
648} 763}
649 764
650void 765void
651aio_readahead(fh,offset,length,callback=&PL_sv_undef) 766aio_readahead(fh,offset,length,callback=&PL_sv_undef)
652 SV * fh 767 SV * fh

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines