ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Linux-AIO/AIO.xs
(Generate patch)

Comparing Linux-AIO/AIO.xs (file contents):
Revision 1.11 by root, Mon Oct 8 12:58:41 2001 UTC vs.
Revision 1.26 by root, Thu Jul 7 23:17:23 2005 UTC

1#define PERL_NO_GET_CONTEXT
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>
8#include <sys/stat.h>
6#include <unistd.h> 9#include <unistd.h>
7#include <fcntl.h> 10#include <fcntl.h>
8#include <signal.h> 11#include <signal.h>
9#include <sched.h> 12#include <sched.h>
13#include <endian.h>
10 14
11#define STACKSIZE 1024 /* yeah */ 15typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */
16typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */
17typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */
12 18
13enum { REQ_QUIT, REQ_READ, REQ_WRITE, REQ_OPEN, REQ_CLOSE }; 19// 128 seems to be enough most everywhere. alpha needs 256.
20#define STACKSIZE (256 * sizeof (long))
21
22enum {
23 REQ_QUIT,
24 REQ_OPEN, REQ_CLOSE, REQ_READ, REQ_WRITE,
25 REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_UNLINK
26};
14 27
15typedef struct { 28typedef struct {
16 char stack[STACKSIZE]; 29 char stack[STACKSIZE];
17} aio_thread; 30} aio_thread;
18 31
19typedef struct { 32typedef struct aio_cb {
33 struct aio_cb *next;
34
20 int type; 35 int type;
21 aio_thread *thread; 36 aio_thread *thread;
22 37
23/* read/write */
24 int fd; 38 int fd;
25 off_t offset; 39 off_t offset;
26 size_t length; 40 size_t length;
27 ssize_t result; 41 ssize_t result;
28 mode_t mode; /* open */ 42 mode_t mode; /* open */
29 int errorno; 43 int errorno;
30 SV *data, *callback; 44 SV *data, *callback;
31 void *dataptr; 45 void *dataptr;
32 STRLEN dataoffset; 46 STRLEN dataoffset;
47
48 Stat_t *statdata;
33} aio_cb; 49} aio_cb;
34 50
35typedef aio_cb *aio_req; 51typedef aio_cb *aio_req;
36 52
37static int started; 53static int started;
38static int nreqs; 54static int nreqs;
39static int reqpipe[2], respipe[2]; 55static int reqpipe[2], respipe[2];
40 56
57static aio_req qs, qe; /* queue start, queue end */
58
41static int aio_proc(void *arg); 59static int aio_proc(void *arg);
42 60
43static void 61static void
44start_thread(void) 62start_thread (void)
45{ 63{
46 aio_thread *thr; 64 aio_thread *thr;
47 65
48 New (0, thr, 1, aio_thread); 66 New (0, thr, 1, aio_thread);
49 67
50 if (clone (aio_proc, 68 if (clone (aio_proc,
51 &(thr->stack[STACKSIZE]), 69 &(thr->stack[STACKSIZE - sizeof (long)]),
52 CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND, 70 CLONE_VM|CLONE_FS|CLONE_FILES,
53 thr) >= 0) 71 thr) >= 0)
54 started++; 72 started++;
55 else 73 else
56 Safefree (thr); 74 Safefree (thr);
57} 75}
58 76
59static void 77static void
78send_reqs (void)
79{
80 /* this write is atomic */
81 while (qs && write (reqpipe[1], &qs, sizeof qs) == sizeof qs)
82 {
83 qs = qs->next;
84 if (!qs) qe = 0;
85 }
86}
87
88static void
89send_req (aio_req req)
90{
91 nreqs++;
92 req->next = 0;
93
94 if (qe)
95 {
96 qe->next = req;
97 qe = req;
98 }
99 else
100 qe = qs = req;
101
102 send_reqs ();
103}
104
105static void
60end_thread(void) 106end_thread (void)
61{ 107{
62 aio_req req; 108 aio_req req;
63 New (0, req, 1, aio_cb); 109 New (0, req, 1, aio_cb);
64 req->type = REQ_QUIT; 110 req->type = REQ_QUIT;
65 write (reqpipe[1], &req, sizeof (aio_req)); 111
112 send_req (req);
66} 113}
67 114
68static void 115static void
69send_req (aio_req req) 116read_write (pTHX_
70{
71 nreqs++;
72 write (reqpipe[1], &req, sizeof (aio_req));
73}
74
75static void
76read_write (pTHX_ int dowrite, int fd, off_t offset, size_t length, 117 int dowrite, int fd, off_t offset, size_t length,
77 SV *data, STRLEN dataoffset, SV*callback) 118 SV *data, STRLEN dataoffset, SV *callback)
78{ 119{
79 aio_req req; 120 aio_req req;
80 STRLEN svlen; 121 STRLEN svlen;
81 char *svptr = SvPV (data, svlen); 122 char *svptr = SvPV (data, svlen);
123
124 SvUPGRADE (data, SVt_PV);
125 SvPOK_on (data);
82 126
83 if (dataoffset < 0) 127 if (dataoffset < 0)
84 dataoffset += svlen; 128 dataoffset += svlen;
85 129
86 if (dataoffset < 0 || dataoffset > svlen) 130 if (dataoffset < 0 || dataoffset > svlen)
99 } 143 }
100 144
101 if (length < 0) 145 if (length < 0)
102 croak ("length must not be negative"); 146 croak ("length must not be negative");
103 147
104 New (0, req, 1, aio_cb); 148 Newz (0, req, 1, aio_cb);
105 149
106 if (!req) 150 if (!req)
107 croak ("out of memory during aio_req allocation"); 151 croak ("out of memory during aio_req allocation");
108 152
109 req->type = dowrite ? REQ_WRITE : REQ_READ; 153 req->type = dowrite ? REQ_WRITE : REQ_READ;
115 req->callback = SvREFCNT_inc (callback); 159 req->callback = SvREFCNT_inc (callback);
116 160
117 send_req (req); 161 send_req (req);
118} 162}
119 163
164static void
165poll_wait ()
166{
167 fd_set rfd;
168 FD_ZERO(&rfd);
169 FD_SET(respipe[0], &rfd);
170
171 select (respipe[0] + 1, &rfd, 0, 0, 0);
172}
173
120static int 174static int
121poll_cb (pTHX) 175poll_cb (pTHX)
122{ 176{
123 dSP; 177 dSP;
124 int count = 0; 178 int count = 0;
125 aio_req req; 179 aio_req req;
126 180
127 while (read (respipe[0], (void *)&req, sizeof (req)) == sizeof (req)) 181 while (read (respipe[0], (void *)&req, sizeof (req)) == sizeof (req))
128 { 182 {
183 nreqs--;
184
129 if (req->type == REQ_QUIT) 185 if (req->type == REQ_QUIT)
130 { 186 {
131 Safefree (req->thread); 187 Safefree (req->thread);
132 started--; 188 started--;
133 } 189 }
138 194
139 if (req->type == REQ_READ) 195 if (req->type == REQ_READ)
140 SvCUR_set (req->data, req->dataoffset 196 SvCUR_set (req->data, req->dataoffset
141 + req->result > 0 ? req->result : 0); 197 + req->result > 0 ? req->result : 0);
142 198
199 if (req->data)
200 SvREFCNT_dec (req->data);
201
202 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT)
203 {
204 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
205 PL_laststatval = req->result;
206 PL_statcache = *(req->statdata);
207
208 Safefree (req->statdata);
209 }
210
143 PUSHMARK (SP); 211 PUSHMARK (SP);
144 XPUSHs (sv_2mortal (newSViv (req->result))); 212 XPUSHs (sv_2mortal (newSViv (req->result)));
145 PUTBACK; 213 PUTBACK;
146 call_sv (req->callback, G_VOID); 214 call_sv (req->callback, G_VOID);
147 SPAGAIN; 215 SPAGAIN;
148 216
149 SvREFCNT_dec (req->data); 217 if (req->callback)
150 SvREFCNT_dec (req->callback); 218 SvREFCNT_dec (req->callback);
151 219
152 errno = errorno; 220 errno = errorno;
153 nreqs--;
154 count++; 221 count++;
155 } 222 }
156 223
157 Safefree (req); 224 Safefree (req);
158 } 225 }
159 226
227 if (qs)
228 send_reqs ();
229
160 return count; 230 return count;
161} 231}
162 232
163static sigset_t fullsigset; 233static sigset_t fullsigset;
164 234
165#undef errno 235#undef errno
166#include <asm/unistd.h> 236#include <asm/unistd.h>
237#include <sys/prctl.h>
238
239#if BYTE_ORDER == LITTLE_ENDIAN
240# define LONG_LONG_PAIR(HI, LO) LO, HI
241#elif BYTE_ORDER == BIG_ENDIAN
242# define LONG_LONG_PAIR(HI, LO) HI, LO
243#endif
244
245#if __alpha || __ia64 || __hppa || __v850__
246# define stat kernelstat
247# define stat64 kernelstat64
248# include <asm/stat.h>
249# undef stat
250# undef stat64
251#else
252# define kernelstat stat
253# define kernelstat64 stat64
254#endif
255
256#define COPY_STATDATA \
257 req->statdata->st_dev = statdata.st_dev; \
258 req->statdata->st_ino = statdata.st_ino; \
259 req->statdata->st_mode = statdata.st_mode; \
260 req->statdata->st_nlink = statdata.st_nlink; \
261 req->statdata->st_uid = statdata.st_uid; \
262 req->statdata->st_gid = statdata.st_gid; \
263 req->statdata->st_rdev = statdata.st_rdev; \
264 req->statdata->st_size = statdata.st_size; \
265 req->statdata->st_atime = statdata.st_atime; \
266 req->statdata->st_mtime = statdata.st_mtime; \
267 req->statdata->st_ctime = statdata.st_ctime; \
268 req->statdata->st_blksize = statdata.st_blksize; \
269 req->statdata->st_blocks = statdata.st_blocks; \
167 270
168static int 271static int
169aio_proc(void *thr_arg) 272aio_proc (void *thr_arg)
170{ 273{
171 aio_thread *thr = thr_arg; 274 aio_thread *thr = thr_arg;
172 aio_req req; 275 aio_req req;
173 int errno; 276 int errno;
174 277
278 /* this is very much kernel-specific :(:(:( */
175 /* we rely on gcc's ability to create closures. */ 279 /* we rely on gcc's ability to create closures. */
176 _syscall3(int,lseek,int,fd,off_t,offset,int,whence)
177 _syscall3(int,read,int,fd,char *,buf,off_t,count) 280 _syscall3(int,read,int,fd,char *,buf,size_t,count)
178 _syscall3(int,write,int,fd,char *,buf,off_t,count) 281 _syscall3(int,write,int,fd,char *,buf,size_t,count)
282
179 _syscall3(int,open,char *,pathname,int,flags,mode_t,mode) 283 _syscall3(int,open,char *,pathname,int,flags,mode_t,mode)
180 _syscall1(int,close,int,fd) 284 _syscall1(int,close,int,fd)
181 285
286#if __NR_pread64
287 _syscall5(int,pread64,int,fd,char *,buf,size_t,count,unsigned int,offset_lh,unsigned int,offset_hl)
288 _syscall5(int,pwrite64,int,fd,char *,buf,size_t,count,unsigned int,offset_lh,unsigned int,offset_hl)
289#elif __NR_pread
290 _syscall4(int,pread,int,fd,char *,buf,size_t,count,offset_t,offset)
291 _syscall4(int,pwrite,int,fd,char *,buf,size_t,count,offset_t,offset)
292#else
293# error "neither pread nor pread64 defined"
294#endif
295
296
297#if __NR_stat64
298 _syscall2(int,stat64, const char *, filename, struct kernelstat64 *, buf)
299 _syscall2(int,lstat64, const char *, filename, struct kernelstat64 *, buf)
300 _syscall2(int,fstat64, int, fd, struct kernelstat64 *, buf)
301#elif __NR_stat
302 _syscall2(int,stat, const char *, filename, struct kernelstat *, buf)
303 _syscall2(int,lstat, const char *, filename, struct kernelstat *, buf)
304 _syscall2(int,fstat, int, fd, struct kernelstat *, buf)
305#else
306# error "neither stat64 nor stat defined"
307#endif
308
309 _syscall1(int,unlink, char *, filename);
310
182 sigprocmask (SIG_SETMASK, &fullsigset, 0); 311 sigprocmask (SIG_SETMASK, &fullsigset, 0);
312 prctl (PR_SET_PDEATHSIG, SIGKILL);
183 313
184 /* then loop */ 314 /* then loop */
185 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) 315 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req))
186 { 316 {
187 req->thread = thr; 317 req->thread = thr;
188 errno = 0; 318 errno = 0; /* strictly unnecessary */
189 319
190 if (req->type == REQ_READ || req->type == REQ_WRITE) 320 switch (req->type)
191 { 321 {
192 if (lseek (req->fd, req->offset, SEEK_SET) == req->offset) 322#if __NR_pread64
193 {
194 if (req->type == REQ_READ)
195 req->result = read (req->fd, req->dataptr, req->length); 323 case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length,
196 else 324 LONG_LONG_PAIR (req->offset >> 32, req->offset & 0xffffffff)); break;
197 req->result = write(req->fd, req->dataptr, req->length); 325 case REQ_WRITE: req->result = pwrite64(req->fd, req->dataptr, req->length,
198 } 326 LONG_LONG_PAIR (req->offset >> 32, req->offset & 0xffffffff)); break;
199 } 327#else
200 else if (req->type == REQ_OPEN) 328 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break;
201 { 329 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break;
330#endif
331#if __NR_stat64
332 struct kernelstat64 statdata;
333 case REQ_STAT: req->result = stat64 (req->dataptr, &statdata); COPY_STATDATA; break;
334 case REQ_LSTAT: req->result = lstat64 (req->dataptr, &statdata); COPY_STATDATA; break;
335 case REQ_FSTAT: req->result = fstat64 (req->fd, &statdata); COPY_STATDATA; break;
336#else
337 struct kernelstat statdata;
338 case REQ_STAT: req->result = stat (req->dataptr, &statdata); COPY_STATDATA; break;
339 case REQ_LSTAT: req->result = lstat (req->dataptr, &statdata); COPY_STATDATA; break;
340 case REQ_FSTAT: req->result = fstat (req->fd, &statdata); COPY_STATDATA; break;
341#endif
202 req->result = open (req->dataptr, req->fd, req->mode); 342 case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break;
203 }
204 else if (req->type == REQ_CLOSE)
205 {
206 req->result = close (req->fd); 343 case REQ_CLOSE: req->result = close (req->fd); break;
207 } 344 case REQ_UNLINK: req->result = unlink (req->dataptr); break;
208 else 345
209 { 346 case REQ_QUIT:
347 default:
210 write (respipe[1], (void *)&req, sizeof (req)); 348 write (respipe[1], (void *)&req, sizeof (req));
211 break; 349 return 0;
212 } 350 }
213 351
214 req->errorno = errno; 352 req->errorno = errno;
215 write (respipe[1], (void *)&req, sizeof (req)); 353 write (respipe[1], (void *)&req, sizeof (req));
216 } 354 }
228 sigdelset (&fullsigset, SIGABRT); 366 sigdelset (&fullsigset, SIGABRT);
229 sigdelset (&fullsigset, SIGINT); 367 sigdelset (&fullsigset, SIGINT);
230 368
231 if (pipe (reqpipe) || pipe (respipe)) 369 if (pipe (reqpipe) || pipe (respipe))
232 croak ("unable to initialize request or result pipe"); 370 croak ("unable to initialize request or result pipe");
371
372 if (fcntl (reqpipe[1], F_SETFL, O_NONBLOCK))
373 croak ("cannot set result pipe to nonblocking mode");
233 374
234 if (fcntl (respipe[0], F_SETFL, O_NONBLOCK)) 375 if (fcntl (respipe[0], F_SETFL, O_NONBLOCK))
235 croak ("cannot set result pipe to nonblocking mode"); 376 croak ("cannot set result pipe to nonblocking mode");
236} 377}
237 378
253 { 394 {
254 end_thread (); 395 end_thread ();
255 cur--; 396 cur--;
256 } 397 }
257 398
258 poll_cb ();
259 while (started > nthreads) 399 while (started > nthreads)
260 { 400 {
261 sched_yield (); 401 poll_wait ();
262 poll_cb (); 402 poll_cb (aTHX);
263 } 403 }
264 404
265void 405void
266aio_read(fh,offset,length,data,dataoffset,callback)
267 PerlIO * fh
268 UV offset
269 STRLEN length
270 SV * data
271 STRLEN dataoffset
272 SV * callback
273 PROTOTYPE: $$$$$$
274 ALIAS:
275 aio_write = 1
276 CODE:
277 SvUPGRADE (data, SVt_PV);
278 SvPOK_on (data);
279 read_write (aTHX_ ix, PerlIO_fileno (fh), offset, length, data, dataoffset, callback);
280
281void
282aio_open(pathname,flags,mode,callback) 406aio_open(pathname,flags,mode,callback)
283 char * pathname 407 SV * pathname
284 int flags 408 int flags
285 int mode 409 int mode
286 SV * callback 410 SV * callback
287 PROTOTYPE: $$$$ 411 PROTOTYPE: $$$$
288 CODE: 412 CODE:
289 aio_req req; 413 aio_req req;
290 414
291 New (0, req, 1, aio_cb); 415 Newz (0, req, 1, aio_cb);
292 416
293 if (!req) 417 if (!req)
294 croak ("out of memory during aio_req allocation"); 418 croak ("out of memory during aio_req allocation");
295 419
296 req->type = REQ_OPEN; 420 req->type = REQ_OPEN;
297 req->dataptr = pathname; 421 req->data = newSVsv (pathname);
422 req->dataptr = SvPV_nolen (req->data);
298 req->fd = flags; 423 req->fd = flags;
299 req->mode = mode; 424 req->mode = mode;
300 req->callback = SvREFCNT_inc (callback); 425 req->callback = SvREFCNT_inc (callback);
301 426
302 send_req (req); 427 send_req (req);
303 428
304void 429void
305aio_close(fh,callback) 430aio_close(fh,callback)
306 PerlIO * fh 431 InputStream fh
307 SV * callback 432 SV * callback
308 PROTOTYPE: $ 433 PROTOTYPE: $$
309 CODE: 434 CODE:
310 aio_req req; 435 aio_req req;
311 436
312 New (0, req, 1, aio_cb); 437 Newz (0, req, 1, aio_cb);
313 438
314 if (!req) 439 if (!req)
315 croak ("out of memory during aio_req allocation"); 440 croak ("out of memory during aio_req allocation");
316 441
317 req->type = REQ_CLOSE; 442 req->type = REQ_CLOSE;
318 req->fd = PerlIO_fileno (fh); 443 req->fd = PerlIO_fileno (fh);
319 req->callback = SvREFCNT_inc (callback); 444 req->callback = SvREFCNT_inc (callback);
320 445
321 send_req (req); 446 send_req (req);
322 447
448void
449aio_read(fh,offset,length,data,dataoffset,callback)
450 InputStream fh
451 UV offset
452 IV length
453 SV * data
454 IV dataoffset
455 SV * callback
456 PROTOTYPE: $$$$$$
457 CODE:
458 read_write (aTHX_ 0, PerlIO_fileno (fh), offset, length, data, dataoffset, callback);
459
460void
461aio_write(fh,offset,length,data,dataoffset,callback)
462 OutputStream fh
463 UV offset
464 IV length
465 SV * data
466 IV dataoffset
467 SV * callback
468 PROTOTYPE: $$$$$$
469 CODE:
470 read_write (aTHX_ 1, PerlIO_fileno (fh), offset, length, data, dataoffset, callback);
471
472void
473aio_stat(fh_or_path,callback)
474 SV * fh_or_path
475 SV * callback
476 PROTOTYPE: $$
477 ALIAS:
478 aio_lstat = 1
479 CODE:
480 aio_req req;
481
482 Newz (0, req, 1, aio_cb);
483
484 if (!req)
485 croak ("out of memory during aio_req allocation");
486
487 New (0, req->statdata, 1, Stat_t);
488
489 if (!req->statdata)
490 croak ("out of memory during aio_req->statdata allocation");
491
492 if (SvPOK (fh_or_path))
493 {
494 req->type = ix ? REQ_LSTAT : REQ_STAT;
495 req->data = newSVsv (fh_or_path);
496 req->dataptr = SvPV_nolen (req->data);
497 }
498 else
499 {
500 req->type = REQ_FSTAT;
501 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
502 }
503
504 req->callback = SvREFCNT_inc (callback);
505
506 send_req (req);
507
508void
509aio_unlink(pathname,callback)
510 SV * pathname
511 SV * callback
512 PROTOTYPE: $$
513 CODE:
514 aio_req req;
515
516 Newz (0, req, 1, aio_cb);
517
518 if (!req)
519 croak ("out of memory during aio_req allocation");
520
521 req->type = REQ_UNLINK;
522 req->data = newSVsv (pathname);
523 req->dataptr = SvPV_nolen (req->data);
524 req->callback = SvREFCNT_inc (callback);
525
526 send_req (req);
527
323int 528int
324poll_fileno() 529poll_fileno()
325 PROTOTYPE: 530 PROTOTYPE:
326 CODE: 531 CODE:
327 RETVAL = respipe[0]; 532 RETVAL = respipe[0];
334 CODE: 539 CODE:
335 RETVAL = poll_cb (aTHX); 540 RETVAL = poll_cb (aTHX);
336 OUTPUT: 541 OUTPUT:
337 RETVAL 542 RETVAL
338 543
544void
545poll_wait()
546 PROTOTYPE:
547 CODE:
548 poll_wait ();
549
339int 550int
340nreqs() 551nreqs()
341 PROTOTYPE: 552 PROTOTYPE:
342 CODE: 553 CODE:
343 RETVAL = nreqs; 554 RETVAL = nreqs;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines