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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines