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.13 by root, Mon Apr 1 20:30:08 2002 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>
11 13
12typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ 14typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */
13typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ 15typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */
14typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ 16typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */
15 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
16#define STACKSIZE 1024 /* yeah */ 25#define STACKSIZE 1024 /* yeah */
17 26
18enum { REQ_QUIT, REQ_OPEN, REQ_CLOSE, REQ_READ, REQ_WRITE, REQ_STAT, REQ_LSTAT, REQ_FSTAT}; 27enum {
28 REQ_QUIT,
29 REQ_OPEN, REQ_CLOSE, REQ_READ, REQ_WRITE,
30 REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_UNLINK
31};
19 32
20typedef struct { 33typedef struct {
21 char stack[STACKSIZE]; 34 char stack[STACKSIZE];
22} aio_thread; 35} aio_thread;
23 36
24typedef struct { 37typedef struct aio_cb {
38 struct aio_cb *next;
39
25 int type; 40 int type;
26 aio_thread *thread; 41 aio_thread *thread;
27
28 SV *savesv;
29 42
30 int fd; 43 int fd;
31 off_t offset; 44 off_t offset;
32 size_t length; 45 size_t length;
33 ssize_t result; 46 ssize_t result;
44 57
45static int started; 58static int started;
46static int nreqs; 59static int nreqs;
47static int reqpipe[2], respipe[2]; 60static int reqpipe[2], respipe[2];
48 61
62static aio_req qs, qe; /* queue start, queue end */
63
49static int aio_proc(void *arg); 64static int aio_proc(void *arg);
50 65
51static void 66static void
52start_thread(void) 67start_thread (void)
53{ 68{
54 aio_thread *thr; 69 aio_thread *thr;
55 70
56 New (0, thr, 1, aio_thread); 71 New (0, thr, 1, aio_thread);
57 72
58 if (clone (aio_proc, 73 if (clone (aio_proc,
59 &(thr->stack[STACKSIZE]), 74 &(thr->stack[STACKSIZE]),
60 CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND, 75 CLONE_VM|CLONE_FS|CLONE_FILES,
61 thr) >= 0) 76 thr) >= 0)
62 started++; 77 started++;
63 else 78 else
64 Safefree (thr); 79 Safefree (thr);
65} 80}
66 81
67static 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
68end_thread(void) 108end_thread (void)
69{ 109{
70 aio_req req; 110 aio_req req;
71 New (0, req, 1, aio_cb); 111 New (0, req, 1, aio_cb);
72 req->type = REQ_QUIT; 112 req->type = REQ_QUIT;
73 write (reqpipe[1], &req, sizeof (aio_req)); 113
114 send_req (req);
74} 115}
75 116
76static void 117static void
77send_req (aio_req req) 118read_write (pTHX_
78{
79 nreqs++;
80 write (reqpipe[1], &req, sizeof (aio_req));
81}
82
83static void
84read_write (pTHX_ int dowrite, int fd, off_t offset, size_t length, 119 int dowrite, int fd, off_t offset, size_t length,
85 SV *data, STRLEN dataoffset, SV*callback) 120 SV *data, STRLEN dataoffset, SV *callback)
86{ 121{
87 aio_req req; 122 aio_req req;
88 STRLEN svlen; 123 STRLEN svlen;
89 char *svptr = SvPV (data, svlen); 124 char *svptr = SvPV (data, svlen);
90 125
110 } 145 }
111 146
112 if (length < 0) 147 if (length < 0)
113 croak ("length must not be negative"); 148 croak ("length must not be negative");
114 149
115 New (0, req, 1, aio_cb); 150 Newz (0, req, 1, aio_cb);
116 151
117 if (!req) 152 if (!req)
118 croak ("out of memory during aio_req allocation"); 153 croak ("out of memory during aio_req allocation");
119 154
120 req->type = dowrite ? REQ_WRITE : REQ_READ; 155 req->type = dowrite ? REQ_WRITE : REQ_READ;
145 else 180 else
146 { 181 {
147 int errorno = errno; 182 int errorno = errno;
148 errno = req->errorno; 183 errno = req->errorno;
149 184
150 if (req->savesv)
151 SvREFCNT_dec (req->savesv);
152
153 if (req->type == REQ_READ) 185 if (req->type == REQ_READ)
154 SvCUR_set (req->data, req->dataoffset 186 SvCUR_set (req->data, req->dataoffset
155 + req->result > 0 ? req->result : 0); 187 + req->result > 0 ? req->result : 0);
188
189 if (req->data)
190 SvREFCNT_dec (req->data);
156 191
157 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) 192 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT)
158 { 193 {
159 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; 194 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
160 PL_laststatval = req->result; 195 PL_laststatval = req->result;
179 XPUSHs (sv_2mortal (newSViv (req->result))); 214 XPUSHs (sv_2mortal (newSViv (req->result)));
180 PUTBACK; 215 PUTBACK;
181 call_sv (req->callback, G_VOID); 216 call_sv (req->callback, G_VOID);
182 SPAGAIN; 217 SPAGAIN;
183 218
184 SvREFCNT_dec (req->data); 219 if (req->callback)
185 SvREFCNT_dec (req->callback); 220 SvREFCNT_dec (req->callback);
186 221
187 errno = errorno; 222 errno = errorno;
188 nreqs--; 223 nreqs--;
189 count++; 224 count++;
190 } 225 }
191 226
192 Safefree (req); 227 Safefree (req);
193 } 228 }
194 229
230 if (qs)
231 send_reqs ();
232
195 return count; 233 return count;
196} 234}
197 235
198static sigset_t fullsigset; 236static sigset_t fullsigset;
199 237
200#undef errno 238#undef errno
201#include <asm/unistd.h> 239#include <asm/unistd.h>
240#include <sys/prctl.h>
202 241
203static int 242static int
204aio_proc(void *thr_arg) 243aio_proc (void *thr_arg)
205{ 244{
206 aio_thread *thr = thr_arg; 245 aio_thread *thr = thr_arg;
207 aio_req req; 246 aio_req req;
208 int errno; 247 int errno;
209 248
249 /* this is very much x86 and kernel-specific :(:(:( */
210 /* we rely on gcc's ability to create closures. */ 250 /* we rely on gcc's ability to create closures. */
211 _syscall3(int,read,int,fd,char *,buf,size_t,count) 251 _syscall3(int,read,int,fd,char *,buf,size_t,count)
212 _syscall3(int,write,int,fd,char *,buf,size_t,count) 252 _syscall3(int,write,int,fd,char *,buf,size_t,count)
213 253
214 _syscall3(int,open,char *,pathname,int,flags,mode_t,mode) 254 _syscall3(int,open,char *,pathname,int,flags,mode_t,mode)
215 _syscall1(int,close,int,fd) 255 _syscall1(int,close,int,fd)
216 256
217 _syscall4(int,pread,int,fd,char *,buf,size_t,count,off_t,offset) 257 _syscall5(int,pread64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi)
218 _syscall4(int,pwrite,int,fd,char *,buf,size_t,count,off_t,offset) 258 _syscall5(int,pwrite64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi)
219 259
220 _syscall2(int,stat64, const char *, filename, struct stat64 *, buf) 260 _syscall2(int,stat64, const char *, filename, struct stat64 *, buf)
221 _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf) 261 _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf)
222 _syscall2(int,fstat64, int, fd, struct stat64 *, buf) 262 _syscall2(int,fstat64, int, fd, struct stat64 *, buf)
223 263
264 _syscall1(int,unlink, char *, filename);
265
224 sigprocmask (SIG_SETMASK, &fullsigset, 0); 266 sigprocmask (SIG_SETMASK, &fullsigset, 0);
267 prctl (PR_SET_PDEATHSIG, SIGKILL);
225 268
226 /* then loop */ 269 /* then loop */
227 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) 270 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req))
228 { 271 {
229 req->thread = thr; 272 req->thread = thr;
230 errno = 0; 273 errno = 0; /* strictly unnecessary */
231 274
232 if (req->type == REQ_READ) 275 switch (req->type)
233 req->result = pread (req->fd, req->dataptr, req->length, req->offset);
234 else if (req->type == REQ_WRITE)
235 req->result = pwrite (req->fd, req->dataptr, req->length, req->offset);
236 else if (req->type == REQ_OPEN)
237 req->result = open (req->dataptr, req->fd, req->mode);
238 else if (req->type == REQ_CLOSE)
239 req->result = close (req->fd);
240 else if (req->type == REQ_STAT)
241 req->result = stat64 (req->dataptr, req->statdata);
242 else if (req->type == REQ_LSTAT)
243 req->result = lstat64 (req->dataptr, req->statdata);
244 else if (req->type == REQ_FSTAT)
245 req->result = fstat64 (req->fd, req->statdata);
246 else
247 { 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:
248 write (respipe[1], (void *)&req, sizeof (req)); 288 write (respipe[1], (void *)&req, sizeof (req));
249 break; 289 return 0;
250 } 290 }
251 291
252 req->errorno = errno; 292 req->errorno = errno;
253 write (respipe[1], (void *)&req, sizeof (req)); 293 write (respipe[1], (void *)&req, sizeof (req));
254 } 294 }
266 sigdelset (&fullsigset, SIGABRT); 306 sigdelset (&fullsigset, SIGABRT);
267 sigdelset (&fullsigset, SIGINT); 307 sigdelset (&fullsigset, SIGINT);
268 308
269 if (pipe (reqpipe) || pipe (respipe)) 309 if (pipe (reqpipe) || pipe (respipe))
270 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");
271 314
272 if (fcntl (respipe[0], F_SETFL, O_NONBLOCK)) 315 if (fcntl (respipe[0], F_SETFL, O_NONBLOCK))
273 croak ("cannot set result pipe to nonblocking mode"); 316 croak ("cannot set result pipe to nonblocking mode");
274} 317}
275 318
291 { 334 {
292 end_thread (); 335 end_thread ();
293 cur--; 336 cur--;
294 } 337 }
295 338
296 poll_cb ();
297 while (started > nthreads) 339 while (started > nthreads)
298 { 340 {
299 sched_yield (); 341 fd_set rfd;
300 fcntl (respipe[0], F_SETFL, 0); 342 FD_ZERO(&rfd);
343 FD_SET(respipe[0], &rfd);
344
345 select (respipe[0] + 1, &rfd, 0, 0, 0);
301 poll_cb (); 346 poll_cb (aTHX);
302 fcntl (respipe[0], F_SETFL, O_NONBLOCK);
303 } 347 }
304 348
305void 349void
306aio_open(pathname,flags,mode,callback) 350aio_open(pathname,flags,mode,callback)
307 SV * pathname 351 SV * pathname
310 SV * callback 354 SV * callback
311 PROTOTYPE: $$$$ 355 PROTOTYPE: $$$$
312 CODE: 356 CODE:
313 aio_req req; 357 aio_req req;
314 358
315 New (0, req, 1, aio_cb); 359 Newz (0, req, 1, aio_cb);
316 360
317 if (!req) 361 if (!req)
318 croak ("out of memory during aio_req allocation"); 362 croak ("out of memory during aio_req allocation");
319 363
320 req->type = REQ_OPEN; 364 req->type = REQ_OPEN;
321 req->savesv = newSVsv (pathname); 365 req->data = newSVsv (pathname);
322 req->dataptr = SvPV_nolen (req->savesv); 366 req->dataptr = SvPV_nolen (req->data);
323 req->fd = flags; 367 req->fd = flags;
324 req->mode = mode; 368 req->mode = mode;
325 req->callback = SvREFCNT_inc (callback); 369 req->callback = SvREFCNT_inc (callback);
326 370
327 send_req (req); 371 send_req (req);
328 372
329void 373void
330aio_close(fh,callback) 374aio_close(fh,callback)
331 InputStream fh 375 InputStream fh
332 SV * callback 376 SV * callback
333 PROTOTYPE: $ 377 PROTOTYPE: $$
334 CODE: 378 CODE:
335 aio_req req; 379 aio_req req;
336 380
337 New (0, req, 1, aio_cb); 381 Newz (0, req, 1, aio_cb);
338 382
339 if (!req) 383 if (!req)
340 croak ("out of memory during aio_req allocation"); 384 croak ("out of memory during aio_req allocation");
341 385
342 req->type = REQ_CLOSE; 386 req->type = REQ_CLOSE;
377 ALIAS: 421 ALIAS:
378 aio_lstat = 1 422 aio_lstat = 1
379 CODE: 423 CODE:
380 aio_req req; 424 aio_req req;
381 425
382 New (0, req, 1, aio_cb); 426 Newz (0, req, 1, aio_cb);
383 427
384 if (!req) 428 if (!req)
385 croak ("out of memory during aio_req allocation"); 429 croak ("out of memory during aio_req allocation");
386 430
387 New (0, req->statdata, 1, struct stat64); 431 New (0, req->statdata, 1, struct stat64);
390 croak ("out of memory during aio_req->statdata allocation"); 434 croak ("out of memory during aio_req->statdata allocation");
391 435
392 if (SvPOK (fh_or_path)) 436 if (SvPOK (fh_or_path))
393 { 437 {
394 req->type = ix ? REQ_LSTAT : REQ_STAT; 438 req->type = ix ? REQ_LSTAT : REQ_STAT;
395 req->savesv = newSVsv (fh_or_path); 439 req->data = newSVsv (fh_or_path);
396 req->dataptr = SvPV_nolen (req->savesv); 440 req->dataptr = SvPV_nolen (req->data);
397 } 441 }
398 else 442 else
399 { 443 {
400 req->type = REQ_FSTAT; 444 req->type = REQ_FSTAT;
401 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); 445 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
403 447
404 req->callback = SvREFCNT_inc (callback); 448 req->callback = SvREFCNT_inc (callback);
405 449
406 send_req (req); 450 send_req (req);
407 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);
471
408int 472int
409poll_fileno() 473poll_fileno()
410 PROTOTYPE: 474 PROTOTYPE:
411 CODE: 475 CODE:
412 RETVAL = respipe[0]; 476 RETVAL = respipe[0];

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines