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.21 by root, Fri Aug 6 17:18:08 2004 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>
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>
11 13
12typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ 14typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */
13typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ 15typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */
14typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ 16typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */
15
16#ifndef __NR_pread64
17# define __NR_pread64 __NR_pread
18#endif
19#ifndef __NR_pwrite64
20# define __NR_pwrite64 __NR_pwrite
21#endif
22 17
23#define STACKSIZE 1024 /* yeah */ 18#define STACKSIZE 1024 /* yeah */
24 19
25enum { REQ_QUIT, REQ_OPEN, REQ_CLOSE, REQ_READ, REQ_WRITE, REQ_STAT, REQ_LSTAT, REQ_FSTAT}; 20enum {
21 REQ_QUIT,
22 REQ_OPEN, REQ_CLOSE, REQ_READ, REQ_WRITE,
23 REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_UNLINK
24};
26 25
27typedef struct { 26typedef struct {
28 char stack[STACKSIZE]; 27 char stack[STACKSIZE];
29} aio_thread; 28} aio_thread;
30 29
31typedef struct { 30typedef struct aio_cb {
31 struct aio_cb *next;
32
32 int type; 33 int type;
33 aio_thread *thread; 34 aio_thread *thread;
34 35
35 int fd; 36 int fd;
36 off_t offset; 37 off_t offset;
40 int errorno; 41 int errorno;
41 SV *data, *callback; 42 SV *data, *callback;
42 void *dataptr; 43 void *dataptr;
43 STRLEN dataoffset; 44 STRLEN dataoffset;
44 45
45 struct stat64 *statdata; 46 Stat_t *statdata;
46} aio_cb; 47} aio_cb;
47 48
48typedef aio_cb *aio_req; 49typedef aio_cb *aio_req;
49 50
50static int started; 51static int started;
51static int nreqs; 52static int nreqs;
52static int reqpipe[2], respipe[2]; 53static int reqpipe[2], respipe[2];
53 54
55static aio_req qs, qe; /* queue start, queue end */
56
54static int aio_proc(void *arg); 57static int aio_proc(void *arg);
55 58
56static void 59static void
57start_thread(void) 60start_thread (void)
58{ 61{
59 aio_thread *thr; 62 aio_thread *thr;
60 63
61 New (0, thr, 1, aio_thread); 64 New (0, thr, 1, aio_thread);
62 65
63 if (clone (aio_proc, 66 if (clone (aio_proc,
64 &(thr->stack[STACKSIZE]), 67 &(thr->stack[STACKSIZE]),
65 CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND, 68 CLONE_VM|CLONE_FS|CLONE_FILES,
66 thr) >= 0) 69 thr) >= 0)
67 started++; 70 started++;
68 else 71 else
69 Safefree (thr); 72 Safefree (thr);
70} 73}
71 74
72static void 75static void
76send_reqs (void)
77{
78 /* this write is atomic */
79 while (qs && write (reqpipe[1], &qs, sizeof qs) == sizeof qs)
80 {
81 qs = qs->next;
82 if (!qs) qe = 0;
83 }
84}
85
86static void
87send_req (aio_req req)
88{
89 nreqs++;
90 req->next = 0;
91
92 if (qe)
93 qe->next = req;
94 else
95 qe = qs = req;
96
97 send_reqs ();
98}
99
100static void
73end_thread(void) 101end_thread (void)
74{ 102{
75 aio_req req; 103 aio_req req;
76 New (0, req, 1, aio_cb); 104 New (0, req, 1, aio_cb);
77 req->type = REQ_QUIT; 105 req->type = REQ_QUIT;
78 write (reqpipe[1], &req, sizeof (aio_req)); 106
107 send_req (req);
79} 108}
80 109
81static void 110static void
82send_req (aio_req req) 111read_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, 112 int dowrite, int fd, off_t offset, size_t length,
90 SV *data, STRLEN dataoffset, SV*callback) 113 SV *data, STRLEN dataoffset, SV *callback)
91{ 114{
92 aio_req req; 115 aio_req req;
93 STRLEN svlen; 116 STRLEN svlen;
94 char *svptr = SvPV (data, svlen); 117 char *svptr = SvPV (data, svlen);
95 118
159 if (req->data) 182 if (req->data)
160 SvREFCNT_dec (req->data); 183 SvREFCNT_dec (req->data);
161 184
162 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) 185 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT)
163 { 186 {
164 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; 187 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
165 PL_laststatval = req->result; 188 PL_laststatval = req->result;
166 PL_statcache.st_dev = req->statdata->st_dev; 189 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 190
180 Safefree (req->statdata); 191 Safefree (req->statdata);
181 } 192 }
182 193
183 PUSHMARK (SP); 194 PUSHMARK (SP);
195 } 206 }
196 207
197 Safefree (req); 208 Safefree (req);
198 } 209 }
199 210
211 if (qs)
212 send_reqs ();
213
200 return count; 214 return count;
201} 215}
202 216
203static sigset_t fullsigset; 217static sigset_t fullsigset;
204 218
205#undef errno 219#undef errno
206#include <asm/unistd.h> 220#include <asm/unistd.h>
221#include <sys/prctl.h>
222
223#define COPY_STATDATA \
224 req->statdata->st_dev = statdata.st_dev; \
225 req->statdata->st_ino = statdata.st_ino; \
226 req->statdata->st_mode = statdata.st_mode; \
227 req->statdata->st_nlink = statdata.st_nlink; \
228 req->statdata->st_uid = statdata.st_uid; \
229 req->statdata->st_gid = statdata.st_gid; \
230 req->statdata->st_rdev = statdata.st_rdev; \
231 req->statdata->st_size = statdata.st_size; \
232 req->statdata->st_atime = statdata.st_atime; \
233 req->statdata->st_mtime = statdata.st_mtime; \
234 req->statdata->st_ctime = statdata.st_ctime; \
235 req->statdata->st_blksize = statdata.st_blksize; \
236 req->statdata->st_blocks = statdata.st_blocks; \
207 237
208static int 238static int
209aio_proc(void *thr_arg) 239aio_proc (void *thr_arg)
210{ 240{
211 aio_thread *thr = thr_arg; 241 aio_thread *thr = thr_arg;
212 aio_req req; 242 aio_req req;
213 int errno; 243 int errno;
214 244
215 /* this is very much x86 and kernel-specific :(:(:( */ 245 /* this is very much kernel-specific :(:(:( */
216 /* we rely on gcc's ability to create closures. */ 246 /* we rely on gcc's ability to create closures. */
217 _syscall3(int,read,int,fd,char *,buf,size_t,count) 247 _syscall3(int,read,int,fd,char *,buf,size_t,count)
218 _syscall3(int,write,int,fd,char *,buf,size_t,count) 248 _syscall3(int,write,int,fd,char *,buf,size_t,count)
219 249
220 _syscall3(int,open,char *,pathname,int,flags,mode_t,mode) 250 _syscall3(int,open,char *,pathname,int,flags,mode_t,mode)
221 _syscall1(int,close,int,fd) 251 _syscall1(int,close,int,fd)
222 252
253#ifdef __NR_pread64
223 _syscall5(int,pread64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi) 254 _syscall5(int,pread64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi)
224 _syscall5(int,pwrite64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi) 255 _syscall5(int,pwrite64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi)
256#elif __NR_pread
257 _syscall4(int,pread,int,fd,char *,buf,size_t,count,offset_t,offset)
258 _syscall4(int,pwrite,int,fd,char *,buf,size_t,count,offset_t,offset)
259#else
260# error "neither pread nor pread64 defined"
261#endif
225 262
263
264#ifdef __NR_stat64
226 _syscall2(int,stat64, const char *, filename, struct stat64 *, buf) 265 _syscall2(int,stat64, const char *, filename, struct stat64 *, buf)
227 _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf) 266 _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf)
228 _syscall2(int,fstat64, int, fd, struct stat64 *, buf) 267 _syscall2(int,fstat64, int, fd, struct stat64 *, buf)
268#elif __NR_stat
269 _syscall2(int,stat, const char *, filename, struct stat *, buf)
270 _syscall2(int,lstat, const char *, filename, struct stat *, buf)
271 _syscall2(int,fstat, int, fd, struct stat *, buf)
272#else
273# error "neither stat64 nor stat defined"
274#endif
275
276 _syscall1(int,unlink, char *, filename);
229 277
230 sigprocmask (SIG_SETMASK, &fullsigset, 0); 278 sigprocmask (SIG_SETMASK, &fullsigset, 0);
279 prctl (PR_SET_PDEATHSIG, SIGKILL);
231 280
232 /* then loop */ 281 /* then loop */
233 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) 282 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req))
234 { 283 {
235 req->thread = thr; 284 req->thread = thr;
236 errno = 0; /* strictly unnecessary */ 285 errno = 0; /* strictly unnecessary */
237 286
238 if (req->type == REQ_READ) 287 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 { 288 {
289#ifdef __NR_pread64
290 case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); break;
291 case REQ_WRITE: req->result = pwrite64(req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); break;
292#else
293 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break;
294 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break;
295#endif
296#ifdef __NR_stat64
297 struct stat64 statdata;
298 case REQ_STAT: req->result = stat64 (req->dataptr, &statdata); COPY_STATDATA; break;
299 case REQ_LSTAT: req->result = lstat64 (req->dataptr, &statdata); COPY_STATDATA; break;
300 case REQ_FSTAT: req->result = fstat64 (req->fd, &statdata); COPY_STATDATA; break;
301#else
302 struct stat statdata;
303 case REQ_STAT: req->result = stat (req->dataptr, &statdata); COPY_STATDATA; break;
304 case REQ_LSTAT: req->result = lstat (req->dataptr, &statdata); COPY_STATDATA; break;
305 case REQ_FSTAT: req->result = fstat (req->fd, &statdata); COPY_STATDATA; break;
306#endif
307 case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break;
308 case REQ_CLOSE: req->result = close (req->fd); break;
309 case REQ_UNLINK: req->result = unlink (req->dataptr); break;
310
311 case REQ_QUIT:
312 default:
254 write (respipe[1], (void *)&req, sizeof (req)); 313 write (respipe[1], (void *)&req, sizeof (req));
255 break; 314 return 0;
256 } 315 }
257 316
258 req->errorno = errno; 317 req->errorno = errno;
259 write (respipe[1], (void *)&req, sizeof (req)); 318 write (respipe[1], (void *)&req, sizeof (req));
260 } 319 }
272 sigdelset (&fullsigset, SIGABRT); 331 sigdelset (&fullsigset, SIGABRT);
273 sigdelset (&fullsigset, SIGINT); 332 sigdelset (&fullsigset, SIGINT);
274 333
275 if (pipe (reqpipe) || pipe (respipe)) 334 if (pipe (reqpipe) || pipe (respipe))
276 croak ("unable to initialize request or result pipe"); 335 croak ("unable to initialize request or result pipe");
336
337 if (fcntl (reqpipe[1], F_SETFL, O_NONBLOCK))
338 croak ("cannot set result pipe to nonblocking mode");
277 339
278 if (fcntl (respipe[0], F_SETFL, O_NONBLOCK)) 340 if (fcntl (respipe[0], F_SETFL, O_NONBLOCK))
279 croak ("cannot set result pipe to nonblocking mode"); 341 croak ("cannot set result pipe to nonblocking mode");
280} 342}
281 343
304 fd_set rfd; 366 fd_set rfd;
305 FD_ZERO(&rfd); 367 FD_ZERO(&rfd);
306 FD_SET(respipe[0], &rfd); 368 FD_SET(respipe[0], &rfd);
307 369
308 select (respipe[0] + 1, &rfd, 0, 0, 0); 370 select (respipe[0] + 1, &rfd, 0, 0, 0);
309 poll_cb (); 371 poll_cb (aTHX);
310 } 372 }
311 373
312void 374void
313aio_open(pathname,flags,mode,callback) 375aio_open(pathname,flags,mode,callback)
314 SV * pathname 376 SV * pathname
389 Newz (0, req, 1, aio_cb); 451 Newz (0, req, 1, aio_cb);
390 452
391 if (!req) 453 if (!req)
392 croak ("out of memory during aio_req allocation"); 454 croak ("out of memory during aio_req allocation");
393 455
394 New (0, req->statdata, 1, struct stat64); 456 New (0, req->statdata, 1, Stat_t);
395 457
396 if (!req->statdata) 458 if (!req->statdata)
397 croak ("out of memory during aio_req->statdata allocation"); 459 croak ("out of memory during aio_req->statdata allocation");
398 460
399 if (SvPOK (fh_or_path)) 461 if (SvPOK (fh_or_path))
410 472
411 req->callback = SvREFCNT_inc (callback); 473 req->callback = SvREFCNT_inc (callback);
412 474
413 send_req (req); 475 send_req (req);
414 476
477void
478aio_unlink(pathname,callback)
479 SV * pathname
480 SV * callback
481 PROTOTYPE: $$
482 CODE:
483 aio_req req;
484
485 Newz (0, req, 1, aio_cb);
486
487 if (!req)
488 croak ("out of memory during aio_req allocation");
489
490 req->type = REQ_UNLINK;
491 req->data = newSVsv (pathname);
492 req->dataptr = SvPV_nolen (req->data);
493 req->callback = SvREFCNT_inc (callback);
494
495 send_req (req);
496
415int 497int
416poll_fileno() 498poll_fileno()
417 PROTOTYPE: 499 PROTOTYPE:
418 CODE: 500 CODE:
419 RETVAL = respipe[0]; 501 RETVAL = respipe[0];

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines