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.7 by root, Wed Aug 15 03:24:08 2001 UTC vs.
Revision 1.9 by root, Thu Aug 16 18:58:34 2001 UTC

3#include "XSUB.h" 3#include "XSUB.h"
4 4
5#include <sys/types.h> 5#include <sys/types.h>
6#include <unistd.h> 6#include <unistd.h>
7#include <fcntl.h> 7#include <fcntl.h>
8#include <signal.h>
8#include <sched.h> 9#include <sched.h>
9 10
10#define STACKSIZE 4096 /* yeah */ 11#define STACKSIZE 1024 /* yeah */
11 12
12#define REQ_QUIT 0 13enum { REQ_QUIT, REQ_READ, REQ_WRITE, REQ_OPEN };
13#define REQ_READ 1
14#define REQ_WRITE 2
15 14
16typedef struct { 15typedef struct {
17 char stack[STACKSIZE]; 16 char stack[STACKSIZE];
18} aio_thread; 17} aio_thread;
19 18
24/* read/write */ 23/* read/write */
25 int fd; 24 int fd;
26 off_t offset; 25 off_t offset;
27 size_t length; 26 size_t length;
28 ssize_t result; 27 ssize_t result;
28 mode_t mode; /* open */
29 int errorno; 29 int errorno;
30
31 SV *data, *callback; 30 SV *data, *callback;
32 void *dataptr; 31 void *dataptr;
33 STRLEN dataoffset; 32 STRLEN dataoffset;
34} aio_cb; 33} aio_cb;
35 34
61end_thread(void) 60end_thread(void)
62{ 61{
63 aio_req req; 62 aio_req req;
64 New (0, req, 1, aio_cb); 63 New (0, req, 1, aio_cb);
65 req->type = REQ_QUIT; 64 req->type = REQ_QUIT;
65 write (reqpipe[1], &req, sizeof (aio_req));
66}
67
68static void
69send_req (aio_req req)
70{
71 nreqs++;
66 write (reqpipe[1], &req, sizeof (aio_req)); 72 write (reqpipe[1], &req, sizeof (aio_req));
67} 73}
68 74
69static void 75static void
70read_write (pTHX_ int dowrite, int fd, off_t offset, size_t length, 76read_write (pTHX_ int dowrite, int fd, off_t offset, size_t length,
106 req->length = length; 112 req->length = length;
107 req->data = SvREFCNT_inc (data); 113 req->data = SvREFCNT_inc (data);
108 req->dataptr = (char *)svptr + dataoffset; 114 req->dataptr = (char *)svptr + dataoffset;
109 req->callback = SvREFCNT_inc (callback); 115 req->callback = SvREFCNT_inc (callback);
110 116
111 nreqs++; 117 send_req (req);
112 write (reqpipe[1], &req, sizeof (aio_req));
113} 118}
114 119
115static int 120static int
116poll_cb (pTHX) 121poll_cb (pTHX)
117{ 122{
153 } 158 }
154 159
155 return count; 160 return count;
156} 161}
157 162
163static sigset_t fullsigset;
164
158#undef errno 165#undef errno
159#include <asm/unistd.h> 166#include <asm/unistd.h>
160 167
161static int 168static int
162aio_proc(void *thr_arg) 169aio_proc(void *thr_arg)
163{ 170{
164 aio_thread *thr = thr_arg; 171 aio_thread *thr = thr_arg;
165 int sig; 172 aio_req req;
166 int errno; 173 int errno;
167 aio_req req; 174
175 sigprocmask (SIG_SETMASK, &fullsigset, 0);
168 176
169 /* we rely on gcc's ability to create closures. */ 177 /* we rely on gcc's ability to create closures. */
170 _syscall3(int,lseek,int,fd,off_t,offset,int,whence); 178 _syscall3(int,lseek,int,fd,off_t,offset,int,whence);
171 _syscall3(int,read,int,fd,char *,buf,off_t,count); 179 _syscall3(int,read,int,fd,char *,buf,off_t,count);
172 _syscall3(int,write,int,fd,char *,buf,off_t,count); 180 _syscall3(int,write,int,fd,char *,buf,off_t,count);
181 _syscall3(int,open,char *,pathname,int,flags,mode_t,mode);
173 182
174 /* first get rid of any signals */
175 for (sig = 1; sig < _NSIG; sig++)
176 signal (sig, SIG_DFL);
177
178 signal (SIGPIPE, SIG_IGN);
179
180 /* then loop */ 183 /* then loop */
181 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) 184 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req))
182 { 185 {
183 req->thread = thr; 186 req->thread = thr;
187 errno = 0;
184 188
185 if (req->type == REQ_READ || req->type == REQ_WRITE) 189 if (req->type == REQ_READ || req->type == REQ_WRITE)
186 { 190 {
187 errno = 0;
188
189 if (lseek (req->fd, req->offset, SEEK_SET) == req->offset) 191 if (lseek (req->fd, req->offset, SEEK_SET) == req->offset)
190 { 192 {
191 if (req->type == REQ_READ) 193 if (req->type == REQ_READ)
192 req->result = read (req->fd, req->dataptr, req->length); 194 req->result = read (req->fd, req->dataptr, req->length);
193 else 195 else
194 req->result = write(req->fd, req->dataptr, req->length); 196 req->result = write(req->fd, req->dataptr, req->length);
195 } 197 }
196 198 }
197 req->errorno = errno; 199 else if (req->type == REQ_OPEN)
200 {
201 req->result = open (req->dataptr, req->fd, req->mode);
198 } 202 }
199 else 203 else
200 { 204 {
201 write (respipe[1], (void *)&req, sizeof (req)); 205 write (respipe[1], (void *)&req, sizeof (req));
202 break; 206 break;
203 } 207 }
204 208
209 req->errorno = errno;
205 write (respipe[1], (void *)&req, sizeof (req)); 210 write (respipe[1], (void *)&req, sizeof (req));
206 } 211 }
207 212
208 return 0; 213 return 0;
209} 214}
210 215
211MODULE = Linux::AIO PACKAGE = Linux::AIO 216MODULE = Linux::AIO PACKAGE = Linux::AIO
212 217
213BOOT: 218BOOT:
214{ 219{
220 sigfillset (&fullsigset);
221 sigdelset (&fullsigset, SIGTERM);
222 sigdelset (&fullsigset, SIGQUIT);
223 sigdelset (&fullsigset, SIGABRT);
224 sigdelset (&fullsigset, SIGINT);
225
215 if (pipe (reqpipe) || pipe (respipe)) 226 if (pipe (reqpipe) || pipe (respipe))
216 croak ("unable to initialize request or result pipe"); 227 croak ("unable to initialize request or result pipe");
217 228
218 if (fcntl (respipe[0], F_SETFL, O_NONBLOCK)) 229 if (fcntl (respipe[0], F_SETFL, O_NONBLOCK))
219 croak ("cannot set result pipe to nonblocking mode"); 230 croak ("cannot set result pipe to nonblocking mode");
260 CODE: 271 CODE:
261 SvUPGRADE (data, SVt_PV); 272 SvUPGRADE (data, SVt_PV);
262 SvPOK_on (data); 273 SvPOK_on (data);
263 read_write (aTHX_ ix, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); 274 read_write (aTHX_ ix, PerlIO_fileno (fh), offset, length, data, dataoffset, callback);
264 275
276void
277aio_open(pathname,flags,mode,callback)
278 char * pathname
279 int flags
280 int mode
281 SV * callback
282 PROTOTYPE: $$$$
283 CODE:
284 aio_req req;
285
286 New (0, req, 1, aio_cb);
287
288 if (!req)
289 croak ("out of memory during aio_req allocation");
290
291 req->type = REQ_OPEN;
292 req->dataptr = pathname;
293 req->fd = flags;
294 req->mode = mode;
295 req->callback = SvREFCNT_inc (callback);
296
297 send_req (req);
298
265int 299int
266poll_fileno() 300poll_fileno()
267 PROTOTYPE: 301 PROTOTYPE:
268 CODE: 302 CODE:
269 RETVAL = respipe[0]; 303 RETVAL = respipe[0];

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines