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.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 2048 /* 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;
106 req->length = length; 128 req->length = length;
107 req->data = SvREFCNT_inc (data); 129 req->data = SvREFCNT_inc (data);
108 req->dataptr = (char *)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{
133 154
134 if (req->type == REQ_READ) 155 if (req->type == REQ_READ)
135 SvCUR_set (req->data, req->dataoffset 156 SvCUR_set (req->data, req->dataoffset
136 + req->result > 0 ? req->result : 0); 157 + req->result > 0 ? req->result : 0);
137 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 }
182
138 PUSHMARK (SP); 183 PUSHMARK (SP);
139 XPUSHs (sv_2mortal (newSViv (req->result))); 184 XPUSHs (sv_2mortal (newSViv (req->result)));
140 PUTBACK; 185 PUTBACK;
141 call_sv (req->callback, G_VOID); 186 call_sv (req->callback, G_VOID);
142 SPAGAIN; 187 SPAGAIN;
143 188
144 SvREFCNT_dec (req->data); 189 if (req->callback)
145 SvREFCNT_dec (req->callback); 190 SvREFCNT_dec (req->callback);
146 191
147 errno = errorno; 192 errno = errorno;
148 nreqs--; 193 nreqs--;
149 count++; 194 count++;
150 } 195 }
153 } 198 }
154 199
155 return count; 200 return count;
156} 201}
157 202
203static sigset_t fullsigset;
204
158#undef errno 205#undef errno
159#include <asm/unistd.h> 206#include <asm/unistd.h>
160 207
161static int 208static int
162aio_proc(void *thr_arg) 209aio_proc(void *thr_arg)
163{ 210{
164 aio_thread *thr = thr_arg; 211 aio_thread *thr = thr_arg;
165 int sig; 212 aio_req req;
166 int errno; 213 int errno;
167 aio_req req;
168 214
215 /* this is very much x86 and kernel-specific :(:(:( */
169 /* we rely on gcc's ability to create closures. */ 216 /* we rely on gcc's ability to create closures. */
170 _syscall3(int,lseek,int,fd,off_t,offset,int,whence);
171 _syscall3(int,read,int,fd,char *,buf,off_t,count); 217 _syscall3(int,read,int,fd,char *,buf,size_t,count)
172 _syscall3(int,write,int,fd,char *,buf,off_t,count); 218 _syscall3(int,write,int,fd,char *,buf,size_t,count)
173 219
174 /* first get rid of any signals */ 220 _syscall3(int,open,char *,pathname,int,flags,mode_t,mode)
175 for (sig = 1; sig < _NSIG; sig++) 221 _syscall1(int,close,int,fd)
176 signal (sig, SIG_DFL);
177 222
178 signal (SIGPIPE, SIG_IGN); 223 _syscall5(int,pread64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi)
179 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
180 /* then loop */ 232 /* then loop */
181 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) 233 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req))
182 { 234 {
183 req->thread = thr; 235 req->thread = thr;
236 errno = 0; /* strictly unnecessary */
184 237
185 if (req->type == REQ_READ || req->type == REQ_WRITE)
186 {
187 errno = 0;
188
189 if (lseek (req->fd, req->offset, SEEK_SET) == req->offset)
190 {
191 if (req->type == REQ_READ) 238 if (req->type == REQ_READ)
192 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);
193 else 240 else if (req->type == REQ_WRITE)
194 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);
195 } 242 else if (req->type == REQ_OPEN)
196 243 req->result = open (req->dataptr, req->fd, req->mode);
197 req->errorno = errno; 244 else if (req->type == REQ_CLOSE)
198 } 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);
199 else 252 else
200 { 253 {
201 write (respipe[1], (void *)&req, sizeof (req)); 254 write (respipe[1], (void *)&req, sizeof (req));
202 break; 255 break;
203 } 256 }
204 257
258 req->errorno = errno;
205 write (respipe[1], (void *)&req, sizeof (req)); 259 write (respipe[1], (void *)&req, sizeof (req));
206 } 260 }
207 261
208 return 0; 262 return 0;
209} 263}
210 264
211MODULE = Linux::AIO PACKAGE = Linux::AIO 265MODULE = Linux::AIO PACKAGE = Linux::AIO
212 266
213BOOT: 267BOOT:
214{ 268{
269 sigfillset (&fullsigset);
270 sigdelset (&fullsigset, SIGTERM);
271 sigdelset (&fullsigset, SIGQUIT);
272 sigdelset (&fullsigset, SIGABRT);
273 sigdelset (&fullsigset, SIGINT);
274
215 if (pipe (reqpipe) || pipe (respipe)) 275 if (pipe (reqpipe) || pipe (respipe))
216 croak ("unable to initialize request or result pipe"); 276 croak ("unable to initialize request or result pipe");
217 277
218 if (fcntl (respipe[0], F_SETFL, O_NONBLOCK)) 278 if (fcntl (respipe[0], F_SETFL, O_NONBLOCK))
219 croak ("cannot set result pipe to nonblocking mode"); 279 croak ("cannot set result pipe to nonblocking mode");
237 { 297 {
238 end_thread (); 298 end_thread ();
239 cur--; 299 cur--;
240 } 300 }
241 301
242 poll_cb ();
243 while (started > nthreads) 302 while (started > nthreads)
244 { 303 {
245 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);
246 poll_cb (); 309 poll_cb ();
247 } 310 }
248 311
249void 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
250aio_read(fh,offset,length,data,dataoffset,callback) 356aio_read(fh,offset,length,data,dataoffset,callback)
251 PerlIO * fh 357 InputStream fh
252 UV offset 358 UV offset
253 STRLEN length 359 IV length
254 SV * data 360 SV * data
255 STRLEN dataoffset 361 IV dataoffset
256 SV * callback 362 SV * callback
257 PROTOTYPE: $$$$$$ 363 PROTOTYPE: $$$$$$
258 ALIAS: 364 CODE:
259 aio_write = 1
260 CODE:
261 SvUPGRADE (data, SVt_PV);
262 SvPOK_on (data);
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:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines