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.9 by root, Thu Aug 16 18:58:34 2001 UTC vs.
Revision 1.17 by root, Thu May 6 12:16:48 2004 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines