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.14 by root, Tue Apr 2 14:01:09 2002 UTC vs.
Revision 1.26 by root, Thu Jul 7 23:17:23 2005 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>
6#include <sys/stat.h> 8#include <sys/stat.h>
7#include <unistd.h> 9#include <unistd.h>
8#include <fcntl.h> 10#include <fcntl.h>
9#include <signal.h> 11#include <signal.h>
10#include <sched.h> 12#include <sched.h>
13#include <endian.h>
11 14
12typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ 15typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */
13typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ 16typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */
14typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ 17typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */
15 18
16#define STACKSIZE 1024 /* yeah */ 19// 128 seems to be enough most everywhere. alpha needs 256.
20#define STACKSIZE (256 * sizeof (long))
17 21
18enum { REQ_QUIT, REQ_OPEN, REQ_CLOSE, REQ_READ, REQ_WRITE, REQ_STAT, REQ_LSTAT, REQ_FSTAT}; 22enum {
23 REQ_QUIT,
24 REQ_OPEN, REQ_CLOSE, REQ_READ, REQ_WRITE,
25 REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_UNLINK
26};
19 27
20typedef struct { 28typedef struct {
21 char stack[STACKSIZE]; 29 char stack[STACKSIZE];
22} aio_thread; 30} aio_thread;
23 31
24typedef struct { 32typedef struct aio_cb {
33 struct aio_cb *next;
34
25 int type; 35 int type;
26 aio_thread *thread; 36 aio_thread *thread;
27 37
28 int fd; 38 int fd;
29 off_t offset; 39 off_t offset;
33 int errorno; 43 int errorno;
34 SV *data, *callback; 44 SV *data, *callback;
35 void *dataptr; 45 void *dataptr;
36 STRLEN dataoffset; 46 STRLEN dataoffset;
37 47
38 struct stat64 *statdata; 48 Stat_t *statdata;
39} aio_cb; 49} aio_cb;
40 50
41typedef aio_cb *aio_req; 51typedef aio_cb *aio_req;
42 52
43static int started; 53static int started;
44static int nreqs; 54static int nreqs;
45static int reqpipe[2], respipe[2]; 55static int reqpipe[2], respipe[2];
46 56
57static aio_req qs, qe; /* queue start, queue end */
58
47static int aio_proc(void *arg); 59static int aio_proc(void *arg);
48 60
49static void 61static void
50start_thread(void) 62start_thread (void)
51{ 63{
52 aio_thread *thr; 64 aio_thread *thr;
53 65
54 New (0, thr, 1, aio_thread); 66 New (0, thr, 1, aio_thread);
55 67
56 if (clone (aio_proc, 68 if (clone (aio_proc,
57 &(thr->stack[STACKSIZE]), 69 &(thr->stack[STACKSIZE - sizeof (long)]),
58 CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND, 70 CLONE_VM|CLONE_FS|CLONE_FILES,
59 thr) >= 0) 71 thr) >= 0)
60 started++; 72 started++;
61 else 73 else
62 Safefree (thr); 74 Safefree (thr);
63} 75}
64 76
65static void 77static void
78send_reqs (void)
79{
80 /* this write is atomic */
81 while (qs && write (reqpipe[1], &qs, sizeof qs) == sizeof qs)
82 {
83 qs = qs->next;
84 if (!qs) qe = 0;
85 }
86}
87
88static void
89send_req (aio_req req)
90{
91 nreqs++;
92 req->next = 0;
93
94 if (qe)
95 {
96 qe->next = req;
97 qe = req;
98 }
99 else
100 qe = qs = req;
101
102 send_reqs ();
103}
104
105static void
66end_thread(void) 106end_thread (void)
67{ 107{
68 aio_req req; 108 aio_req req;
69 New (0, req, 1, aio_cb); 109 New (0, req, 1, aio_cb);
70 req->type = REQ_QUIT; 110 req->type = REQ_QUIT;
71 write (reqpipe[1], &req, sizeof (aio_req)); 111
112 send_req (req);
72} 113}
73 114
74static void 115static void
75send_req (aio_req req) 116read_write (pTHX_
76{
77 nreqs++;
78 write (reqpipe[1], &req, sizeof (aio_req));
79}
80
81static void
82read_write (pTHX_ int dowrite, int fd, off_t offset, size_t length, 117 int dowrite, int fd, off_t offset, size_t length,
83 SV *data, STRLEN dataoffset, SV*callback) 118 SV *data, STRLEN dataoffset, SV *callback)
84{ 119{
85 aio_req req; 120 aio_req req;
86 STRLEN svlen; 121 STRLEN svlen;
87 char *svptr = SvPV (data, svlen); 122 char *svptr = SvPV (data, svlen);
88 123
124 req->callback = SvREFCNT_inc (callback); 159 req->callback = SvREFCNT_inc (callback);
125 160
126 send_req (req); 161 send_req (req);
127} 162}
128 163
164static void
165poll_wait ()
166{
167 fd_set rfd;
168 FD_ZERO(&rfd);
169 FD_SET(respipe[0], &rfd);
170
171 select (respipe[0] + 1, &rfd, 0, 0, 0);
172}
173
129static int 174static int
130poll_cb (pTHX) 175poll_cb (pTHX)
131{ 176{
132 dSP; 177 dSP;
133 int count = 0; 178 int count = 0;
134 aio_req req; 179 aio_req req;
135 180
136 while (read (respipe[0], (void *)&req, sizeof (req)) == sizeof (req)) 181 while (read (respipe[0], (void *)&req, sizeof (req)) == sizeof (req))
137 { 182 {
183 nreqs--;
184
138 if (req->type == REQ_QUIT) 185 if (req->type == REQ_QUIT)
139 { 186 {
140 Safefree (req->thread); 187 Safefree (req->thread);
141 started--; 188 started--;
142 } 189 }
152 if (req->data) 199 if (req->data)
153 SvREFCNT_dec (req->data); 200 SvREFCNT_dec (req->data);
154 201
155 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) 202 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT)
156 { 203 {
157 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; 204 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
158 PL_laststatval = req->result; 205 PL_laststatval = req->result;
159 PL_statcache.st_dev = req->statdata->st_dev; 206 PL_statcache = *(req->statdata);
160 PL_statcache.st_ino = req->statdata->st_ino;
161 PL_statcache.st_mode = req->statdata->st_mode;
162 PL_statcache.st_nlink = req->statdata->st_nlink;
163 PL_statcache.st_uid = req->statdata->st_uid;
164 PL_statcache.st_gid = req->statdata->st_gid;
165 PL_statcache.st_rdev = req->statdata->st_rdev;
166 PL_statcache.st_size = req->statdata->st_size;
167 PL_statcache.st_atime = req->statdata->st_atime;
168 PL_statcache.st_mtime = req->statdata->st_mtime;
169 PL_statcache.st_ctime = req->statdata->st_ctime;
170 PL_statcache.st_blksize = req->statdata->st_blksize;
171 PL_statcache.st_blocks = req->statdata->st_blocks;
172 207
173 Safefree (req->statdata); 208 Safefree (req->statdata);
174 } 209 }
175 210
176 PUSHMARK (SP); 211 PUSHMARK (SP);
181 216
182 if (req->callback) 217 if (req->callback)
183 SvREFCNT_dec (req->callback); 218 SvREFCNT_dec (req->callback);
184 219
185 errno = errorno; 220 errno = errorno;
186 nreqs--;
187 count++; 221 count++;
188 } 222 }
189 223
190 Safefree (req); 224 Safefree (req);
191 } 225 }
192 226
227 if (qs)
228 send_reqs ();
229
193 return count; 230 return count;
194} 231}
195 232
196static sigset_t fullsigset; 233static sigset_t fullsigset;
197 234
198#undef errno 235#undef errno
199#include <asm/unistd.h> 236#include <asm/unistd.h>
237#include <sys/prctl.h>
238
239#if BYTE_ORDER == LITTLE_ENDIAN
240# define LONG_LONG_PAIR(HI, LO) LO, HI
241#elif BYTE_ORDER == BIG_ENDIAN
242# define LONG_LONG_PAIR(HI, LO) HI, LO
243#endif
244
245#if __alpha || __ia64 || __hppa || __v850__
246# define stat kernelstat
247# define stat64 kernelstat64
248# include <asm/stat.h>
249# undef stat
250# undef stat64
251#else
252# define kernelstat stat
253# define kernelstat64 stat64
254#endif
255
256#define COPY_STATDATA \
257 req->statdata->st_dev = statdata.st_dev; \
258 req->statdata->st_ino = statdata.st_ino; \
259 req->statdata->st_mode = statdata.st_mode; \
260 req->statdata->st_nlink = statdata.st_nlink; \
261 req->statdata->st_uid = statdata.st_uid; \
262 req->statdata->st_gid = statdata.st_gid; \
263 req->statdata->st_rdev = statdata.st_rdev; \
264 req->statdata->st_size = statdata.st_size; \
265 req->statdata->st_atime = statdata.st_atime; \
266 req->statdata->st_mtime = statdata.st_mtime; \
267 req->statdata->st_ctime = statdata.st_ctime; \
268 req->statdata->st_blksize = statdata.st_blksize; \
269 req->statdata->st_blocks = statdata.st_blocks; \
200 270
201static int 271static int
202aio_proc(void *thr_arg) 272aio_proc (void *thr_arg)
203{ 273{
204 aio_thread *thr = thr_arg; 274 aio_thread *thr = thr_arg;
205 aio_req req; 275 aio_req req;
206 int errno; 276 int errno;
207 277
278 /* this is very much kernel-specific :(:(:( */
208 /* we rely on gcc's ability to create closures. */ 279 /* we rely on gcc's ability to create closures. */
209 _syscall3(int,read,int,fd,char *,buf,size_t,count) 280 _syscall3(int,read,int,fd,char *,buf,size_t,count)
210 _syscall3(int,write,int,fd,char *,buf,size_t,count) 281 _syscall3(int,write,int,fd,char *,buf,size_t,count)
211 282
212 _syscall3(int,open,char *,pathname,int,flags,mode_t,mode) 283 _syscall3(int,open,char *,pathname,int,flags,mode_t,mode)
213 _syscall1(int,close,int,fd) 284 _syscall1(int,close,int,fd)
214 285
286#if __NR_pread64
287 _syscall5(int,pread64,int,fd,char *,buf,size_t,count,unsigned int,offset_lh,unsigned int,offset_hl)
288 _syscall5(int,pwrite64,int,fd,char *,buf,size_t,count,unsigned int,offset_lh,unsigned int,offset_hl)
289#elif __NR_pread
215 _syscall4(int,pread,int,fd,char *,buf,size_t,count,off_t,offset) 290 _syscall4(int,pread,int,fd,char *,buf,size_t,count,offset_t,offset)
216 _syscall4(int,pwrite,int,fd,char *,buf,size_t,count,off_t,offset) 291 _syscall4(int,pwrite,int,fd,char *,buf,size_t,count,offset_t,offset)
292#else
293# error "neither pread nor pread64 defined"
294#endif
217 295
296
297#if __NR_stat64
218 _syscall2(int,stat64, const char *, filename, struct stat64 *, buf) 298 _syscall2(int,stat64, const char *, filename, struct kernelstat64 *, buf)
219 _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf) 299 _syscall2(int,lstat64, const char *, filename, struct kernelstat64 *, buf)
220 _syscall2(int,fstat64, int, fd, struct stat64 *, buf) 300 _syscall2(int,fstat64, int, fd, struct kernelstat64 *, buf)
301#elif __NR_stat
302 _syscall2(int,stat, const char *, filename, struct kernelstat *, buf)
303 _syscall2(int,lstat, const char *, filename, struct kernelstat *, buf)
304 _syscall2(int,fstat, int, fd, struct kernelstat *, buf)
305#else
306# error "neither stat64 nor stat defined"
307#endif
308
309 _syscall1(int,unlink, char *, filename);
221 310
222 sigprocmask (SIG_SETMASK, &fullsigset, 0); 311 sigprocmask (SIG_SETMASK, &fullsigset, 0);
312 prctl (PR_SET_PDEATHSIG, SIGKILL);
223 313
224 /* then loop */ 314 /* then loop */
225 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) 315 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req))
226 { 316 {
227 req->thread = thr; 317 req->thread = thr;
228 errno = 0; /* strictly unnecessary */ 318 errno = 0; /* strictly unnecessary */
229 319
230 if (req->type == REQ_READ) 320 switch (req->type)
231 req->result = pread (req->fd, req->dataptr, req->length, req->offset);
232 else if (req->type == REQ_WRITE)
233 req->result = pwrite (req->fd, req->dataptr, req->length, req->offset);
234 else if (req->type == REQ_OPEN)
235 req->result = open (req->dataptr, req->fd, req->mode);
236 else if (req->type == REQ_CLOSE)
237 req->result = close (req->fd);
238 else if (req->type == REQ_STAT)
239 req->result = stat64 (req->dataptr, req->statdata);
240 else if (req->type == REQ_LSTAT)
241 req->result = lstat64 (req->dataptr, req->statdata);
242 else if (req->type == REQ_FSTAT)
243 req->result = fstat64 (req->fd, req->statdata);
244 else
245 { 321 {
322#if __NR_pread64
323 case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length,
324 LONG_LONG_PAIR (req->offset >> 32, req->offset & 0xffffffff)); break;
325 case REQ_WRITE: req->result = pwrite64(req->fd, req->dataptr, req->length,
326 LONG_LONG_PAIR (req->offset >> 32, req->offset & 0xffffffff)); break;
327#else
328 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break;
329 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break;
330#endif
331#if __NR_stat64
332 struct kernelstat64 statdata;
333 case REQ_STAT: req->result = stat64 (req->dataptr, &statdata); COPY_STATDATA; break;
334 case REQ_LSTAT: req->result = lstat64 (req->dataptr, &statdata); COPY_STATDATA; break;
335 case REQ_FSTAT: req->result = fstat64 (req->fd, &statdata); COPY_STATDATA; break;
336#else
337 struct kernelstat statdata;
338 case REQ_STAT: req->result = stat (req->dataptr, &statdata); COPY_STATDATA; break;
339 case REQ_LSTAT: req->result = lstat (req->dataptr, &statdata); COPY_STATDATA; break;
340 case REQ_FSTAT: req->result = fstat (req->fd, &statdata); COPY_STATDATA; break;
341#endif
342 case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break;
343 case REQ_CLOSE: req->result = close (req->fd); break;
344 case REQ_UNLINK: req->result = unlink (req->dataptr); break;
345
346 case REQ_QUIT:
347 default:
246 write (respipe[1], (void *)&req, sizeof (req)); 348 write (respipe[1], (void *)&req, sizeof (req));
247 break; 349 return 0;
248 } 350 }
249 351
250 req->errorno = errno; 352 req->errorno = errno;
251 write (respipe[1], (void *)&req, sizeof (req)); 353 write (respipe[1], (void *)&req, sizeof (req));
252 } 354 }
264 sigdelset (&fullsigset, SIGABRT); 366 sigdelset (&fullsigset, SIGABRT);
265 sigdelset (&fullsigset, SIGINT); 367 sigdelset (&fullsigset, SIGINT);
266 368
267 if (pipe (reqpipe) || pipe (respipe)) 369 if (pipe (reqpipe) || pipe (respipe))
268 croak ("unable to initialize request or result pipe"); 370 croak ("unable to initialize request or result pipe");
371
372 if (fcntl (reqpipe[1], F_SETFL, O_NONBLOCK))
373 croak ("cannot set result pipe to nonblocking mode");
269 374
270 if (fcntl (respipe[0], F_SETFL, O_NONBLOCK)) 375 if (fcntl (respipe[0], F_SETFL, O_NONBLOCK))
271 croak ("cannot set result pipe to nonblocking mode"); 376 croak ("cannot set result pipe to nonblocking mode");
272} 377}
273 378
291 cur--; 396 cur--;
292 } 397 }
293 398
294 while (started > nthreads) 399 while (started > nthreads)
295 { 400 {
296 fd_set rfd; 401 poll_wait ();
297 FD_ZERO(&rfd);
298 FD_SET(respipe[0], &rfd);
299
300 select (respipe[0] + 1, &rfd, 0, 0, 0);
301 poll_cb (); 402 poll_cb (aTHX);
302 } 403 }
303 404
304void 405void
305aio_open(pathname,flags,mode,callback) 406aio_open(pathname,flags,mode,callback)
306 SV * pathname 407 SV * pathname
381 Newz (0, req, 1, aio_cb); 482 Newz (0, req, 1, aio_cb);
382 483
383 if (!req) 484 if (!req)
384 croak ("out of memory during aio_req allocation"); 485 croak ("out of memory during aio_req allocation");
385 486
386 New (0, req->statdata, 1, struct stat64); 487 New (0, req->statdata, 1, Stat_t);
387 488
388 if (!req->statdata) 489 if (!req->statdata)
389 croak ("out of memory during aio_req->statdata allocation"); 490 croak ("out of memory during aio_req->statdata allocation");
390 491
391 if (SvPOK (fh_or_path)) 492 if (SvPOK (fh_or_path))
402 503
403 req->callback = SvREFCNT_inc (callback); 504 req->callback = SvREFCNT_inc (callback);
404 505
405 send_req (req); 506 send_req (req);
406 507
508void
509aio_unlink(pathname,callback)
510 SV * pathname
511 SV * callback
512 PROTOTYPE: $$
513 CODE:
514 aio_req req;
515
516 Newz (0, req, 1, aio_cb);
517
518 if (!req)
519 croak ("out of memory during aio_req allocation");
520
521 req->type = REQ_UNLINK;
522 req->data = newSVsv (pathname);
523 req->dataptr = SvPV_nolen (req->data);
524 req->callback = SvREFCNT_inc (callback);
525
526 send_req (req);
527
407int 528int
408poll_fileno() 529poll_fileno()
409 PROTOTYPE: 530 PROTOTYPE:
410 CODE: 531 CODE:
411 RETVAL = respipe[0]; 532 RETVAL = respipe[0];
418 CODE: 539 CODE:
419 RETVAL = poll_cb (aTHX); 540 RETVAL = poll_cb (aTHX);
420 OUTPUT: 541 OUTPUT:
421 RETVAL 542 RETVAL
422 543
544void
545poll_wait()
546 PROTOTYPE:
547 CODE:
548 poll_wait ();
549
423int 550int
424nreqs() 551nreqs()
425 PROTOTYPE: 552 PROTOTYPE:
426 CODE: 553 CODE:
427 RETVAL = nreqs; 554 RETVAL = nreqs;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines