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