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.21 by root, Fri Aug 6 17:18:08 2004 UTC

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 13
14typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ 14typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */
15typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ 15typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */
16typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ 16typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */
17
18#ifndef __NR_pread64
19# define __NR_pread64 __NR_pread
20#endif
21#ifndef __NR_pwrite64
22# define __NR_pwrite64 __NR_pwrite
23#endif
24 17
25#define STACKSIZE 1024 /* yeah */ 18#define STACKSIZE 1024 /* yeah */
26 19
27enum { 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};
28 25
29typedef struct { 26typedef struct {
30 char stack[STACKSIZE]; 27 char stack[STACKSIZE];
31} aio_thread; 28} aio_thread;
32 29
33typedef struct { 30typedef struct aio_cb {
31 struct aio_cb *next;
32
34 int type; 33 int type;
35 aio_thread *thread; 34 aio_thread *thread;
36 35
37 int fd; 36 int fd;
38 off_t offset; 37 off_t offset;
42 int errorno; 41 int errorno;
43 SV *data, *callback; 42 SV *data, *callback;
44 void *dataptr; 43 void *dataptr;
45 STRLEN dataoffset; 44 STRLEN dataoffset;
46 45
47 struct stat64 *statdata; 46 Stat_t *statdata;
48} aio_cb; 47} aio_cb;
49 48
50typedef aio_cb *aio_req; 49typedef aio_cb *aio_req;
51 50
52static int started; 51static int started;
53static int nreqs; 52static int nreqs;
54static int reqpipe[2], respipe[2]; 53static int reqpipe[2], respipe[2];
55 54
55static aio_req qs, qe; /* queue start, queue end */
56
56static int aio_proc(void *arg); 57static int aio_proc(void *arg);
57 58
58static void 59static void
59start_thread(void) 60start_thread (void)
60{ 61{
61 aio_thread *thr; 62 aio_thread *thr;
62 63
63 New (0, thr, 1, aio_thread); 64 New (0, thr, 1, aio_thread);
64 65
65 if (clone (aio_proc, 66 if (clone (aio_proc,
66 &(thr->stack[STACKSIZE]), 67 &(thr->stack[STACKSIZE]),
67 CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND, 68 CLONE_VM|CLONE_FS|CLONE_FILES,
68 thr) >= 0) 69 thr) >= 0)
69 started++; 70 started++;
70 else 71 else
71 Safefree (thr); 72 Safefree (thr);
72} 73}
73 74
74static 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
75end_thread(void) 101end_thread (void)
76{ 102{
77 aio_req req; 103 aio_req req;
78 New (0, req, 1, aio_cb); 104 New (0, req, 1, aio_cb);
79 req->type = REQ_QUIT; 105 req->type = REQ_QUIT;
80 write (reqpipe[1], &req, sizeof (aio_req));
81}
82 106
83static void 107 send_req (req);
84send_req (aio_req req)
85{
86 nreqs++;
87 write (reqpipe[1], &req, sizeof (aio_req));
88} 108}
89 109
90static void 110static void
91read_write (pTHX_ 111read_write (pTHX_
92 int dowrite, int fd, off_t offset, size_t length, 112 int dowrite, int fd, off_t offset, size_t length,
93 SV *data, STRLEN dataoffset, SV*callback) 113 SV *data, STRLEN dataoffset, SV *callback)
94{ 114{
95 aio_req req; 115 aio_req req;
96 STRLEN svlen; 116 STRLEN svlen;
97 char *svptr = SvPV (data, svlen); 117 char *svptr = SvPV (data, svlen);
98 118
162 if (req->data) 182 if (req->data)
163 SvREFCNT_dec (req->data); 183 SvREFCNT_dec (req->data);
164 184
165 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)
166 { 186 {
167 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; 187 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
168 PL_laststatval = req->result; 188 PL_laststatval = req->result;
169 PL_statcache.st_dev = req->statdata->st_dev; 189 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 190
183 Safefree (req->statdata); 191 Safefree (req->statdata);
184 } 192 }
185 193
186 PUSHMARK (SP); 194 PUSHMARK (SP);
198 } 206 }
199 207
200 Safefree (req); 208 Safefree (req);
201 } 209 }
202 210
211 if (qs)
212 send_reqs ();
213
203 return count; 214 return count;
204} 215}
205 216
206static sigset_t fullsigset; 217static sigset_t fullsigset;
207 218
208#undef errno 219#undef errno
209#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; \
210 237
211static int 238static int
212aio_proc(void *thr_arg) 239aio_proc (void *thr_arg)
213{ 240{
214 aio_thread *thr = thr_arg; 241 aio_thread *thr = thr_arg;
215 aio_req req; 242 aio_req req;
216 int errno; 243 int errno;
217 244
218 /* this is very much x86 and kernel-specific :(:(:( */ 245 /* this is very much kernel-specific :(:(:( */
219 /* we rely on gcc's ability to create closures. */ 246 /* we rely on gcc's ability to create closures. */
220 _syscall3(int,read,int,fd,char *,buf,size_t,count) 247 _syscall3(int,read,int,fd,char *,buf,size_t,count)
221 _syscall3(int,write,int,fd,char *,buf,size_t,count) 248 _syscall3(int,write,int,fd,char *,buf,size_t,count)
222 249
223 _syscall3(int,open,char *,pathname,int,flags,mode_t,mode) 250 _syscall3(int,open,char *,pathname,int,flags,mode_t,mode)
224 _syscall1(int,close,int,fd) 251 _syscall1(int,close,int,fd)
225 252
253#ifdef __NR_pread64
226 _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)
227 _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
228 262
263
264#ifdef __NR_stat64
229 _syscall2(int,stat64, const char *, filename, struct stat64 *, buf) 265 _syscall2(int,stat64, const char *, filename, struct stat64 *, buf)
230 _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf) 266 _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf)
231 _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);
232 277
233 sigprocmask (SIG_SETMASK, &fullsigset, 0); 278 sigprocmask (SIG_SETMASK, &fullsigset, 0);
279 prctl (PR_SET_PDEATHSIG, SIGKILL);
234 280
235 /* then loop */ 281 /* then loop */
236 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) 282 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req))
237 { 283 {
238 req->thread = thr; 284 req->thread = thr;
239 errno = 0; /* strictly unnecessary */ 285 errno = 0; /* strictly unnecessary */
240 286
241 if (req->type == REQ_READ) 287 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 { 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:
257 write (respipe[1], (void *)&req, sizeof (req)); 313 write (respipe[1], (void *)&req, sizeof (req));
258 break; 314 return 0;
259 } 315 }
260 316
261 req->errorno = errno; 317 req->errorno = errno;
262 write (respipe[1], (void *)&req, sizeof (req)); 318 write (respipe[1], (void *)&req, sizeof (req));
263 } 319 }
275 sigdelset (&fullsigset, SIGABRT); 331 sigdelset (&fullsigset, SIGABRT);
276 sigdelset (&fullsigset, SIGINT); 332 sigdelset (&fullsigset, SIGINT);
277 333
278 if (pipe (reqpipe) || pipe (respipe)) 334 if (pipe (reqpipe) || pipe (respipe))
279 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");
280 339
281 if (fcntl (respipe[0], F_SETFL, O_NONBLOCK)) 340 if (fcntl (respipe[0], F_SETFL, O_NONBLOCK))
282 croak ("cannot set result pipe to nonblocking mode"); 341 croak ("cannot set result pipe to nonblocking mode");
283} 342}
284 343
392 Newz (0, req, 1, aio_cb); 451 Newz (0, req, 1, aio_cb);
393 452
394 if (!req) 453 if (!req)
395 croak ("out of memory during aio_req allocation"); 454 croak ("out of memory during aio_req allocation");
396 455
397 New (0, req->statdata, 1, struct stat64); 456 New (0, req->statdata, 1, Stat_t);
398 457
399 if (!req->statdata) 458 if (!req->statdata)
400 croak ("out of memory during aio_req->statdata allocation"); 459 croak ("out of memory during aio_req->statdata allocation");
401 460
402 if (SvPOK (fh_or_path)) 461 if (SvPOK (fh_or_path))
413 472
414 req->callback = SvREFCNT_inc (callback); 473 req->callback = SvREFCNT_inc (callback);
415 474
416 send_req (req); 475 send_req (req);
417 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
418int 497int
419poll_fileno() 498poll_fileno()
420 PROTOTYPE: 499 PROTOTYPE:
421 CODE: 500 CODE:
422 RETVAL = respipe[0]; 501 RETVAL = respipe[0];

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines