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.5 by root, Tue Aug 14 18:06:37 2001 UTC vs.
Revision 1.11 by root, Mon Oct 8 12:58:41 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 1024 /* 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,
103 req->type = dowrite ? REQ_WRITE : REQ_READ; 109 req->type = dowrite ? REQ_WRITE : REQ_READ;
104 req->fd = fd; 110 req->fd = fd;
105 req->offset = offset; 111 req->offset = offset;
106 req->length = length; 112 req->length = length;
107 req->data = SvREFCNT_inc (data); 113 req->data = SvREFCNT_inc (data);
108 req->dataptr = 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{
130 { 135 {
131 int errorno = errno; 136 int errorno = errno;
132 errno = req->errorno; 137 errno = req->errorno;
133 138
134 if (req->type == REQ_READ) 139 if (req->type == REQ_READ)
135 SvCUR_set (req->data, req->result > 0 140 SvCUR_set (req->data, req->dataoffset
136 ? req->dataoffset + req->result 141 + req->result > 0 ? req->result : 0);
137 : req->dataoffset);
138 142
139 PUSHMARK (SP); 143 PUSHMARK (SP);
140 XPUSHs (sv_2mortal (newSViv (req->result))); 144 XPUSHs (sv_2mortal (newSViv (req->result)));
141 PUTBACK; 145 PUTBACK;
142 call_sv (req->callback, G_VOID); 146 call_sv (req->callback, G_VOID);
154 } 158 }
155 159
156 return count; 160 return count;
157} 161}
158 162
163static sigset_t fullsigset;
164
159#undef errno 165#undef errno
160#include <asm/unistd.h> 166#include <asm/unistd.h>
161 167
162static int 168static int
163aio_proc(void *thr_arg) 169aio_proc(void *thr_arg)
164{ 170{
165 aio_thread *thr = thr_arg; 171 aio_thread *thr = thr_arg;
166 int sig; 172 aio_req req;
167 int errno; 173 int errno;
168 aio_req req;
169 174
170 /* we rely on gcc's ability to create closures. */ 175 /* we rely on gcc's ability to create closures. */
171 _syscall3(int,lseek,int,fd,off_t,offset,int,whence); 176 _syscall3(int,lseek,int,fd,off_t,offset,int,whence)
172 _syscall3(int,read,int,fd,char *,buf,off_t,count); 177 _syscall3(int,read,int,fd,char *,buf,off_t,count)
173 _syscall3(int,write,int,fd,char *,buf,off_t,count); 178 _syscall3(int,write,int,fd,char *,buf,off_t,count)
179 _syscall3(int,open,char *,pathname,int,flags,mode_t,mode)
180 _syscall1(int,close,int,fd)
174 181
175 /* first get rid of any signals */ 182 sigprocmask (SIG_SETMASK, &fullsigset, 0);
176 for (sig = 1; sig < _NSIG; sig++)
177 signal (sig, SIG_DFL);
178 183
179 signal (SIGPIPE, SIG_IGN);
180
181 /* then loop */ 184 /* then loop */
182 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) 185 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req))
183 { 186 {
184 req->thread = thr; 187 req->thread = thr;
188 errno = 0;
185 189
186 if (req->type == REQ_READ || req->type == REQ_WRITE) 190 if (req->type == REQ_READ || req->type == REQ_WRITE)
187 { 191 {
188 errno = 0;
189
190 if (lseek (req->fd, req->offset, SEEK_SET) == req->offset) 192 if (lseek (req->fd, req->offset, SEEK_SET) == req->offset)
191 { 193 {
192 if (req->type == REQ_READ) 194 if (req->type == REQ_READ)
193 req->result = read (req->fd, req->dataptr, req->length); 195 req->result = read (req->fd, req->dataptr, req->length);
194 else 196 else
195 req->result = write(req->fd, req->dataptr, req->length); 197 req->result = write(req->fd, req->dataptr, req->length);
196 } 198 }
197 199 }
198 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);
199 } 207 }
200 else 208 else
201 { 209 {
202 write (respipe[1], (void *)&req, sizeof (req)); 210 write (respipe[1], (void *)&req, sizeof (req));
203 break; 211 break;
204 } 212 }
205 213
214 req->errorno = errno;
206 write (respipe[1], (void *)&req, sizeof (req)); 215 write (respipe[1], (void *)&req, sizeof (req));
207 } 216 }
208 217
209 return 0; 218 return 0;
210} 219}
211 220
212MODULE = Linux::AIO PACKAGE = Linux::AIO 221MODULE = Linux::AIO PACKAGE = Linux::AIO
213 222
214BOOT: 223BOOT:
215{ 224{
225 sigfillset (&fullsigset);
226 sigdelset (&fullsigset, SIGTERM);
227 sigdelset (&fullsigset, SIGQUIT);
228 sigdelset (&fullsigset, SIGABRT);
229 sigdelset (&fullsigset, SIGINT);
230
216 if (pipe (reqpipe) || pipe (respipe)) 231 if (pipe (reqpipe) || pipe (respipe))
217 croak ("unable to initialize request or result pipe"); 232 croak ("unable to initialize request or result pipe");
218 233
219 if (fcntl (respipe[0], F_SETFL, O_NONBLOCK)) 234 if (fcntl (respipe[0], F_SETFL, O_NONBLOCK))
220 croak ("cannot set result pipe to nonblocking mode"); 235 croak ("cannot set result pipe to nonblocking mode");
248 } 263 }
249 264
250void 265void
251aio_read(fh,offset,length,data,dataoffset,callback) 266aio_read(fh,offset,length,data,dataoffset,callback)
252 PerlIO * fh 267 PerlIO * fh
253 unsigned long offset 268 UV offset
254 long length 269 STRLEN length
255 SV * data 270 SV * data
256 STRLEN dataoffset 271 STRLEN dataoffset
257 SV * callback 272 SV * callback
258 PROTOTYPE: $$$$$$ 273 PROTOTYPE: $$$$$$
259 ALIAS: 274 ALIAS:
260 aio_write = 1 275 aio_write = 1
261 CODE: 276 CODE:
262 sv_upgrade (data, SVt_PV); 277 SvUPGRADE (data, SVt_PV);
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);
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);
264 322
265int 323int
266poll_fileno() 324poll_fileno()
267 PROTOTYPE: 325 PROTOTYPE:
268 CODE: 326 CODE:
269 RETVAL = respipe[0]; 327 RETVAL = respipe[0];
270 OUTPUT: 328 OUTPUT:
271 RETVAL 329 RETVAL
272 330
273int 331int
274poll_cb() 332poll_cb(...)
275 PROTOTYPE: 333 PROTOTYPE:
276 CODE: 334 CODE:
277 RETVAL = poll_cb (aTHX); 335 RETVAL = poll_cb (aTHX);
278 OUTPUT: 336 OUTPUT:
279 RETVAL 337 RETVAL

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines