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.32 by root, Mon Aug 22 23:20:37 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;
66static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; 75static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER;
67static pthread_mutex_t frklock = PTHREAD_MUTEX_INITIALIZER;
68static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; 76static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER;
69 77
70static volatile aio_req reqs, reqe; /* queue start, queue end */ 78static volatile aio_req reqs, reqe; /* queue start, queue end */
71static volatile aio_req ress, rese; /* queue start, queue end */ 79static volatile aio_req ress, rese; /* queue start, queue end */
80
81static void free_req (aio_req req)
82{
83 if (req->data)
84 SvREFCNT_dec (req->data);
85
86 if (req->fh)
87 SvREFCNT_dec (req->fh);
88
89 if (req->fh2)
90 SvREFCNT_dec (req->fh2);
91
92 if (req->statdata)
93 Safefree (req->statdata);
94
95 if (req->callback)
96 SvREFCNT_dec (req->callback);
97
98 Safefree (req);
99}
72 100
73static void 101static void
74poll_wait () 102poll_wait ()
75{ 103{
76 if (nreqs && !ress) 104 if (nreqs && !ress)
86static int 114static int
87poll_cb () 115poll_cb ()
88{ 116{
89 dSP; 117 dSP;
90 int count = 0; 118 int count = 0;
119 int do_croak = 0;
91 aio_req req, prv; 120 aio_req req;
92 121
93 pthread_mutex_lock (&reslock); 122 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 { 123 {
124 pthread_mutex_lock (&reslock);
125 req = ress;
126
127 if (req)
128 {
129 ress = req->next;
130
131 if (!ress)
132 {
133 /* read any signals sent by the worker threads */
134 char buf [32];
135 while (read (respipe [0], buf, 32) == 32)
136 ;
137
138 rese = 0;
139 }
140 }
141
142 pthread_mutex_unlock (&reslock);
143
144 if (!req)
145 break;
146
109 nreqs--; 147 nreqs--;
110 148
111 if (req->type == REQ_QUIT) 149 if (req->type == REQ_QUIT)
112 started--; 150 started--;
113 else 151 else
114 { 152 {
115 int errorno = errno; 153 int errorno = errno;
116 errno = req->errorno; 154 errno = req->errorno;
117 155
118 if (req->type == REQ_READ) 156 if (req->type == REQ_READ)
119 SvCUR_set (req->data, req->dataoffset 157 SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0));
120 + req->result > 0 ? req->result : 0);
121 158
159 if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE))
160 SvREADONLY_off (req->data);
161
122 if (req->data) 162 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 { 163 {
130 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; 164 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
131 PL_laststatval = req->result; 165 PL_laststatval = req->result;
132 PL_statcache = *(req->statdata); 166 PL_statcache = *(req->statdata);
133
134 Safefree (req->statdata);
135 } 167 }
136 168
137 ENTER; 169 ENTER;
138 PUSHMARK (SP); 170 PUSHMARK (SP);
139 XPUSHs (sv_2mortal (newSViv (req->result))); 171 XPUSHs (sv_2mortal (newSViv (req->result)));
156 if (SvOK (req->callback)) 188 if (SvOK (req->callback))
157 { 189 {
158 PUTBACK; 190 PUTBACK;
159 call_sv (req->callback, G_VOID | G_EVAL); 191 call_sv (req->callback, G_VOID | G_EVAL);
160 SPAGAIN; 192 SPAGAIN;
193
194 if (SvTRUE (ERRSV))
195 {
196 free_req (req);
197 croak (0);
198 }
161 } 199 }
162 200
163 LEAVE; 201 LEAVE;
164
165 if (req->callback)
166 SvREFCNT_dec (req->callback);
167 202
168 errno = errorno; 203 errno = errorno;
169 count++; 204 count++;
170 } 205 }
171 206
172 prv = req; 207 free_req (req);
173 req = req->next;
174 Safefree (prv);
175
176 /* TODO: croak on errors? */
177 } 208 }
178 209
179 return count; 210 return count;
180} 211}
181 212
202} 233}
203 234
204static void 235static void
205send_req (aio_req req) 236send_req (aio_req req)
206{ 237{
238 while (started < wanted && nreqs >= started)
239 start_thread ();
240
207 nreqs++; 241 nreqs++;
208 242
209 pthread_mutex_lock (&reqlock); 243 pthread_mutex_lock (&reqlock);
210 244
211 req->next = 0; 245 req->next = 0;
219 reqe = reqs = req; 253 reqe = reqs = req;
220 254
221 pthread_cond_signal (&reqwait); 255 pthread_cond_signal (&reqwait);
222 pthread_mutex_unlock (&reqlock); 256 pthread_mutex_unlock (&reqlock);
223 257
224 while (nreqs > max_outstanding) 258 if (nreqs > max_outstanding)
259 for (;;)
260 {
261 poll_cb ();
262
263 if (nreqs <= max_outstanding)
264 break;
265
266 poll_wait ();
267 }
268}
269
270static void
271end_thread (void)
272{
273 aio_req req;
274 Newz (0, req, 1, aio_cb);
275 req->type = REQ_QUIT;
276
277 send_req (req);
278}
279
280static void min_parallel (int nthreads)
281{
282 if (wanted < nthreads)
283 wanted = nthreads;
284}
285
286static void max_parallel (int nthreads)
287{
288 int cur = started;
289
290 if (wanted > nthreads)
291 wanted = nthreads;
292
293 while (cur > wanted)
294 {
295 end_thread ();
296 cur--;
297 }
298
299 while (started > wanted)
225 { 300 {
226 poll_wait (); 301 poll_wait ();
227 poll_cb (); 302 poll_cb ();
228 } 303 }
229} 304}
230 305
231static void 306static void create_pipe ()
232end_thread (void)
233{ 307{
308 if (pipe (respipe))
309 croak ("unable to initialize result pipe");
310
311 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK))
312 croak ("cannot set result pipe to nonblocking mode");
313
314 if (fcntl (respipe [1], F_SETFL, O_NONBLOCK))
315 croak ("cannot set result pipe to nonblocking mode");
316}
317
318static void atfork_prepare (void)
319{
320 pthread_mutex_lock (&reqlock);
321 pthread_mutex_lock (&reslock);
322}
323
324static void atfork_parent (void)
325{
326 pthread_mutex_unlock (&reslock);
327 pthread_mutex_unlock (&reqlock);
328}
329
330static void atfork_child (void)
331{
234 aio_req req; 332 aio_req prv;
235 New (0, req, 1, aio_cb);
236 req->type = REQ_QUIT;
237 333
238 send_req (req); 334 started = 0;
239}
240 335
241 336 while (reqs)
242static void min_parallel (int nthreads) 337 {
243{ 338 prv = reqs;
244 while (nthreads > started) 339 reqs = prv->next;
245 start_thread (); 340 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 } 341 }
256 342
257 while (started > nthreads) 343 reqs = reqe = 0;
344
345 while (ress)
258 { 346 {
259 poll_wait (); 347 prv = ress;
260 poll_cb (); 348 ress = prv->next;
349 free_req (prv);
261 } 350 }
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 { 351
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; 352 ress = rese = 0;
353
354 close (respipe [0]);
355 close (respipe [1]);
356 create_pipe ();
310 357
311 atfork_parent (); 358 atfork_parent ();
312} 359}
313 360
314/*****************************************************************************/ 361/*****************************************************************************/
381 428
382 errno = 0; 429 errno = 0;
383} 430}
384#endif 431#endif
385 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 res = sbytes;
456 }
457
458#elif __hpux
459 res = sendfile (ofd, ifd, offset, count, 0, 0);
460
461#else
462 res = -1;
463 errno = ENOSYS;
464#endif
465
466 if (res < 0 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK))
467 {
468 /* emulate sendfile. this is a major pain in the ass */
469 char *buf = malloc (4096);
470 res = 0;
471
472 for (;;)
473 {
474 ssize_t cnt;
475
476 cnt = pread (ifd, buf, 4096, offset);
477
478 if (cnt <= 0)
479 {
480 if (cnt && !res) res = -1;
481 break;
482 }
483
484 cnt = write (ofd, buf, cnt);
485
486 if (cnt <= 0)
487 {
488 if (cnt && !res) res = -1;
489 break;
490 }
491
492 offset += cnt;
493 res += cnt;
494 }
495
496 {
497 int errorno = errno;
498 free (buf);
499 errno = errorno;
500 }
501 }
502
503 return res;
504}
505
386/*****************************************************************************/ 506/*****************************************************************************/
387 507
388static void * 508static void *
389aio_proc (void *thr_arg) 509aio_proc (void *thr_arg)
390{ 510{
421 { 541 {
422 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; 542 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; 543 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break;
424 544
425 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; 545 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break;
546 case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length); break;
426 547
427 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; 548 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break;
428 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; 549 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break;
429 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; 550 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break;
430 551
479 \ 600 \
480 Newz (0, req, 1, aio_cb); \ 601 Newz (0, req, 1, aio_cb); \
481 if (!req) \ 602 if (!req) \
482 croak ("out of memory during aio_req allocation"); \ 603 croak ("out of memory during aio_req allocation"); \
483 \ 604 \
484 req->callback = SvREFCNT_inc (callback); 605 req->callback = newSVsv (callback);
485 606
486MODULE = IO::AIO PACKAGE = IO::AIO 607MODULE = IO::AIO PACKAGE = IO::AIO
487 608
488PROTOTYPES: ENABLE 609PROTOTYPES: ENABLE
489 610
490BOOT: 611BOOT:
491{ 612{
492 if (pipe (respipe)) 613 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); 614 pthread_atfork (atfork_prepare, atfork_parent, atfork_child);
502} 615}
503 616
504void 617void
505min_parallel(nthreads) 618min_parallel(nthreads)
561 674
562void 675void
563aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) 676aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef)
564 SV * fh 677 SV * fh
565 UV offset 678 UV offset
566 IV length 679 UV length
567 SV * data 680 SV * data
568 IV dataoffset 681 UV dataoffset
569 SV * callback 682 SV * callback
570 ALIAS: 683 ALIAS:
571 aio_read = REQ_READ 684 aio_read = REQ_READ
572 aio_write = REQ_WRITE 685 aio_write = REQ_WRITE
573 PROTOTYPE: $$$$$;$ 686 PROTOTYPE: $$$$$;$
610 : IoOFP (sv_2io (fh))); 723 : IoOFP (sv_2io (fh)));
611 req->offset = offset; 724 req->offset = offset;
612 req->length = length; 725 req->length = length;
613 req->data = SvREFCNT_inc (data); 726 req->data = SvREFCNT_inc (data);
614 req->dataptr = (char *)svptr + dataoffset; 727 req->dataptr = (char *)svptr + dataoffset;
615 req->callback = SvREFCNT_inc (callback); 728
729 if (!SvREADONLY (data))
730 {
731 SvREADONLY_on (data);
732 req->data2ptr = (void *)data;
733 }
616 734
617 send_req (req); 735 send_req (req);
618 } 736 }
737}
738
739void
740aio_sendfile(out_fh,in_fh,in_offset,length,callback=&PL_sv_undef)
741 SV * out_fh
742 SV * in_fh
743 UV in_offset
744 UV length
745 SV * callback
746 PROTOTYPE: $$$$;$
747 CODE:
748{
749 dREQ;
750
751 req->type = REQ_SENDFILE;
752 req->fh = newSVsv (out_fh);
753 req->fd = PerlIO_fileno (IoIFP (sv_2io (out_fh)));
754 req->fh2 = newSVsv (in_fh);
755 req->fd2 = PerlIO_fileno (IoIFP (sv_2io (in_fh)));
756 req->offset = in_offset;
757 req->length = length;
758
759 send_req (req);
619} 760}
620 761
621void 762void
622aio_readahead(fh,offset,length,callback=&PL_sv_undef) 763aio_readahead(fh,offset,length,callback=&PL_sv_undef)
623 SV * fh 764 SV * fh
649{ 790{
650 dREQ; 791 dREQ;
651 792
652 New (0, req->statdata, 1, Stat_t); 793 New (0, req->statdata, 1, Stat_t);
653 if (!req->statdata) 794 if (!req->statdata)
795 {
796 free_req (req);
654 croak ("out of memory during aio_req->statdata allocation (sorry, i just leaked memory, too)"); 797 croak ("out of memory during aio_req->statdata allocation");
798 }
655 799
656 if (SvPOK (fh_or_path)) 800 if (SvPOK (fh_or_path))
657 { 801 {
658 req->type = ix; 802 req->type = ix;
659 req->data = newSVsv (fh_or_path); 803 req->data = newSVsv (fh_or_path);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines