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.14 by root, Tue Apr 2 14:01:09 2002 UTC vs.
Revision 1.20 by root, Sun Jul 18 10:55:34 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>
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 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
16#define STACKSIZE 1024 /* yeah */ 25#define STACKSIZE 1024 /* yeah */
17 26
18enum { REQ_QUIT, REQ_OPEN, REQ_CLOSE, REQ_READ, REQ_WRITE, REQ_STAT, REQ_LSTAT, REQ_FSTAT}; 27enum {
28 REQ_QUIT,
29 REQ_OPEN, REQ_CLOSE, REQ_READ, REQ_WRITE,
30 REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_UNLINK
31};
19 32
20typedef struct { 33typedef struct {
21 char stack[STACKSIZE]; 34 char stack[STACKSIZE];
22} aio_thread; 35} aio_thread;
23 36
24typedef struct { 37typedef struct aio_cb {
38 struct aio_cb *next;
39
25 int type; 40 int type;
26 aio_thread *thread; 41 aio_thread *thread;
27 42
28 int fd; 43 int fd;
29 off_t offset; 44 off_t offset;
42 57
43static int started; 58static int started;
44static int nreqs; 59static int nreqs;
45static int reqpipe[2], respipe[2]; 60static int reqpipe[2], respipe[2];
46 61
62static aio_req qs, qe; /* queue start, queue end */
63
47static int aio_proc(void *arg); 64static int aio_proc(void *arg);
48 65
49static void 66static void
50start_thread(void) 67start_thread (void)
51{ 68{
52 aio_thread *thr; 69 aio_thread *thr;
53 70
54 New (0, thr, 1, aio_thread); 71 New (0, thr, 1, aio_thread);
55 72
56 if (clone (aio_proc, 73 if (clone (aio_proc,
57 &(thr->stack[STACKSIZE]), 74 &(thr->stack[STACKSIZE]),
58 CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND, 75 CLONE_VM|CLONE_FS|CLONE_FILES,
59 thr) >= 0) 76 thr) >= 0)
60 started++; 77 started++;
61 else 78 else
62 Safefree (thr); 79 Safefree (thr);
63} 80}
64 81
65static void 82static void
83send_reqs (void)
84{
85 /* this write is atomic */
86 while (qs && write (reqpipe[1], &qs, sizeof qs) == sizeof qs)
87 {
88 qs = qs->next;
89 if (!qs) qe = 0;
90 }
91}
92
93static void
94send_req (aio_req req)
95{
96 nreqs++;
97 req->next = 0;
98
99 if (qe)
100 qe->next = req;
101 else
102 qe = qs = req;
103
104 send_reqs ();
105}
106
107static void
66end_thread(void) 108end_thread (void)
67{ 109{
68 aio_req req; 110 aio_req req;
69 New (0, req, 1, aio_cb); 111 New (0, req, 1, aio_cb);
70 req->type = REQ_QUIT; 112 req->type = REQ_QUIT;
71 write (reqpipe[1], &req, sizeof (aio_req)); 113
114 send_req (req);
72} 115}
73 116
74static void 117static void
75send_req (aio_req req) 118read_write (pTHX_
76{
77 nreqs++;
78 write (reqpipe[1], &req, sizeof (aio_req));
79}
80
81static void
82read_write (pTHX_ int dowrite, int fd, off_t offset, size_t length, 119 int dowrite, int fd, off_t offset, size_t length,
83 SV *data, STRLEN dataoffset, SV*callback) 120 SV *data, STRLEN dataoffset, SV *callback)
84{ 121{
85 aio_req req; 122 aio_req req;
86 STRLEN svlen; 123 STRLEN svlen;
87 char *svptr = SvPV (data, svlen); 124 char *svptr = SvPV (data, svlen);
88 125
188 } 225 }
189 226
190 Safefree (req); 227 Safefree (req);
191 } 228 }
192 229
230 if (qs)
231 send_reqs ();
232
193 return count; 233 return count;
194} 234}
195 235
196static sigset_t fullsigset; 236static sigset_t fullsigset;
197 237
198#undef errno 238#undef errno
199#include <asm/unistd.h> 239#include <asm/unistd.h>
240#include <sys/prctl.h>
200 241
201static int 242static int
202aio_proc(void *thr_arg) 243aio_proc (void *thr_arg)
203{ 244{
204 aio_thread *thr = thr_arg; 245 aio_thread *thr = thr_arg;
205 aio_req req; 246 aio_req req;
206 int errno; 247 int errno;
207 248
249 /* this is very much x86 and kernel-specific :(:(:( */
208 /* we rely on gcc's ability to create closures. */ 250 /* we rely on gcc's ability to create closures. */
209 _syscall3(int,read,int,fd,char *,buf,size_t,count) 251 _syscall3(int,read,int,fd,char *,buf,size_t,count)
210 _syscall3(int,write,int,fd,char *,buf,size_t,count) 252 _syscall3(int,write,int,fd,char *,buf,size_t,count)
211 253
212 _syscall3(int,open,char *,pathname,int,flags,mode_t,mode) 254 _syscall3(int,open,char *,pathname,int,flags,mode_t,mode)
213 _syscall1(int,close,int,fd) 255 _syscall1(int,close,int,fd)
214 256
215 _syscall4(int,pread,int,fd,char *,buf,size_t,count,off_t,offset) 257 _syscall5(int,pread64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi)
216 _syscall4(int,pwrite,int,fd,char *,buf,size_t,count,off_t,offset) 258 _syscall5(int,pwrite64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi)
217 259
218 _syscall2(int,stat64, const char *, filename, struct stat64 *, buf) 260 _syscall2(int,stat64, const char *, filename, struct stat64 *, buf)
219 _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf) 261 _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf)
220 _syscall2(int,fstat64, int, fd, struct stat64 *, buf) 262 _syscall2(int,fstat64, int, fd, struct stat64 *, buf)
221 263
264 _syscall1(int,unlink, char *, filename);
265
222 sigprocmask (SIG_SETMASK, &fullsigset, 0); 266 sigprocmask (SIG_SETMASK, &fullsigset, 0);
267 prctl (PR_SET_PDEATHSIG, SIGKILL);
223 268
224 /* then loop */ 269 /* then loop */
225 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) 270 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req))
226 { 271 {
227 req->thread = thr; 272 req->thread = thr;
228 errno = 0; /* strictly unnecessary */ 273 errno = 0; /* strictly unnecessary */
229 274
230 if (req->type == REQ_READ) 275 switch (req->type)
231 req->result = pread (req->fd, req->dataptr, req->length, req->offset);
232 else if (req->type == REQ_WRITE)
233 req->result = pwrite (req->fd, req->dataptr, req->length, req->offset);
234 else if (req->type == REQ_OPEN)
235 req->result = open (req->dataptr, req->fd, req->mode);
236 else if (req->type == REQ_CLOSE)
237 req->result = close (req->fd);
238 else if (req->type == REQ_STAT)
239 req->result = stat64 (req->dataptr, req->statdata);
240 else if (req->type == REQ_LSTAT)
241 req->result = lstat64 (req->dataptr, req->statdata);
242 else if (req->type == REQ_FSTAT)
243 req->result = fstat64 (req->fd, req->statdata);
244 else
245 { 276 {
277 case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); break;
278 case REQ_WRITE: req->result = pwrite64(req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); break;
279 case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break;
280 case REQ_CLOSE: req->result = close (req->fd); break;
281 case REQ_STAT: req->result = stat64 (req->dataptr, req->statdata); break;
282 case REQ_LSTAT: req->result = lstat64 (req->dataptr, req->statdata); break;
283 case REQ_FSTAT: req->result = fstat64 (req->fd, req->statdata); break;
284 case REQ_UNLINK: req->result = unlink (req->dataptr); break;
285
286 case REQ_QUIT:
287 default:
246 write (respipe[1], (void *)&req, sizeof (req)); 288 write (respipe[1], (void *)&req, sizeof (req));
247 break; 289 return 0;
248 } 290 }
249 291
250 req->errorno = errno; 292 req->errorno = errno;
251 write (respipe[1], (void *)&req, sizeof (req)); 293 write (respipe[1], (void *)&req, sizeof (req));
252 } 294 }
264 sigdelset (&fullsigset, SIGABRT); 306 sigdelset (&fullsigset, SIGABRT);
265 sigdelset (&fullsigset, SIGINT); 307 sigdelset (&fullsigset, SIGINT);
266 308
267 if (pipe (reqpipe) || pipe (respipe)) 309 if (pipe (reqpipe) || pipe (respipe))
268 croak ("unable to initialize request or result pipe"); 310 croak ("unable to initialize request or result pipe");
311
312 if (fcntl (reqpipe[1], F_SETFL, O_NONBLOCK))
313 croak ("cannot set result pipe to nonblocking mode");
269 314
270 if (fcntl (respipe[0], F_SETFL, O_NONBLOCK)) 315 if (fcntl (respipe[0], F_SETFL, O_NONBLOCK))
271 croak ("cannot set result pipe to nonblocking mode"); 316 croak ("cannot set result pipe to nonblocking mode");
272} 317}
273 318
296 fd_set rfd; 341 fd_set rfd;
297 FD_ZERO(&rfd); 342 FD_ZERO(&rfd);
298 FD_SET(respipe[0], &rfd); 343 FD_SET(respipe[0], &rfd);
299 344
300 select (respipe[0] + 1, &rfd, 0, 0, 0); 345 select (respipe[0] + 1, &rfd, 0, 0, 0);
301 poll_cb (); 346 poll_cb (aTHX);
302 } 347 }
303 348
304void 349void
305aio_open(pathname,flags,mode,callback) 350aio_open(pathname,flags,mode,callback)
306 SV * pathname 351 SV * pathname
402 447
403 req->callback = SvREFCNT_inc (callback); 448 req->callback = SvREFCNT_inc (callback);
404 449
405 send_req (req); 450 send_req (req);
406 451
452void
453aio_unlink(pathname,callback)
454 SV * pathname
455 SV * callback
456 PROTOTYPE: $$
457 CODE:
458 aio_req req;
459
460 Newz (0, req, 1, aio_cb);
461
462 if (!req)
463 croak ("out of memory during aio_req allocation");
464
465 req->type = REQ_UNLINK;
466 req->data = newSVsv (pathname);
467 req->dataptr = SvPV_nolen (req->data);
468 req->callback = SvREFCNT_inc (callback);
469
470 send_req (req);
471
407int 472int
408poll_fileno() 473poll_fileno()
409 PROTOTYPE: 474 PROTOTYPE:
410 CODE: 475 CODE:
411 RETVAL = respipe[0]; 476 RETVAL = respipe[0];

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines