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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines