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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines