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