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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines