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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines