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.6 by root, Tue Aug 14 23:25:39 2001 UTC vs.
Revision 1.30 by root, Sat Jul 9 23:38:07 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>
8#include <sys/stat.h>
6#include <unistd.h> 9#include <unistd.h>
7#include <fcntl.h> 10#include <fcntl.h>
11#include <signal.h>
8#include <sched.h> 12#include <sched.h>
13#include <endian.h>
9 14
10#define STACKSIZE 2048 /* yeah */ 15typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */
16typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */
17typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */
11 18
12#define REQ_QUIT 0 19#if __i386 || __amd64
13#define REQ_READ 1 20# define STACKSIZE ( 256 * sizeof (long))
14#define REQ_WRITE 2 21#elif __ia64
22# define STACKSIZE (8192 * sizeof (long))
23#else
24# define STACKSIZE ( 512 * sizeof (long))
25#endif
26
27enum {
28 REQ_QUIT,
29 REQ_OPEN, REQ_CLOSE,
30 REQ_READ, REQ_WRITE, REQ_READAHEAD,
31 REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_UNLINK,
32 REQ_FSYNC, REQ_FDATASYNC,
33};
15 34
16typedef struct { 35typedef struct {
17 char stack[STACKSIZE]; 36 char stack[STACKSIZE];
18} aio_thread; 37} aio_thread;
19 38
20typedef struct { 39typedef struct aio_cb {
40 struct aio_cb *next;
41
21 int type; 42 int type;
22 aio_thread *thread; 43 aio_thread *thread;
23 44
24/* read/write */
25 int fd; 45 int fd;
26 off_t offset; 46 off_t offset;
27 size_t length; 47 size_t length;
28 ssize_t result; 48 ssize_t result;
49 mode_t mode; /* open */
29 int errorno; 50 int errorno;
30
31 SV *data, *callback; 51 SV *data, *callback;
32 void *dataptr; 52 void *dataptr;
33 STRLEN dataoffset; 53 STRLEN dataoffset;
54
55 Stat_t *statdata;
34} aio_cb; 56} aio_cb;
35 57
36typedef aio_cb *aio_req; 58typedef aio_cb *aio_req;
37 59
38static int started; 60static int started;
39static int nreqs; 61static int nreqs;
40static int reqpipe[2], respipe[2]; 62static int reqpipe[2], respipe[2];
41 63
64static aio_req qs, qe; /* queue start, queue end */
65
42static int aio_proc(void *arg); 66static int aio_proc(void *arg);
43 67
44static void 68static void
45start_thread(void) 69start_thread (void)
46{ 70{
47 aio_thread *thr; 71 aio_thread *thr;
48 72
49 New (0, thr, 1, aio_thread); 73 New (0, thr, 1, aio_thread);
50 74
51 if (clone (aio_proc, 75 if (clone (aio_proc,
52 &(thr->stack[STACKSIZE]), 76 &(thr->stack[STACKSIZE - 16]),
53 CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND, 77 CLONE_VM|CLONE_FS|CLONE_FILES,
54 thr) >= 0) 78 thr) >= 0)
55 started++; 79 started++;
56 else 80 else
57 Safefree (thr); 81 Safefree (thr);
58} 82}
59 83
60static void 84static void
85send_reqs (void)
86{
87 /* this write is atomic */
88 while (qs && write (reqpipe[1], &qs, sizeof qs) == sizeof qs)
89 {
90 qs = qs->next;
91 if (!qs) qe = 0;
92 }
93}
94
95static void
96send_req (aio_req req)
97{
98 nreqs++;
99 req->next = 0;
100
101 if (qe)
102 {
103 qe->next = req;
104 qe = req;
105 }
106 else
107 qe = qs = req;
108
109 send_reqs ();
110}
111
112static void
61end_thread(void) 113end_thread (void)
62{ 114{
63 aio_req req; 115 aio_req req;
64 New (0, req, 1, aio_cb); 116 New (0, req, 1, aio_cb);
65 req->type = REQ_QUIT; 117 req->type = REQ_QUIT;
66 write (reqpipe[1], &req, sizeof (aio_req)); 118
119 send_req (req);
67} 120}
68 121
69static void 122static void
123read_write (pTHX_
70read_write (pTHX_ int dowrite, int fd, off_t offset, size_t length, 124 int dowrite, int fd, off_t offset, size_t length,
71 SV *data, STRLEN dataoffset, SV*callback) 125 SV *data, STRLEN dataoffset, SV *callback)
72{ 126{
73 aio_req req; 127 aio_req req;
74 STRLEN svlen; 128 STRLEN svlen;
75 char *svptr = SvPV (data, svlen); 129 char *svptr = SvPV (data, svlen);
130
131 SvUPGRADE (data, SVt_PV);
132 SvPOK_on (data);
76 133
77 if (dataoffset < 0) 134 if (dataoffset < 0)
78 dataoffset += svlen; 135 dataoffset += svlen;
79 136
80 if (dataoffset < 0 || dataoffset > svlen) 137 if (dataoffset < 0 || dataoffset > svlen)
93 } 150 }
94 151
95 if (length < 0) 152 if (length < 0)
96 croak ("length must not be negative"); 153 croak ("length must not be negative");
97 154
98 New (0, req, 1, aio_cb); 155 Newz (0, req, 1, aio_cb);
99 156
100 if (!req) 157 if (!req)
101 croak ("out of memory during aio_req allocation"); 158 croak ("out of memory during aio_req allocation");
102 159
103 req->type = dowrite ? REQ_WRITE : REQ_READ; 160 req->type = dowrite ? REQ_WRITE : REQ_READ;
106 req->length = length; 163 req->length = length;
107 req->data = SvREFCNT_inc (data); 164 req->data = SvREFCNT_inc (data);
108 req->dataptr = (char *)svptr + dataoffset; 165 req->dataptr = (char *)svptr + dataoffset;
109 req->callback = SvREFCNT_inc (callback); 166 req->callback = SvREFCNT_inc (callback);
110 167
111 nreqs++; 168 send_req (req);
112 write (reqpipe[1], &req, sizeof (aio_req)); 169}
170
171static void
172poll_wait ()
173{
174 fd_set rfd;
175 FD_ZERO(&rfd);
176 FD_SET(respipe[0], &rfd);
177
178 select (respipe[0] + 1, &rfd, 0, 0, 0);
113} 179}
114 180
115static int 181static int
116poll_cb (pTHX) 182poll_cb (pTHX)
117{ 183{
119 int count = 0; 185 int count = 0;
120 aio_req req; 186 aio_req req;
121 187
122 while (read (respipe[0], (void *)&req, sizeof (req)) == sizeof (req)) 188 while (read (respipe[0], (void *)&req, sizeof (req)) == sizeof (req))
123 { 189 {
190 nreqs--;
191
124 if (req->type == REQ_QUIT) 192 if (req->type == REQ_QUIT)
125 { 193 {
126 Safefree (req->thread); 194 Safefree (req->thread);
127 started--; 195 started--;
128 } 196 }
133 201
134 if (req->type == REQ_READ) 202 if (req->type == REQ_READ)
135 SvCUR_set (req->data, req->dataoffset 203 SvCUR_set (req->data, req->dataoffset
136 + req->result > 0 ? req->result : 0); 204 + req->result > 0 ? req->result : 0);
137 205
206 if (req->data)
207 SvREFCNT_dec (req->data);
208
209 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT)
210 {
211 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
212 PL_laststatval = req->result;
213 PL_statcache = *(req->statdata);
214
215 Safefree (req->statdata);
216 }
217
138 PUSHMARK (SP); 218 PUSHMARK (SP);
139 XPUSHs (sv_2mortal (newSViv (req->result))); 219 XPUSHs (sv_2mortal (newSViv (req->result)));
140 PUTBACK; 220 PUTBACK;
141 call_sv (req->callback, G_VOID); 221 call_sv (req->callback, G_VOID);
142 SPAGAIN; 222 SPAGAIN;
143 223
144 SvREFCNT_dec (req->data); 224 if (req->callback)
145 SvREFCNT_dec (req->callback); 225 SvREFCNT_dec (req->callback);
146 226
147 errno = errorno; 227 errno = errorno;
148 nreqs--;
149 count++; 228 count++;
150 } 229 }
151 230
152 Safefree (req); 231 Safefree (req);
153 } 232 }
154 233
234 if (qs)
235 send_reqs ();
236
155 return count; 237 return count;
156} 238}
239
240static sigset_t fullsigset;
157 241
158#undef errno 242#undef errno
159#include <asm/unistd.h> 243#include <asm/unistd.h>
244#include <linux/types.h>
245#include <sys/prctl.h>
246
247#if __alpha || __ia64 || __hppa || __v850__
248# define stat kernelstat
249# define stat64 kernelstat64
250# include <asm/stat.h>
251# undef stat
252# undef stat64
253#else
254# define kernelstat stat
255# define kernelstat64 stat64
256#endif
257
258#define COPY_STATDATA \
259 req->statdata->st_dev = statdata.st_dev; \
260 req->statdata->st_ino = statdata.st_ino; \
261 req->statdata->st_mode = statdata.st_mode; \
262 req->statdata->st_nlink = statdata.st_nlink; \
263 req->statdata->st_uid = statdata.st_uid; \
264 req->statdata->st_gid = statdata.st_gid; \
265 req->statdata->st_rdev = statdata.st_rdev; \
266 req->statdata->st_size = statdata.st_size; \
267 req->statdata->st_atime = statdata.st_atime; \
268 req->statdata->st_mtime = statdata.st_mtime; \
269 req->statdata->st_ctime = statdata.st_ctime; \
270 req->statdata->st_blksize = statdata.st_blksize; \
271 req->statdata->st_blocks = statdata.st_blocks; \
160 272
161static int 273static int
162aio_proc(void *thr_arg) 274aio_proc (void *thr_arg)
163{ 275{
164 aio_thread *thr = thr_arg; 276 aio_thread *thr = thr_arg;
165 int sig; 277 aio_req req;
166 int errno; 278 int errno;
167 aio_req req;
168 279
280 /* this is very much kernel-specific :(:(:( */
169 /* we rely on gcc's ability to create closures. */ 281 /* we rely on gcc's ability to create closures. */
170 _syscall3(int,lseek,int,fd,off_t,offset,int,whence); 282 _syscall3(__kernel_size_t, read , unsigned int, fd, char *, buf, __kernel_size_t, count)
171 _syscall3(int,read,int,fd,char *,buf,off_t,count); 283 _syscall3(__kernel_size_t, write, unsigned int, fd, char *, buf, __kernel_size_t, count)
172 _syscall3(int,write,int,fd,char *,buf,off_t,count);
173 284
174 /* first get rid of any signals */ 285 _syscall3(long, open, char *, pathname, int, flags, int, mode)
175 for (sig = 1; sig < _NSIG; sig++) 286 _syscall1(long, close, unsigned int, fd)
176 signal (sig, SIG_DFL); 287 _syscall1(long, unlink, char *, filename);
288 _syscall1(long, fsync, unsigned int, fd);
177 289
178 signal (SIGPIPE, SIG_IGN); 290#ifndef __NR_fdatasync
179 291# define __NR_fdatasync __NR_fsync
292#endif
293 _syscall1(long, fdatasync, unsigned int, fd);
294
295#if BYTE_ORDER == LITTLE_ENDIAN
296# define LOFF_ARG(off) (off & 0xffffffff), (off >> 32)
297#elif BYTE_ORDER == BIG_ENDIAN
298# define LOFF_ARG(off) (off >> 32), (off & 0xffffffff)
299#endif
300
301#ifndef __NR_pread64
302# define __NR_pread64 __NR_pread
303# define __NR_pwrite64 __NR_write
304#endif
305 _syscall5(__kernel_ssize_t, pread64 , unsigned int, fd, char *, buf,
306 __kernel_size_t, count, unsigned int, offset_lh, unsigned int, offset_hl)
307 _syscall5(__kernel_ssize_t, pwrite64, unsigned int, fd, char *, buf,
308 __kernel_size_t, count, unsigned int, offset_lh, unsigned int, offset_hl)
309 _syscall4(long, readahead, unsigned int, fd, unsigned int, offset_lh, unsigned int, offset_hl, __kernel_size_t, count);
310
311#if __NR_stat64
312 _syscall2(long, stat64 , const char *, filename, struct kernelstat64 *, buf)
313 _syscall2(long, lstat64, const char *, filename, struct kernelstat64 *, buf)
314 _syscall2(long, fstat64, int , fd , struct kernelstat64 *, buf)
315#elif __NR_stat
316 _syscall2(long, stat , const char *, filename, struct kernelstat *, buf)
317 _syscall2(long, lstat, const char *, filename, struct kernelstat *, buf)
318 _syscall2(long, fstat, int , fd , struct kernelstat *, buf)
319#else
320# error "neither stat64 nor stat defined"
321#endif
322
323 /* the following two calls might clobber errno */
324 sigprocmask (SIG_SETMASK, &fullsigset, 0);
325 prctl (PR_SET_PDEATHSIG, SIGKILL);
326
180 /* then loop */ 327 /* then loop */
181 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) 328 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req))
182 { 329 {
183 req->thread = thr; 330 req->thread = thr;
331 errno = 0; /* strictly unnecessary */
184 332
185 if (req->type == REQ_READ || req->type == REQ_WRITE) 333 switch (req->type)
186 { 334 {
335 case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, LOFF_ARG (req->offset)); break;
336 case REQ_WRITE: req->result = pwrite64 (req->fd, req->dataptr, req->length, LOFF_ARG (req->offset)); break;
337 case REQ_READAHEAD: req->result = readahead (req->fd, LOFF_ARG (req->offset), req->length); break;
338
339#if __NR_stat64
340 struct kernelstat64 statdata;
341 case REQ_STAT: req->result = stat64 (req->dataptr, &statdata); COPY_STATDATA; break;
342 case REQ_LSTAT: req->result = lstat64 (req->dataptr, &statdata); COPY_STATDATA; break;
343 case REQ_FSTAT: req->result = fstat64 (req->fd , &statdata); COPY_STATDATA; break;
344#else
345 struct kernelstat statdata;
346 case REQ_STAT: req->result = stat (req->dataptr, &statdata); COPY_STATDATA; break;
347 case REQ_LSTAT: req->result = lstat (req->dataptr, &statdata); COPY_STATDATA; break;
348 case REQ_FSTAT: req->result = fstat (req->fd , &statdata); COPY_STATDATA; break;
349#endif
350
351 case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break;
352 case REQ_CLOSE: req->result = close (req->fd); break;
353 case REQ_UNLINK: req->result = unlink (req->dataptr); break;
354
355 case REQ_FSYNC: req->result = fsync (req->fd); break;
356 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break;
357
358 case REQ_QUIT:
359 write (respipe[1], (void *)&req, sizeof (req));
187 errno = 0; 360 return 0;
188 361
189 if (lseek (req->fd, req->offset, SEEK_SET) == req->offset) 362 default:
363 req->result = ENOSYS;
190 { 364 break;
191 if (req->type == REQ_READ)
192 req->result = read (req->fd, req->dataptr, req->length);
193 else
194 req->result = write(req->fd, req->dataptr, req->length);
195 }
196
197 req->errorno = errno;
198 } 365 }
199 else
200 {
201 write (respipe[1], (void *)&req, sizeof (req));
202 break;
203 }
204 366
367 req->errorno = errno;
205 write (respipe[1], (void *)&req, sizeof (req)); 368 write (respipe[1], (void *)&req, sizeof (req));
206 } 369 }
207 370
208 return 0; 371 return 0;
209} 372}
210 373
211MODULE = Linux::AIO PACKAGE = Linux::AIO 374MODULE = Linux::AIO PACKAGE = Linux::AIO
212 375
213BOOT: 376BOOT:
214{ 377{
378 sigfillset (&fullsigset);
379 sigdelset (&fullsigset, SIGTERM);
380 sigdelset (&fullsigset, SIGQUIT);
381 sigdelset (&fullsigset, SIGABRT);
382 sigdelset (&fullsigset, SIGINT);
383
215 if (pipe (reqpipe) || pipe (respipe)) 384 if (pipe (reqpipe) || pipe (respipe))
216 croak ("unable to initialize request or result pipe"); 385 croak ("unable to initialize request or result pipe");
386
387 if (fcntl (reqpipe[1], F_SETFL, O_NONBLOCK))
388 croak ("cannot set result pipe to nonblocking mode");
217 389
218 if (fcntl (respipe[0], F_SETFL, O_NONBLOCK)) 390 if (fcntl (respipe[0], F_SETFL, O_NONBLOCK))
219 croak ("cannot set result pipe to nonblocking mode"); 391 croak ("cannot set result pipe to nonblocking mode");
220} 392}
221 393
237 { 409 {
238 end_thread (); 410 end_thread ();
239 cur--; 411 cur--;
240 } 412 }
241 413
242 poll_cb ();
243 while (started > nthreads) 414 while (started > nthreads)
244 { 415 {
245 sched_yield (); 416 poll_wait ();
246 poll_cb (); 417 poll_cb (aTHX);
247 } 418 }
248 419
249void 420void
421aio_open(pathname,flags,mode,callback)
422 SV * pathname
423 int flags
424 int mode
425 SV * callback
426 PROTOTYPE: $$$$
427 CODE:
428 aio_req req;
429
430 Newz (0, req, 1, aio_cb);
431
432 if (!req)
433 croak ("out of memory during aio_req allocation");
434
435 req->type = REQ_OPEN;
436 req->data = newSVsv (pathname);
437 req->dataptr = SvPV_nolen (req->data);
438 req->fd = flags;
439 req->mode = mode;
440 req->callback = SvREFCNT_inc (callback);
441
442 send_req (req);
443
444void
445aio_close(fh,callback)
446 InputStream fh
447 SV * callback
448 PROTOTYPE: $$
449 ALIAS:
450 aio_close = REQ_CLOSE
451 aio_fsync = REQ_FSYNC
452 aio_fdatasync = REQ_FDATASYNC
453 CODE:
454 aio_req req;
455
456 Newz (0, req, 1, aio_cb);
457
458 if (!req)
459 croak ("out of memory during aio_req allocation");
460
461 req->type = ix;
462 req->fd = PerlIO_fileno (fh);
463 req->callback = SvREFCNT_inc (callback);
464
465 send_req (req);
466
467void
250aio_read(fh,offset,length,data,dataoffset,callback) 468aio_read(fh,offset,length,data,dataoffset,callback)
251 PerlIO * fh 469 InputStream fh
252 UV offset 470 UV offset
253 STRLEN length 471 IV length
254 SV * data 472 SV * data
255 STRLEN dataoffset 473 IV dataoffset
256 SV * callback 474 SV * callback
257 PROTOTYPE: $$$$$$ 475 PROTOTYPE: $$$$$$
258 ALIAS: 476 CODE:
259 aio_write = 1
260 CODE:
261 SvUPGRADE (data, SVt_PV);
262 SvPOK_on (data);
263 read_write (aTHX_ ix, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); 477 read_write (aTHX_ 0, PerlIO_fileno (fh), offset, length, data, dataoffset, callback);
478
479void
480aio_write(fh,offset,length,data,dataoffset,callback)
481 OutputStream fh
482 UV offset
483 IV length
484 SV * data
485 IV dataoffset
486 SV * callback
487 PROTOTYPE: $$$$$$
488 CODE:
489 read_write (aTHX_ 1, PerlIO_fileno (fh), offset, length, data, dataoffset, callback);
490
491void
492aio_stat(fh_or_path,callback)
493 SV * fh_or_path
494 SV * callback
495 PROTOTYPE: $$
496 ALIAS:
497 aio_lstat = 1
498 CODE:
499 aio_req req;
500
501 Newz (0, req, 1, aio_cb);
502
503 if (!req)
504 croak ("out of memory during aio_req allocation");
505
506 New (0, req->statdata, 1, Stat_t);
507
508 if (!req->statdata)
509 croak ("out of memory during aio_req->statdata allocation");
510
511 if (SvPOK (fh_or_path))
512 {
513 req->type = ix ? REQ_LSTAT : REQ_STAT;
514 req->data = newSVsv (fh_or_path);
515 req->dataptr = SvPV_nolen (req->data);
516 }
517 else
518 {
519 req->type = REQ_FSTAT;
520 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
521 }
522
523 req->callback = SvREFCNT_inc (callback);
524
525 send_req (req);
526
527void
528aio_unlink(pathname,callback)
529 SV * pathname
530 SV * callback
531 PROTOTYPE: $$
532 CODE:
533 aio_req req;
534
535 Newz (0, req, 1, aio_cb);
536
537 if (!req)
538 croak ("out of memory during aio_req allocation");
539
540 req->type = REQ_UNLINK;
541 req->data = newSVsv (pathname);
542 req->dataptr = SvPV_nolen (req->data);
543 req->callback = SvREFCNT_inc (callback);
544
545 send_req (req);
264 546
265int 547int
266poll_fileno() 548poll_fileno()
267 PROTOTYPE: 549 PROTOTYPE:
268 CODE: 550 CODE:
276 CODE: 558 CODE:
277 RETVAL = poll_cb (aTHX); 559 RETVAL = poll_cb (aTHX);
278 OUTPUT: 560 OUTPUT:
279 RETVAL 561 RETVAL
280 562
563void
564poll_wait()
565 PROTOTYPE:
566 CODE:
567 poll_wait ();
568
281int 569int
282nreqs() 570nreqs()
283 PROTOTYPE: 571 PROTOTYPE:
284 CODE: 572 CODE:
285 RETVAL = nreqs; 573 RETVAL = nreqs;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines