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.17 by root, Thu May 6 12:16:48 2004 UTC vs.
Revision 1.26 by root, Thu Jul 7 23:17:23 2005 UTC

8#include <sys/stat.h> 8#include <sys/stat.h>
9#include <unistd.h> 9#include <unistd.h>
10#include <fcntl.h> 10#include <fcntl.h>
11#include <signal.h> 11#include <signal.h>
12#include <sched.h> 12#include <sched.h>
13#include <endian.h>
13 14
14typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ 15typedef 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 *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */
16typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ 17typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */
17 18
18#ifndef __NR_pread64 19// 128 seems to be enough most everywhere. alpha needs 256.
19# define __NR_pread64 __NR_pread 20#define STACKSIZE (256 * sizeof (long))
20#endif
21#ifndef __NR_pwrite64
22# define __NR_pwrite64 __NR_pwrite
23#endif
24 21
25#define STACKSIZE 1024 /* yeah */ 22enum {
26 23 REQ_QUIT,
27enum { 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};
28 27
29typedef struct { 28typedef struct {
30 char stack[STACKSIZE]; 29 char stack[STACKSIZE];
31} aio_thread; 30} aio_thread;
32 31
33typedef struct { 32typedef struct aio_cb {
33 struct aio_cb *next;
34
34 int type; 35 int type;
35 aio_thread *thread; 36 aio_thread *thread;
36 37
37 int fd; 38 int fd;
38 off_t offset; 39 off_t offset;
42 int errorno; 43 int errorno;
43 SV *data, *callback; 44 SV *data, *callback;
44 void *dataptr; 45 void *dataptr;
45 STRLEN dataoffset; 46 STRLEN dataoffset;
46 47
47 struct stat64 *statdata; 48 Stat_t *statdata;
48} aio_cb; 49} aio_cb;
49 50
50typedef aio_cb *aio_req; 51typedef aio_cb *aio_req;
51 52
52static int started; 53static int started;
53static int nreqs; 54static int nreqs;
54static int reqpipe[2], respipe[2]; 55static int reqpipe[2], respipe[2];
55 56
57static aio_req qs, qe; /* queue start, queue end */
58
56static int aio_proc(void *arg); 59static int aio_proc(void *arg);
57 60
58static void 61static void
59start_thread(void) 62start_thread (void)
60{ 63{
61 aio_thread *thr; 64 aio_thread *thr;
62 65
63 New (0, thr, 1, aio_thread); 66 New (0, thr, 1, aio_thread);
64 67
65 if (clone (aio_proc, 68 if (clone (aio_proc,
66 &(thr->stack[STACKSIZE]), 69 &(thr->stack[STACKSIZE - sizeof (long)]),
67 CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND, 70 CLONE_VM|CLONE_FS|CLONE_FILES,
68 thr) >= 0) 71 thr) >= 0)
69 started++; 72 started++;
70 else 73 else
71 Safefree (thr); 74 Safefree (thr);
72} 75}
73 76
74static 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
75end_thread(void) 106end_thread (void)
76{ 107{
77 aio_req req; 108 aio_req req;
78 New (0, req, 1, aio_cb); 109 New (0, req, 1, aio_cb);
79 req->type = REQ_QUIT; 110 req->type = REQ_QUIT;
80 write (reqpipe[1], &req, sizeof (aio_req));
81}
82 111
83static void 112 send_req (req);
84send_req (aio_req req)
85{
86 nreqs++;
87 write (reqpipe[1], &req, sizeof (aio_req));
88} 113}
89 114
90static void 115static void
91read_write (pTHX_ 116read_write (pTHX_
92 int dowrite, int fd, off_t offset, size_t length, 117 int dowrite, int fd, off_t offset, size_t length,
93 SV *data, STRLEN dataoffset, SV*callback) 118 SV *data, STRLEN dataoffset, SV *callback)
94{ 119{
95 aio_req req; 120 aio_req req;
96 STRLEN svlen; 121 STRLEN svlen;
97 char *svptr = SvPV (data, svlen); 122 char *svptr = SvPV (data, svlen);
98 123
134 req->callback = SvREFCNT_inc (callback); 159 req->callback = SvREFCNT_inc (callback);
135 160
136 send_req (req); 161 send_req (req);
137} 162}
138 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
139static int 174static int
140poll_cb (pTHX) 175poll_cb (pTHX)
141{ 176{
142 dSP; 177 dSP;
143 int count = 0; 178 int count = 0;
144 aio_req req; 179 aio_req req;
145 180
146 while (read (respipe[0], (void *)&req, sizeof (req)) == sizeof (req)) 181 while (read (respipe[0], (void *)&req, sizeof (req)) == sizeof (req))
147 { 182 {
183 nreqs--;
184
148 if (req->type == REQ_QUIT) 185 if (req->type == REQ_QUIT)
149 { 186 {
150 Safefree (req->thread); 187 Safefree (req->thread);
151 started--; 188 started--;
152 } 189 }
162 if (req->data) 199 if (req->data)
163 SvREFCNT_dec (req->data); 200 SvREFCNT_dec (req->data);
164 201
165 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)
166 { 203 {
167 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; 204 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
168 PL_laststatval = req->result; 205 PL_laststatval = req->result;
169 PL_statcache.st_dev = req->statdata->st_dev; 206 PL_statcache = *(req->statdata);
170 PL_statcache.st_ino = req->statdata->st_ino;
171 PL_statcache.st_mode = req->statdata->st_mode;
172 PL_statcache.st_nlink = req->statdata->st_nlink;
173 PL_statcache.st_uid = req->statdata->st_uid;
174 PL_statcache.st_gid = req->statdata->st_gid;
175 PL_statcache.st_rdev = req->statdata->st_rdev;
176 PL_statcache.st_size = req->statdata->st_size;
177 PL_statcache.st_atime = req->statdata->st_atime;
178 PL_statcache.st_mtime = req->statdata->st_mtime;
179 PL_statcache.st_ctime = req->statdata->st_ctime;
180 PL_statcache.st_blksize = req->statdata->st_blksize;
181 PL_statcache.st_blocks = req->statdata->st_blocks;
182 207
183 Safefree (req->statdata); 208 Safefree (req->statdata);
184 } 209 }
185 210
186 PUSHMARK (SP); 211 PUSHMARK (SP);
191 216
192 if (req->callback) 217 if (req->callback)
193 SvREFCNT_dec (req->callback); 218 SvREFCNT_dec (req->callback);
194 219
195 errno = errorno; 220 errno = errorno;
196 nreqs--;
197 count++; 221 count++;
198 } 222 }
199 223
200 Safefree (req); 224 Safefree (req);
201 } 225 }
202 226
227 if (qs)
228 send_reqs ();
229
203 return count; 230 return count;
204} 231}
205 232
206static sigset_t fullsigset; 233static sigset_t fullsigset;
207 234
208#undef errno 235#undef errno
209#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; \
210 270
211static int 271static int
212aio_proc(void *thr_arg) 272aio_proc (void *thr_arg)
213{ 273{
214 aio_thread *thr = thr_arg; 274 aio_thread *thr = thr_arg;
215 aio_req req; 275 aio_req req;
216 int errno; 276 int errno;
217 277
218 /* this is very much x86 and kernel-specific :(:(:( */ 278 /* this is very much kernel-specific :(:(:( */
219 /* we rely on gcc's ability to create closures. */ 279 /* we rely on gcc's ability to create closures. */
220 _syscall3(int,read,int,fd,char *,buf,size_t,count) 280 _syscall3(int,read,int,fd,char *,buf,size_t,count)
221 _syscall3(int,write,int,fd,char *,buf,size_t,count) 281 _syscall3(int,write,int,fd,char *,buf,size_t,count)
222 282
223 _syscall3(int,open,char *,pathname,int,flags,mode_t,mode) 283 _syscall3(int,open,char *,pathname,int,flags,mode_t,mode)
224 _syscall1(int,close,int,fd) 284 _syscall1(int,close,int,fd)
225 285
286#if __NR_pread64
226 _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)
227 _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
228 295
296
297#if __NR_stat64
229 _syscall2(int,stat64, const char *, filename, struct stat64 *, buf) 298 _syscall2(int,stat64, const char *, filename, struct kernelstat64 *, buf)
230 _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf) 299 _syscall2(int,lstat64, const char *, filename, struct kernelstat64 *, buf)
231 _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);
232 310
233 sigprocmask (SIG_SETMASK, &fullsigset, 0); 311 sigprocmask (SIG_SETMASK, &fullsigset, 0);
312 prctl (PR_SET_PDEATHSIG, SIGKILL);
234 313
235 /* then loop */ 314 /* then loop */
236 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) 315 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req))
237 { 316 {
238 req->thread = thr; 317 req->thread = thr;
239 errno = 0; /* strictly unnecessary */ 318 errno = 0; /* strictly unnecessary */
240 319
241 if (req->type == REQ_READ) 320 switch (req->type)
242 req->result = pread64 (req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32);
243 else if (req->type == REQ_WRITE)
244 req->result = pwrite64(req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32);
245 else if (req->type == REQ_OPEN)
246 req->result = open (req->dataptr, req->fd, req->mode);
247 else if (req->type == REQ_CLOSE)
248 req->result = close (req->fd);
249 else if (req->type == REQ_STAT)
250 req->result = stat64 (req->dataptr, req->statdata);
251 else if (req->type == REQ_LSTAT)
252 req->result = lstat64 (req->dataptr, req->statdata);
253 else if (req->type == REQ_FSTAT)
254 req->result = fstat64 (req->fd, req->statdata);
255 else
256 { 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:
257 write (respipe[1], (void *)&req, sizeof (req)); 348 write (respipe[1], (void *)&req, sizeof (req));
258 break; 349 return 0;
259 } 350 }
260 351
261 req->errorno = errno; 352 req->errorno = errno;
262 write (respipe[1], (void *)&req, sizeof (req)); 353 write (respipe[1], (void *)&req, sizeof (req));
263 } 354 }
275 sigdelset (&fullsigset, SIGABRT); 366 sigdelset (&fullsigset, SIGABRT);
276 sigdelset (&fullsigset, SIGINT); 367 sigdelset (&fullsigset, SIGINT);
277 368
278 if (pipe (reqpipe) || pipe (respipe)) 369 if (pipe (reqpipe) || pipe (respipe))
279 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");
280 374
281 if (fcntl (respipe[0], F_SETFL, O_NONBLOCK)) 375 if (fcntl (respipe[0], F_SETFL, O_NONBLOCK))
282 croak ("cannot set result pipe to nonblocking mode"); 376 croak ("cannot set result pipe to nonblocking mode");
283} 377}
284 378
302 cur--; 396 cur--;
303 } 397 }
304 398
305 while (started > nthreads) 399 while (started > nthreads)
306 { 400 {
307 fd_set rfd; 401 poll_wait ();
308 FD_ZERO(&rfd);
309 FD_SET(respipe[0], &rfd);
310
311 select (respipe[0] + 1, &rfd, 0, 0, 0);
312 poll_cb (aTHX); 402 poll_cb (aTHX);
313 } 403 }
314 404
315void 405void
316aio_open(pathname,flags,mode,callback) 406aio_open(pathname,flags,mode,callback)
392 Newz (0, req, 1, aio_cb); 482 Newz (0, req, 1, aio_cb);
393 483
394 if (!req) 484 if (!req)
395 croak ("out of memory during aio_req allocation"); 485 croak ("out of memory during aio_req allocation");
396 486
397 New (0, req->statdata, 1, struct stat64); 487 New (0, req->statdata, 1, Stat_t);
398 488
399 if (!req->statdata) 489 if (!req->statdata)
400 croak ("out of memory during aio_req->statdata allocation"); 490 croak ("out of memory during aio_req->statdata allocation");
401 491
402 if (SvPOK (fh_or_path)) 492 if (SvPOK (fh_or_path))
413 503
414 req->callback = SvREFCNT_inc (callback); 504 req->callback = SvREFCNT_inc (callback);
415 505
416 send_req (req); 506 send_req (req);
417 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
418int 528int
419poll_fileno() 529poll_fileno()
420 PROTOTYPE: 530 PROTOTYPE:
421 CODE: 531 CODE:
422 RETVAL = respipe[0]; 532 RETVAL = respipe[0];
429 CODE: 539 CODE:
430 RETVAL = poll_cb (aTHX); 540 RETVAL = poll_cb (aTHX);
431 OUTPUT: 541 OUTPUT:
432 RETVAL 542 RETVAL
433 543
544void
545poll_wait()
546 PROTOTYPE:
547 CODE:
548 poll_wait ();
549
434int 550int
435nreqs() 551nreqs()
436 PROTOTYPE: 552 PROTOTYPE:
437 CODE: 553 CODE:
438 RETVAL = nreqs; 554 RETVAL = nreqs;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines