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.21 by root, Fri Aug 6 17:18:08 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>
7#include <unistd.h> 9#include <unistd.h>
8#include <fcntl.h> 10#include <fcntl.h>
9#include <signal.h> 11#include <signal.h>
10#include <sched.h> 12#include <sched.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
16#define STACKSIZE 1024 /* yeah */ 18#define STACKSIZE 1024 /* yeah */
17 19
18enum { REQ_QUIT, REQ_OPEN, REQ_CLOSE, REQ_READ, REQ_WRITE, REQ_STAT, REQ_LSTAT, REQ_FSTAT}; 20enum {
21 REQ_QUIT,
22 REQ_OPEN, REQ_CLOSE, REQ_READ, REQ_WRITE,
23 REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_UNLINK
24};
19 25
20typedef struct { 26typedef struct {
21 char stack[STACKSIZE]; 27 char stack[STACKSIZE];
22} aio_thread; 28} aio_thread;
23 29
24typedef struct { 30typedef struct aio_cb {
31 struct aio_cb *next;
32
25 int type; 33 int type;
26 aio_thread *thread; 34 aio_thread *thread;
27
28 SV *savesv;
29 35
30 int fd; 36 int fd;
31 off_t offset; 37 off_t offset;
32 size_t length; 38 size_t length;
33 ssize_t result; 39 ssize_t result;
35 int errorno; 41 int errorno;
36 SV *data, *callback; 42 SV *data, *callback;
37 void *dataptr; 43 void *dataptr;
38 STRLEN dataoffset; 44 STRLEN dataoffset;
39 45
40 struct stat64 *statdata; 46 Stat_t *statdata;
41} aio_cb; 47} aio_cb;
42 48
43typedef aio_cb *aio_req; 49typedef aio_cb *aio_req;
44 50
45static int started; 51static int started;
46static int nreqs; 52static int nreqs;
47static int reqpipe[2], respipe[2]; 53static int reqpipe[2], respipe[2];
48 54
55static aio_req qs, qe; /* queue start, queue end */
56
49static int aio_proc(void *arg); 57static int aio_proc(void *arg);
50 58
51static void 59static void
52start_thread(void) 60start_thread (void)
53{ 61{
54 aio_thread *thr; 62 aio_thread *thr;
55 63
56 New (0, thr, 1, aio_thread); 64 New (0, thr, 1, aio_thread);
57 65
58 if (clone (aio_proc, 66 if (clone (aio_proc,
59 &(thr->stack[STACKSIZE]), 67 &(thr->stack[STACKSIZE]),
60 CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND, 68 CLONE_VM|CLONE_FS|CLONE_FILES,
61 thr) >= 0) 69 thr) >= 0)
62 started++; 70 started++;
63 else 71 else
64 Safefree (thr); 72 Safefree (thr);
65} 73}
66 74
67static void 75static void
76send_reqs (void)
77{
78 /* this write is atomic */
79 while (qs && write (reqpipe[1], &qs, sizeof qs) == sizeof qs)
80 {
81 qs = qs->next;
82 if (!qs) qe = 0;
83 }
84}
85
86static void
87send_req (aio_req req)
88{
89 nreqs++;
90 req->next = 0;
91
92 if (qe)
93 qe->next = req;
94 else
95 qe = qs = req;
96
97 send_reqs ();
98}
99
100static void
68end_thread(void) 101end_thread (void)
69{ 102{
70 aio_req req; 103 aio_req req;
71 New (0, req, 1, aio_cb); 104 New (0, req, 1, aio_cb);
72 req->type = REQ_QUIT; 105 req->type = REQ_QUIT;
73 write (reqpipe[1], &req, sizeof (aio_req)); 106
107 send_req (req);
74} 108}
75 109
76static void 110static void
77send_req (aio_req req) 111read_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, 112 int dowrite, int fd, off_t offset, size_t length,
85 SV *data, STRLEN dataoffset, SV*callback) 113 SV *data, STRLEN dataoffset, SV *callback)
86{ 114{
87 aio_req req; 115 aio_req req;
88 STRLEN svlen; 116 STRLEN svlen;
89 char *svptr = SvPV (data, svlen); 117 char *svptr = SvPV (data, svlen);
90 118
110 } 138 }
111 139
112 if (length < 0) 140 if (length < 0)
113 croak ("length must not be negative"); 141 croak ("length must not be negative");
114 142
115 New (0, req, 1, aio_cb); 143 Newz (0, req, 1, aio_cb);
116 144
117 if (!req) 145 if (!req)
118 croak ("out of memory during aio_req allocation"); 146 croak ("out of memory during aio_req allocation");
119 147
120 req->type = dowrite ? REQ_WRITE : REQ_READ; 148 req->type = dowrite ? REQ_WRITE : REQ_READ;
145 else 173 else
146 { 174 {
147 int errorno = errno; 175 int errorno = errno;
148 errno = req->errorno; 176 errno = req->errorno;
149 177
150 if (req->savesv)
151 SvREFCNT_dec (req->savesv);
152
153 if (req->type == REQ_READ) 178 if (req->type == REQ_READ)
154 SvCUR_set (req->data, req->dataoffset 179 SvCUR_set (req->data, req->dataoffset
155 + req->result > 0 ? req->result : 0); 180 + req->result > 0 ? req->result : 0);
156 181
182 if (req->data)
183 SvREFCNT_dec (req->data);
184
157 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) 185 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT)
158 { 186 {
159 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; 187 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
160 PL_laststatval = req->result; 188 PL_laststatval = req->result;
161 PL_statcache.st_dev = req->statdata->st_dev; 189 PL_statcache = *(req->statdata);
162 PL_statcache.st_ino = req->statdata->st_ino;
163 PL_statcache.st_mode = req->statdata->st_mode;
164 PL_statcache.st_nlink = req->statdata->st_nlink;
165 PL_statcache.st_uid = req->statdata->st_uid;
166 PL_statcache.st_gid = req->statdata->st_gid;
167 PL_statcache.st_rdev = req->statdata->st_rdev;
168 PL_statcache.st_size = req->statdata->st_size;
169 PL_statcache.st_atime = req->statdata->st_atime;
170 PL_statcache.st_mtime = req->statdata->st_mtime;
171 PL_statcache.st_ctime = req->statdata->st_ctime;
172 PL_statcache.st_blksize = req->statdata->st_blksize;
173 PL_statcache.st_blocks = req->statdata->st_blocks;
174 190
175 Safefree (req->statdata); 191 Safefree (req->statdata);
176 } 192 }
177 193
178 PUSHMARK (SP); 194 PUSHMARK (SP);
179 XPUSHs (sv_2mortal (newSViv (req->result))); 195 XPUSHs (sv_2mortal (newSViv (req->result)));
180 PUTBACK; 196 PUTBACK;
181 call_sv (req->callback, G_VOID); 197 call_sv (req->callback, G_VOID);
182 SPAGAIN; 198 SPAGAIN;
183 199
184 SvREFCNT_dec (req->data); 200 if (req->callback)
185 SvREFCNT_dec (req->callback); 201 SvREFCNT_dec (req->callback);
186 202
187 errno = errorno; 203 errno = errorno;
188 nreqs--; 204 nreqs--;
189 count++; 205 count++;
190 } 206 }
191 207
192 Safefree (req); 208 Safefree (req);
193 } 209 }
194 210
211 if (qs)
212 send_reqs ();
213
195 return count; 214 return count;
196} 215}
197 216
198static sigset_t fullsigset; 217static sigset_t fullsigset;
199 218
200#undef errno 219#undef errno
201#include <asm/unistd.h> 220#include <asm/unistd.h>
221#include <sys/prctl.h>
222
223#define COPY_STATDATA \
224 req->statdata->st_dev = statdata.st_dev; \
225 req->statdata->st_ino = statdata.st_ino; \
226 req->statdata->st_mode = statdata.st_mode; \
227 req->statdata->st_nlink = statdata.st_nlink; \
228 req->statdata->st_uid = statdata.st_uid; \
229 req->statdata->st_gid = statdata.st_gid; \
230 req->statdata->st_rdev = statdata.st_rdev; \
231 req->statdata->st_size = statdata.st_size; \
232 req->statdata->st_atime = statdata.st_atime; \
233 req->statdata->st_mtime = statdata.st_mtime; \
234 req->statdata->st_ctime = statdata.st_ctime; \
235 req->statdata->st_blksize = statdata.st_blksize; \
236 req->statdata->st_blocks = statdata.st_blocks; \
202 237
203static int 238static int
204aio_proc(void *thr_arg) 239aio_proc (void *thr_arg)
205{ 240{
206 aio_thread *thr = thr_arg; 241 aio_thread *thr = thr_arg;
207 aio_req req; 242 aio_req req;
208 int errno; 243 int errno;
209 244
245 /* this is very much kernel-specific :(:(:( */
210 /* we rely on gcc's ability to create closures. */ 246 /* we rely on gcc's ability to create closures. */
211 _syscall3(int,read,int,fd,char *,buf,size_t,count) 247 _syscall3(int,read,int,fd,char *,buf,size_t,count)
212 _syscall3(int,write,int,fd,char *,buf,size_t,count) 248 _syscall3(int,write,int,fd,char *,buf,size_t,count)
213 249
214 _syscall3(int,open,char *,pathname,int,flags,mode_t,mode) 250 _syscall3(int,open,char *,pathname,int,flags,mode_t,mode)
215 _syscall1(int,close,int,fd) 251 _syscall1(int,close,int,fd)
216 252
253#ifdef __NR_pread64
254 _syscall5(int,pread64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi)
255 _syscall5(int,pwrite64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi)
256#elif __NR_pread
217 _syscall4(int,pread,int,fd,char *,buf,size_t,count,off_t,offset) 257 _syscall4(int,pread,int,fd,char *,buf,size_t,count,offset_t,offset)
218 _syscall4(int,pwrite,int,fd,char *,buf,size_t,count,off_t,offset) 258 _syscall4(int,pwrite,int,fd,char *,buf,size_t,count,offset_t,offset)
259#else
260# error "neither pread nor pread64 defined"
261#endif
219 262
263
264#ifdef __NR_stat64
220 _syscall2(int,stat64, const char *, filename, struct stat64 *, buf) 265 _syscall2(int,stat64, const char *, filename, struct stat64 *, buf)
221 _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf) 266 _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf)
222 _syscall2(int,fstat64, int, fd, struct stat64 *, buf) 267 _syscall2(int,fstat64, int, fd, struct stat64 *, buf)
268#elif __NR_stat
269 _syscall2(int,stat, const char *, filename, struct stat *, buf)
270 _syscall2(int,lstat, const char *, filename, struct stat *, buf)
271 _syscall2(int,fstat, int, fd, struct stat *, buf)
272#else
273# error "neither stat64 nor stat defined"
274#endif
275
276 _syscall1(int,unlink, char *, filename);
223 277
224 sigprocmask (SIG_SETMASK, &fullsigset, 0); 278 sigprocmask (SIG_SETMASK, &fullsigset, 0);
279 prctl (PR_SET_PDEATHSIG, SIGKILL);
225 280
226 /* then loop */ 281 /* then loop */
227 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) 282 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req))
228 { 283 {
229 req->thread = thr; 284 req->thread = thr;
230 errno = 0; 285 errno = 0; /* strictly unnecessary */
231 286
232 if (req->type == REQ_READ) 287 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 { 288 {
289#ifdef __NR_pread64
290 case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); break;
291 case REQ_WRITE: req->result = pwrite64(req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); break;
292#else
293 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break;
294 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break;
295#endif
296#ifdef __NR_stat64
297 struct stat64 statdata;
298 case REQ_STAT: req->result = stat64 (req->dataptr, &statdata); COPY_STATDATA; break;
299 case REQ_LSTAT: req->result = lstat64 (req->dataptr, &statdata); COPY_STATDATA; break;
300 case REQ_FSTAT: req->result = fstat64 (req->fd, &statdata); COPY_STATDATA; break;
301#else
302 struct stat statdata;
303 case REQ_STAT: req->result = stat (req->dataptr, &statdata); COPY_STATDATA; break;
304 case REQ_LSTAT: req->result = lstat (req->dataptr, &statdata); COPY_STATDATA; break;
305 case REQ_FSTAT: req->result = fstat (req->fd, &statdata); COPY_STATDATA; break;
306#endif
307 case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break;
308 case REQ_CLOSE: req->result = close (req->fd); break;
309 case REQ_UNLINK: req->result = unlink (req->dataptr); break;
310
311 case REQ_QUIT:
312 default:
248 write (respipe[1], (void *)&req, sizeof (req)); 313 write (respipe[1], (void *)&req, sizeof (req));
249 break; 314 return 0;
250 } 315 }
251 316
252 req->errorno = errno; 317 req->errorno = errno;
253 write (respipe[1], (void *)&req, sizeof (req)); 318 write (respipe[1], (void *)&req, sizeof (req));
254 } 319 }
266 sigdelset (&fullsigset, SIGABRT); 331 sigdelset (&fullsigset, SIGABRT);
267 sigdelset (&fullsigset, SIGINT); 332 sigdelset (&fullsigset, SIGINT);
268 333
269 if (pipe (reqpipe) || pipe (respipe)) 334 if (pipe (reqpipe) || pipe (respipe))
270 croak ("unable to initialize request or result pipe"); 335 croak ("unable to initialize request or result pipe");
336
337 if (fcntl (reqpipe[1], F_SETFL, O_NONBLOCK))
338 croak ("cannot set result pipe to nonblocking mode");
271 339
272 if (fcntl (respipe[0], F_SETFL, O_NONBLOCK)) 340 if (fcntl (respipe[0], F_SETFL, O_NONBLOCK))
273 croak ("cannot set result pipe to nonblocking mode"); 341 croak ("cannot set result pipe to nonblocking mode");
274} 342}
275 343
291 { 359 {
292 end_thread (); 360 end_thread ();
293 cur--; 361 cur--;
294 } 362 }
295 363
296 poll_cb ();
297 while (started > nthreads) 364 while (started > nthreads)
298 { 365 {
299 sched_yield (); 366 fd_set rfd;
300 fcntl (respipe[0], F_SETFL, 0); 367 FD_ZERO(&rfd);
368 FD_SET(respipe[0], &rfd);
369
370 select (respipe[0] + 1, &rfd, 0, 0, 0);
301 poll_cb (); 371 poll_cb (aTHX);
302 fcntl (respipe[0], F_SETFL, O_NONBLOCK);
303 } 372 }
304 373
305void 374void
306aio_open(pathname,flags,mode,callback) 375aio_open(pathname,flags,mode,callback)
307 SV * pathname 376 SV * pathname
310 SV * callback 379 SV * callback
311 PROTOTYPE: $$$$ 380 PROTOTYPE: $$$$
312 CODE: 381 CODE:
313 aio_req req; 382 aio_req req;
314 383
315 New (0, req, 1, aio_cb); 384 Newz (0, req, 1, aio_cb);
316 385
317 if (!req) 386 if (!req)
318 croak ("out of memory during aio_req allocation"); 387 croak ("out of memory during aio_req allocation");
319 388
320 req->type = REQ_OPEN; 389 req->type = REQ_OPEN;
321 req->savesv = newSVsv (pathname); 390 req->data = newSVsv (pathname);
322 req->dataptr = SvPV_nolen (req->savesv); 391 req->dataptr = SvPV_nolen (req->data);
323 req->fd = flags; 392 req->fd = flags;
324 req->mode = mode; 393 req->mode = mode;
325 req->callback = SvREFCNT_inc (callback); 394 req->callback = SvREFCNT_inc (callback);
326 395
327 send_req (req); 396 send_req (req);
328 397
329void 398void
330aio_close(fh,callback) 399aio_close(fh,callback)
331 InputStream fh 400 InputStream fh
332 SV * callback 401 SV * callback
333 PROTOTYPE: $ 402 PROTOTYPE: $$
334 CODE: 403 CODE:
335 aio_req req; 404 aio_req req;
336 405
337 New (0, req, 1, aio_cb); 406 Newz (0, req, 1, aio_cb);
338 407
339 if (!req) 408 if (!req)
340 croak ("out of memory during aio_req allocation"); 409 croak ("out of memory during aio_req allocation");
341 410
342 req->type = REQ_CLOSE; 411 req->type = REQ_CLOSE;
377 ALIAS: 446 ALIAS:
378 aio_lstat = 1 447 aio_lstat = 1
379 CODE: 448 CODE:
380 aio_req req; 449 aio_req req;
381 450
382 New (0, req, 1, aio_cb); 451 Newz (0, req, 1, aio_cb);
383 452
384 if (!req) 453 if (!req)
385 croak ("out of memory during aio_req allocation"); 454 croak ("out of memory during aio_req allocation");
386 455
387 New (0, req->statdata, 1, struct stat64); 456 New (0, req->statdata, 1, Stat_t);
388 457
389 if (!req->statdata) 458 if (!req->statdata)
390 croak ("out of memory during aio_req->statdata allocation"); 459 croak ("out of memory during aio_req->statdata allocation");
391 460
392 if (SvPOK (fh_or_path)) 461 if (SvPOK (fh_or_path))
393 { 462 {
394 req->type = ix ? REQ_LSTAT : REQ_STAT; 463 req->type = ix ? REQ_LSTAT : REQ_STAT;
395 req->savesv = newSVsv (fh_or_path); 464 req->data = newSVsv (fh_or_path);
396 req->dataptr = SvPV_nolen (req->savesv); 465 req->dataptr = SvPV_nolen (req->data);
397 } 466 }
398 else 467 else
399 { 468 {
400 req->type = REQ_FSTAT; 469 req->type = REQ_FSTAT;
401 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); 470 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
403 472
404 req->callback = SvREFCNT_inc (callback); 473 req->callback = SvREFCNT_inc (callback);
405 474
406 send_req (req); 475 send_req (req);
407 476
477void
478aio_unlink(pathname,callback)
479 SV * pathname
480 SV * callback
481 PROTOTYPE: $$
482 CODE:
483 aio_req req;
484
485 Newz (0, req, 1, aio_cb);
486
487 if (!req)
488 croak ("out of memory during aio_req allocation");
489
490 req->type = REQ_UNLINK;
491 req->data = newSVsv (pathname);
492 req->dataptr = SvPV_nolen (req->data);
493 req->callback = SvREFCNT_inc (callback);
494
495 send_req (req);
496
408int 497int
409poll_fileno() 498poll_fileno()
410 PROTOTYPE: 499 PROTOTYPE:
411 CODE: 500 CODE:
412 RETVAL = respipe[0]; 501 RETVAL = respipe[0];

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines