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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines