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