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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines