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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines