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.6 by root, Tue Aug 14 23:25:39 2001 UTC vs.
Revision 1.10 by root, Thu Aug 30 03:11:06 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 2048 /* yeah */ 11#define STACKSIZE 1024 /* yeah */
11 12
12#define REQ_QUIT 0 13enum { REQ_QUIT, REQ_READ, REQ_WRITE, REQ_OPEN, REQ_CLOSE };
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);
182 _syscall1(int,close,int,fd);
173 183
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 */ 184 /* then loop */
181 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) 185 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req))
182 { 186 {
183 req->thread = thr; 187 req->thread = thr;
188 errno = 0;
184 189
185 if (req->type == REQ_READ || req->type == REQ_WRITE) 190 if (req->type == REQ_READ || req->type == REQ_WRITE)
186 { 191 {
187 errno = 0;
188
189 if (lseek (req->fd, req->offset, SEEK_SET) == req->offset) 192 if (lseek (req->fd, req->offset, SEEK_SET) == req->offset)
190 { 193 {
191 if (req->type == REQ_READ) 194 if (req->type == REQ_READ)
192 req->result = read (req->fd, req->dataptr, req->length); 195 req->result = read (req->fd, req->dataptr, req->length);
193 else 196 else
194 req->result = write(req->fd, req->dataptr, req->length); 197 req->result = write(req->fd, req->dataptr, req->length);
195 } 198 }
196 199 }
197 req->errorno = errno; 200 else if (req->type == REQ_OPEN)
201 {
202 req->result = open (req->dataptr, req->fd, req->mode);
203 }
204 else if (req->type == REQ_CLOSE)
205 {
206 req->result = close (req->fd);
198 } 207 }
199 else 208 else
200 { 209 {
201 write (respipe[1], (void *)&req, sizeof (req)); 210 write (respipe[1], (void *)&req, sizeof (req));
202 break; 211 break;
203 } 212 }
204 213
214 req->errorno = errno;
205 write (respipe[1], (void *)&req, sizeof (req)); 215 write (respipe[1], (void *)&req, sizeof (req));
206 } 216 }
207 217
208 return 0; 218 return 0;
209} 219}
210 220
211MODULE = Linux::AIO PACKAGE = Linux::AIO 221MODULE = Linux::AIO PACKAGE = Linux::AIO
212 222
213BOOT: 223BOOT:
214{ 224{
225 sigfillset (&fullsigset);
226 sigdelset (&fullsigset, SIGTERM);
227 sigdelset (&fullsigset, SIGQUIT);
228 sigdelset (&fullsigset, SIGABRT);
229 sigdelset (&fullsigset, SIGINT);
230
215 if (pipe (reqpipe) || pipe (respipe)) 231 if (pipe (reqpipe) || pipe (respipe))
216 croak ("unable to initialize request or result pipe"); 232 croak ("unable to initialize request or result pipe");
217 233
218 if (fcntl (respipe[0], F_SETFL, O_NONBLOCK)) 234 if (fcntl (respipe[0], F_SETFL, O_NONBLOCK))
219 croak ("cannot set result pipe to nonblocking mode"); 235 croak ("cannot set result pipe to nonblocking mode");
260 CODE: 276 CODE:
261 SvUPGRADE (data, SVt_PV); 277 SvUPGRADE (data, SVt_PV);
262 SvPOK_on (data); 278 SvPOK_on (data);
263 read_write (aTHX_ ix, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); 279 read_write (aTHX_ ix, PerlIO_fileno (fh), offset, length, data, dataoffset, callback);
264 280
281void
282aio_open(pathname,flags,mode,callback)
283 char * pathname
284 int flags
285 int mode
286 SV * callback
287 PROTOTYPE: $$$$
288 CODE:
289 aio_req req;
290
291 New (0, req, 1, aio_cb);
292
293 if (!req)
294 croak ("out of memory during aio_req allocation");
295
296 req->type = REQ_OPEN;
297 req->dataptr = pathname;
298 req->fd = flags;
299 req->mode = mode;
300 req->callback = SvREFCNT_inc (callback);
301
302 send_req (req);
303
304void
305aio_close(fh,callback)
306 PerlIO * fh
307 SV * callback
308 PROTOTYPE: $
309 CODE:
310 aio_req req;
311
312 New (0, req, 1, aio_cb);
313
314 if (!req)
315 croak ("out of memory during aio_req allocation");
316
317 req->type = REQ_CLOSE;
318 req->fd = PerlIO_fileno (fh);
319 req->callback = SvREFCNT_inc (callback);
320
321 send_req (req);
322
265int 323int
266poll_fileno() 324poll_fileno()
267 PROTOTYPE: 325 PROTOTYPE:
268 CODE: 326 CODE:
269 RETVAL = respipe[0]; 327 RETVAL = respipe[0];

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines