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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines