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.27 by root, Wed Aug 17 04:47:02 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
142 errno = req->errorno; 156 errno = req->errorno;
143 157
144 if (req->type == REQ_READ) 158 if (req->type == REQ_READ)
145 SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); 159 SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0));
146 160
161 if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE))
162 SvREADONLY_off (req->data);
163
147 if (req->statdata) 164 if (req->statdata)
148 { 165 {
149 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; 166 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
150 PL_laststatval = req->result; 167 PL_laststatval = req->result;
151 PL_statcache = *(req->statdata); 168 PL_statcache = *(req->statdata);
218} 235}
219 236
220static void 237static void
221send_req (aio_req req) 238send_req (aio_req req)
222{ 239{
240 while (started < wanted && nreqs >= started)
241 start_thread ();
242
223 nreqs++; 243 nreqs++;
224 244
225 pthread_mutex_lock (&reqlock); 245 pthread_mutex_lock (&reqlock);
226 246
227 req->next = 0; 247 req->next = 0;
259 send_req (req); 279 send_req (req);
260} 280}
261 281
262static void min_parallel (int nthreads) 282static void min_parallel (int nthreads)
263{ 283{
264 while (nthreads > started) 284 if (wanted < nthreads)
265 start_thread (); 285 wanted = nthreads;
266} 286}
267 287
268static void max_parallel (int nthreads) 288static void max_parallel (int nthreads)
269{ 289{
270 int cur = started; 290 int cur = started;
271 291
292 if (wanted > nthreads)
293 wanted = nthreads;
294
272 while (cur > nthreads) 295 while (cur > wanted)
273 { 296 {
274 end_thread (); 297 end_thread ();
275 cur--; 298 cur--;
276 } 299 }
277 300
278 while (started > nthreads) 301 while (started > wanted)
279 { 302 {
280 poll_wait (); 303 poll_wait ();
281 poll_cb (); 304 poll_cb ();
282 } 305 }
283} 306}
294 croak ("cannot set result pipe to nonblocking mode"); 317 croak ("cannot set result pipe to nonblocking mode");
295} 318}
296 319
297static void atfork_prepare (void) 320static void atfork_prepare (void)
298{ 321{
299 for (;;) {
300
301 for (;;)
302 {
303 poll_cb ();
304
305 if (!nreqs)
306 break;
307
308 poll_wait ();
309 }
310
311 pthread_mutex_lock (&reqlock); 322 pthread_mutex_lock (&reqlock);
312
313 if (!nreqs)
314 break;
315
316 pthread_mutex_unlock (&reqlock);
317 }
318
319 pthread_mutex_lock (&reslock); 323 pthread_mutex_lock (&reslock);
320
321 assert (!nreqs && !reqs && !ress);
322} 324}
323 325
324static void atfork_parent (void) 326static void atfork_parent (void)
325{ 327{
326 pthread_mutex_unlock (&reslock); 328 pthread_mutex_unlock (&reslock);
327 pthread_mutex_unlock (&reqlock); 329 pthread_mutex_unlock (&reqlock);
328} 330}
329 331
330static void atfork_child (void) 332static void atfork_child (void)
331{ 333{
332 int restart = started; 334 aio_req prv;
335
333 started = 0; 336 started = 0;
334 337
338 while (reqs)
339 {
340 prv = reqs;
341 reqs = prv->next;
342 free_req (prv);
343 }
344
345 reqs = reqe = 0;
346
347 while (ress)
348 {
349 prv = ress;
350 ress = prv->next;
351 free_req (prv);
352 }
353
354 ress = rese = 0;
355
356 close (respipe [0]);
357 close (respipe [1]);
358 create_pipe ();
359
335 atfork_parent (); 360 atfork_parent ();
336
337 min_parallel (restart);
338} 361}
339 362
340/*****************************************************************************/ 363/*****************************************************************************/
341/* work around various missing functions */ 364/* work around various missing functions */
342 365
407 430
408 errno = 0; 431 errno = 0;
409} 432}
410#endif 433#endif
411 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
412/*****************************************************************************/ 509/*****************************************************************************/
413 510
414static void * 511static void *
415aio_proc (void *thr_arg) 512aio_proc (void *thr_arg)
416{ 513{
447 { 544 {
448 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;
449 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;
450 547
451 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;
452 550
453 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; 551 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break;
454 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; 552 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break;
455 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; 553 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break;
456 554
579 677
580void 678void
581aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) 679aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef)
582 SV * fh 680 SV * fh
583 UV offset 681 UV offset
584 IV length 682 UV length
585 SV * data 683 SV * data
586 IV dataoffset 684 UV dataoffset
587 SV * callback 685 SV * callback
588 ALIAS: 686 ALIAS:
589 aio_read = REQ_READ 687 aio_read = REQ_READ
590 aio_write = REQ_WRITE 688 aio_write = REQ_WRITE
591 PROTOTYPE: $$$$$;$ 689 PROTOTYPE: $$$$$;$
629 req->offset = offset; 727 req->offset = offset;
630 req->length = length; 728 req->length = length;
631 req->data = SvREFCNT_inc (data); 729 req->data = SvREFCNT_inc (data);
632 req->dataptr = (char *)svptr + dataoffset; 730 req->dataptr = (char *)svptr + dataoffset;
633 731
732 if (!SvREADONLY (data))
733 {
734 SvREADONLY_on (data);
735 req->data2ptr = (void *)data;
736 }
737
634 send_req (req); 738 send_req (req);
635 } 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);
636} 763}
637 764
638void 765void
639aio_readahead(fh,offset,length,callback=&PL_sv_undef) 766aio_readahead(fh,offset,length,callback=&PL_sv_undef)
640 SV * fh 767 SV * fh

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines