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.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 36
28 int fd; 37 int fd;
29 off_t offset; 38 off_t offset;
33 int errorno; 42 int errorno;
34 SV *data, *callback; 43 SV *data, *callback;
35 void *dataptr; 44 void *dataptr;
36 STRLEN dataoffset; 45 STRLEN dataoffset;
37 46
38 struct stat64 *statdata; 47 Stat_t *statdata;
39} aio_cb; 48} aio_cb;
40 49
41typedef aio_cb *aio_req; 50typedef aio_cb *aio_req;
42 51
43static int started; 52static int started;
44static int nreqs; 53static int nreqs;
45static int reqpipe[2], respipe[2]; 54static int reqpipe[2], respipe[2];
46 55
56static aio_req qs, qe; /* queue start, queue end */
57
47static int aio_proc(void *arg); 58static int aio_proc(void *arg);
48 59
49static void 60static void
50start_thread(void) 61start_thread (void)
51{ 62{
52 aio_thread *thr; 63 aio_thread *thr;
53 64
54 New (0, thr, 1, aio_thread); 65 New (0, thr, 1, aio_thread);
55 66
56 if (clone (aio_proc, 67 if (clone (aio_proc,
57 &(thr->stack[STACKSIZE]), 68 &(thr->stack[STACKSIZE - sizeof (long)]),
58 CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND, 69 CLONE_VM|CLONE_FS|CLONE_FILES,
59 thr) >= 0) 70 thr) >= 0)
60 started++; 71 started++;
61 else 72 else
62 Safefree (thr); 73 Safefree (thr);
63} 74}
64 75
65static 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
66end_thread(void) 105end_thread (void)
67{ 106{
68 aio_req req; 107 aio_req req;
69 New (0, req, 1, aio_cb); 108 New (0, req, 1, aio_cb);
70 req->type = REQ_QUIT; 109 req->type = REQ_QUIT;
71 write (reqpipe[1], &req, sizeof (aio_req)); 110
111 send_req (req);
72} 112}
73 113
74static void 114static void
75send_req (aio_req req) 115read_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, 116 int dowrite, int fd, off_t offset, size_t length,
83 SV *data, STRLEN dataoffset, SV*callback) 117 SV *data, STRLEN dataoffset, SV *callback)
84{ 118{
85 aio_req req; 119 aio_req req;
86 STRLEN svlen; 120 STRLEN svlen;
87 char *svptr = SvPV (data, svlen); 121 char *svptr = SvPV (data, svlen);
88 122
124 req->callback = SvREFCNT_inc (callback); 158 req->callback = SvREFCNT_inc (callback);
125 159
126 send_req (req); 160 send_req (req);
127} 161}
128 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
129static int 173static int
130poll_cb (pTHX) 174poll_cb (pTHX)
131{ 175{
132 dSP; 176 dSP;
133 int count = 0; 177 int count = 0;
134 aio_req req; 178 aio_req req;
135 179
136 while (read (respipe[0], (void *)&req, sizeof (req)) == sizeof (req)) 180 while (read (respipe[0], (void *)&req, sizeof (req)) == sizeof (req))
137 { 181 {
182 nreqs--;
183
138 if (req->type == REQ_QUIT) 184 if (req->type == REQ_QUIT)
139 { 185 {
140 Safefree (req->thread); 186 Safefree (req->thread);
141 started--; 187 started--;
142 } 188 }
152 if (req->data) 198 if (req->data)
153 SvREFCNT_dec (req->data); 199 SvREFCNT_dec (req->data);
154 200
155 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)
156 { 202 {
157 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; 203 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
158 PL_laststatval = req->result; 204 PL_laststatval = req->result;
159 PL_statcache.st_dev = req->statdata->st_dev; 205 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 206
173 Safefree (req->statdata); 207 Safefree (req->statdata);
174 } 208 }
175 209
176 PUSHMARK (SP); 210 PUSHMARK (SP);
181 215
182 if (req->callback) 216 if (req->callback)
183 SvREFCNT_dec (req->callback); 217 SvREFCNT_dec (req->callback);
184 218
185 errno = errorno; 219 errno = errorno;
186 nreqs--;
187 count++; 220 count++;
188 } 221 }
189 222
190 Safefree (req); 223 Safefree (req);
191 } 224 }
192 225
226 if (qs)
227 send_reqs ();
228
193 return count; 229 return count;
194} 230}
195 231
196static sigset_t fullsigset; 232static sigset_t fullsigset;
197 233
198#undef errno 234#undef errno
199#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; \
200 263
201static int 264static int
202aio_proc(void *thr_arg) 265aio_proc (void *thr_arg)
203{ 266{
204 aio_thread *thr = thr_arg; 267 aio_thread *thr = thr_arg;
205 aio_req req; 268 aio_req req;
206 int errno; 269 int errno;
207 270
271 /* this is very much kernel-specific :(:(:( */
208 /* we rely on gcc's ability to create closures. */ 272 /* we rely on gcc's ability to create closures. */
209 _syscall3(int,read,int,fd,char *,buf,size_t,count) 273 _syscall3(int,read,int,fd,char *,buf,size_t,count)
210 _syscall3(int,write,int,fd,char *,buf,size_t,count) 274 _syscall3(int,write,int,fd,char *,buf,size_t,count)
211 275
212 _syscall3(int,open,char *,pathname,int,flags,mode_t,mode) 276 _syscall3(int,open,char *,pathname,int,flags,mode_t,mode)
213 _syscall1(int,close,int,fd) 277 _syscall1(int,close,int,fd)
214 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
215 _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)
216 _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
217 288
289
290#if __NR_stat64
218 _syscall2(int,stat64, const char *, filename, struct stat64 *, buf) 291 _syscall2(int,stat64, const char *, filename, struct kernelstat64 *, buf)
219 _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf) 292 _syscall2(int,lstat64, const char *, filename, struct kernelstat64 *, buf)
220 _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);
221 303
222 sigprocmask (SIG_SETMASK, &fullsigset, 0); 304 sigprocmask (SIG_SETMASK, &fullsigset, 0);
305 prctl (PR_SET_PDEATHSIG, SIGKILL);
223 306
224 /* then loop */ 307 /* then loop */
225 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) 308 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req))
226 { 309 {
227 req->thread = thr; 310 req->thread = thr;
228 errno = 0; /* strictly unnecessary */ 311 errno = 0; /* strictly unnecessary */
229 312
230 if (req->type == REQ_READ) 313 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 { 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:
246 write (respipe[1], (void *)&req, sizeof (req)); 339 write (respipe[1], (void *)&req, sizeof (req));
247 break; 340 return 0;
248 } 341 }
249 342
250 req->errorno = errno; 343 req->errorno = errno;
251 write (respipe[1], (void *)&req, sizeof (req)); 344 write (respipe[1], (void *)&req, sizeof (req));
252 } 345 }
264 sigdelset (&fullsigset, SIGABRT); 357 sigdelset (&fullsigset, SIGABRT);
265 sigdelset (&fullsigset, SIGINT); 358 sigdelset (&fullsigset, SIGINT);
266 359
267 if (pipe (reqpipe) || pipe (respipe)) 360 if (pipe (reqpipe) || pipe (respipe))
268 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");
269 365
270 if (fcntl (respipe[0], F_SETFL, O_NONBLOCK)) 366 if (fcntl (respipe[0], F_SETFL, O_NONBLOCK))
271 croak ("cannot set result pipe to nonblocking mode"); 367 croak ("cannot set result pipe to nonblocking mode");
272} 368}
273 369
291 cur--; 387 cur--;
292 } 388 }
293 389
294 while (started > nthreads) 390 while (started > nthreads)
295 { 391 {
296 fd_set rfd; 392 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 (); 393 poll_cb (aTHX);
302 } 394 }
303 395
304void 396void
305aio_open(pathname,flags,mode,callback) 397aio_open(pathname,flags,mode,callback)
306 SV * pathname 398 SV * pathname
381 Newz (0, req, 1, aio_cb); 473 Newz (0, req, 1, aio_cb);
382 474
383 if (!req) 475 if (!req)
384 croak ("out of memory during aio_req allocation"); 476 croak ("out of memory during aio_req allocation");
385 477
386 New (0, req->statdata, 1, struct stat64); 478 New (0, req->statdata, 1, Stat_t);
387 479
388 if (!req->statdata) 480 if (!req->statdata)
389 croak ("out of memory during aio_req->statdata allocation"); 481 croak ("out of memory during aio_req->statdata allocation");
390 482
391 if (SvPOK (fh_or_path)) 483 if (SvPOK (fh_or_path))
402 494
403 req->callback = SvREFCNT_inc (callback); 495 req->callback = SvREFCNT_inc (callback);
404 496
405 send_req (req); 497 send_req (req);
406 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
407int 519int
408poll_fileno() 520poll_fileno()
409 PROTOTYPE: 521 PROTOTYPE:
410 CODE: 522 CODE:
411 RETVAL = respipe[0]; 523 RETVAL = respipe[0];
418 CODE: 530 CODE:
419 RETVAL = poll_cb (aTHX); 531 RETVAL = poll_cb (aTHX);
420 OUTPUT: 532 OUTPUT:
421 RETVAL 533 RETVAL
422 534
535void
536poll_wait()
537 PROTOTYPE:
538 CODE:
539 poll_wait ();
540
423int 541int
424nreqs() 542nreqs()
425 PROTOTYPE: 543 PROTOTYPE:
426 CODE: 544 CODE:
427 RETVAL = nreqs; 545 RETVAL = nreqs;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines