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.24 by root, Sat Jul 2 13:16:33 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>
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 (128 * sizeof (long)) /* 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 - sizeof (long)]),
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 {
94 qe->next = req;
95 qe = req;
96 }
97 else
98 qe = qs = req;
99
100 send_reqs ();
101}
102
103static void
68end_thread(void) 104end_thread (void)
69{ 105{
70 aio_req req; 106 aio_req req;
71 New (0, req, 1, aio_cb); 107 New (0, req, 1, aio_cb);
72 req->type = REQ_QUIT; 108 req->type = REQ_QUIT;
73 write (reqpipe[1], &req, sizeof (aio_req)); 109
110 send_req (req);
74} 111}
75 112
76static void 113static void
77send_req (aio_req req) 114read_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, 115 int dowrite, int fd, off_t offset, size_t length,
85 SV *data, STRLEN dataoffset, SV*callback) 116 SV *data, STRLEN dataoffset, SV *callback)
86{ 117{
87 aio_req req; 118 aio_req req;
88 STRLEN svlen; 119 STRLEN svlen;
89 char *svptr = SvPV (data, svlen); 120 char *svptr = SvPV (data, svlen);
90 121
110 } 141 }
111 142
112 if (length < 0) 143 if (length < 0)
113 croak ("length must not be negative"); 144 croak ("length must not be negative");
114 145
115 New (0, req, 1, aio_cb); 146 Newz (0, req, 1, aio_cb);
116 147
117 if (!req) 148 if (!req)
118 croak ("out of memory during aio_req allocation"); 149 croak ("out of memory during aio_req allocation");
119 150
120 req->type = dowrite ? REQ_WRITE : REQ_READ; 151 req->type = dowrite ? REQ_WRITE : REQ_READ;
126 req->callback = SvREFCNT_inc (callback); 157 req->callback = SvREFCNT_inc (callback);
127 158
128 send_req (req); 159 send_req (req);
129} 160}
130 161
162static void
163poll_wait ()
164{
165 fd_set rfd;
166 FD_ZERO(&rfd);
167 FD_SET(respipe[0], &rfd);
168
169 select (respipe[0] + 1, &rfd, 0, 0, 0);
170}
171
131static int 172static int
132poll_cb (pTHX) 173poll_cb (pTHX)
133{ 174{
134 dSP; 175 dSP;
135 int count = 0; 176 int count = 0;
136 aio_req req; 177 aio_req req;
137 178
138 while (read (respipe[0], (void *)&req, sizeof (req)) == sizeof (req)) 179 while (read (respipe[0], (void *)&req, sizeof (req)) == sizeof (req))
139 { 180 {
181 nreqs--;
182
140 if (req->type == REQ_QUIT) 183 if (req->type == REQ_QUIT)
141 { 184 {
142 Safefree (req->thread); 185 Safefree (req->thread);
143 started--; 186 started--;
144 } 187 }
145 else 188 else
146 { 189 {
147 int errorno = errno; 190 int errorno = errno;
148 errno = req->errorno; 191 errno = req->errorno;
149 192
150 if (req->savesv)
151 SvREFCNT_dec (req->savesv);
152
153 if (req->type == REQ_READ) 193 if (req->type == REQ_READ)
154 SvCUR_set (req->data, req->dataoffset 194 SvCUR_set (req->data, req->dataoffset
155 + req->result > 0 ? req->result : 0); 195 + req->result > 0 ? req->result : 0);
156 196
197 if (req->data)
198 SvREFCNT_dec (req->data);
199
157 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) 200 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT)
158 { 201 {
159 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; 202 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
160 PL_laststatval = req->result; 203 PL_laststatval = req->result;
161 PL_statcache.st_dev = req->statdata->st_dev; 204 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 205
175 Safefree (req->statdata); 206 Safefree (req->statdata);
176 } 207 }
177 208
178 PUSHMARK (SP); 209 PUSHMARK (SP);
179 XPUSHs (sv_2mortal (newSViv (req->result))); 210 XPUSHs (sv_2mortal (newSViv (req->result)));
180 PUTBACK; 211 PUTBACK;
181 call_sv (req->callback, G_VOID); 212 call_sv (req->callback, G_VOID);
182 SPAGAIN; 213 SPAGAIN;
183 214
184 SvREFCNT_dec (req->data); 215 if (req->callback)
185 SvREFCNT_dec (req->callback); 216 SvREFCNT_dec (req->callback);
186 217
187 errno = errorno; 218 errno = errorno;
188 nreqs--;
189 count++; 219 count++;
190 } 220 }
191 221
192 Safefree (req); 222 Safefree (req);
193 } 223 }
194 224
225 if (qs)
226 send_reqs ();
227
195 return count; 228 return count;
196} 229}
197 230
198static sigset_t fullsigset; 231static sigset_t fullsigset;
199 232
200#undef errno 233#undef errno
201#include <asm/unistd.h> 234#include <asm/unistd.h>
235#include <sys/prctl.h>
236
237#define COPY_STATDATA \
238 req->statdata->st_dev = statdata.st_dev; \
239 req->statdata->st_ino = statdata.st_ino; \
240 req->statdata->st_mode = statdata.st_mode; \
241 req->statdata->st_nlink = statdata.st_nlink; \
242 req->statdata->st_uid = statdata.st_uid; \
243 req->statdata->st_gid = statdata.st_gid; \
244 req->statdata->st_rdev = statdata.st_rdev; \
245 req->statdata->st_size = statdata.st_size; \
246 req->statdata->st_atime = statdata.st_atime; \
247 req->statdata->st_mtime = statdata.st_mtime; \
248 req->statdata->st_ctime = statdata.st_ctime; \
249 req->statdata->st_blksize = statdata.st_blksize; \
250 req->statdata->st_blocks = statdata.st_blocks; \
202 251
203static int 252static int
204aio_proc(void *thr_arg) 253aio_proc (void *thr_arg)
205{ 254{
206 aio_thread *thr = thr_arg; 255 aio_thread *thr = thr_arg;
207 aio_req req; 256 aio_req req;
208 int errno; 257 int errno;
209 258
259 /* this is very much kernel-specific :(:(:( */
210 /* we rely on gcc's ability to create closures. */ 260 /* we rely on gcc's ability to create closures. */
211 _syscall3(int,read,int,fd,char *,buf,size_t,count) 261 _syscall3(int,read,int,fd,char *,buf,size_t,count)
212 _syscall3(int,write,int,fd,char *,buf,size_t,count) 262 _syscall3(int,write,int,fd,char *,buf,size_t,count)
213 263
214 _syscall3(int,open,char *,pathname,int,flags,mode_t,mode) 264 _syscall3(int,open,char *,pathname,int,flags,mode_t,mode)
215 _syscall1(int,close,int,fd) 265 _syscall1(int,close,int,fd)
216 266
267#define arch64 (__ia64 || __alpha)
268
269#ifdef __NR_pread64 && !arch64
270 _syscall5(int,pread64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi)
271 _syscall5(int,pwrite64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi)
272#elif __NR_pread
217 _syscall4(int,pread,int,fd,char *,buf,size_t,count,off_t,offset) 273 _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) 274 _syscall4(int,pwrite,int,fd,char *,buf,size_t,count,offset_t,offset)
275#else
276# error "neither pread nor pread64 defined"
277#endif
219 278
279
280#ifdef __NR_stat64 && !arch64
220 _syscall2(int,stat64, const char *, filename, struct stat64 *, buf) 281 _syscall2(int,stat64, const char *, filename, struct stat64 *, buf)
221 _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf) 282 _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf)
222 _syscall2(int,fstat64, int, fd, struct stat64 *, buf) 283 _syscall2(int,fstat64, int, fd, struct stat64 *, buf)
284#elif __NR_stat
285 _syscall2(int,stat, const char *, filename, struct stat *, buf)
286 _syscall2(int,lstat, const char *, filename, struct stat *, buf)
287 _syscall2(int,fstat, int, fd, struct stat *, buf)
288#else
289# error "neither stat64 nor stat defined"
290#endif
291
292 _syscall1(int,unlink, char *, filename);
223 293
224 sigprocmask (SIG_SETMASK, &fullsigset, 0); 294 sigprocmask (SIG_SETMASK, &fullsigset, 0);
295 prctl (PR_SET_PDEATHSIG, SIGKILL);
225 296
226 /* then loop */ 297 /* then loop */
227 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) 298 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req))
228 { 299 {
229 req->thread = thr; 300 req->thread = thr;
230 errno = 0; 301 errno = 0; /* strictly unnecessary */
231 302
232 if (req->type == REQ_READ) 303 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 { 304 {
305#ifdef __NR_pread64
306 case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); break;
307 case REQ_WRITE: req->result = pwrite64(req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); break;
308#else
309 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break;
310 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break;
311#endif
312#ifdef __NR_stat64
313 struct stat64 statdata;
314 case REQ_STAT: req->result = stat64 (req->dataptr, &statdata); COPY_STATDATA; break;
315 case REQ_LSTAT: req->result = lstat64 (req->dataptr, &statdata); COPY_STATDATA; break;
316 case REQ_FSTAT: req->result = fstat64 (req->fd, &statdata); COPY_STATDATA; break;
317#else
318 struct stat statdata;
319 case REQ_STAT: req->result = stat (req->dataptr, &statdata); COPY_STATDATA; break;
320 case REQ_LSTAT: req->result = lstat (req->dataptr, &statdata); COPY_STATDATA; break;
321 case REQ_FSTAT: req->result = fstat (req->fd, &statdata); COPY_STATDATA; break;
322#endif
323 case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break;
324 case REQ_CLOSE: req->result = close (req->fd); break;
325 case REQ_UNLINK: req->result = unlink (req->dataptr); break;
326
327 case REQ_QUIT:
328 default:
248 write (respipe[1], (void *)&req, sizeof (req)); 329 write (respipe[1], (void *)&req, sizeof (req));
249 break; 330 return 0;
250 } 331 }
251 332
252 req->errorno = errno; 333 req->errorno = errno;
253 write (respipe[1], (void *)&req, sizeof (req)); 334 write (respipe[1], (void *)&req, sizeof (req));
254 } 335 }
266 sigdelset (&fullsigset, SIGABRT); 347 sigdelset (&fullsigset, SIGABRT);
267 sigdelset (&fullsigset, SIGINT); 348 sigdelset (&fullsigset, SIGINT);
268 349
269 if (pipe (reqpipe) || pipe (respipe)) 350 if (pipe (reqpipe) || pipe (respipe))
270 croak ("unable to initialize request or result pipe"); 351 croak ("unable to initialize request or result pipe");
352
353 if (fcntl (reqpipe[1], F_SETFL, O_NONBLOCK))
354 croak ("cannot set result pipe to nonblocking mode");
271 355
272 if (fcntl (respipe[0], F_SETFL, O_NONBLOCK)) 356 if (fcntl (respipe[0], F_SETFL, O_NONBLOCK))
273 croak ("cannot set result pipe to nonblocking mode"); 357 croak ("cannot set result pipe to nonblocking mode");
274} 358}
275 359
291 { 375 {
292 end_thread (); 376 end_thread ();
293 cur--; 377 cur--;
294 } 378 }
295 379
296 poll_cb ();
297 while (started > nthreads) 380 while (started > nthreads)
298 { 381 {
299 sched_yield (); 382 poll_wait ();
300 fcntl (respipe[0], F_SETFL, 0);
301 poll_cb (); 383 poll_cb (aTHX);
302 fcntl (respipe[0], F_SETFL, O_NONBLOCK);
303 } 384 }
304 385
305void 386void
306aio_open(pathname,flags,mode,callback) 387aio_open(pathname,flags,mode,callback)
307 SV * pathname 388 SV * pathname
310 SV * callback 391 SV * callback
311 PROTOTYPE: $$$$ 392 PROTOTYPE: $$$$
312 CODE: 393 CODE:
313 aio_req req; 394 aio_req req;
314 395
315 New (0, req, 1, aio_cb); 396 Newz (0, req, 1, aio_cb);
316 397
317 if (!req) 398 if (!req)
318 croak ("out of memory during aio_req allocation"); 399 croak ("out of memory during aio_req allocation");
319 400
320 req->type = REQ_OPEN; 401 req->type = REQ_OPEN;
321 req->savesv = newSVsv (pathname); 402 req->data = newSVsv (pathname);
322 req->dataptr = SvPV_nolen (req->savesv); 403 req->dataptr = SvPV_nolen (req->data);
323 req->fd = flags; 404 req->fd = flags;
324 req->mode = mode; 405 req->mode = mode;
325 req->callback = SvREFCNT_inc (callback); 406 req->callback = SvREFCNT_inc (callback);
326 407
327 send_req (req); 408 send_req (req);
328 409
329void 410void
330aio_close(fh,callback) 411aio_close(fh,callback)
331 InputStream fh 412 InputStream fh
332 SV * callback 413 SV * callback
333 PROTOTYPE: $ 414 PROTOTYPE: $$
334 CODE: 415 CODE:
335 aio_req req; 416 aio_req req;
336 417
337 New (0, req, 1, aio_cb); 418 Newz (0, req, 1, aio_cb);
338 419
339 if (!req) 420 if (!req)
340 croak ("out of memory during aio_req allocation"); 421 croak ("out of memory during aio_req allocation");
341 422
342 req->type = REQ_CLOSE; 423 req->type = REQ_CLOSE;
377 ALIAS: 458 ALIAS:
378 aio_lstat = 1 459 aio_lstat = 1
379 CODE: 460 CODE:
380 aio_req req; 461 aio_req req;
381 462
382 New (0, req, 1, aio_cb); 463 Newz (0, req, 1, aio_cb);
383 464
384 if (!req) 465 if (!req)
385 croak ("out of memory during aio_req allocation"); 466 croak ("out of memory during aio_req allocation");
386 467
387 New (0, req->statdata, 1, struct stat64); 468 New (0, req->statdata, 1, Stat_t);
388 469
389 if (!req->statdata) 470 if (!req->statdata)
390 croak ("out of memory during aio_req->statdata allocation"); 471 croak ("out of memory during aio_req->statdata allocation");
391 472
392 if (SvPOK (fh_or_path)) 473 if (SvPOK (fh_or_path))
393 { 474 {
394 req->type = ix ? REQ_LSTAT : REQ_STAT; 475 req->type = ix ? REQ_LSTAT : REQ_STAT;
395 req->savesv = newSVsv (fh_or_path); 476 req->data = newSVsv (fh_or_path);
396 req->dataptr = SvPV_nolen (req->savesv); 477 req->dataptr = SvPV_nolen (req->data);
397 } 478 }
398 else 479 else
399 { 480 {
400 req->type = REQ_FSTAT; 481 req->type = REQ_FSTAT;
401 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); 482 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
403 484
404 req->callback = SvREFCNT_inc (callback); 485 req->callback = SvREFCNT_inc (callback);
405 486
406 send_req (req); 487 send_req (req);
407 488
489void
490aio_unlink(pathname,callback)
491 SV * pathname
492 SV * callback
493 PROTOTYPE: $$
494 CODE:
495 aio_req req;
496
497 Newz (0, req, 1, aio_cb);
498
499 if (!req)
500 croak ("out of memory during aio_req allocation");
501
502 req->type = REQ_UNLINK;
503 req->data = newSVsv (pathname);
504 req->dataptr = SvPV_nolen (req->data);
505 req->callback = SvREFCNT_inc (callback);
506
507 send_req (req);
508
408int 509int
409poll_fileno() 510poll_fileno()
410 PROTOTYPE: 511 PROTOTYPE:
411 CODE: 512 CODE:
412 RETVAL = respipe[0]; 513 RETVAL = respipe[0];
419 CODE: 520 CODE:
420 RETVAL = poll_cb (aTHX); 521 RETVAL = poll_cb (aTHX);
421 OUTPUT: 522 OUTPUT:
422 RETVAL 523 RETVAL
423 524
525void
526poll_wait()
527 PROTOTYPE:
528 CODE:
529 poll_wait ();
530
424int 531int
425nreqs() 532nreqs()
426 PROTOTYPE: 533 PROTOTYPE:
427 CODE: 534 CODE:
428 RETVAL = nreqs; 535 RETVAL = nreqs;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines