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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines