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.15 by root, Sat May 18 21:48:36 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
211 _syscall3(int,write,int,fd,char *,buf,size_t,count) 250 _syscall3(int,write,int,fd,char *,buf,size_t,count)
212 251
213 _syscall3(int,open,char *,pathname,int,flags,mode_t,mode) 252 _syscall3(int,open,char *,pathname,int,flags,mode_t,mode)
214 _syscall1(int,close,int,fd) 253 _syscall1(int,close,int,fd)
215 254
216 _syscall5(int,pread,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi) 255 _syscall5(int,pread64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi)
217 _syscall5(int,pwrite,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi) 256 _syscall5(int,pwrite64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi)
218 257
219 _syscall2(int,stat64, const char *, filename, struct stat64 *, buf) 258 _syscall2(int,stat64, const char *, filename, struct stat64 *, buf)
220 _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf) 259 _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf)
221 _syscall2(int,fstat64, int, fd, struct stat64 *, buf) 260 _syscall2(int,fstat64, int, fd, struct stat64 *, buf)
222 261
226 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) 265 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req))
227 { 266 {
228 req->thread = thr; 267 req->thread = thr;
229 errno = 0; /* strictly unnecessary */ 268 errno = 0; /* strictly unnecessary */
230 269
231 if (req->type == REQ_READ) 270 switch (req->type)
232 req->result = pread (req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32);
233 else if (req->type == REQ_WRITE)
234 req->result = pwrite(req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32);
235 else if (req->type == REQ_OPEN)
236 req->result = open (req->dataptr, req->fd, req->mode);
237 else if (req->type == REQ_CLOSE)
238 req->result = close (req->fd);
239 else if (req->type == REQ_STAT)
240 req->result = stat64 (req->dataptr, req->statdata);
241 else if (req->type == REQ_LSTAT)
242 req->result = lstat64 (req->dataptr, req->statdata);
243 else if (req->type == REQ_FSTAT)
244 req->result = fstat64 (req->fd, req->statdata);
245 else
246 { 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:
247 write (respipe[1], (void *)&req, sizeof (req)); 282 write (respipe[1], (void *)&req, sizeof (req));
248 break; 283 return 0;
249 } 284 }
250 285
251 req->errorno = errno; 286 req->errorno = errno;
252 write (respipe[1], (void *)&req, sizeof (req)); 287 write (respipe[1], (void *)&req, sizeof (req));
253 } 288 }
265 sigdelset (&fullsigset, SIGABRT); 300 sigdelset (&fullsigset, SIGABRT);
266 sigdelset (&fullsigset, SIGINT); 301 sigdelset (&fullsigset, SIGINT);
267 302
268 if (pipe (reqpipe) || pipe (respipe)) 303 if (pipe (reqpipe) || pipe (respipe))
269 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");
270 308
271 if (fcntl (respipe[0], F_SETFL, O_NONBLOCK)) 309 if (fcntl (respipe[0], F_SETFL, O_NONBLOCK))
272 croak ("cannot set result pipe to nonblocking mode"); 310 croak ("cannot set result pipe to nonblocking mode");
273} 311}
274 312
297 fd_set rfd; 335 fd_set rfd;
298 FD_ZERO(&rfd); 336 FD_ZERO(&rfd);
299 FD_SET(respipe[0], &rfd); 337 FD_SET(respipe[0], &rfd);
300 338
301 select (respipe[0] + 1, &rfd, 0, 0, 0); 339 select (respipe[0] + 1, &rfd, 0, 0, 0);
302 poll_cb (); 340 poll_cb (aTHX);
303 } 341 }
304 342
305void 343void
306aio_open(pathname,flags,mode,callback) 344aio_open(pathname,flags,mode,callback)
307 SV * pathname 345 SV * pathname

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines