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.14 by root, Tue Apr 2 14:01:09 2002 UTC vs.
Revision 1.22 by root, Wed Jun 29 15:22:36 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 35
28 int fd; 36 int fd;
29 off_t offset; 37 off_t offset;
33 int errorno; 41 int errorno;
34 SV *data, *callback; 42 SV *data, *callback;
35 void *dataptr; 43 void *dataptr;
36 STRLEN dataoffset; 44 STRLEN dataoffset;
37 45
38 struct stat64 *statdata; 46 Stat_t *statdata;
39} aio_cb; 47} aio_cb;
40 48
41typedef aio_cb *aio_req; 49typedef aio_cb *aio_req;
42 50
43static int started; 51static int started;
44static int nreqs; 52static int nreqs;
45static int reqpipe[2], respipe[2]; 53static int reqpipe[2], respipe[2];
46 54
55static aio_req qs, qe; /* queue start, queue end */
56
47static int aio_proc(void *arg); 57static int aio_proc(void *arg);
48 58
49static void 59static void
50start_thread(void) 60start_thread (void)
51{ 61{
52 aio_thread *thr; 62 aio_thread *thr;
53 63
54 New (0, thr, 1, aio_thread); 64 New (0, thr, 1, aio_thread);
55 65
56 if (clone (aio_proc, 66 if (clone (aio_proc,
57 &(thr->stack[STACKSIZE]), 67 &(thr->stack[STACKSIZE - sizeof (long)]),
58 CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND, 68 CLONE_VM|CLONE_FS|CLONE_FILES,
59 thr) >= 0) 69 thr) >= 0)
60 started++; 70 started++;
61 else 71 else
62 Safefree (thr); 72 Safefree (thr);
63} 73}
64 74
65static 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
66end_thread(void) 104end_thread (void)
67{ 105{
68 aio_req req; 106 aio_req req;
69 New (0, req, 1, aio_cb); 107 New (0, req, 1, aio_cb);
70 req->type = REQ_QUIT; 108 req->type = REQ_QUIT;
71 write (reqpipe[1], &req, sizeof (aio_req)); 109
110 send_req (req);
72} 111}
73 112
74static void 113static void
75send_req (aio_req req) 114read_write (pTHX_
76{
77 nreqs++;
78 write (reqpipe[1], &req, sizeof (aio_req));
79}
80
81static void
82read_write (pTHX_ int dowrite, int fd, off_t offset, size_t length, 115 int dowrite, int fd, off_t offset, size_t length,
83 SV *data, STRLEN dataoffset, SV*callback) 116 SV *data, STRLEN dataoffset, SV *callback)
84{ 117{
85 aio_req req; 118 aio_req req;
86 STRLEN svlen; 119 STRLEN svlen;
87 char *svptr = SvPV (data, svlen); 120 char *svptr = SvPV (data, svlen);
88 121
124 req->callback = SvREFCNT_inc (callback); 157 req->callback = SvREFCNT_inc (callback);
125 158
126 send_req (req); 159 send_req (req);
127} 160}
128 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
129static int 172static int
130poll_cb (pTHX) 173poll_cb (pTHX)
131{ 174{
132 dSP; 175 dSP;
133 int count = 0; 176 int count = 0;
134 aio_req req; 177 aio_req req;
135 178
136 while (read (respipe[0], (void *)&req, sizeof (req)) == sizeof (req)) 179 while (read (respipe[0], (void *)&req, sizeof (req)) == sizeof (req))
137 { 180 {
181 nreqs--;
182
138 if (req->type == REQ_QUIT) 183 if (req->type == REQ_QUIT)
139 { 184 {
140 Safefree (req->thread); 185 Safefree (req->thread);
141 started--; 186 started--;
142 } 187 }
152 if (req->data) 197 if (req->data)
153 SvREFCNT_dec (req->data); 198 SvREFCNT_dec (req->data);
154 199
155 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)
156 { 201 {
157 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; 202 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
158 PL_laststatval = req->result; 203 PL_laststatval = req->result;
159 PL_statcache.st_dev = req->statdata->st_dev; 204 PL_statcache = *(req->statdata);
160 PL_statcache.st_ino = req->statdata->st_ino;
161 PL_statcache.st_mode = req->statdata->st_mode;
162 PL_statcache.st_nlink = req->statdata->st_nlink;
163 PL_statcache.st_uid = req->statdata->st_uid;
164 PL_statcache.st_gid = req->statdata->st_gid;
165 PL_statcache.st_rdev = req->statdata->st_rdev;
166 PL_statcache.st_size = req->statdata->st_size;
167 PL_statcache.st_atime = req->statdata->st_atime;
168 PL_statcache.st_mtime = req->statdata->st_mtime;
169 PL_statcache.st_ctime = req->statdata->st_ctime;
170 PL_statcache.st_blksize = req->statdata->st_blksize;
171 PL_statcache.st_blocks = req->statdata->st_blocks;
172 205
173 Safefree (req->statdata); 206 Safefree (req->statdata);
174 } 207 }
175 208
176 PUSHMARK (SP); 209 PUSHMARK (SP);
181 214
182 if (req->callback) 215 if (req->callback)
183 SvREFCNT_dec (req->callback); 216 SvREFCNT_dec (req->callback);
184 217
185 errno = errorno; 218 errno = errorno;
186 nreqs--;
187 count++; 219 count++;
188 } 220 }
189 221
190 Safefree (req); 222 Safefree (req);
191 } 223 }
192 224
225 if (qs)
226 send_reqs ();
227
193 return count; 228 return count;
194} 229}
195 230
196static sigset_t fullsigset; 231static sigset_t fullsigset;
197 232
198#undef errno 233#undef errno
199#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; \
200 251
201static int 252static int
202aio_proc(void *thr_arg) 253aio_proc (void *thr_arg)
203{ 254{
204 aio_thread *thr = thr_arg; 255 aio_thread *thr = thr_arg;
205 aio_req req; 256 aio_req req;
206 int errno; 257 int errno;
207 258
259 /* this is very much kernel-specific :(:(:( */
208 /* we rely on gcc's ability to create closures. */ 260 /* we rely on gcc's ability to create closures. */
209 _syscall3(int,read,int,fd,char *,buf,size_t,count) 261 _syscall3(int,read,int,fd,char *,buf,size_t,count)
210 _syscall3(int,write,int,fd,char *,buf,size_t,count) 262 _syscall3(int,write,int,fd,char *,buf,size_t,count)
211 263
212 _syscall3(int,open,char *,pathname,int,flags,mode_t,mode) 264 _syscall3(int,open,char *,pathname,int,flags,mode_t,mode)
213 _syscall1(int,close,int,fd) 265 _syscall1(int,close,int,fd)
214 266
267#ifdef __NR_pread64
268 _syscall5(int,pread64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi)
269 _syscall5(int,pwrite64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi)
270#elif __NR_pread
215 _syscall4(int,pread,int,fd,char *,buf,size_t,count,off_t,offset) 271 _syscall4(int,pread,int,fd,char *,buf,size_t,count,offset_t,offset)
216 _syscall4(int,pwrite,int,fd,char *,buf,size_t,count,off_t,offset) 272 _syscall4(int,pwrite,int,fd,char *,buf,size_t,count,offset_t,offset)
273#else
274# error "neither pread nor pread64 defined"
275#endif
217 276
277
278#ifdef __NR_stat64
218 _syscall2(int,stat64, const char *, filename, struct stat64 *, buf) 279 _syscall2(int,stat64, const char *, filename, struct stat64 *, buf)
219 _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf) 280 _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf)
220 _syscall2(int,fstat64, int, fd, struct stat64 *, buf) 281 _syscall2(int,fstat64, int, fd, struct stat64 *, buf)
282#elif __NR_stat
283 _syscall2(int,stat, const char *, filename, struct stat *, buf)
284 _syscall2(int,lstat, const char *, filename, struct stat *, buf)
285 _syscall2(int,fstat, int, fd, struct stat *, buf)
286#else
287# error "neither stat64 nor stat defined"
288#endif
289
290 _syscall1(int,unlink, char *, filename);
221 291
222 sigprocmask (SIG_SETMASK, &fullsigset, 0); 292 sigprocmask (SIG_SETMASK, &fullsigset, 0);
293 prctl (PR_SET_PDEATHSIG, SIGKILL);
223 294
224 /* then loop */ 295 /* then loop */
225 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) 296 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req))
226 { 297 {
227 req->thread = thr; 298 req->thread = thr;
228 errno = 0; /* strictly unnecessary */ 299 errno = 0; /* strictly unnecessary */
229 300
230 if (req->type == REQ_READ) 301 switch (req->type)
231 req->result = pread (req->fd, req->dataptr, req->length, req->offset);
232 else if (req->type == REQ_WRITE)
233 req->result = pwrite (req->fd, req->dataptr, req->length, req->offset);
234 else if (req->type == REQ_OPEN)
235 req->result = open (req->dataptr, req->fd, req->mode);
236 else if (req->type == REQ_CLOSE)
237 req->result = close (req->fd);
238 else if (req->type == REQ_STAT)
239 req->result = stat64 (req->dataptr, req->statdata);
240 else if (req->type == REQ_LSTAT)
241 req->result = lstat64 (req->dataptr, req->statdata);
242 else if (req->type == REQ_FSTAT)
243 req->result = fstat64 (req->fd, req->statdata);
244 else
245 { 302 {
303#ifdef __NR_pread64
304 case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); break;
305 case REQ_WRITE: req->result = pwrite64(req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); break;
306#else
307 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break;
308 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break;
309#endif
310#ifdef __NR_stat64
311 struct stat64 statdata;
312 case REQ_STAT: req->result = stat64 (req->dataptr, &statdata); COPY_STATDATA; break;
313 case REQ_LSTAT: req->result = lstat64 (req->dataptr, &statdata); COPY_STATDATA; break;
314 case REQ_FSTAT: req->result = fstat64 (req->fd, &statdata); COPY_STATDATA; break;
315#else
316 struct stat statdata;
317 case REQ_STAT: req->result = stat (req->dataptr, &statdata); COPY_STATDATA; break;
318 case REQ_LSTAT: req->result = lstat (req->dataptr, &statdata); COPY_STATDATA; break;
319 case REQ_FSTAT: req->result = fstat (req->fd, &statdata); COPY_STATDATA; break;
320#endif
321 case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break;
322 case REQ_CLOSE: req->result = close (req->fd); break;
323 case REQ_UNLINK: req->result = unlink (req->dataptr); break;
324
325 case REQ_QUIT:
326 default:
246 write (respipe[1], (void *)&req, sizeof (req)); 327 write (respipe[1], (void *)&req, sizeof (req));
247 break; 328 return 0;
248 } 329 }
249 330
250 req->errorno = errno; 331 req->errorno = errno;
251 write (respipe[1], (void *)&req, sizeof (req)); 332 write (respipe[1], (void *)&req, sizeof (req));
252 } 333 }
264 sigdelset (&fullsigset, SIGABRT); 345 sigdelset (&fullsigset, SIGABRT);
265 sigdelset (&fullsigset, SIGINT); 346 sigdelset (&fullsigset, SIGINT);
266 347
267 if (pipe (reqpipe) || pipe (respipe)) 348 if (pipe (reqpipe) || pipe (respipe))
268 croak ("unable to initialize request or result pipe"); 349 croak ("unable to initialize request or result pipe");
350
351 if (fcntl (reqpipe[1], F_SETFL, O_NONBLOCK))
352 croak ("cannot set result pipe to nonblocking mode");
269 353
270 if (fcntl (respipe[0], F_SETFL, O_NONBLOCK)) 354 if (fcntl (respipe[0], F_SETFL, O_NONBLOCK))
271 croak ("cannot set result pipe to nonblocking mode"); 355 croak ("cannot set result pipe to nonblocking mode");
272} 356}
273 357
291 cur--; 375 cur--;
292 } 376 }
293 377
294 while (started > nthreads) 378 while (started > nthreads)
295 { 379 {
296 fd_set rfd; 380 poll_wait ();
297 FD_ZERO(&rfd);
298 FD_SET(respipe[0], &rfd);
299
300 select (respipe[0] + 1, &rfd, 0, 0, 0);
301 poll_cb (); 381 poll_cb (aTHX);
302 } 382 }
303 383
304void 384void
305aio_open(pathname,flags,mode,callback) 385aio_open(pathname,flags,mode,callback)
306 SV * pathname 386 SV * pathname
381 Newz (0, req, 1, aio_cb); 461 Newz (0, req, 1, aio_cb);
382 462
383 if (!req) 463 if (!req)
384 croak ("out of memory during aio_req allocation"); 464 croak ("out of memory during aio_req allocation");
385 465
386 New (0, req->statdata, 1, struct stat64); 466 New (0, req->statdata, 1, Stat_t);
387 467
388 if (!req->statdata) 468 if (!req->statdata)
389 croak ("out of memory during aio_req->statdata allocation"); 469 croak ("out of memory during aio_req->statdata allocation");
390 470
391 if (SvPOK (fh_or_path)) 471 if (SvPOK (fh_or_path))
402 482
403 req->callback = SvREFCNT_inc (callback); 483 req->callback = SvREFCNT_inc (callback);
404 484
405 send_req (req); 485 send_req (req);
406 486
487void
488aio_unlink(pathname,callback)
489 SV * pathname
490 SV * callback
491 PROTOTYPE: $$
492 CODE:
493 aio_req req;
494
495 Newz (0, req, 1, aio_cb);
496
497 if (!req)
498 croak ("out of memory during aio_req allocation");
499
500 req->type = REQ_UNLINK;
501 req->data = newSVsv (pathname);
502 req->dataptr = SvPV_nolen (req->data);
503 req->callback = SvREFCNT_inc (callback);
504
505 send_req (req);
506
407int 507int
408poll_fileno() 508poll_fileno()
409 PROTOTYPE: 509 PROTOTYPE:
410 CODE: 510 CODE:
411 RETVAL = respipe[0]; 511 RETVAL = respipe[0];

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines