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.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>
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// 128 seems to be enough most everywhere. alpha needs 256.
17# define __NR_pread64 __NR_pread 20#define STACKSIZE (256 * sizeof (long))
18#endif
19#ifndef __NR_pwrite64
20# define __NR_pwrite64 __NR_pwrite
21#endif
22 21
23#define STACKSIZE 1024 /* yeah */ 22enum {
24 23 REQ_QUIT,
25enum { REQ_QUIT, REQ_OPEN, REQ_CLOSE, REQ_READ, REQ_WRITE, REQ_STAT, REQ_LSTAT, REQ_FSTAT}; 24 REQ_OPEN, REQ_CLOSE, REQ_READ, REQ_WRITE,
25 REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_UNLINK
26};
26 27
27typedef struct { 28typedef struct {
28 char stack[STACKSIZE]; 29 char stack[STACKSIZE];
29} aio_thread; 30} aio_thread;
30 31
31typedef struct { 32typedef struct aio_cb {
33 struct aio_cb *next;
34
32 int type; 35 int type;
33 aio_thread *thread; 36 aio_thread *thread;
34 37
35 int fd; 38 int fd;
36 off_t offset; 39 off_t offset;
40 int errorno; 43 int errorno;
41 SV *data, *callback; 44 SV *data, *callback;
42 void *dataptr; 45 void *dataptr;
43 STRLEN dataoffset; 46 STRLEN dataoffset;
44 47
45 struct stat64 *statdata; 48 Stat_t *statdata;
46} aio_cb; 49} aio_cb;
47 50
48typedef aio_cb *aio_req; 51typedef aio_cb *aio_req;
49 52
50static int started; 53static int started;
51static int nreqs; 54static int nreqs;
52static int reqpipe[2], respipe[2]; 55static int reqpipe[2], respipe[2];
53 56
57static aio_req qs, qe; /* queue start, queue end */
58
54static int aio_proc(void *arg); 59static int aio_proc(void *arg);
55 60
56static void 61static void
57start_thread(void) 62start_thread (void)
58{ 63{
59 aio_thread *thr; 64 aio_thread *thr;
60 65
61 New (0, thr, 1, aio_thread); 66 New (0, thr, 1, aio_thread);
62 67
63 if (clone (aio_proc, 68 if (clone (aio_proc,
64 &(thr->stack[STACKSIZE]), 69 &(thr->stack[STACKSIZE - sizeof (long)]),
65 CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND, 70 CLONE_VM|CLONE_FS|CLONE_FILES,
66 thr) >= 0) 71 thr) >= 0)
67 started++; 72 started++;
68 else 73 else
69 Safefree (thr); 74 Safefree (thr);
70} 75}
71 76
72static 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
73end_thread(void) 106end_thread (void)
74{ 107{
75 aio_req req; 108 aio_req req;
76 New (0, req, 1, aio_cb); 109 New (0, req, 1, aio_cb);
77 req->type = REQ_QUIT; 110 req->type = REQ_QUIT;
78 write (reqpipe[1], &req, sizeof (aio_req)); 111
112 send_req (req);
79} 113}
80 114
81static void 115static void
82send_req (aio_req req) 116read_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, 117 int dowrite, int fd, off_t offset, size_t length,
90 SV *data, STRLEN dataoffset, SV*callback) 118 SV *data, STRLEN dataoffset, SV *callback)
91{ 119{
92 aio_req req; 120 aio_req req;
93 STRLEN svlen; 121 STRLEN svlen;
94 char *svptr = SvPV (data, svlen); 122 char *svptr = SvPV (data, svlen);
95 123
131 req->callback = SvREFCNT_inc (callback); 159 req->callback = SvREFCNT_inc (callback);
132 160
133 send_req (req); 161 send_req (req);
134} 162}
135 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
136static int 174static int
137poll_cb (pTHX) 175poll_cb (pTHX)
138{ 176{
139 dSP; 177 dSP;
140 int count = 0; 178 int count = 0;
141 aio_req req; 179 aio_req req;
142 180
143 while (read (respipe[0], (void *)&req, sizeof (req)) == sizeof (req)) 181 while (read (respipe[0], (void *)&req, sizeof (req)) == sizeof (req))
144 { 182 {
183 nreqs--;
184
145 if (req->type == REQ_QUIT) 185 if (req->type == REQ_QUIT)
146 { 186 {
147 Safefree (req->thread); 187 Safefree (req->thread);
148 started--; 188 started--;
149 } 189 }
159 if (req->data) 199 if (req->data)
160 SvREFCNT_dec (req->data); 200 SvREFCNT_dec (req->data);
161 201
162 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) 202 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT)
163 { 203 {
164 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; 204 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
165 PL_laststatval = req->result; 205 PL_laststatval = req->result;
166 PL_statcache.st_dev = req->statdata->st_dev; 206 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 207
180 Safefree (req->statdata); 208 Safefree (req->statdata);
181 } 209 }
182 210
183 PUSHMARK (SP); 211 PUSHMARK (SP);
188 216
189 if (req->callback) 217 if (req->callback)
190 SvREFCNT_dec (req->callback); 218 SvREFCNT_dec (req->callback);
191 219
192 errno = errorno; 220 errno = errorno;
193 nreqs--;
194 count++; 221 count++;
195 } 222 }
196 223
197 Safefree (req); 224 Safefree (req);
198 } 225 }
199 226
227 if (qs)
228 send_reqs ();
229
200 return count; 230 return count;
201} 231}
202 232
203static sigset_t fullsigset; 233static sigset_t fullsigset;
204 234
205#undef errno 235#undef errno
206#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; \
207 270
208static int 271static int
209aio_proc(void *thr_arg) 272aio_proc (void *thr_arg)
210{ 273{
211 aio_thread *thr = thr_arg; 274 aio_thread *thr = thr_arg;
212 aio_req req; 275 aio_req req;
213 int errno; 276 int errno;
214 277
215 /* this is very much x86 and kernel-specific :(:(:( */ 278 /* this is very much kernel-specific :(:(:( */
216 /* we rely on gcc's ability to create closures. */ 279 /* we rely on gcc's ability to create closures. */
217 _syscall3(int,read,int,fd,char *,buf,size_t,count) 280 _syscall3(int,read,int,fd,char *,buf,size_t,count)
218 _syscall3(int,write,int,fd,char *,buf,size_t,count) 281 _syscall3(int,write,int,fd,char *,buf,size_t,count)
219 282
220 _syscall3(int,open,char *,pathname,int,flags,mode_t,mode) 283 _syscall3(int,open,char *,pathname,int,flags,mode_t,mode)
221 _syscall1(int,close,int,fd) 284 _syscall1(int,close,int,fd)
222 285
286#if __NR_pread64
223 _syscall5(int,pread64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi) 287 _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) 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
225 295
296
297#if __NR_stat64
226 _syscall2(int,stat64, const char *, filename, struct stat64 *, buf) 298 _syscall2(int,stat64, const char *, filename, struct kernelstat64 *, buf)
227 _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf) 299 _syscall2(int,lstat64, const char *, filename, struct kernelstat64 *, buf)
228 _syscall2(int,fstat64, int, fd, struct stat64 *, 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);
229 310
230 sigprocmask (SIG_SETMASK, &fullsigset, 0); 311 sigprocmask (SIG_SETMASK, &fullsigset, 0);
312 prctl (PR_SET_PDEATHSIG, SIGKILL);
231 313
232 /* then loop */ 314 /* then loop */
233 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) 315 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req))
234 { 316 {
235 req->thread = thr; 317 req->thread = thr;
236 errno = 0; /* strictly unnecessary */ 318 errno = 0; /* strictly unnecessary */
237 319
238 if (req->type == REQ_READ) 320 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 { 321 {
322#if __NR_pread64
323 case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length,
324 LONG_LONG_PAIR (req->offset >> 32, req->offset & 0xffffffff)); break;
325 case REQ_WRITE: req->result = pwrite64(req->fd, req->dataptr, req->length,
326 LONG_LONG_PAIR (req->offset >> 32, req->offset & 0xffffffff)); break;
327#else
328 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break;
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
342 case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break;
343 case REQ_CLOSE: req->result = close (req->fd); break;
344 case REQ_UNLINK: req->result = unlink (req->dataptr); break;
345
346 case REQ_QUIT:
347 default:
254 write (respipe[1], (void *)&req, sizeof (req)); 348 write (respipe[1], (void *)&req, sizeof (req));
255 break; 349 return 0;
256 } 350 }
257 351
258 req->errorno = errno; 352 req->errorno = errno;
259 write (respipe[1], (void *)&req, sizeof (req)); 353 write (respipe[1], (void *)&req, sizeof (req));
260 } 354 }
272 sigdelset (&fullsigset, SIGABRT); 366 sigdelset (&fullsigset, SIGABRT);
273 sigdelset (&fullsigset, SIGINT); 367 sigdelset (&fullsigset, SIGINT);
274 368
275 if (pipe (reqpipe) || pipe (respipe)) 369 if (pipe (reqpipe) || pipe (respipe))
276 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");
277 374
278 if (fcntl (respipe[0], F_SETFL, O_NONBLOCK)) 375 if (fcntl (respipe[0], F_SETFL, O_NONBLOCK))
279 croak ("cannot set result pipe to nonblocking mode"); 376 croak ("cannot set result pipe to nonblocking mode");
280} 377}
281 378
299 cur--; 396 cur--;
300 } 397 }
301 398
302 while (started > nthreads) 399 while (started > nthreads)
303 { 400 {
304 fd_set rfd; 401 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 (); 402 poll_cb (aTHX);
310 } 403 }
311 404
312void 405void
313aio_open(pathname,flags,mode,callback) 406aio_open(pathname,flags,mode,callback)
314 SV * pathname 407 SV * pathname
389 Newz (0, req, 1, aio_cb); 482 Newz (0, req, 1, aio_cb);
390 483
391 if (!req) 484 if (!req)
392 croak ("out of memory during aio_req allocation"); 485 croak ("out of memory during aio_req allocation");
393 486
394 New (0, req->statdata, 1, struct stat64); 487 New (0, req->statdata, 1, Stat_t);
395 488
396 if (!req->statdata) 489 if (!req->statdata)
397 croak ("out of memory during aio_req->statdata allocation"); 490 croak ("out of memory during aio_req->statdata allocation");
398 491
399 if (SvPOK (fh_or_path)) 492 if (SvPOK (fh_or_path))
410 503
411 req->callback = SvREFCNT_inc (callback); 504 req->callback = SvREFCNT_inc (callback);
412 505
413 send_req (req); 506 send_req (req);
414 507
508void
509aio_unlink(pathname,callback)
510 SV * pathname
511 SV * callback
512 PROTOTYPE: $$
513 CODE:
514 aio_req req;
515
516 Newz (0, req, 1, aio_cb);
517
518 if (!req)
519 croak ("out of memory during aio_req allocation");
520
521 req->type = REQ_UNLINK;
522 req->data = newSVsv (pathname);
523 req->dataptr = SvPV_nolen (req->data);
524 req->callback = SvREFCNT_inc (callback);
525
526 send_req (req);
527
415int 528int
416poll_fileno() 529poll_fileno()
417 PROTOTYPE: 530 PROTOTYPE:
418 CODE: 531 CODE:
419 RETVAL = respipe[0]; 532 RETVAL = respipe[0];
426 CODE: 539 CODE:
427 RETVAL = poll_cb (aTHX); 540 RETVAL = poll_cb (aTHX);
428 OUTPUT: 541 OUTPUT:
429 RETVAL 542 RETVAL
430 543
544void
545poll_wait()
546 PROTOTYPE:
547 CODE:
548 poll_wait ();
549
431int 550int
432nreqs() 551nreqs()
433 PROTOTYPE: 552 PROTOTYPE:
434 CODE: 553 CODE:
435 RETVAL = nreqs; 554 RETVAL = nreqs;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines