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.20 by root, Sun Jul 18 10:55:34 2004 UTC vs.
Revision 1.25 by root, Thu Jul 7 22:24:09 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// 128 seems to be enough most everywhere. alpha needs 256.
19# define __NR_pread64 __NR_pread 19#define STACKSIZE (256 * sizeof (long))
20#endif
21#ifndef __NR_pwrite64
22# define __NR_pwrite64 __NR_pwrite
23#endif
24
25#define STACKSIZE 1024 /* yeah */
26 20
27enum { 21enum {
28 REQ_QUIT, 22 REQ_QUIT,
29 REQ_OPEN, REQ_CLOSE, REQ_READ, REQ_WRITE, 23 REQ_OPEN, REQ_CLOSE, REQ_READ, REQ_WRITE,
30 REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_UNLINK 24 REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_UNLINK
48 int errorno; 42 int errorno;
49 SV *data, *callback; 43 SV *data, *callback;
50 void *dataptr; 44 void *dataptr;
51 STRLEN dataoffset; 45 STRLEN dataoffset;
52 46
53 struct stat64 *statdata; 47 Stat_t *statdata;
54} aio_cb; 48} aio_cb;
55 49
56typedef aio_cb *aio_req; 50typedef aio_cb *aio_req;
57 51
58static int started; 52static int started;
69 aio_thread *thr; 63 aio_thread *thr;
70 64
71 New (0, thr, 1, aio_thread); 65 New (0, thr, 1, aio_thread);
72 66
73 if (clone (aio_proc, 67 if (clone (aio_proc,
74 &(thr->stack[STACKSIZE]), 68 &(thr->stack[STACKSIZE - sizeof (long)]),
75 CLONE_VM|CLONE_FS|CLONE_FILES, 69 CLONE_VM|CLONE_FS|CLONE_FILES,
76 thr) >= 0) 70 thr) >= 0)
77 started++; 71 started++;
78 else 72 else
79 Safefree (thr); 73 Safefree (thr);
95{ 89{
96 nreqs++; 90 nreqs++;
97 req->next = 0; 91 req->next = 0;
98 92
99 if (qe) 93 if (qe)
94 {
100 qe->next = req; 95 qe->next = req;
96 qe = req;
97 }
101 else 98 else
102 qe = qs = req; 99 qe = qs = req;
103 100
104 send_reqs (); 101 send_reqs ();
105} 102}
161 req->callback = SvREFCNT_inc (callback); 158 req->callback = SvREFCNT_inc (callback);
162 159
163 send_req (req); 160 send_req (req);
164} 161}
165 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
166static int 173static int
167poll_cb (pTHX) 174poll_cb (pTHX)
168{ 175{
169 dSP; 176 dSP;
170 int count = 0; 177 int count = 0;
171 aio_req req; 178 aio_req req;
172 179
173 while (read (respipe[0], (void *)&req, sizeof (req)) == sizeof (req)) 180 while (read (respipe[0], (void *)&req, sizeof (req)) == sizeof (req))
174 { 181 {
182 nreqs--;
183
175 if (req->type == REQ_QUIT) 184 if (req->type == REQ_QUIT)
176 { 185 {
177 Safefree (req->thread); 186 Safefree (req->thread);
178 started--; 187 started--;
179 } 188 }
189 if (req->data) 198 if (req->data)
190 SvREFCNT_dec (req->data); 199 SvREFCNT_dec (req->data);
191 200
192 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)
193 { 202 {
194 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; 203 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
195 PL_laststatval = req->result; 204 PL_laststatval = req->result;
196 PL_statcache.st_dev = req->statdata->st_dev; 205 PL_statcache = *(req->statdata);
197 PL_statcache.st_ino = req->statdata->st_ino;
198 PL_statcache.st_mode = req->statdata->st_mode;
199 PL_statcache.st_nlink = req->statdata->st_nlink;
200 PL_statcache.st_uid = req->statdata->st_uid;
201 PL_statcache.st_gid = req->statdata->st_gid;
202 PL_statcache.st_rdev = req->statdata->st_rdev;
203 PL_statcache.st_size = req->statdata->st_size;
204 PL_statcache.st_atime = req->statdata->st_atime;
205 PL_statcache.st_mtime = req->statdata->st_mtime;
206 PL_statcache.st_ctime = req->statdata->st_ctime;
207 PL_statcache.st_blksize = req->statdata->st_blksize;
208 PL_statcache.st_blocks = req->statdata->st_blocks;
209 206
210 Safefree (req->statdata); 207 Safefree (req->statdata);
211 } 208 }
212 209
213 PUSHMARK (SP); 210 PUSHMARK (SP);
218 215
219 if (req->callback) 216 if (req->callback)
220 SvREFCNT_dec (req->callback); 217 SvREFCNT_dec (req->callback);
221 218
222 errno = errorno; 219 errno = errorno;
223 nreqs--;
224 count++; 220 count++;
225 } 221 }
226 222
227 Safefree (req); 223 Safefree (req);
228 } 224 }
237 233
238#undef errno 234#undef errno
239#include <asm/unistd.h> 235#include <asm/unistd.h>
240#include <sys/prctl.h> 236#include <sys/prctl.h>
241 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; \
263
242static int 264static int
243aio_proc (void *thr_arg) 265aio_proc (void *thr_arg)
244{ 266{
245 aio_thread *thr = thr_arg; 267 aio_thread *thr = thr_arg;
246 aio_req req; 268 aio_req req;
247 int errno; 269 int errno;
248 270
249 /* this is very much x86 and kernel-specific :(:(:( */ 271 /* this is very much kernel-specific :(:(:( */
250 /* we rely on gcc's ability to create closures. */ 272 /* we rely on gcc's ability to create closures. */
251 _syscall3(int,read,int,fd,char *,buf,size_t,count) 273 _syscall3(int,read,int,fd,char *,buf,size_t,count)
252 _syscall3(int,write,int,fd,char *,buf,size_t,count) 274 _syscall3(int,write,int,fd,char *,buf,size_t,count)
253 275
254 _syscall3(int,open,char *,pathname,int,flags,mode_t,mode) 276 _syscall3(int,open,char *,pathname,int,flags,mode_t,mode)
255 _syscall1(int,close,int,fd) 277 _syscall1(int,close,int,fd)
256 278
279#if __NR_pread64
257 _syscall5(int,pread64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi) 280 _syscall5(int,pread64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi)
258 _syscall5(int,pwrite64,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
283 _syscall4(int,pread,int,fd,char *,buf,size_t,count,offset_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
259 288
289
290#if __NR_stat64
260 _syscall2(int,stat64, const char *, filename, struct stat64 *, buf) 291 _syscall2(int,stat64, const char *, filename, struct kernelstat64 *, buf)
261 _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf) 292 _syscall2(int,lstat64, const char *, filename, struct kernelstat64 *, buf)
262 _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
263 301
264 _syscall1(int,unlink, char *, filename); 302 _syscall1(int,unlink, char *, filename);
265 303
266 sigprocmask (SIG_SETMASK, &fullsigset, 0); 304 sigprocmask (SIG_SETMASK, &fullsigset, 0);
267 prctl (PR_SET_PDEATHSIG, SIGKILL); 305 prctl (PR_SET_PDEATHSIG, SIGKILL);
272 req->thread = thr; 310 req->thread = thr;
273 errno = 0; /* strictly unnecessary */ 311 errno = 0; /* strictly unnecessary */
274 312
275 switch (req->type) 313 switch (req->type)
276 { 314 {
315#if __NR_pread64
277 case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); break; 316 case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); break;
278 case REQ_WRITE: req->result = pwrite64(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
279 case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; 333 case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break;
280 case REQ_CLOSE: req->result = close (req->fd); break; 334 case REQ_CLOSE: req->result = close (req->fd); break;
281 case REQ_STAT: req->result = stat64 (req->dataptr, req->statdata); break;
282 case REQ_LSTAT: req->result = lstat64 (req->dataptr, req->statdata); break;
283 case REQ_FSTAT: req->result = fstat64 (req->fd, req->statdata); break;
284 case REQ_UNLINK: req->result = unlink (req->dataptr); break; 335 case REQ_UNLINK: req->result = unlink (req->dataptr); break;
285 336
286 case REQ_QUIT: 337 case REQ_QUIT:
287 default: 338 default:
288 write (respipe[1], (void *)&req, sizeof (req)); 339 write (respipe[1], (void *)&req, sizeof (req));
336 cur--; 387 cur--;
337 } 388 }
338 389
339 while (started > nthreads) 390 while (started > nthreads)
340 { 391 {
341 fd_set rfd; 392 poll_wait ();
342 FD_ZERO(&rfd);
343 FD_SET(respipe[0], &rfd);
344
345 select (respipe[0] + 1, &rfd, 0, 0, 0);
346 poll_cb (aTHX); 393 poll_cb (aTHX);
347 } 394 }
348 395
349void 396void
350aio_open(pathname,flags,mode,callback) 397aio_open(pathname,flags,mode,callback)
426 Newz (0, req, 1, aio_cb); 473 Newz (0, req, 1, aio_cb);
427 474
428 if (!req) 475 if (!req)
429 croak ("out of memory during aio_req allocation"); 476 croak ("out of memory during aio_req allocation");
430 477
431 New (0, req->statdata, 1, struct stat64); 478 New (0, req->statdata, 1, Stat_t);
432 479
433 if (!req->statdata) 480 if (!req->statdata)
434 croak ("out of memory during aio_req->statdata allocation"); 481 croak ("out of memory during aio_req->statdata allocation");
435 482
436 if (SvPOK (fh_or_path)) 483 if (SvPOK (fh_or_path))
483 CODE: 530 CODE:
484 RETVAL = poll_cb (aTHX); 531 RETVAL = poll_cb (aTHX);
485 OUTPUT: 532 OUTPUT:
486 RETVAL 533 RETVAL
487 534
535void
536poll_wait()
537 PROTOTYPE:
538 CODE:
539 poll_wait ();
540
488int 541int
489nreqs() 542nreqs()
490 PROTOTYPE: 543 PROTOTYPE:
491 CODE: 544 CODE:
492 RETVAL = nreqs; 545 RETVAL = nreqs;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines