|
|
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> |
|
|
11 | #include <signal.h> |
8 | #include <sched.h> |
12 | #include <sched.h> |
9 | |
13 | |
|
|
14 | typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
|
|
15 | typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
|
|
16 | typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
|
|
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 | |
10 | #define STACKSIZE 2048 /* yeah */ |
25 | #define STACKSIZE 1024 /* yeah */ |
11 | |
26 | |
12 | #define REQ_QUIT 0 |
27 | enum { REQ_QUIT, REQ_OPEN, REQ_CLOSE, REQ_READ, REQ_WRITE, REQ_STAT, REQ_LSTAT, REQ_FSTAT}; |
13 | #define REQ_READ 1 |
|
|
14 | #define REQ_WRITE 2 |
|
|
15 | |
28 | |
16 | typedef struct { |
29 | typedef struct { |
17 | char stack[STACKSIZE]; |
30 | char stack[STACKSIZE]; |
18 | } aio_thread; |
31 | } aio_thread; |
19 | |
32 | |
20 | typedef struct { |
33 | typedef struct aio_cb { |
|
|
34 | struct aio_cb *next; |
|
|
35 | |
21 | int type; |
36 | int type; |
22 | aio_thread *thread; |
37 | aio_thread *thread; |
23 | |
38 | |
24 | /* read/write */ |
|
|
25 | int fd; |
39 | int fd; |
26 | off_t offset; |
40 | off_t offset; |
27 | size_t length; |
41 | size_t length; |
28 | ssize_t result; |
42 | ssize_t result; |
|
|
43 | mode_t mode; /* open */ |
29 | int errorno; |
44 | int errorno; |
30 | |
|
|
31 | SV *data, *callback; |
45 | SV *data, *callback; |
32 | void *dataptr; |
46 | void *dataptr; |
33 | STRLEN dataoffset; |
47 | STRLEN dataoffset; |
|
|
48 | |
|
|
49 | struct stat64 *statdata; |
34 | } aio_cb; |
50 | } aio_cb; |
35 | |
51 | |
36 | typedef aio_cb *aio_req; |
52 | typedef aio_cb *aio_req; |
37 | |
53 | |
38 | static int started; |
54 | static int started; |
39 | static int nreqs; |
55 | static int nreqs; |
40 | static int reqpipe[2], respipe[2]; |
56 | static int reqpipe[2], respipe[2]; |
41 | |
57 | |
|
|
58 | static aio_req qs, qe; /* queue start, queue end */ |
|
|
59 | |
42 | static int aio_proc(void *arg); |
60 | static int aio_proc(void *arg); |
43 | |
61 | |
44 | static void |
62 | static void |
45 | start_thread(void) |
63 | start_thread (void) |
46 | { |
64 | { |
47 | aio_thread *thr; |
65 | aio_thread *thr; |
48 | |
66 | |
49 | New (0, thr, 1, aio_thread); |
67 | New (0, thr, 1, aio_thread); |
50 | |
68 | |
… | |
… | |
56 | else |
74 | else |
57 | Safefree (thr); |
75 | Safefree (thr); |
58 | } |
76 | } |
59 | |
77 | |
60 | static void |
78 | static void |
|
|
79 | send_reqs (void) |
|
|
80 | { |
|
|
81 | /* this write is atomic */ |
|
|
82 | while (qs && write (reqpipe[1], &qs, sizeof qs) == sizeof qs) |
|
|
83 | { |
|
|
84 | qs = qs->next; |
|
|
85 | if (!qs) qe = 0; |
|
|
86 | } |
|
|
87 | } |
|
|
88 | |
|
|
89 | static void |
|
|
90 | send_req (aio_req req) |
|
|
91 | { |
|
|
92 | nreqs++; |
|
|
93 | req->next = 0; |
|
|
94 | |
|
|
95 | if (qe) |
|
|
96 | qe->next = req; |
|
|
97 | else |
|
|
98 | qe = qs = req; |
|
|
99 | |
|
|
100 | send_reqs (); |
|
|
101 | } |
|
|
102 | |
|
|
103 | static void |
61 | end_thread(void) |
104 | end_thread (void) |
62 | { |
105 | { |
63 | aio_req req; |
106 | aio_req req; |
64 | New (0, req, 1, aio_cb); |
107 | New (0, req, 1, aio_cb); |
65 | req->type = REQ_QUIT; |
108 | req->type = REQ_QUIT; |
66 | write (reqpipe[1], &req, sizeof (aio_req)); |
109 | |
|
|
110 | send_req (req); |
67 | } |
111 | } |
68 | |
112 | |
69 | static void |
113 | static void |
|
|
114 | read_write (pTHX_ |
70 | read_write (pTHX_ int dowrite, int fd, off_t offset, size_t length, |
115 | int dowrite, int fd, off_t offset, size_t length, |
71 | SV *data, STRLEN dataoffset, SV*callback) |
116 | SV *data, STRLEN dataoffset, SV *callback) |
72 | { |
117 | { |
73 | aio_req req; |
118 | aio_req req; |
74 | STRLEN svlen; |
119 | STRLEN svlen; |
75 | char *svptr = SvPV (data, svlen); |
120 | char *svptr = SvPV (data, svlen); |
|
|
121 | |
|
|
122 | SvUPGRADE (data, SVt_PV); |
|
|
123 | SvPOK_on (data); |
76 | |
124 | |
77 | if (dataoffset < 0) |
125 | if (dataoffset < 0) |
78 | dataoffset += svlen; |
126 | dataoffset += svlen; |
79 | |
127 | |
80 | if (dataoffset < 0 || dataoffset > svlen) |
128 | if (dataoffset < 0 || dataoffset > svlen) |
… | |
… | |
93 | } |
141 | } |
94 | |
142 | |
95 | if (length < 0) |
143 | if (length < 0) |
96 | croak ("length must not be negative"); |
144 | croak ("length must not be negative"); |
97 | |
145 | |
98 | New (0, req, 1, aio_cb); |
146 | Newz (0, req, 1, aio_cb); |
99 | |
147 | |
100 | if (!req) |
148 | if (!req) |
101 | croak ("out of memory during aio_req allocation"); |
149 | croak ("out of memory during aio_req allocation"); |
102 | |
150 | |
103 | req->type = dowrite ? REQ_WRITE : REQ_READ; |
151 | req->type = dowrite ? REQ_WRITE : REQ_READ; |
… | |
… | |
106 | req->length = length; |
154 | req->length = length; |
107 | req->data = SvREFCNT_inc (data); |
155 | req->data = SvREFCNT_inc (data); |
108 | req->dataptr = (char *)svptr + dataoffset; |
156 | req->dataptr = (char *)svptr + dataoffset; |
109 | req->callback = SvREFCNT_inc (callback); |
157 | req->callback = SvREFCNT_inc (callback); |
110 | |
158 | |
111 | nreqs++; |
159 | send_req (req); |
112 | write (reqpipe[1], &req, sizeof (aio_req)); |
|
|
113 | } |
160 | } |
114 | |
161 | |
115 | static int |
162 | static int |
116 | poll_cb (pTHX) |
163 | poll_cb (pTHX) |
117 | { |
164 | { |
… | |
… | |
133 | |
180 | |
134 | if (req->type == REQ_READ) |
181 | if (req->type == REQ_READ) |
135 | SvCUR_set (req->data, req->dataoffset |
182 | SvCUR_set (req->data, req->dataoffset |
136 | + req->result > 0 ? req->result : 0); |
183 | + req->result > 0 ? req->result : 0); |
137 | |
184 | |
|
|
185 | if (req->data) |
|
|
186 | SvREFCNT_dec (req->data); |
|
|
187 | |
|
|
188 | if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) |
|
|
189 | { |
|
|
190 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
|
|
191 | PL_laststatval = req->result; |
|
|
192 | PL_statcache.st_dev = req->statdata->st_dev; |
|
|
193 | PL_statcache.st_ino = req->statdata->st_ino; |
|
|
194 | PL_statcache.st_mode = req->statdata->st_mode; |
|
|
195 | PL_statcache.st_nlink = req->statdata->st_nlink; |
|
|
196 | PL_statcache.st_uid = req->statdata->st_uid; |
|
|
197 | PL_statcache.st_gid = req->statdata->st_gid; |
|
|
198 | PL_statcache.st_rdev = req->statdata->st_rdev; |
|
|
199 | PL_statcache.st_size = req->statdata->st_size; |
|
|
200 | PL_statcache.st_atime = req->statdata->st_atime; |
|
|
201 | PL_statcache.st_mtime = req->statdata->st_mtime; |
|
|
202 | PL_statcache.st_ctime = req->statdata->st_ctime; |
|
|
203 | PL_statcache.st_blksize = req->statdata->st_blksize; |
|
|
204 | PL_statcache.st_blocks = req->statdata->st_blocks; |
|
|
205 | |
|
|
206 | Safefree (req->statdata); |
|
|
207 | } |
|
|
208 | |
138 | PUSHMARK (SP); |
209 | PUSHMARK (SP); |
139 | XPUSHs (sv_2mortal (newSViv (req->result))); |
210 | XPUSHs (sv_2mortal (newSViv (req->result))); |
140 | PUTBACK; |
211 | PUTBACK; |
141 | call_sv (req->callback, G_VOID); |
212 | call_sv (req->callback, G_VOID); |
142 | SPAGAIN; |
213 | SPAGAIN; |
143 | |
214 | |
144 | SvREFCNT_dec (req->data); |
215 | if (req->callback) |
145 | SvREFCNT_dec (req->callback); |
216 | SvREFCNT_dec (req->callback); |
146 | |
217 | |
147 | errno = errorno; |
218 | errno = errorno; |
148 | nreqs--; |
219 | nreqs--; |
149 | count++; |
220 | count++; |
150 | } |
221 | } |
151 | |
222 | |
152 | Safefree (req); |
223 | Safefree (req); |
153 | } |
224 | } |
154 | |
225 | |
|
|
226 | if (qs) |
|
|
227 | { |
|
|
228 | printf ("outstanding %p %d\n", qs, nreqs); |
|
|
229 | send_reqs (); |
|
|
230 | } |
|
|
231 | |
155 | return count; |
232 | return count; |
156 | } |
233 | } |
|
|
234 | |
|
|
235 | static sigset_t fullsigset; |
157 | |
236 | |
158 | #undef errno |
237 | #undef errno |
159 | #include <asm/unistd.h> |
238 | #include <asm/unistd.h> |
160 | |
239 | |
161 | static int |
240 | static int |
162 | aio_proc(void *thr_arg) |
241 | aio_proc (void *thr_arg) |
163 | { |
242 | { |
164 | aio_thread *thr = thr_arg; |
243 | aio_thread *thr = thr_arg; |
165 | int sig; |
244 | aio_req req; |
166 | int errno; |
245 | int errno; |
167 | aio_req req; |
|
|
168 | |
246 | |
|
|
247 | /* this is very much x86 and kernel-specific :(:(:( */ |
169 | /* we rely on gcc's ability to create closures. */ |
248 | /* we rely on gcc's ability to create closures. */ |
170 | _syscall3(int,lseek,int,fd,off_t,offset,int,whence); |
|
|
171 | _syscall3(int,read,int,fd,char *,buf,off_t,count); |
249 | _syscall3(int,read,int,fd,char *,buf,size_t,count) |
172 | _syscall3(int,write,int,fd,char *,buf,off_t,count); |
250 | _syscall3(int,write,int,fd,char *,buf,size_t,count) |
173 | |
251 | |
174 | /* first get rid of any signals */ |
252 | _syscall3(int,open,char *,pathname,int,flags,mode_t,mode) |
175 | for (sig = 1; sig < _NSIG; sig++) |
253 | _syscall1(int,close,int,fd) |
176 | signal (sig, SIG_DFL); |
|
|
177 | |
254 | |
178 | signal (SIGPIPE, SIG_IGN); |
255 | _syscall5(int,pread64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi) |
179 | |
256 | _syscall5(int,pwrite64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi) |
|
|
257 | |
|
|
258 | _syscall2(int,stat64, const char *, filename, struct stat64 *, buf) |
|
|
259 | _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf) |
|
|
260 | _syscall2(int,fstat64, int, fd, struct stat64 *, buf) |
|
|
261 | |
|
|
262 | sigprocmask (SIG_SETMASK, &fullsigset, 0); |
|
|
263 | |
180 | /* then loop */ |
264 | /* then loop */ |
181 | while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
265 | while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
182 | { |
266 | { |
183 | req->thread = thr; |
267 | req->thread = thr; |
|
|
268 | errno = 0; /* strictly unnecessary */ |
184 | |
269 | |
185 | if (req->type == REQ_READ || req->type == REQ_WRITE) |
270 | switch (req->type) |
186 | { |
271 | { |
|
|
272 | case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); break; |
|
|
273 | case REQ_WRITE: req->result = pwrite64(req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); break; |
|
|
274 | case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
|
|
275 | case REQ_CLOSE: req->result = close (req->fd); break; |
|
|
276 | case REQ_STAT: req->result = stat64 (req->dataptr, req->statdata); break; |
|
|
277 | case REQ_LSTAT: req->result = lstat64 (req->dataptr, req->statdata); break; |
|
|
278 | case REQ_FSTAT: req->result = fstat64 (req->fd, req->statdata); break; |
|
|
279 | |
|
|
280 | case REQ_QUIT: |
|
|
281 | default: |
|
|
282 | write (respipe[1], (void *)&req, sizeof (req)); |
187 | errno = 0; |
283 | return 0; |
188 | |
|
|
189 | if (lseek (req->fd, req->offset, SEEK_SET) == req->offset) |
|
|
190 | { |
|
|
191 | if (req->type == REQ_READ) |
|
|
192 | req->result = read (req->fd, req->dataptr, req->length); |
|
|
193 | else |
|
|
194 | req->result = write(req->fd, req->dataptr, req->length); |
|
|
195 | } |
|
|
196 | |
|
|
197 | req->errorno = errno; |
|
|
198 | } |
284 | } |
199 | else |
|
|
200 | { |
|
|
201 | write (respipe[1], (void *)&req, sizeof (req)); |
|
|
202 | break; |
|
|
203 | } |
|
|
204 | |
285 | |
|
|
286 | req->errorno = errno; |
205 | write (respipe[1], (void *)&req, sizeof (req)); |
287 | write (respipe[1], (void *)&req, sizeof (req)); |
206 | } |
288 | } |
207 | |
289 | |
208 | return 0; |
290 | return 0; |
209 | } |
291 | } |
210 | |
292 | |
211 | MODULE = Linux::AIO PACKAGE = Linux::AIO |
293 | MODULE = Linux::AIO PACKAGE = Linux::AIO |
212 | |
294 | |
213 | BOOT: |
295 | BOOT: |
214 | { |
296 | { |
|
|
297 | sigfillset (&fullsigset); |
|
|
298 | sigdelset (&fullsigset, SIGTERM); |
|
|
299 | sigdelset (&fullsigset, SIGQUIT); |
|
|
300 | sigdelset (&fullsigset, SIGABRT); |
|
|
301 | sigdelset (&fullsigset, SIGINT); |
|
|
302 | |
215 | if (pipe (reqpipe) || pipe (respipe)) |
303 | if (pipe (reqpipe) || pipe (respipe)) |
216 | croak ("unable to initialize request or result pipe"); |
304 | croak ("unable to initialize request or result pipe"); |
|
|
305 | |
|
|
306 | if (fcntl (reqpipe[1], F_SETFL, O_NONBLOCK)) |
|
|
307 | croak ("cannot set result pipe to nonblocking mode"); |
217 | |
308 | |
218 | if (fcntl (respipe[0], F_SETFL, O_NONBLOCK)) |
309 | if (fcntl (respipe[0], F_SETFL, O_NONBLOCK)) |
219 | croak ("cannot set result pipe to nonblocking mode"); |
310 | croak ("cannot set result pipe to nonblocking mode"); |
220 | } |
311 | } |
221 | |
312 | |
… | |
… | |
237 | { |
328 | { |
238 | end_thread (); |
329 | end_thread (); |
239 | cur--; |
330 | cur--; |
240 | } |
331 | } |
241 | |
332 | |
242 | poll_cb (); |
|
|
243 | while (started > nthreads) |
333 | while (started > nthreads) |
244 | { |
334 | { |
245 | sched_yield (); |
335 | fd_set rfd; |
|
|
336 | FD_ZERO(&rfd); |
|
|
337 | FD_SET(respipe[0], &rfd); |
|
|
338 | |
|
|
339 | select (respipe[0] + 1, &rfd, 0, 0, 0); |
246 | poll_cb (); |
340 | poll_cb (aTHX); |
247 | } |
341 | } |
248 | |
342 | |
249 | void |
343 | void |
|
|
344 | aio_open(pathname,flags,mode,callback) |
|
|
345 | SV * pathname |
|
|
346 | int flags |
|
|
347 | int mode |
|
|
348 | SV * callback |
|
|
349 | PROTOTYPE: $$$$ |
|
|
350 | CODE: |
|
|
351 | aio_req req; |
|
|
352 | |
|
|
353 | Newz (0, req, 1, aio_cb); |
|
|
354 | |
|
|
355 | if (!req) |
|
|
356 | croak ("out of memory during aio_req allocation"); |
|
|
357 | |
|
|
358 | req->type = REQ_OPEN; |
|
|
359 | req->data = newSVsv (pathname); |
|
|
360 | req->dataptr = SvPV_nolen (req->data); |
|
|
361 | req->fd = flags; |
|
|
362 | req->mode = mode; |
|
|
363 | req->callback = SvREFCNT_inc (callback); |
|
|
364 | |
|
|
365 | send_req (req); |
|
|
366 | |
|
|
367 | void |
|
|
368 | aio_close(fh,callback) |
|
|
369 | InputStream fh |
|
|
370 | SV * callback |
|
|
371 | PROTOTYPE: $$ |
|
|
372 | CODE: |
|
|
373 | aio_req req; |
|
|
374 | |
|
|
375 | Newz (0, req, 1, aio_cb); |
|
|
376 | |
|
|
377 | if (!req) |
|
|
378 | croak ("out of memory during aio_req allocation"); |
|
|
379 | |
|
|
380 | req->type = REQ_CLOSE; |
|
|
381 | req->fd = PerlIO_fileno (fh); |
|
|
382 | req->callback = SvREFCNT_inc (callback); |
|
|
383 | |
|
|
384 | send_req (req); |
|
|
385 | |
|
|
386 | void |
250 | aio_read(fh,offset,length,data,dataoffset,callback) |
387 | aio_read(fh,offset,length,data,dataoffset,callback) |
251 | PerlIO * fh |
388 | InputStream fh |
252 | UV offset |
389 | UV offset |
253 | STRLEN length |
390 | IV length |
254 | SV * data |
391 | SV * data |
255 | STRLEN dataoffset |
392 | IV dataoffset |
256 | SV * callback |
393 | SV * callback |
257 | PROTOTYPE: $$$$$$ |
394 | PROTOTYPE: $$$$$$ |
258 | ALIAS: |
395 | CODE: |
259 | aio_write = 1 |
|
|
260 | CODE: |
|
|
261 | SvUPGRADE (data, SVt_PV); |
|
|
262 | SvPOK_on (data); |
|
|
263 | read_write (aTHX_ ix, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
396 | read_write (aTHX_ 0, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
|
|
397 | |
|
|
398 | void |
|
|
399 | aio_write(fh,offset,length,data,dataoffset,callback) |
|
|
400 | OutputStream fh |
|
|
401 | UV offset |
|
|
402 | IV length |
|
|
403 | SV * data |
|
|
404 | IV dataoffset |
|
|
405 | SV * callback |
|
|
406 | PROTOTYPE: $$$$$$ |
|
|
407 | CODE: |
|
|
408 | read_write (aTHX_ 1, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
|
|
409 | |
|
|
410 | void |
|
|
411 | aio_stat(fh_or_path,callback) |
|
|
412 | SV * fh_or_path |
|
|
413 | SV * callback |
|
|
414 | PROTOTYPE: $$ |
|
|
415 | ALIAS: |
|
|
416 | aio_lstat = 1 |
|
|
417 | CODE: |
|
|
418 | aio_req req; |
|
|
419 | |
|
|
420 | Newz (0, req, 1, aio_cb); |
|
|
421 | |
|
|
422 | if (!req) |
|
|
423 | croak ("out of memory during aio_req allocation"); |
|
|
424 | |
|
|
425 | New (0, req->statdata, 1, struct stat64); |
|
|
426 | |
|
|
427 | if (!req->statdata) |
|
|
428 | croak ("out of memory during aio_req->statdata allocation"); |
|
|
429 | |
|
|
430 | if (SvPOK (fh_or_path)) |
|
|
431 | { |
|
|
432 | req->type = ix ? REQ_LSTAT : REQ_STAT; |
|
|
433 | req->data = newSVsv (fh_or_path); |
|
|
434 | req->dataptr = SvPV_nolen (req->data); |
|
|
435 | } |
|
|
436 | else |
|
|
437 | { |
|
|
438 | req->type = REQ_FSTAT; |
|
|
439 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
|
|
440 | } |
|
|
441 | |
|
|
442 | req->callback = SvREFCNT_inc (callback); |
|
|
443 | |
|
|
444 | send_req (req); |
264 | |
445 | |
265 | int |
446 | int |
266 | poll_fileno() |
447 | poll_fileno() |
267 | PROTOTYPE: |
448 | PROTOTYPE: |
268 | CODE: |
449 | CODE: |