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.15 by root, Sat May 18 21:48:36 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 42
28 int fd; 43 int fd;
29 off_t offset; 44 off_t offset;
33 int errorno; 48 int errorno;
34 SV *data, *callback; 49 SV *data, *callback;
35 void *dataptr; 50 void *dataptr;
36 STRLEN dataoffset; 51 STRLEN dataoffset;
37 52
38 struct stat64 *statdata; 53 Stat_t *statdata;
39} aio_cb; 54} aio_cb;
40 55
41typedef aio_cb *aio_req; 56typedef aio_cb *aio_req;
42 57
43static int started; 58static int started;
44static int nreqs; 59static int nreqs;
45static int reqpipe[2], respipe[2]; 60static int reqpipe[2], respipe[2];
46 61
62static aio_req qs, qe; /* queue start, queue end */
63
47static int aio_proc(void *arg); 64static int aio_proc(void *arg);
48 65
49static void 66static void
50start_thread(void) 67start_thread (void)
51{ 68{
52 aio_thread *thr; 69 aio_thread *thr;
53 70
54 New (0, thr, 1, aio_thread); 71 New (0, thr, 1, aio_thread);
55 72
56 if (clone (aio_proc, 73 if (clone (aio_proc,
57 &(thr->stack[STACKSIZE]), 74 &(thr->stack[STACKSIZE - 16]),
58 CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND, 75 CLONE_VM|CLONE_FS|CLONE_FILES,
59 thr) >= 0) 76 thr) >= 0)
60 started++; 77 started++;
61 else 78 else
62 Safefree (thr); 79 Safefree (thr);
63} 80}
64 81
65static 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
66end_thread(void) 111end_thread (void)
67{ 112{
68 aio_req req; 113 aio_req req;
69 New (0, req, 1, aio_cb); 114 New (0, req, 1, aio_cb);
70 req->type = REQ_QUIT; 115 req->type = REQ_QUIT;
71 write (reqpipe[1], &req, sizeof (aio_req)); 116
117 send_req (req);
72} 118}
73 119
74static void 120static void
75send_req (aio_req req) 121read_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, 122 int dowrite, int fd, off_t offset, size_t length,
83 SV *data, STRLEN dataoffset, SV*callback) 123 SV *data, STRLEN dataoffset, SV *callback)
84{ 124{
85 aio_req req; 125 aio_req req;
86 STRLEN svlen; 126 STRLEN svlen;
87 char *svptr = SvPV (data, svlen); 127 char *svptr = SvPV (data, svlen);
88 128
124 req->callback = SvREFCNT_inc (callback); 164 req->callback = SvREFCNT_inc (callback);
125 165
126 send_req (req); 166 send_req (req);
127} 167}
128 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
129static int 179static int
130poll_cb (pTHX) 180poll_cb (pTHX)
131{ 181{
132 dSP; 182 dSP;
133 int count = 0; 183 int count = 0;
134 aio_req req; 184 aio_req req;
135 185
136 while (read (respipe[0], (void *)&req, sizeof (req)) == sizeof (req)) 186 while (read (respipe[0], (void *)&req, sizeof (req)) == sizeof (req))
137 { 187 {
188 nreqs--;
189
138 if (req->type == REQ_QUIT) 190 if (req->type == REQ_QUIT)
139 { 191 {
140 Safefree (req->thread); 192 Safefree (req->thread);
141 started--; 193 started--;
142 } 194 }
152 if (req->data) 204 if (req->data)
153 SvREFCNT_dec (req->data); 205 SvREFCNT_dec (req->data);
154 206
155 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)
156 { 208 {
157 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; 209 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
158 PL_laststatval = req->result; 210 PL_laststatval = req->result;
159 PL_statcache.st_dev = req->statdata->st_dev; 211 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 212
173 Safefree (req->statdata); 213 Safefree (req->statdata);
174 } 214 }
175 215
176 PUSHMARK (SP); 216 PUSHMARK (SP);
181 221
182 if (req->callback) 222 if (req->callback)
183 SvREFCNT_dec (req->callback); 223 SvREFCNT_dec (req->callback);
184 224
185 errno = errorno; 225 errno = errorno;
186 nreqs--;
187 count++; 226 count++;
188 } 227 }
189 228
190 Safefree (req); 229 Safefree (req);
191 } 230 }
192 231
232 if (qs)
233 send_reqs ();
234
193 return count; 235 return count;
194} 236}
195 237
196static sigset_t fullsigset; 238static sigset_t fullsigset;
197 239
198#undef errno 240#undef errno
199#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; \
200 276
201static int 277static int
202aio_proc(void *thr_arg) 278aio_proc (void *thr_arg)
203{ 279{
204 aio_thread *thr = thr_arg; 280 aio_thread *thr = thr_arg;
205 aio_req req; 281 aio_req req;
206 int errno; 282 int errno;
207 283
208 /* this is very much x86 and kernel-specific :(:(:( */ 284 /* this is very much kernel-specific :(:(:( */
209 /* we rely on gcc's ability to create closures. */ 285 /* we rely on gcc's ability to create closures. */
210 _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)
211 _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)
212 288
213 _syscall3(int,open,char *,pathname,int,flags,mode_t,mode) 289 _syscall3(int,open,char *,pathname,int,flags,int,mode)
214 _syscall1(int,close,int,fd) 290 _syscall1(int,close,unsigned int,fd)
215 291
292#ifndef __NR_pread64
293# define __NR_pread64 __NR_pread
294# define __NR_pwrite64 __NR_write
295#endif
216 _syscall5(int,pread,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi) 296 _syscall5(__kernel_ssize_t,pread64,unsigned int,fd,char *,buf,__kernel_size_t,count,unsigned int,offset_lh,unsigned int,offset_hl)
217 _syscall5(int,pwrite,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi) 297 _syscall5(__kernel_ssize_t,pwrite64,unsigned int,fd,char *,buf,__kernel_size_t,count,unsigned int,offset_lh,unsigned int,offset_hl)
218 298
299#if __NR_stat64
219 _syscall2(int,stat64, const char *, filename, struct stat64 *, buf) 300 _syscall2(int,stat64, const char *, filename, struct kernelstat64 *, buf)
220 _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf) 301 _syscall2(int,lstat64, const char *, filename, struct kernelstat64 *, buf)
221 _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);
222 312
223 sigprocmask (SIG_SETMASK, &fullsigset, 0); 313 sigprocmask (SIG_SETMASK, &fullsigset, 0);
314 prctl (PR_SET_PDEATHSIG, SIGKILL);
224 315
225 /* then loop */ 316 /* then loop */
226 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) 317 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req))
227 { 318 {
228 req->thread = thr; 319 req->thread = thr;
229 errno = 0; /* strictly unnecessary */ 320 errno = 0; /* strictly unnecessary */
230 321
231 if (req->type == REQ_READ) 322 switch (req->type)
232 req->result = pread (req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32);
233 else if (req->type == REQ_WRITE)
234 req->result = pwrite(req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32);
235 else if (req->type == REQ_OPEN)
236 req->result = open (req->dataptr, req->fd, req->mode);
237 else if (req->type == REQ_CLOSE)
238 req->result = close (req->fd);
239 else if (req->type == REQ_STAT)
240 req->result = stat64 (req->dataptr, req->statdata);
241 else if (req->type == REQ_LSTAT)
242 req->result = lstat64 (req->dataptr, req->statdata);
243 else if (req->type == REQ_FSTAT)
244 req->result = fstat64 (req->fd, req->statdata);
245 else
246 { 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:
247 write (respipe[1], (void *)&req, sizeof (req)); 345 write (respipe[1], (void *)&req, sizeof (req));
248 break; 346 return 0;
249 } 347 }
250 348
251 req->errorno = errno; 349 req->errorno = errno;
252 write (respipe[1], (void *)&req, sizeof (req)); 350 write (respipe[1], (void *)&req, sizeof (req));
253 } 351 }
265 sigdelset (&fullsigset, SIGABRT); 363 sigdelset (&fullsigset, SIGABRT);
266 sigdelset (&fullsigset, SIGINT); 364 sigdelset (&fullsigset, SIGINT);
267 365
268 if (pipe (reqpipe) || pipe (respipe)) 366 if (pipe (reqpipe) || pipe (respipe))
269 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");
270 371
271 if (fcntl (respipe[0], F_SETFL, O_NONBLOCK)) 372 if (fcntl (respipe[0], F_SETFL, O_NONBLOCK))
272 croak ("cannot set result pipe to nonblocking mode"); 373 croak ("cannot set result pipe to nonblocking mode");
273} 374}
274 375
292 cur--; 393 cur--;
293 } 394 }
294 395
295 while (started > nthreads) 396 while (started > nthreads)
296 { 397 {
297 fd_set rfd; 398 poll_wait ();
298 FD_ZERO(&rfd);
299 FD_SET(respipe[0], &rfd);
300
301 select (respipe[0] + 1, &rfd, 0, 0, 0);
302 poll_cb (); 399 poll_cb (aTHX);
303 } 400 }
304 401
305void 402void
306aio_open(pathname,flags,mode,callback) 403aio_open(pathname,flags,mode,callback)
307 SV * pathname 404 SV * pathname
382 Newz (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))
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