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.23 by root, Tue Aug 16 23:33:34 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 */
82
83static void free_req (aio_req req)
84{
85 if (req->data)
86 SvREFCNT_dec (req->data);
87
88 if (req->fh)
89 SvREFCNT_dec (req->fh);
90
91 if (req->fh2)
92 SvREFCNT_dec (req->fh2);
93
94 if (req->statdata)
95 Safefree (req->statdata);
96
97 if (req->callback)
98 SvREFCNT_dec (req->callback);
99
100 Safefree (req);
101}
72 102
73static void 103static void
74poll_wait () 104poll_wait ()
75{ 105{
76 if (nreqs && !ress) 106 if (nreqs && !ress)
86static int 116static int
87poll_cb () 117poll_cb ()
88{ 118{
89 dSP; 119 dSP;
90 int count = 0; 120 int count = 0;
121 int do_croak = 0;
91 aio_req req, prv; 122 aio_req req;
92 123
93 pthread_mutex_lock (&reslock); 124 for (;;)
94
95 {
96 /* read any signals sent by the worker threads */
97 char buf [32];
98 while (read (respipe [0], buf, 32) == 32)
99 ;
100 }
101
102 req = ress;
103 ress = rese = 0;
104
105 pthread_mutex_unlock (&reslock);
106
107 while (req)
108 { 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
109 nreqs--; 149 nreqs--;
110 150
111 if (req->type == REQ_QUIT) 151 if (req->type == REQ_QUIT)
112 started--; 152 started--;
113 else 153 else
114 { 154 {
115 int errorno = errno; 155 int errorno = errno;
116 errno = req->errorno; 156 errno = req->errorno;
117 157
118 if (req->type == REQ_READ) 158 if (req->type == REQ_READ)
119 SvCUR_set (req->data, req->dataoffset 159 SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0));
120 + req->result > 0 ? req->result : 0);
121 160
161 if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE))
162 SvREADONLY_off (req->data);
163
122 if (req->data) 164 if (req->statdata)
123 SvREFCNT_dec (req->data);
124
125 if (req->fh)
126 SvREFCNT_dec (req->fh);
127
128 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT)
129 { 165 {
130 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; 166 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
131 PL_laststatval = req->result; 167 PL_laststatval = req->result;
132 PL_statcache = *(req->statdata); 168 PL_statcache = *(req->statdata);
133
134 Safefree (req->statdata);
135 } 169 }
136 170
137 ENTER; 171 ENTER;
138 PUSHMARK (SP); 172 PUSHMARK (SP);
139 XPUSHs (sv_2mortal (newSViv (req->result))); 173 XPUSHs (sv_2mortal (newSViv (req->result)));
156 if (SvOK (req->callback)) 190 if (SvOK (req->callback))
157 { 191 {
158 PUTBACK; 192 PUTBACK;
159 call_sv (req->callback, G_VOID | G_EVAL); 193 call_sv (req->callback, G_VOID | G_EVAL);
160 SPAGAIN; 194 SPAGAIN;
195
196 if (SvTRUE (ERRSV))
197 {
198 free_req (req);
199 croak (0);
200 }
161 } 201 }
162 202
163 LEAVE; 203 LEAVE;
164
165 if (req->callback)
166 SvREFCNT_dec (req->callback);
167 204
168 errno = errorno; 205 errno = errorno;
169 count++; 206 count++;
170 } 207 }
171 208
172 prv = req; 209 free_req (req);
173 req = req->next;
174 Safefree (prv);
175
176 /* TODO: croak on errors? */
177 } 210 }
178 211
179 return count; 212 return count;
180} 213}
181 214
202} 235}
203 236
204static void 237static void
205send_req (aio_req req) 238send_req (aio_req req)
206{ 239{
240 while (started < wanted && nreqs >= started)
241 start_thread ();
242
207 nreqs++; 243 nreqs++;
208 244
209 pthread_mutex_lock (&reqlock); 245 pthread_mutex_lock (&reqlock);
210 246
211 req->next = 0; 247 req->next = 0;
219 reqe = reqs = req; 255 reqe = reqs = req;
220 256
221 pthread_cond_signal (&reqwait); 257 pthread_cond_signal (&reqwait);
222 pthread_mutex_unlock (&reqlock); 258 pthread_mutex_unlock (&reqlock);
223 259
224 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)
225 { 302 {
226 poll_wait (); 303 poll_wait ();
227 poll_cb (); 304 poll_cb ();
228 } 305 }
229} 306}
230 307
231static void 308static void create_pipe ()
232end_thread (void)
233{ 309{
310 if (pipe (respipe))
311 croak ("unable to initialize result pipe");
312
313 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK))
314 croak ("cannot set result pipe to nonblocking mode");
315
316 if (fcntl (respipe [1], F_SETFL, O_NONBLOCK))
317 croak ("cannot set result pipe to nonblocking mode");
318}
319
320static void atfork_prepare (void)
321{
322 pthread_mutex_lock (&reqlock);
323 pthread_mutex_lock (&reslock);
324}
325
326static void atfork_parent (void)
327{
328 pthread_mutex_unlock (&reslock);
329 pthread_mutex_unlock (&reqlock);
330}
331
332static void atfork_child (void)
333{
234 aio_req req; 334 aio_req prv;
235 New (0, req, 1, aio_cb);
236 req->type = REQ_QUIT;
237 335
238 send_req (req); 336 started = 0;
239}
240 337
241 338 while (reqs)
242static void min_parallel (int nthreads) 339 {
243{ 340 prv = reqs;
244 while (nthreads > started) 341 reqs = prv->next;
245 start_thread (); 342 free_req (prv);
246}
247
248static void max_parallel (int nthreads)
249{
250 int cur = started;
251 while (cur > nthreads)
252 {
253 end_thread ();
254 cur--;
255 } 343 }
256 344
257 while (started > nthreads) 345 reqs = reqe = 0;
346
347 while (ress)
258 { 348 {
259 poll_wait (); 349 prv = ress;
260 poll_cb (); 350 ress = prv->next;
351 free_req (prv);
261 } 352 }
262}
263
264static int fork_started;
265
266static void atfork_prepare (void)
267{
268 pthread_mutex_lock (&frklock);
269
270 fork_started = started;
271
272 for (;;) {
273 while (nreqs)
274 { 353
275 poll_wait ();
276 poll_cb ();
277 }
278
279 max_parallel (0);
280
281 pthread_mutex_lock (&reqlock);
282
283 if (!nreqs && !started)
284 break;
285
286 pthread_mutex_unlock (&reqlock);
287
288 min_parallel (fork_started);
289 }
290
291 pthread_mutex_lock (&reslock);
292
293 assert (!started);
294 assert (!nreqs);
295 assert (!reqs && !reqe);
296 assert (!ress && !rese);
297}
298
299static void atfork_parent (void)
300{
301 pthread_mutex_unlock (&reslock);
302 min_parallel (fork_started);
303 pthread_mutex_unlock (&reqlock);
304 pthread_mutex_unlock (&frklock);
305}
306
307static void atfork_child (void)
308{
309 reqs = reqe = 0; 354 ress = rese = 0;
355
356 close (respipe [0]);
357 close (respipe [1]);
358 create_pipe ();
310 359
311 atfork_parent (); 360 atfork_parent ();
312} 361}
313 362
314/*****************************************************************************/ 363/*****************************************************************************/
381 430
382 errno = 0; 431 errno = 0;
383} 432}
384#endif 433#endif
385 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
386/*****************************************************************************/ 509/*****************************************************************************/
387 510
388static void * 511static void *
389aio_proc (void *thr_arg) 512aio_proc (void *thr_arg)
390{ 513{
421 { 544 {
422 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;
423 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;
424 547
425 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;
426 550
427 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; 551 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break;
428 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; 552 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break;
429 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; 553 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break;
430 554
479 \ 603 \
480 Newz (0, req, 1, aio_cb); \ 604 Newz (0, req, 1, aio_cb); \
481 if (!req) \ 605 if (!req) \
482 croak ("out of memory during aio_req allocation"); \ 606 croak ("out of memory during aio_req allocation"); \
483 \ 607 \
484 req->callback = SvREFCNT_inc (callback); 608 req->callback = newSVsv (callback);
485 609
486MODULE = IO::AIO PACKAGE = IO::AIO 610MODULE = IO::AIO PACKAGE = IO::AIO
487 611
488PROTOTYPES: ENABLE 612PROTOTYPES: ENABLE
489 613
490BOOT: 614BOOT:
491{ 615{
492 if (pipe (respipe)) 616 create_pipe ();
493 croak ("unable to initialize result pipe");
494
495 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK))
496 croak ("cannot set result pipe to nonblocking mode");
497
498 if (fcntl (respipe [1], F_SETFL, O_NONBLOCK))
499 croak ("cannot set result pipe to nonblocking mode");
500
501 pthread_atfork (atfork_prepare, atfork_parent, atfork_child); 617 pthread_atfork (atfork_prepare, atfork_parent, atfork_child);
502} 618}
503 619
504void 620void
505min_parallel(nthreads) 621min_parallel(nthreads)
561 677
562void 678void
563aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) 679aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef)
564 SV * fh 680 SV * fh
565 UV offset 681 UV offset
566 IV length 682 UV length
567 SV * data 683 SV * data
568 IV dataoffset 684 UV dataoffset
569 SV * callback 685 SV * callback
570 ALIAS: 686 ALIAS:
571 aio_read = REQ_READ 687 aio_read = REQ_READ
572 aio_write = REQ_WRITE 688 aio_write = REQ_WRITE
573 PROTOTYPE: $$$$$;$ 689 PROTOTYPE: $$$$$;$
610 : IoOFP (sv_2io (fh))); 726 : IoOFP (sv_2io (fh)));
611 req->offset = offset; 727 req->offset = offset;
612 req->length = length; 728 req->length = length;
613 req->data = SvREFCNT_inc (data); 729 req->data = SvREFCNT_inc (data);
614 req->dataptr = (char *)svptr + dataoffset; 730 req->dataptr = (char *)svptr + dataoffset;
615 req->callback = SvREFCNT_inc (callback); 731
732 if (!SvREADONLY (data))
733 {
734 SvREADONLY_on (data);
735 req->data2ptr = (void *)data;
736 }
616 737
617 send_req (req); 738 send_req (req);
618 } 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);
619} 763}
620 764
621void 765void
622aio_readahead(fh,offset,length,callback=&PL_sv_undef) 766aio_readahead(fh,offset,length,callback=&PL_sv_undef)
623 SV * fh 767 SV * fh
649{ 793{
650 dREQ; 794 dREQ;
651 795
652 New (0, req->statdata, 1, Stat_t); 796 New (0, req->statdata, 1, Stat_t);
653 if (!req->statdata) 797 if (!req->statdata)
798 {
799 free_req (req);
654 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 }
655 802
656 if (SvPOK (fh_or_path)) 803 if (SvPOK (fh_or_path))
657 { 804 {
658 req->type = ix; 805 req->type = ix;
659 req->data = newSVsv (fh_or_path); 806 req->data = newSVsv (fh_or_path);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines