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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines