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.8 by root, Mon Jul 11 02:53:59 2005 UTC vs.
Revision 1.13 by root, Fri Jul 22 08:25:22 2005 UTC

1#define _XOPEN_SOURCE 500
2
1#include "EXTERN.h" 3#include "EXTERN.h"
2#include "perl.h" 4#include "perl.h"
3#include "XSUB.h" 5#include "XSUB.h"
4 6
5#include <sys/types.h> 7#include <sys/types.h>
6#include <sys/stat.h> 8#include <sys/stat.h>
9
7#include <unistd.h> 10#include <unistd.h>
8#include <fcntl.h> 11#include <fcntl.h>
9#include <signal.h> 12#include <signal.h>
10#include <sched.h> 13#include <sched.h>
11#include <endian.h> 14#if __linux
15#include <sys/syscall.h>
16#endif
12 17
13#include <pthread.h> 18#include <pthread.h>
14#include <sys/syscall.h>
15 19
16typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ 20typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */
17typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ 21typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */
18typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ 22typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */
19 23
40 off_t offset; 44 off_t offset;
41 size_t length; 45 size_t length;
42 ssize_t result; 46 ssize_t result;
43 mode_t mode; /* open */ 47 mode_t mode; /* open */
44 int errorno; 48 int errorno;
45 SV *data, *callback; 49 SV *data, *callback, *fh;
46 void *dataptr; 50 void *dataptr;
47 STRLEN dataoffset; 51 STRLEN dataoffset;
48 52
49 Stat_t *statdata; 53 Stat_t *statdata;
50} aio_cb; 54} aio_cb;
51 55
52typedef aio_cb *aio_req; 56typedef aio_cb *aio_req;
53 57
54static int started; 58static int started;
55static int nreqs; 59static volatile int nreqs;
56static int max_outstanding = 1<<30; 60static int max_outstanding = 1<<30;
57static int respipe [2]; 61static int respipe [2];
58 62
59static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; 63static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER;
60static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; 64static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER;
64static volatile aio_req ress, rese; /* queue start, queue end */ 68static volatile aio_req ress, rese; /* queue start, queue end */
65 69
66static void 70static void
67poll_wait () 71poll_wait ()
68{ 72{
69 if (!nreqs) 73 if (nreqs && !ress)
70 return; 74 {
71
72 fd_set rfd; 75 fd_set rfd;
73 FD_ZERO(&rfd); 76 FD_ZERO(&rfd);
74 FD_SET(respipe [0], &rfd); 77 FD_SET(respipe [0], &rfd);
75 78
76 select (respipe [0] + 1, &rfd, 0, 0, 0); 79 select (respipe [0] + 1, &rfd, 0, 0, 0);
80 }
77} 81}
78 82
79static int 83static int
80poll_cb () 84poll_cb ()
81{ 85{
82 dSP; 86 dSP;
83 int count = 0; 87 int count = 0;
84 aio_req req; 88 aio_req req, prv;
85 89
90 pthread_mutex_lock (&reslock);
91
86 { 92 {
87 /* read and signals sent by the worker threads */ 93 /* read any signals sent by the worker threads */
88 char buf [32]; 94 char buf [32];
89 while (read (respipe [0], buf, 32) > 0) 95 while (read (respipe [0], buf, 32) > 0)
90 ; 96 ;
91 } 97 }
92 98
93 for (;;) 99 req = ress;
100 ress = rese = 0;
101
102 pthread_mutex_unlock (&reslock);
103
104 while (req)
94 { 105 {
95 pthread_mutex_lock (&reslock);
96
97 req = ress;
98
99 if (ress)
100 {
101 ress = ress->next;
102 if (!ress) rese = 0;
103 }
104
105 pthread_mutex_unlock (&reslock);
106
107 if (!req)
108 break;
109
110 nreqs--; 106 nreqs--;
111 107
112 if (req->type == REQ_QUIT) 108 if (req->type == REQ_QUIT)
113 started--; 109 started--;
114 else 110 else
121 + req->result > 0 ? req->result : 0); 117 + req->result > 0 ? req->result : 0);
122 118
123 if (req->data) 119 if (req->data)
124 SvREFCNT_dec (req->data); 120 SvREFCNT_dec (req->data);
125 121
122 if (req->fh)
123 SvREFCNT_dec (req->fh);
124
126 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) 125 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT)
127 { 126 {
128 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; 127 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
129 PL_laststatval = req->result; 128 PL_laststatval = req->result;
130 PL_statcache = *(req->statdata); 129 PL_statcache = *(req->statdata);
131 130
132 Safefree (req->statdata); 131 Safefree (req->statdata);
133 } 132 }
134 133
134 ENTER;
135 PUSHMARK (SP); 135 PUSHMARK (SP);
136 XPUSHs (sv_2mortal (newSViv (req->result))); 136 XPUSHs (sv_2mortal (newSViv (req->result)));
137 137
138 if (req->type == REQ_OPEN) 138 if (req->type == REQ_OPEN)
139 { 139 {
142 142
143 PUTBACK; 143 PUTBACK;
144 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); 144 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL);
145 SPAGAIN; 145 SPAGAIN;
146 146
147 fh = POPs; 147 fh = SvREFCNT_inc (POPs);
148 148
149 PUSHMARK (SP); 149 PUSHMARK (SP);
150 XPUSHs (fh); 150 XPUSHs (sv_2mortal (fh));
151 } 151 }
152 152
153 if (SvOK (req->callback)) 153 if (SvOK (req->callback))
154 { 154 {
155 PUTBACK; 155 PUTBACK;
156 call_sv (req->callback, G_VOID | G_EVAL); 156 call_sv (req->callback, G_VOID | G_EVAL);
157 SPAGAIN; 157 SPAGAIN;
158 } 158 }
159
160 LEAVE;
159 161
160 if (req->callback) 162 if (req->callback)
161 SvREFCNT_dec (req->callback); 163 SvREFCNT_dec (req->callback);
162 164
163 errno = errorno; 165 errno = errorno;
164 count++; 166 count++;
165 } 167 }
166 168
169 prv = req;
170 req = req->next;
167 Safefree (req); 171 Safefree (prv);
172
173 /* TODO: croak on errors? */
168 } 174 }
169 175
170 return count; 176 return count;
171} 177}
172 178
227 req->type = REQ_QUIT; 233 req->type = REQ_QUIT;
228 234
229 send_req (req); 235 send_req (req);
230} 236}
231 237
232static void
233read_write (int dowrite, int fd, off_t offset, size_t length,
234 SV *data, STRLEN dataoffset, SV *callback)
235{
236 aio_req req;
237 STRLEN svlen;
238 char *svptr = SvPV (data, svlen);
239
240 SvUPGRADE (data, SVt_PV);
241 SvPOK_on (data);
242
243 if (dataoffset < 0)
244 dataoffset += svlen;
245
246 if (dataoffset < 0 || dataoffset > svlen)
247 croak ("data offset outside of string");
248
249 if (dowrite)
250 {
251 /* write: check length and adjust. */
252 if (length < 0 || length + dataoffset > svlen)
253 length = svlen - dataoffset;
254 }
255 else
256 {
257 /* read: grow scalar as necessary */
258 svptr = SvGROW (data, length + dataoffset);
259 }
260
261 if (length < 0)
262 croak ("length must not be negative");
263
264 Newz (0, req, 1, aio_cb);
265
266 if (!req)
267 croak ("out of memory during aio_req allocation");
268
269 req->type = dowrite ? REQ_WRITE : REQ_READ;
270 req->fd = fd;
271 req->offset = offset;
272 req->length = length;
273 req->data = SvREFCNT_inc (data);
274 req->dataptr = (char *)svptr + dataoffset;
275 req->callback = SvREFCNT_inc (callback);
276
277 send_req (req);
278}
279
280static void * 238static void *
281aio_proc (void *thr_arg) 239aio_proc (void *thr_arg)
282{ 240{
283 aio_req req; 241 aio_req req;
284 int type; 242 int type;
309 267
310 type = req->type; 268 type = req->type;
311 269
312 switch (type) 270 switch (type)
313 { 271 {
314 case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, req->offset); break; 272 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break;
315 case REQ_WRITE: req->result = pwrite64 (req->fd, req->dataptr, req->length, req->offset); break; 273 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break;
316#if SYS_readahead 274#if SYS_readahead
317 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; 275 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break;
318#else 276#else
319 case REQ_READAHEAD: req->result = -1; errno = ENOSYS; break; 277 case REQ_READAHEAD: req->result = -1; errno = ENOSYS; break;
320#endif 278#endif
442 send_req (req); 400 send_req (req);
443} 401}
444 402
445void 403void
446aio_close(fh,callback=&PL_sv_undef) 404aio_close(fh,callback=&PL_sv_undef)
447 InputStream fh 405 SV * fh
448 SV * callback 406 SV * callback
449 PROTOTYPE: $;$ 407 PROTOTYPE: $;$
450 ALIAS: 408 ALIAS:
451 aio_close = REQ_CLOSE 409 aio_close = REQ_CLOSE
452 aio_fsync = REQ_FSYNC 410 aio_fsync = REQ_FSYNC
453 aio_fdatasync = REQ_FDATASYNC 411 aio_fdatasync = REQ_FDATASYNC
459 417
460 if (!req) 418 if (!req)
461 croak ("out of memory during aio_req allocation"); 419 croak ("out of memory during aio_req allocation");
462 420
463 req->type = ix; 421 req->type = ix;
422 req->fh = newSVsv (fh);
464 req->fd = PerlIO_fileno (fh); 423 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh)));
465 req->callback = SvREFCNT_inc (callback); 424 req->callback = SvREFCNT_inc (callback);
466 425
467 send_req (req); 426 send_req (req);
468} 427}
469 428
470void 429void
471aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) 430aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef)
472 InputStream fh 431 SV * fh
473 UV offset 432 UV offset
474 IV length 433 IV length
475 SV * data 434 SV * data
476 IV dataoffset 435 IV dataoffset
477 SV * callback 436 SV * callback
437 ALIAS:
438 aio_read = REQ_READ
439 aio_write = REQ_WRITE
478 PROTOTYPE: $$$$$;$ 440 PROTOTYPE: $$$$$;$
479 CODE: 441 CODE:
480 read_write (0, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); 442{
443 aio_req req;
444 STRLEN svlen;
445 char *svptr = SvPV (data, svlen);
481 446
482void 447 SvUPGRADE (data, SVt_PV);
483aio_write(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) 448 SvPOK_on (data);
484 OutputStream fh 449
485 UV offset 450 if (dataoffset < 0)
486 IV length 451 dataoffset += svlen;
487 SV * data 452
488 IV dataoffset 453 if (dataoffset < 0 || dataoffset > svlen)
489 SV * callback 454 croak ("data offset outside of string");
490 PROTOTYPE: $$$$$;$ 455
491 CODE: 456 if (ix == REQ_WRITE)
492 read_write (1, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); 457 {
458 /* write: check length and adjust. */
459 if (length < 0 || length + dataoffset > svlen)
460 length = svlen - dataoffset;
461 }
462 else
463 {
464 /* read: grow scalar as necessary */
465 svptr = SvGROW (data, length + dataoffset);
466 }
467
468 if (length < 0)
469 croak ("length must not be negative");
470
471 Newz (0, req, 1, aio_cb);
472
473 if (!req)
474 croak ("out of memory during aio_req allocation");
475
476 req->type = ix;
477 req->fh = newSVsv (fh);
478 req->fd = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh))
479 : IoOFP (sv_2io (fh)));
480 req->offset = offset;
481 req->length = length;
482 req->data = SvREFCNT_inc (data);
483 req->dataptr = (char *)svptr + dataoffset;
484 req->callback = SvREFCNT_inc (callback);
485
486 send_req (req);
487}
493 488
494void 489void
495aio_readahead(fh,offset,length,callback=&PL_sv_undef) 490aio_readahead(fh,offset,length,callback=&PL_sv_undef)
496 InputStream fh 491 SV * fh
497 UV offset 492 UV offset
498 IV length 493 IV length
499 SV * callback 494 SV * callback
500 PROTOTYPE: $$$;$ 495 PROTOTYPE: $$$;$
501 CODE: 496 CODE:
502{ 497{
503 aio_req req; 498 aio_req req;
504 499
509 504
510 if (!req) 505 if (!req)
511 croak ("out of memory during aio_req allocation"); 506 croak ("out of memory during aio_req allocation");
512 507
513 req->type = REQ_READAHEAD; 508 req->type = REQ_READAHEAD;
509 req->fh = newSVsv (fh);
514 req->fd = PerlIO_fileno (fh); 510 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh)));
515 req->offset = offset; 511 req->offset = offset;
516 req->length = length; 512 req->length = length;
517 req->callback = SvREFCNT_inc (callback); 513 req->callback = SvREFCNT_inc (callback);
518 514
519 send_req (req); 515 send_req (req);
547 req->dataptr = SvPV_nolen (req->data); 543 req->dataptr = SvPV_nolen (req->data);
548 } 544 }
549 else 545 else
550 { 546 {
551 req->type = REQ_FSTAT; 547 req->type = REQ_FSTAT;
548 req->fh = newSVsv (fh_or_path);
552 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); 549 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
553 } 550 }
554 551
555 req->callback = SvREFCNT_inc (callback); 552 req->callback = SvREFCNT_inc (callback);
556 553

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines