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.18 by root, Thu May 6 15:05:57 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 { REQ_QUIT, REQ_OPEN, REQ_CLOSE, REQ_READ, REQ_WRITE, REQ_STAT, REQ_LSTAT, REQ_FSTAT};
19 28
20typedef struct { 29typedef struct {
21 char stack[STACKSIZE]; 30 char stack[STACKSIZE];
22} aio_thread; 31} aio_thread;
23 32
24typedef struct { 33typedef struct aio_cb {
34 struct aio_cb *next;
35
25 int type; 36 int type;
26 aio_thread *thread; 37 aio_thread *thread;
27 38
28 int fd; 39 int fd;
29 off_t offset; 40 off_t offset;
42 53
43static int started; 54static int started;
44static int nreqs; 55static int nreqs;
45static int reqpipe[2], respipe[2]; 56static int reqpipe[2], respipe[2];
46 57
58static aio_req qs, qe; /* queue start, queue end */
59
47static int aio_proc(void *arg); 60static int aio_proc(void *arg);
48 61
49static void 62static void
50start_thread(void) 63start_thread (void)
51{ 64{
52 aio_thread *thr; 65 aio_thread *thr;
53 66
54 New (0, thr, 1, aio_thread); 67 New (0, thr, 1, aio_thread);
55 68
61 else 74 else
62 Safefree (thr); 75 Safefree (thr);
63} 76}
64 77
65static void 78static void
79send_reqs (void)
80{
81 /* this write is atomic */
82 while (qs && write (reqpipe[1], &qs, sizeof qs) == sizeof qs)
83 {
84 qs = qs->next;
85 if (!qs) qe = 0;
86 }
87}
88
89static void
90send_req (aio_req req)
91{
92 nreqs++;
93 req->next = 0;
94
95 if (qe)
96 qe->next = req;
97 else
98 qe = qs = req;
99
100 send_reqs ();
101}
102
103static void
66end_thread(void) 104end_thread (void)
67{ 105{
68 aio_req req; 106 aio_req req;
69 New (0, req, 1, aio_cb); 107 New (0, req, 1, aio_cb);
70 req->type = REQ_QUIT; 108 req->type = REQ_QUIT;
71 write (reqpipe[1], &req, sizeof (aio_req)); 109
110 send_req (req);
72} 111}
73 112
74static void 113static void
75send_req (aio_req req) 114read_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, 115 int dowrite, int fd, off_t offset, size_t length,
83 SV *data, STRLEN dataoffset, SV*callback) 116 SV *data, STRLEN dataoffset, SV *callback)
84{ 117{
85 aio_req req; 118 aio_req req;
86 STRLEN svlen; 119 STRLEN svlen;
87 char *svptr = SvPV (data, svlen); 120 char *svptr = SvPV (data, svlen);
88 121
188 } 221 }
189 222
190 Safefree (req); 223 Safefree (req);
191 } 224 }
192 225
226 if (qs)
227 {
228 printf ("outstanding %p %d\n", qs, nreqs);
229 send_reqs ();
230 }
231
193 return count; 232 return count;
194} 233}
195 234
196static sigset_t fullsigset; 235static sigset_t fullsigset;
197 236
198#undef errno 237#undef errno
199#include <asm/unistd.h> 238#include <asm/unistd.h>
200 239
201static int 240static int
202aio_proc(void *thr_arg) 241aio_proc (void *thr_arg)
203{ 242{
204 aio_thread *thr = thr_arg; 243 aio_thread *thr = thr_arg;
205 aio_req req; 244 aio_req req;
206 int errno; 245 int errno;
207 246
247 /* this is very much x86 and kernel-specific :(:(:( */
208 /* we rely on gcc's ability to create closures. */ 248 /* we rely on gcc's ability to create closures. */
209 _syscall3(int,read,int,fd,char *,buf,size_t,count) 249 _syscall3(int,read,int,fd,char *,buf,size_t,count)
210 _syscall3(int,write,int,fd,char *,buf,size_t,count) 250 _syscall3(int,write,int,fd,char *,buf,size_t,count)
211 251
212 _syscall3(int,open,char *,pathname,int,flags,mode_t,mode) 252 _syscall3(int,open,char *,pathname,int,flags,mode_t,mode)
213 _syscall1(int,close,int,fd) 253 _syscall1(int,close,int,fd)
214 254
215 _syscall4(int,pread,int,fd,char *,buf,size_t,count,off_t,offset) 255 _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) 256 _syscall5(int,pwrite64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi)
217 257
218 _syscall2(int,stat64, const char *, filename, struct stat64 *, buf) 258 _syscall2(int,stat64, const char *, filename, struct stat64 *, buf)
219 _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf) 259 _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf)
220 _syscall2(int,fstat64, int, fd, struct stat64 *, buf) 260 _syscall2(int,fstat64, int, fd, struct stat64 *, buf)
221 261
225 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) 265 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req))
226 { 266 {
227 req->thread = thr; 267 req->thread = thr;
228 errno = 0; /* strictly unnecessary */ 268 errno = 0; /* strictly unnecessary */
229 269
230 if (req->type == REQ_READ) 270 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 { 271 {
272 case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); break;
273 case REQ_WRITE: req->result = pwrite64(req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); break;
274 case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break;
275 case REQ_CLOSE: req->result = close (req->fd); break;
276 case REQ_STAT: req->result = stat64 (req->dataptr, req->statdata); break;
277 case REQ_LSTAT: req->result = lstat64 (req->dataptr, req->statdata); break;
278 case REQ_FSTAT: req->result = fstat64 (req->fd, req->statdata); break;
279
280 case REQ_QUIT:
281 default:
246 write (respipe[1], (void *)&req, sizeof (req)); 282 write (respipe[1], (void *)&req, sizeof (req));
247 break; 283 return 0;
248 } 284 }
249 285
250 req->errorno = errno; 286 req->errorno = errno;
251 write (respipe[1], (void *)&req, sizeof (req)); 287 write (respipe[1], (void *)&req, sizeof (req));
252 } 288 }
264 sigdelset (&fullsigset, SIGABRT); 300 sigdelset (&fullsigset, SIGABRT);
265 sigdelset (&fullsigset, SIGINT); 301 sigdelset (&fullsigset, SIGINT);
266 302
267 if (pipe (reqpipe) || pipe (respipe)) 303 if (pipe (reqpipe) || pipe (respipe))
268 croak ("unable to initialize request or result pipe"); 304 croak ("unable to initialize request or result pipe");
305
306 if (fcntl (reqpipe[1], F_SETFL, O_NONBLOCK))
307 croak ("cannot set result pipe to nonblocking mode");
269 308
270 if (fcntl (respipe[0], F_SETFL, O_NONBLOCK)) 309 if (fcntl (respipe[0], F_SETFL, O_NONBLOCK))
271 croak ("cannot set result pipe to nonblocking mode"); 310 croak ("cannot set result pipe to nonblocking mode");
272} 311}
273 312
296 fd_set rfd; 335 fd_set rfd;
297 FD_ZERO(&rfd); 336 FD_ZERO(&rfd);
298 FD_SET(respipe[0], &rfd); 337 FD_SET(respipe[0], &rfd);
299 338
300 select (respipe[0] + 1, &rfd, 0, 0, 0); 339 select (respipe[0] + 1, &rfd, 0, 0, 0);
301 poll_cb (); 340 poll_cb (aTHX);
302 } 341 }
303 342
304void 343void
305aio_open(pathname,flags,mode,callback) 344aio_open(pathname,flags,mode,callback)
306 SV * pathname 345 SV * pathname

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines