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.12 by root, Tue Dec 25 02:33:48 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
11typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */
12
10#define STACKSIZE 1024 /* yeah */ 13#define STACKSIZE 1024 /* yeah */
11 14
12#define REQ_QUIT 0 15enum { REQ_QUIT, REQ_READ, REQ_WRITE, REQ_OPEN, REQ_CLOSE };
13#define REQ_READ 1
14#define REQ_WRITE 2
15 16
16typedef struct { 17typedef struct {
17 char stack[STACKSIZE]; 18 char stack[STACKSIZE];
18} aio_thread; 19} aio_thread;
19 20
24/* read/write */ 25/* read/write */
25 int fd; 26 int fd;
26 off_t offset; 27 off_t offset;
27 size_t length; 28 size_t length;
28 ssize_t result; 29 ssize_t result;
30 mode_t mode; /* open */
29 int errorno; 31 int errorno;
30
31 SV *data, *callback; 32 SV *data, *callback;
32 void *dataptr; 33 void *dataptr;
33 STRLEN dataoffset; 34 STRLEN dataoffset;
34} aio_cb; 35} aio_cb;
35 36
61end_thread(void) 62end_thread(void)
62{ 63{
63 aio_req req; 64 aio_req req;
64 New (0, req, 1, aio_cb); 65 New (0, req, 1, aio_cb);
65 req->type = REQ_QUIT; 66 req->type = REQ_QUIT;
67 write (reqpipe[1], &req, sizeof (aio_req));
68}
69
70static void
71send_req (aio_req req)
72{
73 nreqs++;
66 write (reqpipe[1], &req, sizeof (aio_req)); 74 write (reqpipe[1], &req, sizeof (aio_req));
67} 75}
68 76
69static void 77static void
70read_write (pTHX_ int dowrite, int fd, off_t offset, size_t length, 78read_write (pTHX_ int dowrite, int fd, off_t offset, size_t length,
103 req->type = dowrite ? REQ_WRITE : REQ_READ; 111 req->type = dowrite ? REQ_WRITE : REQ_READ;
104 req->fd = fd; 112 req->fd = fd;
105 req->offset = offset; 113 req->offset = offset;
106 req->length = length; 114 req->length = length;
107 req->data = SvREFCNT_inc (data); 115 req->data = SvREFCNT_inc (data);
108 req->dataptr = svptr + dataoffset; 116 req->dataptr = (char *)svptr + dataoffset;
109 req->callback = SvREFCNT_inc (callback); 117 req->callback = SvREFCNT_inc (callback);
110 118
111 nreqs++; 119 send_req (req);
112 write (reqpipe[1], &req, sizeof (aio_req));
113} 120}
114 121
115static int 122static int
116poll_cb (pTHX) 123poll_cb (pTHX)
117{ 124{
130 { 137 {
131 int errorno = errno; 138 int errorno = errno;
132 errno = req->errorno; 139 errno = req->errorno;
133 140
134 if (req->type == REQ_READ) 141 if (req->type == REQ_READ)
135 SvCUR_set (req->data, req->result > 0 142 SvCUR_set (req->data, req->dataoffset
136 ? req->dataoffset + req->result 143 + req->result > 0 ? req->result : 0);
137 : req->dataoffset);
138 144
139 PUSHMARK (SP); 145 PUSHMARK (SP);
140 XPUSHs (sv_2mortal (newSViv (req->result))); 146 XPUSHs (sv_2mortal (newSViv (req->result)));
141 PUTBACK; 147 PUTBACK;
142 call_sv (req->callback, G_VOID); 148 call_sv (req->callback, G_VOID);
154 } 160 }
155 161
156 return count; 162 return count;
157} 163}
158 164
165static sigset_t fullsigset;
166
159#undef errno 167#undef errno
160#include <asm/unistd.h> 168#include <asm/unistd.h>
161 169
162static int 170static int
163aio_proc(void *thr_arg) 171aio_proc(void *thr_arg)
164{ 172{
165 aio_thread *thr = thr_arg; 173 aio_thread *thr = thr_arg;
166 int sig; 174 aio_req req;
167 int errno; 175 int errno;
168 aio_req req;
169 176
170 /* we rely on gcc's ability to create closures. */ 177 /* we rely on gcc's ability to create closures. */
171 _syscall3(int,lseek,int,fd,off_t,offset,int,whence); 178 _syscall3(int,lseek,int,fd,off_t,offset,int,whence)
172 _syscall3(int,read,int,fd,char *,buf,off_t,count); 179 _syscall3(int,read,int,fd,char *,buf,off_t,count)
173 _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)
174 183
175 /* first get rid of any signals */ 184 sigprocmask (SIG_SETMASK, &fullsigset, 0);
176 for (sig = 1; sig < _NSIG; sig++)
177 signal (sig, SIG_DFL);
178 185
179 signal (SIGPIPE, SIG_IGN);
180
181 /* then loop */ 186 /* then loop */
182 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) 187 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req))
183 { 188 {
184 req->thread = thr; 189 req->thread = thr;
190 errno = 0;
185 191
186 if (req->type == REQ_READ || req->type == REQ_WRITE) 192 if (req->type == REQ_READ || req->type == REQ_WRITE)
187 { 193 {
188 errno = 0;
189
190 if (lseek (req->fd, req->offset, SEEK_SET) == req->offset) 194 if (lseek (req->fd, req->offset, SEEK_SET) == req->offset)
191 { 195 {
192 if (req->type == REQ_READ) 196 if (req->type == REQ_READ)
193 req->result = read (req->fd, req->dataptr, req->length); 197 req->result = read (req->fd, req->dataptr, req->length);
194 else 198 else
195 req->result = write(req->fd, req->dataptr, req->length); 199 req->result = write(req->fd, req->dataptr, req->length);
196 } 200 }
197 201 }
198 req->errorno = errno; 202 else if (req->type == REQ_OPEN)
203 {
204 req->result = open (req->dataptr, req->fd, req->mode);
205 }
206 else if (req->type == REQ_CLOSE)
207 {
208 req->result = close (req->fd);
199 } 209 }
200 else 210 else
201 { 211 {
202 write (respipe[1], (void *)&req, sizeof (req)); 212 write (respipe[1], (void *)&req, sizeof (req));
203 break; 213 break;
204 } 214 }
205 215
216 req->errorno = errno;
206 write (respipe[1], (void *)&req, sizeof (req)); 217 write (respipe[1], (void *)&req, sizeof (req));
207 } 218 }
208 219
209 return 0; 220 return 0;
210} 221}
211 222
212MODULE = Linux::AIO PACKAGE = Linux::AIO 223MODULE = Linux::AIO PACKAGE = Linux::AIO
213 224
214BOOT: 225BOOT:
215{ 226{
227 sigfillset (&fullsigset);
228 sigdelset (&fullsigset, SIGTERM);
229 sigdelset (&fullsigset, SIGQUIT);
230 sigdelset (&fullsigset, SIGABRT);
231 sigdelset (&fullsigset, SIGINT);
232
216 if (pipe (reqpipe) || pipe (respipe)) 233 if (pipe (reqpipe) || pipe (respipe))
217 croak ("unable to initialize request or result pipe"); 234 croak ("unable to initialize request or result pipe");
218 235
219 if (fcntl (respipe[0], F_SETFL, O_NONBLOCK)) 236 if (fcntl (respipe[0], F_SETFL, O_NONBLOCK))
220 croak ("cannot set result pipe to nonblocking mode"); 237 croak ("cannot set result pipe to nonblocking mode");
242 259
243 poll_cb (); 260 poll_cb ();
244 while (started > nthreads) 261 while (started > nthreads)
245 { 262 {
246 sched_yield (); 263 sched_yield ();
264 fcntl (respipe[0], F_SETFL, 0);
247 poll_cb (); 265 poll_cb ();
266 fcntl (respipe[0], F_SETFL, O_NONBLOCK);
248 } 267 }
249 268
250void 269void
251aio_read(fh,offset,length,data,dataoffset,callback) 270aio_read(fh,offset,length,data,dataoffset,callback)
252 PerlIO * fh 271 InOutStream fh
253 unsigned long offset 272 UV offset
254 long length 273 IV length
255 SV * data 274 SV * data
256 STRLEN dataoffset 275 IV dataoffset
257 SV * callback 276 SV * callback
258 PROTOTYPE: $$$$$$ 277 PROTOTYPE: $$$$$$
259 ALIAS: 278 ALIAS:
260 aio_write = 1 279 aio_write = 1
261 CODE: 280 CODE:
262 sv_upgrade (data, SVt_PV); 281 SvUPGRADE (data, SVt_PV);
282 SvPOK_on (data);
263 read_write (aTHX_ ix, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); 283 read_write (aTHX_ ix, PerlIO_fileno (fh), offset, length, data, dataoffset, callback);
284
285void
286aio_open(pathname,flags,mode,callback)
287 char * pathname
288 int flags
289 int mode
290 SV * callback
291 PROTOTYPE: $$$$
292 CODE:
293 aio_req req;
294
295 New (0, req, 1, aio_cb);
296
297 if (!req)
298 croak ("out of memory during aio_req allocation");
299
300 req->type = REQ_OPEN;
301 req->dataptr = pathname;
302 req->fd = flags;
303 req->mode = mode;
304 req->callback = SvREFCNT_inc (callback);
305
306 send_req (req);
307
308void
309aio_close(fh,callback)
310 InOutStream fh
311 SV * callback
312 PROTOTYPE: $
313 CODE:
314 aio_req req;
315
316 New (0, req, 1, aio_cb);
317
318 if (!req)
319 croak ("out of memory during aio_req allocation");
320
321 req->type = REQ_CLOSE;
322 req->fd = PerlIO_fileno (fh);
323 req->callback = SvREFCNT_inc (callback);
324
325 send_req (req);
264 326
265int 327int
266poll_fileno() 328poll_fileno()
267 PROTOTYPE: 329 PROTOTYPE:
268 CODE: 330 CODE:
269 RETVAL = respipe[0]; 331 RETVAL = respipe[0];
270 OUTPUT: 332 OUTPUT:
271 RETVAL 333 RETVAL
272 334
273int 335int
274poll_cb() 336poll_cb(...)
275 PROTOTYPE: 337 PROTOTYPE:
276 CODE: 338 CODE:
277 RETVAL = poll_cb (aTHX); 339 RETVAL = poll_cb (aTHX);
278 OUTPUT: 340 OUTPUT:
279 RETVAL 341 RETVAL

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines