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.26 by root, Wed Aug 17 03:52:20 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;
66static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; 77static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER;
67static pthread_mutex_t frklock = PTHREAD_MUTEX_INITIALIZER;
68static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; 78static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER;
69 79
70static volatile aio_req reqs, reqe; /* queue start, queue end */ 80static volatile aio_req reqs, reqe; /* queue start, queue end */
71static volatile aio_req ress, rese; /* queue start, queue end */ 81static volatile aio_req ress, rese; /* queue start, queue end */
72 82
76 SvREFCNT_dec (req->data); 86 SvREFCNT_dec (req->data);
77 87
78 if (req->fh) 88 if (req->fh)
79 SvREFCNT_dec (req->fh); 89 SvREFCNT_dec (req->fh);
80 90
81 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) 91 if (req->fh2)
92 SvREFCNT_dec (req->fh2);
93
94 if (req->statdata)
82 Safefree (req->statdata); 95 Safefree (req->statdata);
83 96
84 if (req->callback) 97 if (req->callback)
85 SvREFCNT_dec (req->callback); 98 SvREFCNT_dec (req->callback);
86 99
104poll_cb () 117poll_cb ()
105{ 118{
106 dSP; 119 dSP;
107 int count = 0; 120 int count = 0;
108 int do_croak = 0; 121 int do_croak = 0;
109 aio_req req, prv; 122 aio_req req;
110 123
111 pthread_mutex_lock (&reslock); 124 for (;;)
112
113 {
114 /* read any signals sent by the worker threads */
115 char buf [32];
116 while (read (respipe [0], buf, 32) == 32)
117 ;
118 }
119
120 req = ress;
121 ress = rese = 0;
122
123 pthread_mutex_unlock (&reslock);
124
125 while (req)
126 { 125 {
126 pthread_mutex_lock (&reslock);
127 req = ress;
128
129 if (req)
130 {
131 ress = req->next;
132
133 if (!ress)
134 {
135 /* read any signals sent by the worker threads */
136 char buf [32];
137 while (read (respipe [0], buf, 32) == 32)
138 ;
139
140 rese = 0;
141 }
142 }
143
144 pthread_mutex_unlock (&reslock);
145
146 if (!req)
147 break;
148
127 nreqs--; 149 nreqs--;
128 150
129 if (req->type == REQ_QUIT) 151 if (req->type == REQ_QUIT)
130 started--; 152 started--;
131 else 153 else
132 { 154 {
133 int errorno = errno; 155 int errorno = errno;
134 errno = req->errorno; 156 errno = req->errorno;
135 157
136 if (req->type == REQ_READ) 158 if (req->type == REQ_READ)
137 SvCUR_set (req->data, req->dataoffset 159 SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0));
138 + req->result > 0 ? req->result : 0);
139 160
140 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) 161 if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE))
162 SvREADONLY_off (req->data);
163
164 if (req->statdata)
141 { 165 {
142 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; 166 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
143 PL_laststatval = req->result; 167 PL_laststatval = req->result;
144 PL_statcache = *(req->statdata); 168 PL_statcache = *(req->statdata);
145 } 169 }
166 if (SvOK (req->callback)) 190 if (SvOK (req->callback))
167 { 191 {
168 PUTBACK; 192 PUTBACK;
169 call_sv (req->callback, G_VOID | G_EVAL); 193 call_sv (req->callback, G_VOID | G_EVAL);
170 SPAGAIN; 194 SPAGAIN;
195
196 if (SvTRUE (ERRSV))
197 {
198 free_req (req);
199 croak (0);
200 }
171 } 201 }
172 202
173 LEAVE; 203 LEAVE;
174 204
175 do_croak = SvTRUE (ERRSV);
176
177 errno = errorno; 205 errno = errorno;
178 count++; 206 count++;
179 } 207 }
180 208
181 prv = req;
182 req = req->next;
183 free_req (prv); 209 free_req (req);
184
185 if (do_croak)
186 croak (0);
187 } 210 }
188 211
189 return count; 212 return count;
190} 213}
191 214
212} 235}
213 236
214static void 237static void
215send_req (aio_req req) 238send_req (aio_req req)
216{ 239{
240 while (started < wanted && nreqs >= started)
241 start_thread ();
242
217 nreqs++; 243 nreqs++;
218 244
219 pthread_mutex_lock (&reqlock); 245 pthread_mutex_lock (&reqlock);
220 246
221 req->next = 0; 247 req->next = 0;
229 reqe = reqs = req; 255 reqe = reqs = req;
230 256
231 pthread_cond_signal (&reqwait); 257 pthread_cond_signal (&reqwait);
232 pthread_mutex_unlock (&reqlock); 258 pthread_mutex_unlock (&reqlock);
233 259
234 while (nreqs > max_outstanding) 260 if (nreqs > max_outstanding)
261 for (;;)
262 {
263 poll_cb ();
264
265 if (nreqs <= max_outstanding)
266 break;
267
268 poll_wait ();
269 }
270}
271
272static void
273end_thread (void)
274{
275 aio_req req;
276 Newz (0, req, 1, aio_cb);
277 req->type = REQ_QUIT;
278
279 send_req (req);
280}
281
282static void min_parallel (int nthreads)
283{
284 if (wanted < nthreads)
285 wanted = nthreads;
286}
287
288static void max_parallel (int nthreads)
289{
290 int cur = started;
291
292 if (wanted > nthreads)
293 wanted = nthreads;
294
295 while (cur > wanted)
296 {
297 end_thread ();
298 cur--;
299 }
300
301 while (started > wanted)
235 { 302 {
236 poll_wait (); 303 poll_wait ();
237 poll_cb (); 304 poll_cb ();
238 } 305 }
239} 306}
240 307
241static void
242end_thread (void)
243{
244 aio_req req;
245 Newz (0, req, 1, aio_cb);
246 req->type = REQ_QUIT;
247
248 send_req (req);
249}
250
251
252static void min_parallel (int nthreads)
253{
254 while (nthreads > started)
255 start_thread ();
256}
257
258static void max_parallel (int nthreads)
259{
260 int cur = started;
261 while (cur > nthreads)
262 {
263 end_thread ();
264 cur--;
265 }
266
267 while (started > nthreads)
268 {
269 poll_wait ();
270 poll_cb ();
271 }
272}
273
274static void create_pipe () 308static void create_pipe ()
275{ 309{
276 if (pipe (respipe)) 310 if (pipe (respipe))
277 croak ("unable to initialize result pipe"); 311 croak ("unable to initialize result pipe");
278 312
283 croak ("cannot set result pipe to nonblocking mode"); 317 croak ("cannot set result pipe to nonblocking mode");
284} 318}
285 319
286static void atfork_prepare (void) 320static void atfork_prepare (void)
287{ 321{
288 pthread_mutex_lock (&frklock);
289 pthread_mutex_lock (&reqlock); 322 pthread_mutex_lock (&reqlock);
290 pthread_mutex_lock (&reslock); 323 pthread_mutex_lock (&reslock);
291} 324}
292 325
293static void atfork_parent (void) 326static void atfork_parent (void)
294{ 327{
295 pthread_mutex_unlock (&reslock); 328 pthread_mutex_unlock (&reslock);
296 pthread_mutex_unlock (&reqlock); 329 pthread_mutex_unlock (&reqlock);
297 pthread_mutex_unlock (&frklock);
298} 330}
299 331
300static void atfork_child (void) 332static void atfork_child (void)
301{ 333{
302 int restart = started; 334 aio_req prv;
335
303 started = 0; 336 started = 0;
304 337
305 while (reqs) 338 while (reqs)
306 { 339 {
307 free_req (reqs); 340 prv = reqs;
308 reqs = reqs->next; 341 reqs = prv->next;
342 free_req (prv);
309 } 343 }
310 344
311 reqs = reqe = 0; 345 reqs = reqe = 0;
312 346
313 while (ress) 347 while (ress)
314 { 348 {
315 free_req (ress); 349 prv = ress;
316 ress = ress->next; 350 ress = prv->next;
351 free_req (prv);
317 } 352 }
318 353
319 ress = rese = 0; 354 ress = rese = 0;
320 355
321 close (respipe [0]); 356 close (respipe [0]);
322 close (respipe [1]); 357 close (respipe [1]);
323
324 create_pipe (); 358 create_pipe ();
325 359
326 atfork_parent (); 360 atfork_parent ();
327
328 min_parallel (restart);
329} 361}
330 362
331/*****************************************************************************/ 363/*****************************************************************************/
332/* work around various missing functions */ 364/* work around various missing functions */
333 365
398 430
399 errno = 0; 431 errno = 0;
400} 432}
401#endif 433#endif
402 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
403/*****************************************************************************/ 509/*****************************************************************************/
404 510
405static void * 511static void *
406aio_proc (void *thr_arg) 512aio_proc (void *thr_arg)
407{ 513{
438 { 544 {
439 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;
440 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;
441 547
442 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;
443 550
444 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; 551 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break;
445 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; 552 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break;
446 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; 553 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break;
447 554
496 \ 603 \
497 Newz (0, req, 1, aio_cb); \ 604 Newz (0, req, 1, aio_cb); \
498 if (!req) \ 605 if (!req) \
499 croak ("out of memory during aio_req allocation"); \ 606 croak ("out of memory during aio_req allocation"); \
500 \ 607 \
501 req->callback = SvREFCNT_inc (callback); 608 req->callback = newSVsv (callback);
502 609
503MODULE = IO::AIO PACKAGE = IO::AIO 610MODULE = IO::AIO PACKAGE = IO::AIO
504 611
505PROTOTYPES: ENABLE 612PROTOTYPES: ENABLE
506 613
570 677
571void 678void
572aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) 679aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef)
573 SV * fh 680 SV * fh
574 UV offset 681 UV offset
575 IV length 682 UV length
576 SV * data 683 SV * data
577 IV dataoffset 684 UV dataoffset
578 SV * callback 685 SV * callback
579 ALIAS: 686 ALIAS:
580 aio_read = REQ_READ 687 aio_read = REQ_READ
581 aio_write = REQ_WRITE 688 aio_write = REQ_WRITE
582 PROTOTYPE: $$$$$;$ 689 PROTOTYPE: $$$$$;$
619 : IoOFP (sv_2io (fh))); 726 : IoOFP (sv_2io (fh)));
620 req->offset = offset; 727 req->offset = offset;
621 req->length = length; 728 req->length = length;
622 req->data = SvREFCNT_inc (data); 729 req->data = SvREFCNT_inc (data);
623 req->dataptr = (char *)svptr + dataoffset; 730 req->dataptr = (char *)svptr + dataoffset;
624 req->callback = SvREFCNT_inc (callback); 731
732 if (!SvREADONLY (data))
733 {
734 SvREADONLY_on (data);
735 req->data2ptr = (void *)data;
736 }
625 737
626 send_req (req); 738 send_req (req);
627 } 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);
628} 763}
629 764
630void 765void
631aio_readahead(fh,offset,length,callback=&PL_sv_undef) 766aio_readahead(fh,offset,length,callback=&PL_sv_undef)
632 SV * fh 767 SV * fh
658{ 793{
659 dREQ; 794 dREQ;
660 795
661 New (0, req->statdata, 1, Stat_t); 796 New (0, req->statdata, 1, Stat_t);
662 if (!req->statdata) 797 if (!req->statdata)
798 {
799 free_req (req);
663 croak ("out of memory during aio_req->statdata allocation (sorry, i just leaked memory, too)"); 800 croak ("out of memory during aio_req->statdata allocation");
801 }
664 802
665 if (SvPOK (fh_or_path)) 803 if (SvPOK (fh_or_path))
666 { 804 {
667 req->type = ix; 805 req->type = ix;
668 req->data = newSVsv (fh_or_path); 806 req->data = newSVsv (fh_or_path);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines