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.16 by root, Wed May 5 10:13:30 2004 UTC vs.
Revision 1.27 by root, Fri Jul 8 03:10:00 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>
6#include <sys/stat.h> 8#include <sys/stat.h>
7#include <unistd.h> 9#include <unistd.h>
8#include <fcntl.h> 10#include <fcntl.h>
9#include <signal.h> 11#include <signal.h>
10#include <sched.h> 12#include <sched.h>
13#include <endian.h>
11 14
12typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ 15typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */
13typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ 16typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */
14typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ 17typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */
15 18
16#ifndef __NR_pread64 19#if __i386 || __amd64
17# define __NR_pread64 __NR_pread 20# define STACKSIZE ( 256 * sizeof (long))
21#elif __ia64
22# define STACKSIZE (8192 * sizeof (long))
23#else
24# define STACKSIZE ( 512 * sizeof (long))
18#endif 25#endif
19#ifndef __NR_pwrite64
20# define __NR_pwrite64 __NR_pwrite
21#endif
22 26
23#define STACKSIZE 1024 /* yeah */ 27enum {
24 28 REQ_QUIT,
25enum { REQ_QUIT, REQ_OPEN, REQ_CLOSE, REQ_READ, REQ_WRITE, REQ_STAT, REQ_LSTAT, REQ_FSTAT}; 29 REQ_OPEN, REQ_CLOSE, REQ_READ, REQ_WRITE,
30 REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_UNLINK
31};
26 32
27typedef struct { 33typedef struct {
28 char stack[STACKSIZE]; 34 char stack[STACKSIZE];
29} aio_thread; 35} aio_thread;
30 36
31typedef struct { 37typedef struct aio_cb {
38 struct aio_cb *next;
39
32 int type; 40 int type;
33 aio_thread *thread; 41 aio_thread *thread;
34 42
35 int fd; 43 int fd;
36 off_t offset; 44 off_t offset;
40 int errorno; 48 int errorno;
41 SV *data, *callback; 49 SV *data, *callback;
42 void *dataptr; 50 void *dataptr;
43 STRLEN dataoffset; 51 STRLEN dataoffset;
44 52
45 struct stat64 *statdata; 53 Stat_t *statdata;
46} aio_cb; 54} aio_cb;
47 55
48typedef aio_cb *aio_req; 56typedef aio_cb *aio_req;
49 57
50static int started; 58static int started;
51static int nreqs; 59static int nreqs;
52static int reqpipe[2], respipe[2]; 60static int reqpipe[2], respipe[2];
53 61
62static aio_req qs, qe; /* queue start, queue end */
63
54static int aio_proc(void *arg); 64static int aio_proc(void *arg);
55 65
56static void 66static void
57start_thread(void) 67start_thread (void)
58{ 68{
59 aio_thread *thr; 69 aio_thread *thr;
60 70
61 New (0, thr, 1, aio_thread); 71 New (0, thr, 1, aio_thread);
62 72
63 if (clone (aio_proc, 73 if (clone (aio_proc,
64 &(thr->stack[STACKSIZE]), 74 &(thr->stack[STACKSIZE - 16]),
65 CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND, 75 CLONE_VM|CLONE_FS|CLONE_FILES,
66 thr) >= 0) 76 thr) >= 0)
67 started++; 77 started++;
68 else 78 else
69 Safefree (thr); 79 Safefree (thr);
70} 80}
71 81
72static 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
73end_thread(void) 111end_thread (void)
74{ 112{
75 aio_req req; 113 aio_req req;
76 New (0, req, 1, aio_cb); 114 New (0, req, 1, aio_cb);
77 req->type = REQ_QUIT; 115 req->type = REQ_QUIT;
78 write (reqpipe[1], &req, sizeof (aio_req)); 116
117 send_req (req);
79} 118}
80 119
81static void 120static void
82send_req (aio_req req) 121read_write (pTHX_
83{
84 nreqs++;
85 write (reqpipe[1], &req, sizeof (aio_req));
86}
87
88static void
89read_write (pTHX_ int dowrite, int fd, off_t offset, size_t length, 122 int dowrite, int fd, off_t offset, size_t length,
90 SV *data, STRLEN dataoffset, SV*callback) 123 SV *data, STRLEN dataoffset, SV *callback)
91{ 124{
92 aio_req req; 125 aio_req req;
93 STRLEN svlen; 126 STRLEN svlen;
94 char *svptr = SvPV (data, svlen); 127 char *svptr = SvPV (data, svlen);
95 128
131 req->callback = SvREFCNT_inc (callback); 164 req->callback = SvREFCNT_inc (callback);
132 165
133 send_req (req); 166 send_req (req);
134} 167}
135 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
136static int 179static int
137poll_cb (pTHX) 180poll_cb (pTHX)
138{ 181{
139 dSP; 182 dSP;
140 int count = 0; 183 int count = 0;
141 aio_req req; 184 aio_req req;
142 185
143 while (read (respipe[0], (void *)&req, sizeof (req)) == sizeof (req)) 186 while (read (respipe[0], (void *)&req, sizeof (req)) == sizeof (req))
144 { 187 {
188 nreqs--;
189
145 if (req->type == REQ_QUIT) 190 if (req->type == REQ_QUIT)
146 { 191 {
147 Safefree (req->thread); 192 Safefree (req->thread);
148 started--; 193 started--;
149 } 194 }
159 if (req->data) 204 if (req->data)
160 SvREFCNT_dec (req->data); 205 SvREFCNT_dec (req->data);
161 206
162 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) 207 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT)
163 { 208 {
164 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; 209 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
165 PL_laststatval = req->result; 210 PL_laststatval = req->result;
166 PL_statcache.st_dev = req->statdata->st_dev; 211 PL_statcache = *(req->statdata);
167 PL_statcache.st_ino = req->statdata->st_ino;
168 PL_statcache.st_mode = req->statdata->st_mode;
169 PL_statcache.st_nlink = req->statdata->st_nlink;
170 PL_statcache.st_uid = req->statdata->st_uid;
171 PL_statcache.st_gid = req->statdata->st_gid;
172 PL_statcache.st_rdev = req->statdata->st_rdev;
173 PL_statcache.st_size = req->statdata->st_size;
174 PL_statcache.st_atime = req->statdata->st_atime;
175 PL_statcache.st_mtime = req->statdata->st_mtime;
176 PL_statcache.st_ctime = req->statdata->st_ctime;
177 PL_statcache.st_blksize = req->statdata->st_blksize;
178 PL_statcache.st_blocks = req->statdata->st_blocks;
179 212
180 Safefree (req->statdata); 213 Safefree (req->statdata);
181 } 214 }
182 215
183 PUSHMARK (SP); 216 PUSHMARK (SP);
188 221
189 if (req->callback) 222 if (req->callback)
190 SvREFCNT_dec (req->callback); 223 SvREFCNT_dec (req->callback);
191 224
192 errno = errorno; 225 errno = errorno;
193 nreqs--;
194 count++; 226 count++;
195 } 227 }
196 228
197 Safefree (req); 229 Safefree (req);
198 } 230 }
199 231
232 if (qs)
233 send_reqs ();
234
200 return count; 235 return count;
201} 236}
202 237
203static sigset_t fullsigset; 238static sigset_t fullsigset;
204 239
205#undef errno 240#undef errno
206#include <asm/unistd.h> 241#include <asm/unistd.h>
242#include <sys/prctl.h>
243
244#if BYTE_ORDER == LITTLE_ENDIAN
245# define LONG_LONG_PAIR(HI, LO) LO, HI
246#elif BYTE_ORDER == BIG_ENDIAN
247# define LONG_LONG_PAIR(HI, LO) HI, LO
248#endif
249
250#if __alpha || __ia64 || __hppa || __v850__
251# define stat kernelstat
252# define stat64 kernelstat64
253# include <asm/stat.h>
254# undef stat
255# undef stat64
256#else
257# define kernelstat stat
258# define kernelstat64 stat64
259#endif
260
261#define COPY_STATDATA \
262 req->statdata->st_dev = statdata.st_dev; \
263 req->statdata->st_ino = statdata.st_ino; \
264 req->statdata->st_mode = statdata.st_mode; \
265 req->statdata->st_nlink = statdata.st_nlink; \
266 req->statdata->st_uid = statdata.st_uid; \
267 req->statdata->st_gid = statdata.st_gid; \
268 req->statdata->st_rdev = statdata.st_rdev; \
269 req->statdata->st_size = statdata.st_size; \
270 req->statdata->st_atime = statdata.st_atime; \
271 req->statdata->st_mtime = statdata.st_mtime; \
272 req->statdata->st_ctime = statdata.st_ctime; \
273 req->statdata->st_blksize = statdata.st_blksize; \
274 req->statdata->st_blocks = statdata.st_blocks; \
207 275
208static int 276static int
209aio_proc(void *thr_arg) 277aio_proc (void *thr_arg)
210{ 278{
211 aio_thread *thr = thr_arg; 279 aio_thread *thr = thr_arg;
212 aio_req req; 280 aio_req req;
213 int errno; 281 int errno;
214 282
215 /* this is very much x86 and kernel-specific :(:(:( */ 283 /* this is very much kernel-specific :(:(:( */
216 /* we rely on gcc's ability to create closures. */ 284 /* we rely on gcc's ability to create closures. */
217 _syscall3(int,read,int,fd,char *,buf,size_t,count) 285 _syscall3(int,read,int,fd,char *,buf,size_t,count)
218 _syscall3(int,write,int,fd,char *,buf,size_t,count) 286 _syscall3(int,write,int,fd,char *,buf,size_t,count)
219 287
220 _syscall3(int,open,char *,pathname,int,flags,mode_t,mode) 288 _syscall3(int,open,char *,pathname,int,flags,mode_t,mode)
221 _syscall1(int,close,int,fd) 289 _syscall1(int,close,int,fd)
222 290
291#if __NR_pread64
223 _syscall5(int,pread64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi) 292 _syscall5(int,pread64,int,fd,char *,buf,size_t,count,unsigned int,offset_lh,unsigned int,offset_hl)
224 _syscall5(int,pwrite64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi) 293 _syscall5(int,pwrite64,int,fd,char *,buf,size_t,count,unsigned int,offset_lh,unsigned int,offset_hl)
294#elif __NR_pread
295 _syscall4(int,pread,int,fd,char *,buf,size_t,count,offset_t,offset)
296 _syscall4(int,pwrite,int,fd,char *,buf,size_t,count,offset_t,offset)
297#else
298# error "neither pread nor pread64 defined"
299#endif
225 300
301
302#if __NR_stat64
226 _syscall2(int,stat64, const char *, filename, struct stat64 *, buf) 303 _syscall2(int,stat64, const char *, filename, struct kernelstat64 *, buf)
227 _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf) 304 _syscall2(int,lstat64, const char *, filename, struct kernelstat64 *, buf)
228 _syscall2(int,fstat64, int, fd, struct stat64 *, buf) 305 _syscall2(int,fstat64, int, fd, struct kernelstat64 *, buf)
306#elif __NR_stat
307 _syscall2(int,stat, const char *, filename, struct kernelstat *, buf)
308 _syscall2(int,lstat, const char *, filename, struct kernelstat *, buf)
309 _syscall2(int,fstat, int, fd, struct kernelstat *, buf)
310#else
311# error "neither stat64 nor stat defined"
312#endif
313
314 _syscall1(int,unlink, char *, filename);
229 315
230 sigprocmask (SIG_SETMASK, &fullsigset, 0); 316 sigprocmask (SIG_SETMASK, &fullsigset, 0);
317 prctl (PR_SET_PDEATHSIG, SIGKILL);
231 318
232 /* then loop */ 319 /* then loop */
233 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) 320 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req))
234 { 321 {
235 req->thread = thr; 322 req->thread = thr;
236 errno = 0; /* strictly unnecessary */ 323 errno = 0; /* strictly unnecessary */
237 324
238 if (req->type == REQ_READ) 325 switch (req->type)
239 req->result = pread64 (req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32);
240 else if (req->type == REQ_WRITE)
241 req->result = pwrite64(req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32);
242 else if (req->type == REQ_OPEN)
243 req->result = open (req->dataptr, req->fd, req->mode);
244 else if (req->type == REQ_CLOSE)
245 req->result = close (req->fd);
246 else if (req->type == REQ_STAT)
247 req->result = stat64 (req->dataptr, req->statdata);
248 else if (req->type == REQ_LSTAT)
249 req->result = lstat64 (req->dataptr, req->statdata);
250 else if (req->type == REQ_FSTAT)
251 req->result = fstat64 (req->fd, req->statdata);
252 else
253 { 326 {
327#if __NR_pread64
328 case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length,
329 LONG_LONG_PAIR (req->offset >> 32, req->offset & 0xffffffff)); break;
330 case REQ_WRITE: req->result = pwrite64(req->fd, req->dataptr, req->length,
331 LONG_LONG_PAIR (req->offset >> 32, req->offset & 0xffffffff)); break;
332#else
333 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break;
334 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break;
335#endif
336#if __NR_stat64
337 struct kernelstat64 statdata;
338 case REQ_STAT: req->result = stat64 (req->dataptr, &statdata); COPY_STATDATA; break;
339 case REQ_LSTAT: req->result = lstat64 (req->dataptr, &statdata); COPY_STATDATA; break;
340 case REQ_FSTAT: req->result = fstat64 (req->fd, &statdata); COPY_STATDATA; break;
341#else
342 struct kernelstat statdata;
343 case REQ_STAT: req->result = stat (req->dataptr, &statdata); COPY_STATDATA; break;
344 case REQ_LSTAT: req->result = lstat (req->dataptr, &statdata); COPY_STATDATA; break;
345 case REQ_FSTAT: req->result = fstat (req->fd, &statdata); COPY_STATDATA; break;
346#endif
347 case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break;
348 case REQ_CLOSE: req->result = close (req->fd); break;
349 case REQ_UNLINK: req->result = unlink (req->dataptr); break;
350
351 case REQ_QUIT:
352 default:
254 write (respipe[1], (void *)&req, sizeof (req)); 353 write (respipe[1], (void *)&req, sizeof (req));
255 break; 354 return 0;
256 } 355 }
257 356
258 req->errorno = errno; 357 req->errorno = errno;
259 write (respipe[1], (void *)&req, sizeof (req)); 358 write (respipe[1], (void *)&req, sizeof (req));
260 } 359 }
272 sigdelset (&fullsigset, SIGABRT); 371 sigdelset (&fullsigset, SIGABRT);
273 sigdelset (&fullsigset, SIGINT); 372 sigdelset (&fullsigset, SIGINT);
274 373
275 if (pipe (reqpipe) || pipe (respipe)) 374 if (pipe (reqpipe) || pipe (respipe))
276 croak ("unable to initialize request or result pipe"); 375 croak ("unable to initialize request or result pipe");
376
377 if (fcntl (reqpipe[1], F_SETFL, O_NONBLOCK))
378 croak ("cannot set result pipe to nonblocking mode");
277 379
278 if (fcntl (respipe[0], F_SETFL, O_NONBLOCK)) 380 if (fcntl (respipe[0], F_SETFL, O_NONBLOCK))
279 croak ("cannot set result pipe to nonblocking mode"); 381 croak ("cannot set result pipe to nonblocking mode");
280} 382}
281 383
299 cur--; 401 cur--;
300 } 402 }
301 403
302 while (started > nthreads) 404 while (started > nthreads)
303 { 405 {
304 fd_set rfd; 406 poll_wait ();
305 FD_ZERO(&rfd);
306 FD_SET(respipe[0], &rfd);
307
308 select (respipe[0] + 1, &rfd, 0, 0, 0);
309 poll_cb (); 407 poll_cb (aTHX);
310 } 408 }
311 409
312void 410void
313aio_open(pathname,flags,mode,callback) 411aio_open(pathname,flags,mode,callback)
314 SV * pathname 412 SV * pathname
389 Newz (0, req, 1, aio_cb); 487 Newz (0, req, 1, aio_cb);
390 488
391 if (!req) 489 if (!req)
392 croak ("out of memory during aio_req allocation"); 490 croak ("out of memory during aio_req allocation");
393 491
394 New (0, req->statdata, 1, struct stat64); 492 New (0, req->statdata, 1, Stat_t);
395 493
396 if (!req->statdata) 494 if (!req->statdata)
397 croak ("out of memory during aio_req->statdata allocation"); 495 croak ("out of memory during aio_req->statdata allocation");
398 496
399 if (SvPOK (fh_or_path)) 497 if (SvPOK (fh_or_path))
410 508
411 req->callback = SvREFCNT_inc (callback); 509 req->callback = SvREFCNT_inc (callback);
412 510
413 send_req (req); 511 send_req (req);
414 512
513void
514aio_unlink(pathname,callback)
515 SV * pathname
516 SV * callback
517 PROTOTYPE: $$
518 CODE:
519 aio_req req;
520
521 Newz (0, req, 1, aio_cb);
522
523 if (!req)
524 croak ("out of memory during aio_req allocation");
525
526 req->type = REQ_UNLINK;
527 req->data = newSVsv (pathname);
528 req->dataptr = SvPV_nolen (req->data);
529 req->callback = SvREFCNT_inc (callback);
530
531 send_req (req);
532
415int 533int
416poll_fileno() 534poll_fileno()
417 PROTOTYPE: 535 PROTOTYPE:
418 CODE: 536 CODE:
419 RETVAL = respipe[0]; 537 RETVAL = respipe[0];
426 CODE: 544 CODE:
427 RETVAL = poll_cb (aTHX); 545 RETVAL = poll_cb (aTHX);
428 OUTPUT: 546 OUTPUT:
429 RETVAL 547 RETVAL
430 548
549void
550poll_wait()
551 PROTOTYPE:
552 CODE:
553 poll_wait ();
554
431int 555int
432nreqs() 556nreqs()
433 PROTOTYPE: 557 PROTOTYPE:
434 CODE: 558 CODE:
435 RETVAL = nreqs; 559 RETVAL = nreqs;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines