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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines