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.19 by root, Fri May 7 00:57:29 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
44 int errorno; 43 int errorno;
45 SV *data, *callback; 44 SV *data, *callback;
46 void *dataptr; 45 void *dataptr;
47 STRLEN dataoffset; 46 STRLEN dataoffset;
48 47
49 struct stat64 *statdata; 48 Stat_t *statdata;
50} aio_cb; 49} aio_cb;
51 50
52typedef aio_cb *aio_req; 51typedef aio_cb *aio_req;
53 52
54static int started; 53static int started;
65 aio_thread *thr; 64 aio_thread *thr;
66 65
67 New (0, thr, 1, aio_thread); 66 New (0, thr, 1, aio_thread);
68 67
69 if (clone (aio_proc, 68 if (clone (aio_proc,
70 &(thr->stack[STACKSIZE]), 69 &(thr->stack[STACKSIZE - sizeof (long)]),
71 CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND, 70 CLONE_VM|CLONE_FS|CLONE_FILES,
72 thr) >= 0) 71 thr) >= 0)
73 started++; 72 started++;
74 else 73 else
75 Safefree (thr); 74 Safefree (thr);
76} 75}
91{ 90{
92 nreqs++; 91 nreqs++;
93 req->next = 0; 92 req->next = 0;
94 93
95 if (qe) 94 if (qe)
95 {
96 qe->next = req; 96 qe->next = req;
97 qe = req;
98 }
97 else 99 else
98 qe = qs = req; 100 qe = qs = req;
99 101
100 send_reqs (); 102 send_reqs ();
101} 103}
157 req->callback = SvREFCNT_inc (callback); 159 req->callback = SvREFCNT_inc (callback);
158 160
159 send_req (req); 161 send_req (req);
160} 162}
161 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
162static int 174static int
163poll_cb (pTHX) 175poll_cb (pTHX)
164{ 176{
165 dSP; 177 dSP;
166 int count = 0; 178 int count = 0;
167 aio_req req; 179 aio_req req;
168 180
169 while (read (respipe[0], (void *)&req, sizeof (req)) == sizeof (req)) 181 while (read (respipe[0], (void *)&req, sizeof (req)) == sizeof (req))
170 { 182 {
183 nreqs--;
184
171 if (req->type == REQ_QUIT) 185 if (req->type == REQ_QUIT)
172 { 186 {
173 Safefree (req->thread); 187 Safefree (req->thread);
174 started--; 188 started--;
175 } 189 }
185 if (req->data) 199 if (req->data)
186 SvREFCNT_dec (req->data); 200 SvREFCNT_dec (req->data);
187 201
188 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)
189 { 203 {
190 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; 204 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
191 PL_laststatval = req->result; 205 PL_laststatval = req->result;
192 PL_statcache.st_dev = req->statdata->st_dev; 206 PL_statcache = *(req->statdata);
193 PL_statcache.st_ino = req->statdata->st_ino;
194 PL_statcache.st_mode = req->statdata->st_mode;
195 PL_statcache.st_nlink = req->statdata->st_nlink;
196 PL_statcache.st_uid = req->statdata->st_uid;
197 PL_statcache.st_gid = req->statdata->st_gid;
198 PL_statcache.st_rdev = req->statdata->st_rdev;
199 PL_statcache.st_size = req->statdata->st_size;
200 PL_statcache.st_atime = req->statdata->st_atime;
201 PL_statcache.st_mtime = req->statdata->st_mtime;
202 PL_statcache.st_ctime = req->statdata->st_ctime;
203 PL_statcache.st_blksize = req->statdata->st_blksize;
204 PL_statcache.st_blocks = req->statdata->st_blocks;
205 207
206 Safefree (req->statdata); 208 Safefree (req->statdata);
207 } 209 }
208 210
209 PUSHMARK (SP); 211 PUSHMARK (SP);
214 216
215 if (req->callback) 217 if (req->callback)
216 SvREFCNT_dec (req->callback); 218 SvREFCNT_dec (req->callback);
217 219
218 errno = errorno; 220 errno = errorno;
219 nreqs--;
220 count++; 221 count++;
221 } 222 }
222 223
223 Safefree (req); 224 Safefree (req);
224 } 225 }
231 232
232static sigset_t fullsigset; 233static sigset_t fullsigset;
233 234
234#undef errno 235#undef errno
235#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; \
236 270
237static int 271static int
238aio_proc (void *thr_arg) 272aio_proc (void *thr_arg)
239{ 273{
240 aio_thread *thr = thr_arg; 274 aio_thread *thr = thr_arg;
241 aio_req req; 275 aio_req req;
242 int errno; 276 int errno;
243 277
244 /* this is very much x86 and kernel-specific :(:(:( */ 278 /* this is very much kernel-specific :(:(:( */
245 /* we rely on gcc's ability to create closures. */ 279 /* we rely on gcc's ability to create closures. */
246 _syscall3(int,read,int,fd,char *,buf,size_t,count) 280 _syscall3(int,read,int,fd,char *,buf,size_t,count)
247 _syscall3(int,write,int,fd,char *,buf,size_t,count) 281 _syscall3(int,write,int,fd,char *,buf,size_t,count)
248 282
249 _syscall3(int,open,char *,pathname,int,flags,mode_t,mode) 283 _syscall3(int,open,char *,pathname,int,flags,mode_t,mode)
250 _syscall1(int,close,int,fd) 284 _syscall1(int,close,int,fd)
251 285
286#if __NR_pread64
252 _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)
253 _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
254 295
296
297#if __NR_stat64
255 _syscall2(int,stat64, const char *, filename, struct stat64 *, buf) 298 _syscall2(int,stat64, const char *, filename, struct kernelstat64 *, buf)
256 _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf) 299 _syscall2(int,lstat64, const char *, filename, struct kernelstat64 *, buf)
257 _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);
258 310
259 sigprocmask (SIG_SETMASK, &fullsigset, 0); 311 sigprocmask (SIG_SETMASK, &fullsigset, 0);
312 prctl (PR_SET_PDEATHSIG, SIGKILL);
260 313
261 /* then loop */ 314 /* then loop */
262 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) 315 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req))
263 { 316 {
264 req->thread = thr; 317 req->thread = thr;
265 errno = 0; /* strictly unnecessary */ 318 errno = 0; /* strictly unnecessary */
266 319
267 switch (req->type) 320 switch (req->type)
268 { 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
269 case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); break; 328 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break;
270 case REQ_WRITE: req->result = pwrite64(req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); 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
271 case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; 342 case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break;
272 case REQ_CLOSE: req->result = close (req->fd); break; 343 case REQ_CLOSE: req->result = close (req->fd); break;
273 case REQ_STAT: req->result = stat64 (req->dataptr, req->statdata); break;
274 case REQ_LSTAT: req->result = lstat64 (req->dataptr, req->statdata); break; 344 case REQ_UNLINK: req->result = unlink (req->dataptr); break;
275 case REQ_FSTAT: req->result = fstat64 (req->fd, req->statdata); break;
276 345
277 case REQ_QUIT: 346 case REQ_QUIT:
278 default: 347 default:
279 write (respipe[1], (void *)&req, sizeof (req)); 348 write (respipe[1], (void *)&req, sizeof (req));
280 return 0; 349 return 0;
327 cur--; 396 cur--;
328 } 397 }
329 398
330 while (started > nthreads) 399 while (started > nthreads)
331 { 400 {
332 fd_set rfd; 401 poll_wait ();
333 FD_ZERO(&rfd);
334 FD_SET(respipe[0], &rfd);
335
336 select (respipe[0] + 1, &rfd, 0, 0, 0);
337 poll_cb (aTHX); 402 poll_cb (aTHX);
338 } 403 }
339 404
340void 405void
341aio_open(pathname,flags,mode,callback) 406aio_open(pathname,flags,mode,callback)
417 Newz (0, req, 1, aio_cb); 482 Newz (0, req, 1, aio_cb);
418 483
419 if (!req) 484 if (!req)
420 croak ("out of memory during aio_req allocation"); 485 croak ("out of memory during aio_req allocation");
421 486
422 New (0, req->statdata, 1, struct stat64); 487 New (0, req->statdata, 1, Stat_t);
423 488
424 if (!req->statdata) 489 if (!req->statdata)
425 croak ("out of memory during aio_req->statdata allocation"); 490 croak ("out of memory during aio_req->statdata allocation");
426 491
427 if (SvPOK (fh_or_path)) 492 if (SvPOK (fh_or_path))
438 503
439 req->callback = SvREFCNT_inc (callback); 504 req->callback = SvREFCNT_inc (callback);
440 505
441 send_req (req); 506 send_req (req);
442 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
443int 528int
444poll_fileno() 529poll_fileno()
445 PROTOTYPE: 530 PROTOTYPE:
446 CODE: 531 CODE:
447 RETVAL = respipe[0]; 532 RETVAL = respipe[0];
454 CODE: 539 CODE:
455 RETVAL = poll_cb (aTHX); 540 RETVAL = poll_cb (aTHX);
456 OUTPUT: 541 OUTPUT:
457 RETVAL 542 RETVAL
458 543
544void
545poll_wait()
546 PROTOTYPE:
547 CODE:
548 poll_wait ();
549
459int 550int
460nreqs() 551nreqs()
461 PROTOTYPE: 552 PROTOTYPE:
462 CODE: 553 CODE:
463 RETVAL = nreqs; 554 RETVAL = nreqs;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines