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.20 by root, Sun Jul 18 10:55:34 2004 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>
9 13
14typedef void *InputStream; /* 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 ;) */
17
18#ifndef __NR_pread64
19# define __NR_pread64 __NR_pread
20#endif
21#ifndef __NR_pwrite64
22# define __NR_pwrite64 __NR_pwrite
23#endif
24
10#define STACKSIZE 4096 /* yeah */ 25#define STACKSIZE 1024 /* yeah */
11 26
12#define REQ_QUIT 0 27enum {
13#define REQ_READ 1 28 REQ_QUIT,
14#define REQ_WRITE 2 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 struct stat64 *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]),
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 qe->next = req;
101 else
102 qe = qs = req;
103
104 send_reqs ();
105}
106
107static void
61end_thread(void) 108end_thread (void)
62{ 109{
63 aio_req req; 110 aio_req req;
64 New (0, req, 1, aio_cb); 111 New (0, req, 1, aio_cb);
65 req->type = REQ_QUIT; 112 req->type = REQ_QUIT;
66 write (reqpipe[1], &req, sizeof (aio_req)); 113
114 send_req (req);
67} 115}
68 116
69static void 117static void
118read_write (pTHX_
70read_write (pTHX_ int dowrite, int fd, off_t offset, size_t length, 119 int dowrite, int fd, off_t offset, size_t length,
71 SV *data, STRLEN dataoffset, SV*callback) 120 SV *data, STRLEN dataoffset, SV *callback)
72{ 121{
73 aio_req req; 122 aio_req req;
74 STRLEN svlen; 123 STRLEN svlen;
75 char *svptr = SvPV (data, svlen); 124 char *svptr = SvPV (data, svlen);
125
126 SvUPGRADE (data, SVt_PV);
127 SvPOK_on (data);
76 128
77 if (dataoffset < 0) 129 if (dataoffset < 0)
78 dataoffset += svlen; 130 dataoffset += svlen;
79 131
80 if (dataoffset < 0 || dataoffset > svlen) 132 if (dataoffset < 0 || dataoffset > svlen)
93 } 145 }
94 146
95 if (length < 0) 147 if (length < 0)
96 croak ("length must not be negative"); 148 croak ("length must not be negative");
97 149
98 New (0, req, 1, aio_cb); 150 Newz (0, req, 1, aio_cb);
99 151
100 if (!req) 152 if (!req)
101 croak ("out of memory during aio_req allocation"); 153 croak ("out of memory during aio_req allocation");
102 154
103 req->type = dowrite ? REQ_WRITE : REQ_READ; 155 req->type = dowrite ? REQ_WRITE : REQ_READ;
106 req->length = length; 158 req->length = length;
107 req->data = SvREFCNT_inc (data); 159 req->data = SvREFCNT_inc (data);
108 req->dataptr = (char *)svptr + dataoffset; 160 req->dataptr = (char *)svptr + dataoffset;
109 req->callback = SvREFCNT_inc (callback); 161 req->callback = SvREFCNT_inc (callback);
110 162
111 nreqs++; 163 send_req (req);
112 write (reqpipe[1], &req, sizeof (aio_req));
113} 164}
114 165
115static int 166static int
116poll_cb (pTHX) 167poll_cb (pTHX)
117{ 168{
133 184
134 if (req->type == REQ_READ) 185 if (req->type == REQ_READ)
135 SvCUR_set (req->data, req->dataoffset 186 SvCUR_set (req->data, req->dataoffset
136 + req->result > 0 ? req->result : 0); 187 + req->result > 0 ? req->result : 0);
137 188
189 if (req->data)
190 SvREFCNT_dec (req->data);
191
192 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT)
193 {
194 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
195 PL_laststatval = req->result;
196 PL_statcache.st_dev = req->statdata->st_dev;
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
210 Safefree (req->statdata);
211 }
212
138 PUSHMARK (SP); 213 PUSHMARK (SP);
139 XPUSHs (sv_2mortal (newSViv (req->result))); 214 XPUSHs (sv_2mortal (newSViv (req->result)));
140 PUTBACK; 215 PUTBACK;
141 call_sv (req->callback, G_VOID); 216 call_sv (req->callback, G_VOID);
142 SPAGAIN; 217 SPAGAIN;
143 218
144 SvREFCNT_dec (req->data); 219 if (req->callback)
145 SvREFCNT_dec (req->callback); 220 SvREFCNT_dec (req->callback);
146 221
147 errno = errorno; 222 errno = errorno;
148 nreqs--; 223 nreqs--;
149 count++; 224 count++;
150 } 225 }
151 226
152 Safefree (req); 227 Safefree (req);
153 } 228 }
154 229
230 if (qs)
231 send_reqs ();
232
155 return count; 233 return count;
156} 234}
235
236static sigset_t fullsigset;
157 237
158#undef errno 238#undef errno
159#include <asm/unistd.h> 239#include <asm/unistd.h>
240#include <sys/prctl.h>
160 241
161static int 242static int
162aio_proc(void *thr_arg) 243aio_proc (void *thr_arg)
163{ 244{
164 aio_thread *thr = thr_arg; 245 aio_thread *thr = thr_arg;
165 int sig; 246 aio_req req;
166 int errno; 247 int errno;
167 aio_req req;
168 248
249 /* this is very much x86 and kernel-specific :(:(:( */
169 /* we rely on gcc's ability to create closures. */ 250 /* 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); 251 _syscall3(int,read,int,fd,char *,buf,size_t,count)
172 _syscall3(int,write,int,fd,char *,buf,off_t,count); 252 _syscall3(int,write,int,fd,char *,buf,size_t,count)
173 253
174 /* first get rid of any signals */ 254 _syscall3(int,open,char *,pathname,int,flags,mode_t,mode)
175 for (sig = 1; sig < _NSIG; sig++) 255 _syscall1(int,close,int,fd)
176 signal (sig, SIG_DFL);
177 256
178 signal (SIGPIPE, SIG_IGN); 257 _syscall5(int,pread64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi)
179 258 _syscall5(int,pwrite64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi)
259
260 _syscall2(int,stat64, const char *, filename, struct stat64 *, buf)
261 _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf)
262 _syscall2(int,fstat64, int, fd, struct stat64 *, buf)
263
264 _syscall1(int,unlink, char *, filename);
265
266 sigprocmask (SIG_SETMASK, &fullsigset, 0);
267 prctl (PR_SET_PDEATHSIG, SIGKILL);
268
180 /* then loop */ 269 /* then loop */
181 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) 270 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req))
182 { 271 {
183 req->thread = thr; 272 req->thread = thr;
273 errno = 0; /* strictly unnecessary */
184 274
185 if (req->type == REQ_READ || req->type == REQ_WRITE) 275 switch (req->type)
186 { 276 {
277 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;
279 case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break;
280 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;
285
286 case REQ_QUIT:
287 default:
288 write (respipe[1], (void *)&req, sizeof (req));
187 errno = 0; 289 return 0;
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);
193 else
194 req->result = write(req->fd, req->dataptr, req->length);
195 }
196
197 req->errorno = errno;
198 } 290 }
199 else
200 {
201 write (respipe[1], (void *)&req, sizeof (req));
202 break;
203 }
204 291
292 req->errorno = errno;
205 write (respipe[1], (void *)&req, sizeof (req)); 293 write (respipe[1], (void *)&req, sizeof (req));
206 } 294 }
207 295
208 return 0; 296 return 0;
209} 297}
210 298
211MODULE = Linux::AIO PACKAGE = Linux::AIO 299MODULE = Linux::AIO PACKAGE = Linux::AIO
212 300
213BOOT: 301BOOT:
214{ 302{
303 sigfillset (&fullsigset);
304 sigdelset (&fullsigset, SIGTERM);
305 sigdelset (&fullsigset, SIGQUIT);
306 sigdelset (&fullsigset, SIGABRT);
307 sigdelset (&fullsigset, SIGINT);
308
215 if (pipe (reqpipe) || pipe (respipe)) 309 if (pipe (reqpipe) || pipe (respipe))
216 croak ("unable to initialize request or result pipe"); 310 croak ("unable to initialize request or result pipe");
311
312 if (fcntl (reqpipe[1], F_SETFL, O_NONBLOCK))
313 croak ("cannot set result pipe to nonblocking mode");
217 314
218 if (fcntl (respipe[0], F_SETFL, O_NONBLOCK)) 315 if (fcntl (respipe[0], F_SETFL, O_NONBLOCK))
219 croak ("cannot set result pipe to nonblocking mode"); 316 croak ("cannot set result pipe to nonblocking mode");
220} 317}
221 318
237 { 334 {
238 end_thread (); 335 end_thread ();
239 cur--; 336 cur--;
240 } 337 }
241 338
242 poll_cb ();
243 while (started > nthreads) 339 while (started > nthreads)
244 { 340 {
245 sched_yield (); 341 fd_set rfd;
342 FD_ZERO(&rfd);
343 FD_SET(respipe[0], &rfd);
344
345 select (respipe[0] + 1, &rfd, 0, 0, 0);
246 poll_cb (); 346 poll_cb (aTHX);
247 } 347 }
248 348
249void 349void
350aio_open(pathname,flags,mode,callback)
351 SV * pathname
352 int flags
353 int mode
354 SV * callback
355 PROTOTYPE: $$$$
356 CODE:
357 aio_req req;
358
359 Newz (0, req, 1, aio_cb);
360
361 if (!req)
362 croak ("out of memory during aio_req allocation");
363
364 req->type = REQ_OPEN;
365 req->data = newSVsv (pathname);
366 req->dataptr = SvPV_nolen (req->data);
367 req->fd = flags;
368 req->mode = mode;
369 req->callback = SvREFCNT_inc (callback);
370
371 send_req (req);
372
373void
374aio_close(fh,callback)
375 InputStream fh
376 SV * callback
377 PROTOTYPE: $$
378 CODE:
379 aio_req req;
380
381 Newz (0, req, 1, aio_cb);
382
383 if (!req)
384 croak ("out of memory during aio_req allocation");
385
386 req->type = REQ_CLOSE;
387 req->fd = PerlIO_fileno (fh);
388 req->callback = SvREFCNT_inc (callback);
389
390 send_req (req);
391
392void
250aio_read(fh,offset,length,data,dataoffset,callback) 393aio_read(fh,offset,length,data,dataoffset,callback)
251 PerlIO * fh 394 InputStream fh
252 UV offset 395 UV offset
253 STRLEN length 396 IV length
254 SV * data 397 SV * data
255 STRLEN dataoffset 398 IV dataoffset
256 SV * callback 399 SV * callback
257 PROTOTYPE: $$$$$$ 400 PROTOTYPE: $$$$$$
258 ALIAS: 401 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); 402 read_write (aTHX_ 0, PerlIO_fileno (fh), offset, length, data, dataoffset, callback);
403
404void
405aio_write(fh,offset,length,data,dataoffset,callback)
406 OutputStream fh
407 UV offset
408 IV length
409 SV * data
410 IV dataoffset
411 SV * callback
412 PROTOTYPE: $$$$$$
413 CODE:
414 read_write (aTHX_ 1, PerlIO_fileno (fh), offset, length, data, dataoffset, callback);
415
416void
417aio_stat(fh_or_path,callback)
418 SV * fh_or_path
419 SV * callback
420 PROTOTYPE: $$
421 ALIAS:
422 aio_lstat = 1
423 CODE:
424 aio_req req;
425
426 Newz (0, req, 1, aio_cb);
427
428 if (!req)
429 croak ("out of memory during aio_req allocation");
430
431 New (0, req->statdata, 1, struct stat64);
432
433 if (!req->statdata)
434 croak ("out of memory during aio_req->statdata allocation");
435
436 if (SvPOK (fh_or_path))
437 {
438 req->type = ix ? REQ_LSTAT : REQ_STAT;
439 req->data = newSVsv (fh_or_path);
440 req->dataptr = SvPV_nolen (req->data);
441 }
442 else
443 {
444 req->type = REQ_FSTAT;
445 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
446 }
447
448 req->callback = SvREFCNT_inc (callback);
449
450 send_req (req);
451
452void
453aio_unlink(pathname,callback)
454 SV * pathname
455 SV * callback
456 PROTOTYPE: $$
457 CODE:
458 aio_req req;
459
460 Newz (0, req, 1, aio_cb);
461
462 if (!req)
463 croak ("out of memory during aio_req allocation");
464
465 req->type = REQ_UNLINK;
466 req->data = newSVsv (pathname);
467 req->dataptr = SvPV_nolen (req->data);
468 req->callback = SvREFCNT_inc (callback);
469
470 send_req (req);
264 471
265int 472int
266poll_fileno() 473poll_fileno()
267 PROTOTYPE: 474 PROTOTYPE:
268 CODE: 475 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines