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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines