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.18 by root, Thu May 6 15:05:57 2004 UTC vs.
Revision 1.24 by root, Sat Jul 2 13:16:33 2005 UTC

9#include <unistd.h> 9#include <unistd.h>
10#include <fcntl.h> 10#include <fcntl.h>
11#include <signal.h> 11#include <signal.h>
12#include <sched.h> 12#include <sched.h>
13 13
14typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ 14typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */
15typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ 15typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */
16typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ 16typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */
17 17
18#ifndef __NR_pread64 18#define STACKSIZE (128 * sizeof (long)) /* yeah */
19# define __NR_pread64 __NR_pread
20#endif
21#ifndef __NR_pwrite64
22# define __NR_pwrite64 __NR_pwrite
23#endif
24 19
25#define STACKSIZE 1024 /* yeah */ 20enum {
26 21 REQ_QUIT,
27enum { REQ_QUIT, REQ_OPEN, REQ_CLOSE, REQ_READ, REQ_WRITE, REQ_STAT, REQ_LSTAT, REQ_FSTAT}; 22 REQ_OPEN, REQ_CLOSE, REQ_READ, REQ_WRITE,
23 REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_UNLINK
24};
28 25
29typedef struct { 26typedef struct {
30 char stack[STACKSIZE]; 27 char stack[STACKSIZE];
31} aio_thread; 28} aio_thread;
32 29
44 int errorno; 41 int errorno;
45 SV *data, *callback; 42 SV *data, *callback;
46 void *dataptr; 43 void *dataptr;
47 STRLEN dataoffset; 44 STRLEN dataoffset;
48 45
49 struct stat64 *statdata; 46 Stat_t *statdata;
50} aio_cb; 47} aio_cb;
51 48
52typedef aio_cb *aio_req; 49typedef aio_cb *aio_req;
53 50
54static int started; 51static int started;
65 aio_thread *thr; 62 aio_thread *thr;
66 63
67 New (0, thr, 1, aio_thread); 64 New (0, thr, 1, aio_thread);
68 65
69 if (clone (aio_proc, 66 if (clone (aio_proc,
70 &(thr->stack[STACKSIZE]), 67 &(thr->stack[STACKSIZE - sizeof (long)]),
71 CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND, 68 CLONE_VM|CLONE_FS|CLONE_FILES,
72 thr) >= 0) 69 thr) >= 0)
73 started++; 70 started++;
74 else 71 else
75 Safefree (thr); 72 Safefree (thr);
76} 73}
91{ 88{
92 nreqs++; 89 nreqs++;
93 req->next = 0; 90 req->next = 0;
94 91
95 if (qe) 92 if (qe)
93 {
96 qe->next = req; 94 qe->next = req;
95 qe = req;
96 }
97 else 97 else
98 qe = qs = req; 98 qe = qs = req;
99 99
100 send_reqs (); 100 send_reqs ();
101} 101}
157 req->callback = SvREFCNT_inc (callback); 157 req->callback = SvREFCNT_inc (callback);
158 158
159 send_req (req); 159 send_req (req);
160} 160}
161 161
162static void
163poll_wait ()
164{
165 fd_set rfd;
166 FD_ZERO(&rfd);
167 FD_SET(respipe[0], &rfd);
168
169 select (respipe[0] + 1, &rfd, 0, 0, 0);
170}
171
162static int 172static int
163poll_cb (pTHX) 173poll_cb (pTHX)
164{ 174{
165 dSP; 175 dSP;
166 int count = 0; 176 int count = 0;
167 aio_req req; 177 aio_req req;
168 178
169 while (read (respipe[0], (void *)&req, sizeof (req)) == sizeof (req)) 179 while (read (respipe[0], (void *)&req, sizeof (req)) == sizeof (req))
170 { 180 {
181 nreqs--;
182
171 if (req->type == REQ_QUIT) 183 if (req->type == REQ_QUIT)
172 { 184 {
173 Safefree (req->thread); 185 Safefree (req->thread);
174 started--; 186 started--;
175 } 187 }
185 if (req->data) 197 if (req->data)
186 SvREFCNT_dec (req->data); 198 SvREFCNT_dec (req->data);
187 199
188 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) 200 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT)
189 { 201 {
190 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; 202 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
191 PL_laststatval = req->result; 203 PL_laststatval = req->result;
192 PL_statcache.st_dev = req->statdata->st_dev; 204 PL_statcache = *(req->statdata);
193 PL_statcache.st_ino = req->statdata->st_ino;
194 PL_statcache.st_mode = req->statdata->st_mode;
195 PL_statcache.st_nlink = req->statdata->st_nlink;
196 PL_statcache.st_uid = req->statdata->st_uid;
197 PL_statcache.st_gid = req->statdata->st_gid;
198 PL_statcache.st_rdev = req->statdata->st_rdev;
199 PL_statcache.st_size = req->statdata->st_size;
200 PL_statcache.st_atime = req->statdata->st_atime;
201 PL_statcache.st_mtime = req->statdata->st_mtime;
202 PL_statcache.st_ctime = req->statdata->st_ctime;
203 PL_statcache.st_blksize = req->statdata->st_blksize;
204 PL_statcache.st_blocks = req->statdata->st_blocks;
205 205
206 Safefree (req->statdata); 206 Safefree (req->statdata);
207 } 207 }
208 208
209 PUSHMARK (SP); 209 PUSHMARK (SP);
214 214
215 if (req->callback) 215 if (req->callback)
216 SvREFCNT_dec (req->callback); 216 SvREFCNT_dec (req->callback);
217 217
218 errno = errorno; 218 errno = errorno;
219 nreqs--;
220 count++; 219 count++;
221 } 220 }
222 221
223 Safefree (req); 222 Safefree (req);
224 } 223 }
225 224
226 if (qs) 225 if (qs)
227 {
228 printf ("outstanding %p %d\n", qs, nreqs);
229 send_reqs (); 226 send_reqs ();
230 }
231 227
232 return count; 228 return count;
233} 229}
234 230
235static sigset_t fullsigset; 231static sigset_t fullsigset;
236 232
237#undef errno 233#undef errno
238#include <asm/unistd.h> 234#include <asm/unistd.h>
235#include <sys/prctl.h>
236
237#define COPY_STATDATA \
238 req->statdata->st_dev = statdata.st_dev; \
239 req->statdata->st_ino = statdata.st_ino; \
240 req->statdata->st_mode = statdata.st_mode; \
241 req->statdata->st_nlink = statdata.st_nlink; \
242 req->statdata->st_uid = statdata.st_uid; \
243 req->statdata->st_gid = statdata.st_gid; \
244 req->statdata->st_rdev = statdata.st_rdev; \
245 req->statdata->st_size = statdata.st_size; \
246 req->statdata->st_atime = statdata.st_atime; \
247 req->statdata->st_mtime = statdata.st_mtime; \
248 req->statdata->st_ctime = statdata.st_ctime; \
249 req->statdata->st_blksize = statdata.st_blksize; \
250 req->statdata->st_blocks = statdata.st_blocks; \
239 251
240static int 252static int
241aio_proc (void *thr_arg) 253aio_proc (void *thr_arg)
242{ 254{
243 aio_thread *thr = thr_arg; 255 aio_thread *thr = thr_arg;
244 aio_req req; 256 aio_req req;
245 int errno; 257 int errno;
246 258
247 /* this is very much x86 and kernel-specific :(:(:( */ 259 /* this is very much kernel-specific :(:(:( */
248 /* we rely on gcc's ability to create closures. */ 260 /* we rely on gcc's ability to create closures. */
249 _syscall3(int,read,int,fd,char *,buf,size_t,count) 261 _syscall3(int,read,int,fd,char *,buf,size_t,count)
250 _syscall3(int,write,int,fd,char *,buf,size_t,count) 262 _syscall3(int,write,int,fd,char *,buf,size_t,count)
251 263
252 _syscall3(int,open,char *,pathname,int,flags,mode_t,mode) 264 _syscall3(int,open,char *,pathname,int,flags,mode_t,mode)
253 _syscall1(int,close,int,fd) 265 _syscall1(int,close,int,fd)
254 266
267#define arch64 (__ia64 || __alpha)
268
269#ifdef __NR_pread64 && !arch64
255 _syscall5(int,pread64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi) 270 _syscall5(int,pread64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi)
256 _syscall5(int,pwrite64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi) 271 _syscall5(int,pwrite64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi)
272#elif __NR_pread
273 _syscall4(int,pread,int,fd,char *,buf,size_t,count,offset_t,offset)
274 _syscall4(int,pwrite,int,fd,char *,buf,size_t,count,offset_t,offset)
275#else
276# error "neither pread nor pread64 defined"
277#endif
257 278
279
280#ifdef __NR_stat64 && !arch64
258 _syscall2(int,stat64, const char *, filename, struct stat64 *, buf) 281 _syscall2(int,stat64, const char *, filename, struct stat64 *, buf)
259 _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf) 282 _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf)
260 _syscall2(int,fstat64, int, fd, struct stat64 *, buf) 283 _syscall2(int,fstat64, int, fd, struct stat64 *, buf)
284#elif __NR_stat
285 _syscall2(int,stat, const char *, filename, struct stat *, buf)
286 _syscall2(int,lstat, const char *, filename, struct stat *, buf)
287 _syscall2(int,fstat, int, fd, struct stat *, buf)
288#else
289# error "neither stat64 nor stat defined"
290#endif
291
292 _syscall1(int,unlink, char *, filename);
261 293
262 sigprocmask (SIG_SETMASK, &fullsigset, 0); 294 sigprocmask (SIG_SETMASK, &fullsigset, 0);
295 prctl (PR_SET_PDEATHSIG, SIGKILL);
263 296
264 /* then loop */ 297 /* then loop */
265 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) 298 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req))
266 { 299 {
267 req->thread = thr; 300 req->thread = thr;
268 errno = 0; /* strictly unnecessary */ 301 errno = 0; /* strictly unnecessary */
269 302
270 switch (req->type) 303 switch (req->type)
271 { 304 {
305#ifdef __NR_pread64
272 case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); break; 306 case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); break;
273 case REQ_WRITE: req->result = pwrite64(req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); break; 307 case REQ_WRITE: req->result = pwrite64(req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); break;
308#else
309 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break;
310 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break;
311#endif
312#ifdef __NR_stat64
313 struct stat64 statdata;
314 case REQ_STAT: req->result = stat64 (req->dataptr, &statdata); COPY_STATDATA; break;
315 case REQ_LSTAT: req->result = lstat64 (req->dataptr, &statdata); COPY_STATDATA; break;
316 case REQ_FSTAT: req->result = fstat64 (req->fd, &statdata); COPY_STATDATA; break;
317#else
318 struct stat statdata;
319 case REQ_STAT: req->result = stat (req->dataptr, &statdata); COPY_STATDATA; break;
320 case REQ_LSTAT: req->result = lstat (req->dataptr, &statdata); COPY_STATDATA; break;
321 case REQ_FSTAT: req->result = fstat (req->fd, &statdata); COPY_STATDATA; break;
322#endif
274 case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; 323 case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break;
275 case REQ_CLOSE: req->result = close (req->fd); break; 324 case REQ_CLOSE: req->result = close (req->fd); break;
276 case REQ_STAT: req->result = stat64 (req->dataptr, req->statdata); break;
277 case REQ_LSTAT: req->result = lstat64 (req->dataptr, req->statdata); break; 325 case REQ_UNLINK: req->result = unlink (req->dataptr); break;
278 case REQ_FSTAT: req->result = fstat64 (req->fd, req->statdata); break;
279 326
280 case REQ_QUIT: 327 case REQ_QUIT:
281 default: 328 default:
282 write (respipe[1], (void *)&req, sizeof (req)); 329 write (respipe[1], (void *)&req, sizeof (req));
283 return 0; 330 return 0;
330 cur--; 377 cur--;
331 } 378 }
332 379
333 while (started > nthreads) 380 while (started > nthreads)
334 { 381 {
335 fd_set rfd; 382 poll_wait ();
336 FD_ZERO(&rfd);
337 FD_SET(respipe[0], &rfd);
338
339 select (respipe[0] + 1, &rfd, 0, 0, 0);
340 poll_cb (aTHX); 383 poll_cb (aTHX);
341 } 384 }
342 385
343void 386void
344aio_open(pathname,flags,mode,callback) 387aio_open(pathname,flags,mode,callback)
420 Newz (0, req, 1, aio_cb); 463 Newz (0, req, 1, aio_cb);
421 464
422 if (!req) 465 if (!req)
423 croak ("out of memory during aio_req allocation"); 466 croak ("out of memory during aio_req allocation");
424 467
425 New (0, req->statdata, 1, struct stat64); 468 New (0, req->statdata, 1, Stat_t);
426 469
427 if (!req->statdata) 470 if (!req->statdata)
428 croak ("out of memory during aio_req->statdata allocation"); 471 croak ("out of memory during aio_req->statdata allocation");
429 472
430 if (SvPOK (fh_or_path)) 473 if (SvPOK (fh_or_path))
441 484
442 req->callback = SvREFCNT_inc (callback); 485 req->callback = SvREFCNT_inc (callback);
443 486
444 send_req (req); 487 send_req (req);
445 488
489void
490aio_unlink(pathname,callback)
491 SV * pathname
492 SV * callback
493 PROTOTYPE: $$
494 CODE:
495 aio_req req;
496
497 Newz (0, req, 1, aio_cb);
498
499 if (!req)
500 croak ("out of memory during aio_req allocation");
501
502 req->type = REQ_UNLINK;
503 req->data = newSVsv (pathname);
504 req->dataptr = SvPV_nolen (req->data);
505 req->callback = SvREFCNT_inc (callback);
506
507 send_req (req);
508
446int 509int
447poll_fileno() 510poll_fileno()
448 PROTOTYPE: 511 PROTOTYPE:
449 CODE: 512 CODE:
450 RETVAL = respipe[0]; 513 RETVAL = respipe[0];
457 CODE: 520 CODE:
458 RETVAL = poll_cb (aTHX); 521 RETVAL = poll_cb (aTHX);
459 OUTPUT: 522 OUTPUT:
460 RETVAL 523 RETVAL
461 524
525void
526poll_wait()
527 PROTOTYPE:
528 CODE:
529 poll_wait ();
530
462int 531int
463nreqs() 532nreqs()
464 PROTOTYPE: 533 PROTOTYPE:
465 CODE: 534 CODE:
466 RETVAL = nreqs; 535 RETVAL = nreqs;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines