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.24 by root, Wed Aug 17 03:01:56 2005 UTC vs.
Revision 1.36 by root, Tue Aug 23 01:18:04 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# elif __solaris /* not yet */
29# include <sys/sendfile.h>
30# else
31# error sendfile support requested but not available
32# endif
33#endif
23 34
24#if __ia64 35#if __ia64
25# define STACKSIZE 65536 36# define STACKSIZE 65536
26#else 37#else
27# define STACKSIZE 4096 38# define STACKSIZE 4096
29 40
30enum { 41enum {
31 REQ_QUIT, 42 REQ_QUIT,
32 REQ_OPEN, REQ_CLOSE, 43 REQ_OPEN, REQ_CLOSE,
33 REQ_READ, REQ_WRITE, REQ_READAHEAD, 44 REQ_READ, REQ_WRITE, REQ_READAHEAD,
45 REQ_SENDFILE,
34 REQ_STAT, REQ_LSTAT, REQ_FSTAT, 46 REQ_STAT, REQ_LSTAT, REQ_FSTAT,
35 REQ_FSYNC, REQ_FDATASYNC, 47 REQ_FSYNC, REQ_FDATASYNC,
36 REQ_UNLINK, REQ_RMDIR, 48 REQ_UNLINK, REQ_RMDIR,
37 REQ_SYMLINK, 49 REQ_SYMLINK,
38}; 50};
40typedef struct aio_cb { 52typedef struct aio_cb {
41 struct aio_cb *volatile next; 53 struct aio_cb *volatile next;
42 54
43 int type; 55 int type;
44 56
45 int fd; 57 int fd, fd2;
46 off_t offset; 58 off_t offset;
47 size_t length; 59 size_t length;
48 ssize_t result; 60 ssize_t result;
49 mode_t mode; /* open */ 61 mode_t mode; /* open */
50 int errorno; 62 int errorno;
51 SV *data, *callback, *fh; 63 SV *data, *callback;
64 SV *fh, *fh2;
52 void *dataptr, *data2ptr; 65 void *dataptr, *data2ptr;
53 STRLEN dataoffset; 66 STRLEN dataoffset;
54 67
55 Stat_t *statdata; 68 Stat_t *statdata;
56} aio_cb; 69} aio_cb;
57 70
58typedef aio_cb *aio_req; 71typedef aio_cb *aio_req;
59 72
60static int started; 73static int started, wanted;
61static volatile int nreqs; 74static volatile int nreqs;
62static int max_outstanding = 1<<30; 75static int max_outstanding = 1<<30;
63static int respipe [2]; 76static int respipe [2];
64 77
65static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; 78static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER;
66static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; 79static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER;
67static pthread_mutex_t frklock = PTHREAD_MUTEX_INITIALIZER;
68static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; 80static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER;
69 81
70static volatile aio_req reqs, reqe; /* queue start, queue end */ 82static volatile aio_req reqs, reqe; /* queue start, queue end */
71static volatile aio_req ress, rese; /* queue start, queue end */ 83static volatile aio_req ress, rese; /* queue start, queue end */
84
85static void free_req (aio_req req)
86{
87 if (req->data)
88 SvREFCNT_dec (req->data);
89
90 if (req->fh)
91 SvREFCNT_dec (req->fh);
92
93 if (req->fh2)
94 SvREFCNT_dec (req->fh2);
95
96 if (req->statdata)
97 Safefree (req->statdata);
98
99 if (req->callback)
100 SvREFCNT_dec (req->callback);
101
102 Safefree (req);
103}
72 104
73static void 105static void
74poll_wait () 106poll_wait ()
75{ 107{
76 if (nreqs && !ress) 108 if (nreqs && !ress)
87poll_cb () 119poll_cb ()
88{ 120{
89 dSP; 121 dSP;
90 int count = 0; 122 int count = 0;
91 int do_croak = 0; 123 int do_croak = 0;
92 aio_req req, prv; 124 aio_req req;
93 125
94 pthread_mutex_lock (&reslock); 126 for (;;)
95
96 {
97 /* read any signals sent by the worker threads */
98 char buf [32];
99 while (read (respipe [0], buf, 32) == 32)
100 ;
101 }
102
103 req = ress;
104 ress = rese = 0;
105
106 pthread_mutex_unlock (&reslock);
107
108 while (req)
109 { 127 {
128 pthread_mutex_lock (&reslock);
129 req = ress;
130
131 if (req)
132 {
133 ress = req->next;
134
135 if (!ress)
136 {
137 /* read any signals sent by the worker threads */
138 char buf [32];
139 while (read (respipe [0], buf, 32) == 32)
140 ;
141
142 rese = 0;
143 }
144 }
145
146 pthread_mutex_unlock (&reslock);
147
148 if (!req)
149 break;
150
110 nreqs--; 151 nreqs--;
111 152
112 if (req->type == REQ_QUIT) 153 if (req->type == REQ_QUIT)
113 started--; 154 started--;
114 else 155 else
115 { 156 {
116 int errorno = errno; 157 int errorno = errno;
117 errno = req->errorno; 158 errno = req->errorno;
118 159
119 if (req->type == REQ_READ) 160 if (req->type == REQ_READ)
120 SvCUR_set (req->data, req->dataoffset 161 SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0));
121 + req->result > 0 ? req->result : 0);
122 162
163 if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE))
164 SvREADONLY_off (req->data);
165
123 if (req->data) 166 if (req->statdata)
124 SvREFCNT_dec (req->data);
125
126 if (req->fh)
127 SvREFCNT_dec (req->fh);
128
129 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT)
130 { 167 {
131 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; 168 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
132 PL_laststatval = req->result; 169 PL_laststatval = req->result;
133 PL_statcache = *(req->statdata); 170 PL_statcache = *(req->statdata);
134
135 Safefree (req->statdata);
136 } 171 }
137 172
138 ENTER; 173 ENTER;
139 PUSHMARK (SP); 174 PUSHMARK (SP);
140 XPUSHs (sv_2mortal (newSViv (req->result))); 175 XPUSHs (sv_2mortal (newSViv (req->result)));
157 if (SvOK (req->callback)) 192 if (SvOK (req->callback))
158 { 193 {
159 PUTBACK; 194 PUTBACK;
160 call_sv (req->callback, G_VOID | G_EVAL); 195 call_sv (req->callback, G_VOID | G_EVAL);
161 SPAGAIN; 196 SPAGAIN;
197
198 if (SvTRUE (ERRSV))
199 {
200 free_req (req);
201 croak (0);
202 }
162 } 203 }
163 204
164 do_croak = SvTRUE (ERRSV);
165
166 LEAVE; 205 LEAVE;
167
168 if (req->callback)
169 SvREFCNT_dec (req->callback);
170 206
171 errno = errorno; 207 errno = errorno;
172 count++; 208 count++;
173 } 209 }
174 210
175 prv = req; 211 free_req (req);
176 req = req->next;
177 Safefree (prv);
178
179 if (do_croak)
180 croak (0);
181 } 212 }
182 213
183 return count; 214 return count;
184} 215}
185 216
206} 237}
207 238
208static void 239static void
209send_req (aio_req req) 240send_req (aio_req req)
210{ 241{
242 while (started < wanted && nreqs >= started)
243 start_thread ();
244
211 nreqs++; 245 nreqs++;
212 246
213 pthread_mutex_lock (&reqlock); 247 pthread_mutex_lock (&reqlock);
214 248
215 req->next = 0; 249 req->next = 0;
223 reqe = reqs = req; 257 reqe = reqs = req;
224 258
225 pthread_cond_signal (&reqwait); 259 pthread_cond_signal (&reqwait);
226 pthread_mutex_unlock (&reqlock); 260 pthread_mutex_unlock (&reqlock);
227 261
228 while (nreqs > max_outstanding) 262 if (nreqs > max_outstanding)
263 for (;;)
264 {
265 poll_cb ();
266
267 if (nreqs <= max_outstanding)
268 break;
269
270 poll_wait ();
271 }
272}
273
274static void
275end_thread (void)
276{
277 aio_req req;
278 Newz (0, req, 1, aio_cb);
279 req->type = REQ_QUIT;
280
281 send_req (req);
282}
283
284static void min_parallel (int nthreads)
285{
286 if (wanted < nthreads)
287 wanted = nthreads;
288}
289
290static void max_parallel (int nthreads)
291{
292 int cur = started;
293
294 if (wanted > nthreads)
295 wanted = nthreads;
296
297 while (cur > wanted)
298 {
299 end_thread ();
300 cur--;
301 }
302
303 while (started > wanted)
229 { 304 {
230 poll_wait (); 305 poll_wait ();
231 poll_cb (); 306 poll_cb ();
232 } 307 }
233} 308}
234 309
235static void 310static void create_pipe ()
236end_thread (void)
237{ 311{
312 if (pipe (respipe))
313 croak ("unable to initialize result pipe");
314
315 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK))
316 croak ("cannot set result pipe to nonblocking mode");
317
318 if (fcntl (respipe [1], F_SETFL, O_NONBLOCK))
319 croak ("cannot set result pipe to nonblocking mode");
320}
321
322static void atfork_prepare (void)
323{
324 pthread_mutex_lock (&reqlock);
325 pthread_mutex_lock (&reslock);
326}
327
328static void atfork_parent (void)
329{
330 pthread_mutex_unlock (&reslock);
331 pthread_mutex_unlock (&reqlock);
332}
333
334static void atfork_child (void)
335{
238 aio_req req; 336 aio_req prv;
239 New (0, req, 1, aio_cb);
240 req->type = REQ_QUIT;
241 337
242 send_req (req); 338 started = 0;
243}
244 339
245 340 while (reqs)
246static void min_parallel (int nthreads) 341 {
247{ 342 prv = reqs;
248 while (nthreads > started) 343 reqs = prv->next;
249 start_thread (); 344 free_req (prv);
250}
251
252static void max_parallel (int nthreads)
253{
254 int cur = started;
255 while (cur > nthreads)
256 {
257 end_thread ();
258 cur--;
259 } 345 }
260 346
261 while (started > nthreads) 347 reqs = reqe = 0;
348
349 while (ress)
262 { 350 {
263 poll_wait (); 351 prv = ress;
264 poll_cb (); 352 ress = prv->next;
353 free_req (prv);
265 } 354 }
266}
267
268static int fork_started;
269
270static void atfork_prepare (void)
271{
272 pthread_mutex_lock (&frklock);
273
274 fork_started = started;
275
276 for (;;) {
277 while (nreqs)
278 { 355
279 poll_wait ();
280 poll_cb ();
281 }
282
283 max_parallel (0);
284
285 pthread_mutex_lock (&reqlock);
286
287 if (!nreqs && !started)
288 break;
289
290 pthread_mutex_unlock (&reqlock);
291
292 min_parallel (fork_started);
293 }
294
295 pthread_mutex_lock (&reslock);
296
297 assert (!started);
298 assert (!nreqs);
299 assert (!reqs && !reqe);
300 assert (!ress && !rese);
301}
302
303static void atfork_parent (void)
304{
305 pthread_mutex_unlock (&reslock);
306 min_parallel (fork_started);
307 pthread_mutex_unlock (&reqlock);
308 pthread_mutex_unlock (&frklock);
309}
310
311static void atfork_child (void)
312{
313 reqs = reqe = 0; 356 ress = rese = 0;
357
358 close (respipe [0]);
359 close (respipe [1]);
360 create_pipe ();
314 361
315 atfork_parent (); 362 atfork_parent ();
316} 363}
317 364
318/*****************************************************************************/ 365/*****************************************************************************/
385 432
386 errno = 0; 433 errno = 0;
387} 434}
388#endif 435#endif
389 436
437/* sendfile always needs emulation */
438static ssize_t
439sendfile_ (int ofd, int ifd, off_t offset, size_t count)
440{
441 ssize_t res;
442
443 if (!count)
444 return 0;
445
446#if HAVE_SENDFILE
447# if __linux
448 res = sendfile (ofd, ifd, &offset, count);
449
450# elif __freebsd
451 /*
452 * Of course, the freebsd sendfile is a dire hack with no thoughts
453 * wasted on making it similar to other I/O functions.
454 */
455 {
456 off_t sbytes;
457 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0);
458
459 if (res < 0 && sbytes)
460 /* maybe only on EAGAIN only: as usual, the manpage leaves you guessing */
461 res = sbytes;
462 }
463
464# elif __hpux
465 res = sendfile (ofd, ifd, offset, count, 0, 0);
466
467# elif __solaris
468 {
469 struct sendfilevec vec;
470 size_t sbytes;
471
472 vec.sfv_fd = ifd;
473 vec.sfv_flag = 0;
474 vec.sfv_off = offset;
475 vec.sfv_len = count;
476
477 res = sendfilev (ofd, &vec, 1, &sbytes);
478
479 if (res < 0 && sbytes)
480 res = sbytes;
481 }
482
483# else
484 res = -1;
485 errno = ENOSYS;
486# endif
487#endif
488
489 if (res < 0
490 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK
491#if __solaris
492 || errno == EAFNOSUPPORT || errno == EPROTOTYPE
493#endif
494 )
495 )
496 {
497 /* emulate sendfile. this is a major pain in the ass */
498 char *buf = malloc (4096);
499 res = 0;
500
501 for (;;)
502 {
503 ssize_t cnt;
504
505 cnt = pread (ifd, buf, 4096, offset);
506
507 if (cnt <= 0)
508 {
509 if (cnt && !res) res = -1;
510 break;
511 }
512
513 cnt = write (ofd, buf, cnt);
514
515 if (cnt <= 0)
516 {
517 if (cnt && !res) res = -1;
518 break;
519 }
520
521 offset += cnt;
522 res += cnt;
523 }
524
525 {
526 int errorno = errno;
527 free (buf);
528 errno = errorno;
529 }
530 }
531
532 return res;
533}
534
390/*****************************************************************************/ 535/*****************************************************************************/
391 536
392static void * 537static void *
393aio_proc (void *thr_arg) 538aio_proc (void *thr_arg)
394{ 539{
425 { 570 {
426 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; 571 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break;
427 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; 572 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break;
428 573
429 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; 574 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break;
575 case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length); break;
430 576
431 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; 577 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break;
432 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; 578 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break;
433 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; 579 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break;
434 580
483 \ 629 \
484 Newz (0, req, 1, aio_cb); \ 630 Newz (0, req, 1, aio_cb); \
485 if (!req) \ 631 if (!req) \
486 croak ("out of memory during aio_req allocation"); \ 632 croak ("out of memory during aio_req allocation"); \
487 \ 633 \
488 req->callback = SvREFCNT_inc (callback); 634 req->callback = newSVsv (callback);
489 635
490MODULE = IO::AIO PACKAGE = IO::AIO 636MODULE = IO::AIO PACKAGE = IO::AIO
491 637
492PROTOTYPES: ENABLE 638PROTOTYPES: ENABLE
493 639
494BOOT: 640BOOT:
495{ 641{
496 if (pipe (respipe)) 642 create_pipe ();
497 croak ("unable to initialize result pipe");
498
499 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK))
500 croak ("cannot set result pipe to nonblocking mode");
501
502 if (fcntl (respipe [1], F_SETFL, O_NONBLOCK))
503 croak ("cannot set result pipe to nonblocking mode");
504
505 pthread_atfork (atfork_prepare, atfork_parent, atfork_child); 643 pthread_atfork (atfork_prepare, atfork_parent, atfork_child);
506} 644}
507 645
508void 646void
509min_parallel(nthreads) 647min_parallel(nthreads)
565 703
566void 704void
567aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) 705aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef)
568 SV * fh 706 SV * fh
569 UV offset 707 UV offset
570 IV length 708 UV length
571 SV * data 709 SV * data
572 IV dataoffset 710 UV dataoffset
573 SV * callback 711 SV * callback
574 ALIAS: 712 ALIAS:
575 aio_read = REQ_READ 713 aio_read = REQ_READ
576 aio_write = REQ_WRITE 714 aio_write = REQ_WRITE
577 PROTOTYPE: $$$$$;$ 715 PROTOTYPE: $$$$$;$
614 : IoOFP (sv_2io (fh))); 752 : IoOFP (sv_2io (fh)));
615 req->offset = offset; 753 req->offset = offset;
616 req->length = length; 754 req->length = length;
617 req->data = SvREFCNT_inc (data); 755 req->data = SvREFCNT_inc (data);
618 req->dataptr = (char *)svptr + dataoffset; 756 req->dataptr = (char *)svptr + dataoffset;
619 req->callback = SvREFCNT_inc (callback); 757
758 if (!SvREADONLY (data))
759 {
760 SvREADONLY_on (data);
761 req->data2ptr = (void *)data;
762 }
620 763
621 send_req (req); 764 send_req (req);
622 } 765 }
766}
767
768void
769aio_sendfile(out_fh,in_fh,in_offset,length,callback=&PL_sv_undef)
770 SV * out_fh
771 SV * in_fh
772 UV in_offset
773 UV length
774 SV * callback
775 PROTOTYPE: $$$$;$
776 CODE:
777{
778 dREQ;
779
780 req->type = REQ_SENDFILE;
781 req->fh = newSVsv (out_fh);
782 req->fd = PerlIO_fileno (IoIFP (sv_2io (out_fh)));
783 req->fh2 = newSVsv (in_fh);
784 req->fd2 = PerlIO_fileno (IoIFP (sv_2io (in_fh)));
785 req->offset = in_offset;
786 req->length = length;
787
788 send_req (req);
623} 789}
624 790
625void 791void
626aio_readahead(fh,offset,length,callback=&PL_sv_undef) 792aio_readahead(fh,offset,length,callback=&PL_sv_undef)
627 SV * fh 793 SV * fh
653{ 819{
654 dREQ; 820 dREQ;
655 821
656 New (0, req->statdata, 1, Stat_t); 822 New (0, req->statdata, 1, Stat_t);
657 if (!req->statdata) 823 if (!req->statdata)
824 {
825 free_req (req);
658 croak ("out of memory during aio_req->statdata allocation (sorry, i just leaked memory, too)"); 826 croak ("out of memory during aio_req->statdata allocation");
827 }
659 828
660 if (SvPOK (fh_or_path)) 829 if (SvPOK (fh_or_path))
661 { 830 {
662 req->type = ix; 831 req->type = ix;
663 req->data = newSVsv (fh_or_path); 832 req->data = newSVsv (fh_or_path);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines