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.35 by root, Tue Aug 23 01:16:50 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 */
72 84
76 SvREFCNT_dec (req->data); 88 SvREFCNT_dec (req->data);
77 89
78 if (req->fh) 90 if (req->fh)
79 SvREFCNT_dec (req->fh); 91 SvREFCNT_dec (req->fh);
80 92
81 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) 93 if (req->fh2)
94 SvREFCNT_dec (req->fh2);
95
96 if (req->statdata)
82 Safefree (req->statdata); 97 Safefree (req->statdata);
83 98
84 if (req->callback) 99 if (req->callback)
85 SvREFCNT_dec (req->callback); 100 SvREFCNT_dec (req->callback);
86 101
104poll_cb () 119poll_cb ()
105{ 120{
106 dSP; 121 dSP;
107 int count = 0; 122 int count = 0;
108 int do_croak = 0; 123 int do_croak = 0;
109 aio_req req, prv; 124 aio_req req;
110 125
111 pthread_mutex_lock (&reslock); 126 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 { 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
127 nreqs--; 151 nreqs--;
128 152
129 if (req->type == REQ_QUIT) 153 if (req->type == REQ_QUIT)
130 started--; 154 started--;
131 else 155 else
132 { 156 {
133 int errorno = errno; 157 int errorno = errno;
134 errno = req->errorno; 158 errno = req->errorno;
135 159
136 if (req->type == REQ_READ) 160 if (req->type == REQ_READ)
137 SvCUR_set (req->data, req->dataoffset 161 SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0));
138 + req->result > 0 ? req->result : 0);
139 162
140 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) 163 if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE))
164 SvREADONLY_off (req->data);
165
166 if (req->statdata)
141 { 167 {
142 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; 168 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
143 PL_laststatval = req->result; 169 PL_laststatval = req->result;
144 PL_statcache = *(req->statdata); 170 PL_statcache = *(req->statdata);
145 } 171 }
166 if (SvOK (req->callback)) 192 if (SvOK (req->callback))
167 { 193 {
168 PUTBACK; 194 PUTBACK;
169 call_sv (req->callback, G_VOID | G_EVAL); 195 call_sv (req->callback, G_VOID | G_EVAL);
170 SPAGAIN; 196 SPAGAIN;
197
198 if (SvTRUE (ERRSV))
199 {
200 free_req (req);
201 croak (0);
202 }
171 } 203 }
172 204
173 LEAVE; 205 LEAVE;
174 206
175 do_croak = SvTRUE (ERRSV);
176
177 errno = errorno; 207 errno = errorno;
178 count++; 208 count++;
179 } 209 }
180 210
181 prv = req;
182 req = req->next;
183 free_req (prv); 211 free_req (req);
184
185 if (do_croak)
186 croak (0);
187 } 212 }
188 213
189 return count; 214 return count;
190} 215}
191 216
212} 237}
213 238
214static void 239static void
215send_req (aio_req req) 240send_req (aio_req req)
216{ 241{
242 while (started < wanted && nreqs >= started)
243 start_thread ();
244
217 nreqs++; 245 nreqs++;
218 246
219 pthread_mutex_lock (&reqlock); 247 pthread_mutex_lock (&reqlock);
220 248
221 req->next = 0; 249 req->next = 0;
229 reqe = reqs = req; 257 reqe = reqs = req;
230 258
231 pthread_cond_signal (&reqwait); 259 pthread_cond_signal (&reqwait);
232 pthread_mutex_unlock (&reqlock); 260 pthread_mutex_unlock (&reqlock);
233 261
234 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)
235 { 304 {
236 poll_wait (); 305 poll_wait ();
237 poll_cb (); 306 poll_cb ();
238 } 307 }
239} 308}
240 309
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 () 310static void create_pipe ()
275{ 311{
276 if (pipe (respipe)) 312 if (pipe (respipe))
277 croak ("unable to initialize result pipe"); 313 croak ("unable to initialize result pipe");
278 314
283 croak ("cannot set result pipe to nonblocking mode"); 319 croak ("cannot set result pipe to nonblocking mode");
284} 320}
285 321
286static void atfork_prepare (void) 322static void atfork_prepare (void)
287{ 323{
288 pthread_mutex_lock (&frklock);
289 pthread_mutex_lock (&reqlock); 324 pthread_mutex_lock (&reqlock);
290 pthread_mutex_lock (&reslock); 325 pthread_mutex_lock (&reslock);
291} 326}
292 327
293static void atfork_parent (void) 328static void atfork_parent (void)
294{ 329{
295 pthread_mutex_unlock (&reslock); 330 pthread_mutex_unlock (&reslock);
296 pthread_mutex_unlock (&reqlock); 331 pthread_mutex_unlock (&reqlock);
297 pthread_mutex_unlock (&frklock);
298} 332}
299 333
300static void atfork_child (void) 334static void atfork_child (void)
301{ 335{
302 int restart = started; 336 aio_req prv;
337
303 started = 0; 338 started = 0;
304 339
305 while (reqs) 340 while (reqs)
306 { 341 {
307 free_req (reqs); 342 prv = reqs;
308 reqs = reqs->next; 343 reqs = prv->next;
344 free_req (prv);
309 } 345 }
310 346
311 reqs = reqe = 0; 347 reqs = reqe = 0;
312 348
313 while (ress) 349 while (ress)
314 { 350 {
315 free_req (ress); 351 prv = ress;
316 ress = ress->next; 352 ress = prv->next;
353 free_req (prv);
317 } 354 }
318 355
319 ress = rese = 0; 356 ress = rese = 0;
320 357
321 close (respipe [0]); 358 close (respipe [0]);
322 close (respipe [1]); 359 close (respipe [1]);
323
324 create_pipe (); 360 create_pipe ();
325 361
326 atfork_parent (); 362 atfork_parent ();
327
328 min_parallel (restart);
329} 363}
364
365/* currently noops */
366#define LOCK_FD(fd) do { } while (0)
367#define UNLOCK_FD(fd) do { } while (0)
330 368
331/*****************************************************************************/ 369/*****************************************************************************/
332/* work around various missing functions */ 370/* work around various missing functions */
333 371
334#if !HAVE_PREADWRITE 372#if !HAVE_PREADWRITE
346pread (int fd, void *buf, size_t count, off_t offset) 384pread (int fd, void *buf, size_t count, off_t offset)
347{ 385{
348 ssize_t res; 386 ssize_t res;
349 off_t ooffset; 387 off_t ooffset;
350 388
351 pthread_mutex_lock (&iolock); 389 LOCK_FD (fd);
390 pthread_mutex_lock (&iolock); /* replace by LOCK_FD and private buffer */
352 ooffset = lseek (fd, 0, SEEK_CUR); 391 ooffset = lseek (fd, 0, SEEK_CUR);
353 lseek (fd, offset, SEEK_SET); 392 lseek (fd, offset, SEEK_SET);
354 res = read (fd, buf, count); 393 res = read (fd, buf, count);
355 lseek (fd, ooffset, SEEK_SET); 394 lseek (fd, ooffset, SEEK_SET);
356 pthread_mutex_unlock (&iolock); 395 pthread_mutex_unlock (&iolock);
396 UNLOCK_FD (d);
357 397
358 return res; 398 return res;
359} 399}
360 400
361static ssize_t 401static ssize_t
362pwrite (int fd, void *buf, size_t count, off_t offset) 402pwrite (int fd, void *buf, size_t count, off_t offset)
363{ 403{
364 ssize_t res; 404 ssize_t res;
365 off_t ooffset; 405 off_t ooffset;
366 406
367 pthread_mutex_lock (&iolock); 407 LOCK_FD (fd);
408 pthread_mutex_lock (&iolock); /* replace by LOCK_FD and private buffer */
368 ooffset = lseek (fd, 0, SEEK_CUR); 409 ooffset = lseek (fd, 0, SEEK_CUR);
369 lseek (fd, offset, SEEK_SET); 410 lseek (fd, offset, SEEK_SET);
370 res = write (fd, buf, count); 411 res = write (fd, buf, count);
371 lseek (fd, offset, SEEK_SET); 412 lseek (fd, offset, SEEK_SET);
372 pthread_mutex_unlock (&iolock); 413 pthread_mutex_unlock (&iolock);
414 UNLOCK_FD (d);
373 415
374 return res; 416 return res;
375} 417}
376#endif 418#endif
377 419
397 } 439 }
398 440
399 errno = 0; 441 errno = 0;
400} 442}
401#endif 443#endif
444
445/* sendfile always needs emulation */
446static ssize_t
447sendfile_ (int ofd, int ifd, off_t offset, size_t count)
448{
449 ssize_t res;
450
451 if (!count)
452 return 0;
453
454 LOCK_FD (ofd);
455
456#if HAVE_SENDFILE
457# if __linux
458 res = sendfile (ofd, ifd, &offset, count);
459
460# elif __freebsd
461 /*
462 * Of course, the freebsd sendfile is a dire hack with no thoughts
463 * wasted on making it similar to other I/O functions.
464 */
465 {
466 off_t sbytes;
467 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0);
468
469 if (res < 0 && sbytes)
470 /* maybe only on EAGAIN only: as usual, the manpage leaves you guessing */
471 res = sbytes;
472 }
473
474# elif __hpux
475 res = sendfile (ofd, ifd, offset, count, 0, 0);
476
477# elif __solaris
478 {
479 struct sendfilevec vec;
480 size_t sbytes;
481
482 vec.sfv_fd = ifd;
483 vec.sfv_flag = 0;
484 vec.sfv_off = offset;
485 vec.sfv_len = count;
486
487 res = sendfilev (ofd, &vec, 1, &sbytes);
488
489 if (res < 0 && sbytes)
490 res = sbytes;
491 }
492
493# else
494 res = -1;
495 errno = ENOSYS;
496# endif
497#endif
498
499 if (res < 0
500 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK
501#if __solaris
502 || errno == EAFNOSUPPORT || errno == EPROTOTYPE
503#endif
504 )
505 )
506 {
507 /* emulate sendfile. this is a major pain in the ass */
508 char *buf = malloc (4096);
509 res = 0;
510
511 for (;;)
512 {
513 ssize_t cnt;
514
515 cnt = pread (ifd, buf, 4096, offset);
516
517 if (cnt <= 0)
518 {
519 if (cnt && !res) res = -1;
520 break;
521 }
522
523 cnt = write (ofd, buf, cnt);
524
525 if (cnt <= 0)
526 {
527 if (cnt && !res) res = -1;
528 break;
529 }
530
531 offset += cnt;
532 res += cnt;
533 }
534
535 {
536 int errorno = errno;
537 free (buf);
538 errno = errorno;
539 }
540 }
541
542 UNLOCK_FD (ofd);
543
544 return res;
545}
402 546
403/*****************************************************************************/ 547/*****************************************************************************/
404 548
405static void * 549static void *
406aio_proc (void *thr_arg) 550aio_proc (void *thr_arg)
438 { 582 {
439 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; 583 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; 584 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break;
441 585
442 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; 586 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break;
587 case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length); break;
443 588
444 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; 589 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break;
445 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; 590 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break;
446 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; 591 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break;
447 592
496 \ 641 \
497 Newz (0, req, 1, aio_cb); \ 642 Newz (0, req, 1, aio_cb); \
498 if (!req) \ 643 if (!req) \
499 croak ("out of memory during aio_req allocation"); \ 644 croak ("out of memory during aio_req allocation"); \
500 \ 645 \
501 req->callback = SvREFCNT_inc (callback); 646 req->callback = newSVsv (callback);
502 647
503MODULE = IO::AIO PACKAGE = IO::AIO 648MODULE = IO::AIO PACKAGE = IO::AIO
504 649
505PROTOTYPES: ENABLE 650PROTOTYPES: ENABLE
506 651
570 715
571void 716void
572aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) 717aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef)
573 SV * fh 718 SV * fh
574 UV offset 719 UV offset
575 IV length 720 UV length
576 SV * data 721 SV * data
577 IV dataoffset 722 UV dataoffset
578 SV * callback 723 SV * callback
579 ALIAS: 724 ALIAS:
580 aio_read = REQ_READ 725 aio_read = REQ_READ
581 aio_write = REQ_WRITE 726 aio_write = REQ_WRITE
582 PROTOTYPE: $$$$$;$ 727 PROTOTYPE: $$$$$;$
619 : IoOFP (sv_2io (fh))); 764 : IoOFP (sv_2io (fh)));
620 req->offset = offset; 765 req->offset = offset;
621 req->length = length; 766 req->length = length;
622 req->data = SvREFCNT_inc (data); 767 req->data = SvREFCNT_inc (data);
623 req->dataptr = (char *)svptr + dataoffset; 768 req->dataptr = (char *)svptr + dataoffset;
624 req->callback = SvREFCNT_inc (callback); 769
770 if (!SvREADONLY (data))
771 {
772 SvREADONLY_on (data);
773 req->data2ptr = (void *)data;
774 }
625 775
626 send_req (req); 776 send_req (req);
627 } 777 }
778}
779
780void
781aio_sendfile(out_fh,in_fh,in_offset,length,callback=&PL_sv_undef)
782 SV * out_fh
783 SV * in_fh
784 UV in_offset
785 UV length
786 SV * callback
787 PROTOTYPE: $$$$;$
788 CODE:
789{
790 dREQ;
791
792 req->type = REQ_SENDFILE;
793 req->fh = newSVsv (out_fh);
794 req->fd = PerlIO_fileno (IoIFP (sv_2io (out_fh)));
795 req->fh2 = newSVsv (in_fh);
796 req->fd2 = PerlIO_fileno (IoIFP (sv_2io (in_fh)));
797 req->offset = in_offset;
798 req->length = length;
799
800 send_req (req);
628} 801}
629 802
630void 803void
631aio_readahead(fh,offset,length,callback=&PL_sv_undef) 804aio_readahead(fh,offset,length,callback=&PL_sv_undef)
632 SV * fh 805 SV * fh
658{ 831{
659 dREQ; 832 dREQ;
660 833
661 New (0, req->statdata, 1, Stat_t); 834 New (0, req->statdata, 1, Stat_t);
662 if (!req->statdata) 835 if (!req->statdata)
836 {
837 free_req (req);
663 croak ("out of memory during aio_req->statdata allocation (sorry, i just leaked memory, too)"); 838 croak ("out of memory during aio_req->statdata allocation");
839 }
664 840
665 if (SvPOK (fh_or_path)) 841 if (SvPOK (fh_or_path))
666 { 842 {
667 req->type = ix; 843 req->type = ix;
668 req->data = newSVsv (fh_or_path); 844 req->data = newSVsv (fh_or_path);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines