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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines