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.13 by root, Mon Apr 1 20:30:08 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
28 SV *savesv;
29 37
30 int fd; 38 int fd;
31 off_t offset; 39 off_t offset;
32 size_t length; 40 size_t length;
33 ssize_t result; 41 ssize_t result;
35 int errorno; 43 int errorno;
36 SV *data, *callback; 44 SV *data, *callback;
37 void *dataptr; 45 void *dataptr;
38 STRLEN dataoffset; 46 STRLEN dataoffset;
39 47
40 struct stat64 *statdata; 48 Stat_t *statdata;
41} aio_cb; 49} aio_cb;
42 50
43typedef aio_cb *aio_req; 51typedef aio_cb *aio_req;
44 52
45static int started; 53static int started;
46static int nreqs; 54static int nreqs;
47static int reqpipe[2], respipe[2]; 55static int reqpipe[2], respipe[2];
48 56
57static aio_req qs, qe; /* queue start, queue end */
58
49static int aio_proc(void *arg); 59static int aio_proc(void *arg);
50 60
51static void 61static void
52start_thread(void) 62start_thread (void)
53{ 63{
54 aio_thread *thr; 64 aio_thread *thr;
55 65
56 New (0, thr, 1, aio_thread); 66 New (0, thr, 1, aio_thread);
57 67
58 if (clone (aio_proc, 68 if (clone (aio_proc,
59 &(thr->stack[STACKSIZE]), 69 &(thr->stack[STACKSIZE - sizeof (long)]),
60 CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND, 70 CLONE_VM|CLONE_FS|CLONE_FILES,
61 thr) >= 0) 71 thr) >= 0)
62 started++; 72 started++;
63 else 73 else
64 Safefree (thr); 74 Safefree (thr);
65} 75}
66 76
67static 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
68end_thread(void) 106end_thread (void)
69{ 107{
70 aio_req req; 108 aio_req req;
71 New (0, req, 1, aio_cb); 109 New (0, req, 1, aio_cb);
72 req->type = REQ_QUIT; 110 req->type = REQ_QUIT;
73 write (reqpipe[1], &req, sizeof (aio_req)); 111
112 send_req (req);
74} 113}
75 114
76static void 115static void
77send_req (aio_req req) 116read_write (pTHX_
78{
79 nreqs++;
80 write (reqpipe[1], &req, sizeof (aio_req));
81}
82
83static void
84read_write (pTHX_ int dowrite, int fd, off_t offset, size_t length, 117 int dowrite, int fd, off_t offset, size_t length,
85 SV *data, STRLEN dataoffset, SV*callback) 118 SV *data, STRLEN dataoffset, SV *callback)
86{ 119{
87 aio_req req; 120 aio_req req;
88 STRLEN svlen; 121 STRLEN svlen;
89 char *svptr = SvPV (data, svlen); 122 char *svptr = SvPV (data, svlen);
90 123
110 } 143 }
111 144
112 if (length < 0) 145 if (length < 0)
113 croak ("length must not be negative"); 146 croak ("length must not be negative");
114 147
115 New (0, req, 1, aio_cb); 148 Newz (0, req, 1, aio_cb);
116 149
117 if (!req) 150 if (!req)
118 croak ("out of memory during aio_req allocation"); 151 croak ("out of memory during aio_req allocation");
119 152
120 req->type = dowrite ? REQ_WRITE : REQ_READ; 153 req->type = dowrite ? REQ_WRITE : REQ_READ;
126 req->callback = SvREFCNT_inc (callback); 159 req->callback = SvREFCNT_inc (callback);
127 160
128 send_req (req); 161 send_req (req);
129} 162}
130 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
131static int 174static int
132poll_cb (pTHX) 175poll_cb (pTHX)
133{ 176{
134 dSP; 177 dSP;
135 int count = 0; 178 int count = 0;
136 aio_req req; 179 aio_req req;
137 180
138 while (read (respipe[0], (void *)&req, sizeof (req)) == sizeof (req)) 181 while (read (respipe[0], (void *)&req, sizeof (req)) == sizeof (req))
139 { 182 {
183 nreqs--;
184
140 if (req->type == REQ_QUIT) 185 if (req->type == REQ_QUIT)
141 { 186 {
142 Safefree (req->thread); 187 Safefree (req->thread);
143 started--; 188 started--;
144 } 189 }
145 else 190 else
146 { 191 {
147 int errorno = errno; 192 int errorno = errno;
148 errno = req->errorno; 193 errno = req->errorno;
149 194
150 if (req->savesv)
151 SvREFCNT_dec (req->savesv);
152
153 if (req->type == REQ_READ) 195 if (req->type == REQ_READ)
154 SvCUR_set (req->data, req->dataoffset 196 SvCUR_set (req->data, req->dataoffset
155 + req->result > 0 ? req->result : 0); 197 + req->result > 0 ? req->result : 0);
156 198
199 if (req->data)
200 SvREFCNT_dec (req->data);
201
157 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)
158 { 203 {
159 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; 204 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
160 PL_laststatval = req->result; 205 PL_laststatval = req->result;
161 PL_statcache.st_dev = req->statdata->st_dev; 206 PL_statcache = *(req->statdata);
162 PL_statcache.st_ino = req->statdata->st_ino;
163 PL_statcache.st_mode = req->statdata->st_mode;
164 PL_statcache.st_nlink = req->statdata->st_nlink;
165 PL_statcache.st_uid = req->statdata->st_uid;
166 PL_statcache.st_gid = req->statdata->st_gid;
167 PL_statcache.st_rdev = req->statdata->st_rdev;
168 PL_statcache.st_size = req->statdata->st_size;
169 PL_statcache.st_atime = req->statdata->st_atime;
170 PL_statcache.st_mtime = req->statdata->st_mtime;
171 PL_statcache.st_ctime = req->statdata->st_ctime;
172 PL_statcache.st_blksize = req->statdata->st_blksize;
173 PL_statcache.st_blocks = req->statdata->st_blocks;
174 207
175 Safefree (req->statdata); 208 Safefree (req->statdata);
176 } 209 }
177 210
178 PUSHMARK (SP); 211 PUSHMARK (SP);
179 XPUSHs (sv_2mortal (newSViv (req->result))); 212 XPUSHs (sv_2mortal (newSViv (req->result)));
180 PUTBACK; 213 PUTBACK;
181 call_sv (req->callback, G_VOID); 214 call_sv (req->callback, G_VOID);
182 SPAGAIN; 215 SPAGAIN;
183 216
184 SvREFCNT_dec (req->data); 217 if (req->callback)
185 SvREFCNT_dec (req->callback); 218 SvREFCNT_dec (req->callback);
186 219
187 errno = errorno; 220 errno = errorno;
188 nreqs--;
189 count++; 221 count++;
190 } 222 }
191 223
192 Safefree (req); 224 Safefree (req);
193 } 225 }
194 226
227 if (qs)
228 send_reqs ();
229
195 return count; 230 return count;
196} 231}
197 232
198static sigset_t fullsigset; 233static sigset_t fullsigset;
199 234
200#undef errno 235#undef errno
201#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; \
202 270
203static int 271static int
204aio_proc(void *thr_arg) 272aio_proc (void *thr_arg)
205{ 273{
206 aio_thread *thr = thr_arg; 274 aio_thread *thr = thr_arg;
207 aio_req req; 275 aio_req req;
208 int errno; 276 int errno;
209 277
278 /* this is very much kernel-specific :(:(:( */
210 /* we rely on gcc's ability to create closures. */ 279 /* we rely on gcc's ability to create closures. */
211 _syscall3(int,read,int,fd,char *,buf,size_t,count) 280 _syscall3(int,read,int,fd,char *,buf,size_t,count)
212 _syscall3(int,write,int,fd,char *,buf,size_t,count) 281 _syscall3(int,write,int,fd,char *,buf,size_t,count)
213 282
214 _syscall3(int,open,char *,pathname,int,flags,mode_t,mode) 283 _syscall3(int,open,char *,pathname,int,flags,mode_t,mode)
215 _syscall1(int,close,int,fd) 284 _syscall1(int,close,int,fd)
216 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
217 _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)
218 _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
219 295
296
297#if __NR_stat64
220 _syscall2(int,stat64, const char *, filename, struct stat64 *, buf) 298 _syscall2(int,stat64, const char *, filename, struct kernelstat64 *, buf)
221 _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf) 299 _syscall2(int,lstat64, const char *, filename, struct kernelstat64 *, buf)
222 _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);
223 310
224 sigprocmask (SIG_SETMASK, &fullsigset, 0); 311 sigprocmask (SIG_SETMASK, &fullsigset, 0);
312 prctl (PR_SET_PDEATHSIG, SIGKILL);
225 313
226 /* then loop */ 314 /* then loop */
227 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) 315 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req))
228 { 316 {
229 req->thread = thr; 317 req->thread = thr;
230 errno = 0; 318 errno = 0; /* strictly unnecessary */
231 319
232 if (req->type == REQ_READ) 320 switch (req->type)
233 req->result = pread (req->fd, req->dataptr, req->length, req->offset);
234 else if (req->type == REQ_WRITE)
235 req->result = pwrite (req->fd, req->dataptr, req->length, req->offset);
236 else if (req->type == REQ_OPEN)
237 req->result = open (req->dataptr, req->fd, req->mode);
238 else if (req->type == REQ_CLOSE)
239 req->result = close (req->fd);
240 else if (req->type == REQ_STAT)
241 req->result = stat64 (req->dataptr, req->statdata);
242 else if (req->type == REQ_LSTAT)
243 req->result = lstat64 (req->dataptr, req->statdata);
244 else if (req->type == REQ_FSTAT)
245 req->result = fstat64 (req->fd, req->statdata);
246 else
247 { 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:
248 write (respipe[1], (void *)&req, sizeof (req)); 348 write (respipe[1], (void *)&req, sizeof (req));
249 break; 349 return 0;
250 } 350 }
251 351
252 req->errorno = errno; 352 req->errorno = errno;
253 write (respipe[1], (void *)&req, sizeof (req)); 353 write (respipe[1], (void *)&req, sizeof (req));
254 } 354 }
266 sigdelset (&fullsigset, SIGABRT); 366 sigdelset (&fullsigset, SIGABRT);
267 sigdelset (&fullsigset, SIGINT); 367 sigdelset (&fullsigset, SIGINT);
268 368
269 if (pipe (reqpipe) || pipe (respipe)) 369 if (pipe (reqpipe) || pipe (respipe))
270 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");
271 374
272 if (fcntl (respipe[0], F_SETFL, O_NONBLOCK)) 375 if (fcntl (respipe[0], F_SETFL, O_NONBLOCK))
273 croak ("cannot set result pipe to nonblocking mode"); 376 croak ("cannot set result pipe to nonblocking mode");
274} 377}
275 378
291 { 394 {
292 end_thread (); 395 end_thread ();
293 cur--; 396 cur--;
294 } 397 }
295 398
296 poll_cb ();
297 while (started > nthreads) 399 while (started > nthreads)
298 { 400 {
299 sched_yield (); 401 poll_wait ();
300 fcntl (respipe[0], F_SETFL, 0);
301 poll_cb (); 402 poll_cb (aTHX);
302 fcntl (respipe[0], F_SETFL, O_NONBLOCK);
303 } 403 }
304 404
305void 405void
306aio_open(pathname,flags,mode,callback) 406aio_open(pathname,flags,mode,callback)
307 SV * pathname 407 SV * pathname
310 SV * callback 410 SV * callback
311 PROTOTYPE: $$$$ 411 PROTOTYPE: $$$$
312 CODE: 412 CODE:
313 aio_req req; 413 aio_req req;
314 414
315 New (0, req, 1, aio_cb); 415 Newz (0, req, 1, aio_cb);
316 416
317 if (!req) 417 if (!req)
318 croak ("out of memory during aio_req allocation"); 418 croak ("out of memory during aio_req allocation");
319 419
320 req->type = REQ_OPEN; 420 req->type = REQ_OPEN;
321 req->savesv = newSVsv (pathname); 421 req->data = newSVsv (pathname);
322 req->dataptr = SvPV_nolen (req->savesv); 422 req->dataptr = SvPV_nolen (req->data);
323 req->fd = flags; 423 req->fd = flags;
324 req->mode = mode; 424 req->mode = mode;
325 req->callback = SvREFCNT_inc (callback); 425 req->callback = SvREFCNT_inc (callback);
326 426
327 send_req (req); 427 send_req (req);
328 428
329void 429void
330aio_close(fh,callback) 430aio_close(fh,callback)
331 InputStream fh 431 InputStream fh
332 SV * callback 432 SV * callback
333 PROTOTYPE: $ 433 PROTOTYPE: $$
334 CODE: 434 CODE:
335 aio_req req; 435 aio_req req;
336 436
337 New (0, req, 1, aio_cb); 437 Newz (0, req, 1, aio_cb);
338 438
339 if (!req) 439 if (!req)
340 croak ("out of memory during aio_req allocation"); 440 croak ("out of memory during aio_req allocation");
341 441
342 req->type = REQ_CLOSE; 442 req->type = REQ_CLOSE;
377 ALIAS: 477 ALIAS:
378 aio_lstat = 1 478 aio_lstat = 1
379 CODE: 479 CODE:
380 aio_req req; 480 aio_req req;
381 481
382 New (0, req, 1, aio_cb); 482 Newz (0, req, 1, aio_cb);
383 483
384 if (!req) 484 if (!req)
385 croak ("out of memory during aio_req allocation"); 485 croak ("out of memory during aio_req allocation");
386 486
387 New (0, req->statdata, 1, struct stat64); 487 New (0, req->statdata, 1, Stat_t);
388 488
389 if (!req->statdata) 489 if (!req->statdata)
390 croak ("out of memory during aio_req->statdata allocation"); 490 croak ("out of memory during aio_req->statdata allocation");
391 491
392 if (SvPOK (fh_or_path)) 492 if (SvPOK (fh_or_path))
393 { 493 {
394 req->type = ix ? REQ_LSTAT : REQ_STAT; 494 req->type = ix ? REQ_LSTAT : REQ_STAT;
395 req->savesv = newSVsv (fh_or_path); 495 req->data = newSVsv (fh_or_path);
396 req->dataptr = SvPV_nolen (req->savesv); 496 req->dataptr = SvPV_nolen (req->data);
397 } 497 }
398 else 498 else
399 { 499 {
400 req->type = REQ_FSTAT; 500 req->type = REQ_FSTAT;
401 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); 501 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
403 503
404 req->callback = SvREFCNT_inc (callback); 504 req->callback = SvREFCNT_inc (callback);
405 505
406 send_req (req); 506 send_req (req);
407 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
408int 528int
409poll_fileno() 529poll_fileno()
410 PROTOTYPE: 530 PROTOTYPE:
411 CODE: 531 CODE:
412 RETVAL = respipe[0]; 532 RETVAL = respipe[0];
419 CODE: 539 CODE:
420 RETVAL = poll_cb (aTHX); 540 RETVAL = poll_cb (aTHX);
421 OUTPUT: 541 OUTPUT:
422 RETVAL 542 RETVAL
423 543
544void
545poll_wait()
546 PROTOTYPE:
547 CODE:
548 poll_wait ();
549
424int 550int
425nreqs() 551nreqs()
426 PROTOTYPE: 552 PROTOTYPE:
427 CODE: 553 CODE:
428 RETVAL = nreqs; 554 RETVAL = nreqs;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines