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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines