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.13 by root, Mon Apr 1 20:30:08 2002 UTC

1#include "EXTERN.h" 1#include "EXTERN.h"
2#include "perl.h" 2#include "perl.h"
3#include "XSUB.h" 3#include "XSUB.h"
4 4
5#include <sys/types.h> 5#include <sys/types.h>
6#include <sys/stat.h>
6#include <unistd.h> 7#include <unistd.h>
7#include <fcntl.h> 8#include <fcntl.h>
9#include <signal.h>
8#include <sched.h> 10#include <sched.h>
9 11
12typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */
13typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */
14typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */
15
10#define STACKSIZE 1024 /* yeah */ 16#define STACKSIZE 1024 /* yeah */
11 17
12#define REQ_QUIT 0 18enum { REQ_QUIT, REQ_OPEN, REQ_CLOSE, REQ_READ, REQ_WRITE, REQ_STAT, REQ_LSTAT, REQ_FSTAT};
13#define REQ_READ 1
14#define REQ_WRITE 2
15 19
16typedef struct { 20typedef struct {
17 char stack[STACKSIZE]; 21 char stack[STACKSIZE];
18} aio_thread; 22} aio_thread;
19 23
20typedef struct { 24typedef struct {
21 int type; 25 int type;
22 aio_thread *thread; 26 aio_thread *thread;
23 27
24/* read/write */ 28 SV *savesv;
29
25 int fd; 30 int fd;
26 off_t offset; 31 off_t offset;
27 size_t length; 32 size_t length;
28 ssize_t result; 33 ssize_t result;
34 mode_t mode; /* open */
29 int errorno; 35 int errorno;
30
31 SV *data, *callback; 36 SV *data, *callback;
32 void *dataptr; 37 void *dataptr;
33 STRLEN dataoffset; 38 STRLEN dataoffset;
39
40 struct stat64 *statdata;
34} aio_cb; 41} aio_cb;
35 42
36typedef aio_cb *aio_req; 43typedef aio_cb *aio_req;
37 44
38static int started; 45static int started;
65 req->type = REQ_QUIT; 72 req->type = REQ_QUIT;
66 write (reqpipe[1], &req, sizeof (aio_req)); 73 write (reqpipe[1], &req, sizeof (aio_req));
67} 74}
68 75
69static void 76static void
77send_req (aio_req req)
78{
79 nreqs++;
80 write (reqpipe[1], &req, sizeof (aio_req));
81}
82
83static void
70read_write (pTHX_ int dowrite, int fd, off_t offset, size_t length, 84read_write (pTHX_ int dowrite, int fd, off_t offset, size_t length,
71 SV *data, STRLEN dataoffset, SV*callback) 85 SV *data, STRLEN dataoffset, SV*callback)
72{ 86{
73 aio_req req; 87 aio_req req;
74 STRLEN svlen; 88 STRLEN svlen;
75 char *svptr = SvPV (data, svlen); 89 char *svptr = SvPV (data, svlen);
90
91 SvUPGRADE (data, SVt_PV);
92 SvPOK_on (data);
76 93
77 if (dataoffset < 0) 94 if (dataoffset < 0)
78 dataoffset += svlen; 95 dataoffset += svlen;
79 96
80 if (dataoffset < 0 || dataoffset > svlen) 97 if (dataoffset < 0 || dataoffset > svlen)
103 req->type = dowrite ? REQ_WRITE : REQ_READ; 120 req->type = dowrite ? REQ_WRITE : REQ_READ;
104 req->fd = fd; 121 req->fd = fd;
105 req->offset = offset; 122 req->offset = offset;
106 req->length = length; 123 req->length = length;
107 req->data = SvREFCNT_inc (data); 124 req->data = SvREFCNT_inc (data);
108 req->dataptr = svptr + dataoffset; 125 req->dataptr = (char *)svptr + dataoffset;
109 req->callback = SvREFCNT_inc (callback); 126 req->callback = SvREFCNT_inc (callback);
110 127
111 nreqs++; 128 send_req (req);
112 write (reqpipe[1], &req, sizeof (aio_req));
113} 129}
114 130
115static int 131static int
116poll_cb (pTHX) 132poll_cb (pTHX)
117{ 133{
129 else 145 else
130 { 146 {
131 int errorno = errno; 147 int errorno = errno;
132 errno = req->errorno; 148 errno = req->errorno;
133 149
150 if (req->savesv)
151 SvREFCNT_dec (req->savesv);
152
134 if (req->type == REQ_READ) 153 if (req->type == REQ_READ)
135 SvCUR_set (req->data, req->result > 0 154 SvCUR_set (req->data, req->dataoffset
136 ? req->dataoffset + req->result 155 + req->result > 0 ? req->result : 0);
137 : req->dataoffset); 156
157 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT)
158 {
159 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
160 PL_laststatval = req->result;
161 PL_statcache.st_dev = req->statdata->st_dev;
162 PL_statcache.st_ino = req->statdata->st_ino;
163 PL_statcache.st_mode = req->statdata->st_mode;
164 PL_statcache.st_nlink = req->statdata->st_nlink;
165 PL_statcache.st_uid = req->statdata->st_uid;
166 PL_statcache.st_gid = req->statdata->st_gid;
167 PL_statcache.st_rdev = req->statdata->st_rdev;
168 PL_statcache.st_size = req->statdata->st_size;
169 PL_statcache.st_atime = req->statdata->st_atime;
170 PL_statcache.st_mtime = req->statdata->st_mtime;
171 PL_statcache.st_ctime = req->statdata->st_ctime;
172 PL_statcache.st_blksize = req->statdata->st_blksize;
173 PL_statcache.st_blocks = req->statdata->st_blocks;
174
175 Safefree (req->statdata);
176 }
138 177
139 PUSHMARK (SP); 178 PUSHMARK (SP);
140 XPUSHs (sv_2mortal (newSViv (req->result))); 179 XPUSHs (sv_2mortal (newSViv (req->result)));
141 PUTBACK; 180 PUTBACK;
142 call_sv (req->callback, G_VOID); 181 call_sv (req->callback, G_VOID);
154 } 193 }
155 194
156 return count; 195 return count;
157} 196}
158 197
198static sigset_t fullsigset;
199
159#undef errno 200#undef errno
160#include <asm/unistd.h> 201#include <asm/unistd.h>
161 202
162static int 203static int
163aio_proc(void *thr_arg) 204aio_proc(void *thr_arg)
164{ 205{
165 aio_thread *thr = thr_arg; 206 aio_thread *thr = thr_arg;
166 int sig; 207 aio_req req;
167 int errno; 208 int errno;
168 aio_req req;
169 209
170 /* we rely on gcc's ability to create closures. */ 210 /* we rely on gcc's ability to create closures. */
171 _syscall3(int,lseek,int,fd,off_t,offset,int,whence);
172 _syscall3(int,read,int,fd,char *,buf,off_t,count); 211 _syscall3(int,read,int,fd,char *,buf,size_t,count)
173 _syscall3(int,write,int,fd,char *,buf,off_t,count); 212 _syscall3(int,write,int,fd,char *,buf,size_t,count)
174 213
175 /* first get rid of any signals */ 214 _syscall3(int,open,char *,pathname,int,flags,mode_t,mode)
176 for (sig = 1; sig < _NSIG; sig++) 215 _syscall1(int,close,int,fd)
177 signal (sig, SIG_DFL);
178 216
179 signal (SIGPIPE, SIG_IGN); 217 _syscall4(int,pread,int,fd,char *,buf,size_t,count,off_t,offset)
180 218 _syscall4(int,pwrite,int,fd,char *,buf,size_t,count,off_t,offset)
219
220 _syscall2(int,stat64, const char *, filename, struct stat64 *, buf)
221 _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf)
222 _syscall2(int,fstat64, int, fd, struct stat64 *, buf)
223
224 sigprocmask (SIG_SETMASK, &fullsigset, 0);
225
181 /* then loop */ 226 /* then loop */
182 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) 227 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req))
183 { 228 {
184 req->thread = thr; 229 req->thread = thr;
185
186 if (req->type == REQ_READ || req->type == REQ_WRITE)
187 {
188 errno = 0; 230 errno = 0;
189 231
190 if (lseek (req->fd, req->offset, SEEK_SET) == req->offset)
191 {
192 if (req->type == REQ_READ) 232 if (req->type == REQ_READ)
193 req->result = read (req->fd, req->dataptr, req->length); 233 req->result = pread (req->fd, req->dataptr, req->length, req->offset);
194 else 234 else if (req->type == REQ_WRITE)
195 req->result = write(req->fd, req->dataptr, req->length); 235 req->result = pwrite (req->fd, req->dataptr, req->length, req->offset);
196 } 236 else if (req->type == REQ_OPEN)
197 237 req->result = open (req->dataptr, req->fd, req->mode);
198 req->errorno = errno; 238 else if (req->type == REQ_CLOSE)
199 } 239 req->result = close (req->fd);
240 else if (req->type == REQ_STAT)
241 req->result = stat64 (req->dataptr, req->statdata);
242 else if (req->type == REQ_LSTAT)
243 req->result = lstat64 (req->dataptr, req->statdata);
244 else if (req->type == REQ_FSTAT)
245 req->result = fstat64 (req->fd, req->statdata);
200 else 246 else
201 { 247 {
202 write (respipe[1], (void *)&req, sizeof (req)); 248 write (respipe[1], (void *)&req, sizeof (req));
203 break; 249 break;
204 } 250 }
205 251
252 req->errorno = errno;
206 write (respipe[1], (void *)&req, sizeof (req)); 253 write (respipe[1], (void *)&req, sizeof (req));
207 } 254 }
208 255
209 return 0; 256 return 0;
210} 257}
211 258
212MODULE = Linux::AIO PACKAGE = Linux::AIO 259MODULE = Linux::AIO PACKAGE = Linux::AIO
213 260
214BOOT: 261BOOT:
215{ 262{
263 sigfillset (&fullsigset);
264 sigdelset (&fullsigset, SIGTERM);
265 sigdelset (&fullsigset, SIGQUIT);
266 sigdelset (&fullsigset, SIGABRT);
267 sigdelset (&fullsigset, SIGINT);
268
216 if (pipe (reqpipe) || pipe (respipe)) 269 if (pipe (reqpipe) || pipe (respipe))
217 croak ("unable to initialize request or result pipe"); 270 croak ("unable to initialize request or result pipe");
218 271
219 if (fcntl (respipe[0], F_SETFL, O_NONBLOCK)) 272 if (fcntl (respipe[0], F_SETFL, O_NONBLOCK))
220 croak ("cannot set result pipe to nonblocking mode"); 273 croak ("cannot set result pipe to nonblocking mode");
242 295
243 poll_cb (); 296 poll_cb ();
244 while (started > nthreads) 297 while (started > nthreads)
245 { 298 {
246 sched_yield (); 299 sched_yield ();
300 fcntl (respipe[0], F_SETFL, 0);
247 poll_cb (); 301 poll_cb ();
302 fcntl (respipe[0], F_SETFL, O_NONBLOCK);
248 } 303 }
249 304
250void 305void
306aio_open(pathname,flags,mode,callback)
307 SV * pathname
308 int flags
309 int mode
310 SV * callback
311 PROTOTYPE: $$$$
312 CODE:
313 aio_req req;
314
315 New (0, req, 1, aio_cb);
316
317 if (!req)
318 croak ("out of memory during aio_req allocation");
319
320 req->type = REQ_OPEN;
321 req->savesv = newSVsv (pathname);
322 req->dataptr = SvPV_nolen (req->savesv);
323 req->fd = flags;
324 req->mode = mode;
325 req->callback = SvREFCNT_inc (callback);
326
327 send_req (req);
328
329void
330aio_close(fh,callback)
331 InputStream fh
332 SV * callback
333 PROTOTYPE: $
334 CODE:
335 aio_req req;
336
337 New (0, req, 1, aio_cb);
338
339 if (!req)
340 croak ("out of memory during aio_req allocation");
341
342 req->type = REQ_CLOSE;
343 req->fd = PerlIO_fileno (fh);
344 req->callback = SvREFCNT_inc (callback);
345
346 send_req (req);
347
348void
251aio_read(fh,offset,length,data,dataoffset,callback) 349aio_read(fh,offset,length,data,dataoffset,callback)
252 PerlIO * fh 350 InputStream fh
253 unsigned long offset 351 UV offset
254 long length 352 IV length
255 SV * data 353 SV * data
256 STRLEN dataoffset 354 IV dataoffset
257 SV * callback 355 SV * callback
258 PROTOTYPE: $$$$$$ 356 PROTOTYPE: $$$$$$
259 ALIAS: 357 CODE:
260 aio_write = 1
261 CODE:
262 sv_upgrade (data, SVt_PV);
263 read_write (aTHX_ ix, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); 358 read_write (aTHX_ 0, PerlIO_fileno (fh), offset, length, data, dataoffset, callback);
359
360void
361aio_write(fh,offset,length,data,dataoffset,callback)
362 OutputStream fh
363 UV offset
364 IV length
365 SV * data
366 IV dataoffset
367 SV * callback
368 PROTOTYPE: $$$$$$
369 CODE:
370 read_write (aTHX_ 1, PerlIO_fileno (fh), offset, length, data, dataoffset, callback);
371
372void
373aio_stat(fh_or_path,callback)
374 SV * fh_or_path
375 SV * callback
376 PROTOTYPE: $$
377 ALIAS:
378 aio_lstat = 1
379 CODE:
380 aio_req req;
381
382 New (0, req, 1, aio_cb);
383
384 if (!req)
385 croak ("out of memory during aio_req allocation");
386
387 New (0, req->statdata, 1, struct stat64);
388
389 if (!req->statdata)
390 croak ("out of memory during aio_req->statdata allocation");
391
392 if (SvPOK (fh_or_path))
393 {
394 req->type = ix ? REQ_LSTAT : REQ_STAT;
395 req->savesv = newSVsv (fh_or_path);
396 req->dataptr = SvPV_nolen (req->savesv);
397 }
398 else
399 {
400 req->type = REQ_FSTAT;
401 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
402 }
403
404 req->callback = SvREFCNT_inc (callback);
405
406 send_req (req);
264 407
265int 408int
266poll_fileno() 409poll_fileno()
267 PROTOTYPE: 410 PROTOTYPE:
268 CODE: 411 CODE:
269 RETVAL = respipe[0]; 412 RETVAL = respipe[0];
270 OUTPUT: 413 OUTPUT:
271 RETVAL 414 RETVAL
272 415
273int 416int
274poll_cb() 417poll_cb(...)
275 PROTOTYPE: 418 PROTOTYPE:
276 CODE: 419 CODE:
277 RETVAL = poll_cb (aTHX); 420 RETVAL = poll_cb (aTHX);
278 OUTPUT: 421 OUTPUT:
279 RETVAL 422 RETVAL

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines