|
|
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> |
|
|
10 | #include <fcntl.h> |
|
|
11 | #include <signal.h> |
7 | #include <sched.h> |
12 | #include <sched.h> |
8 | |
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 | |
9 | #define STACKSIZE 128 /* yeah */ |
25 | #define STACKSIZE 1024 /* yeah */ |
10 | |
26 | |
11 | #define REQ_QUIT 0 |
27 | 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 | |
28 | |
15 | typedef struct { |
29 | typedef struct { |
16 | char stack[STACKSIZE]; |
30 | char stack[STACKSIZE]; |
17 | } aio_thread; |
31 | } aio_thread; |
18 | |
32 | |
19 | typedef struct { |
33 | typedef struct aio_cb { |
|
|
34 | struct aio_cb *next; |
|
|
35 | |
20 | int type; |
36 | int type; |
21 | aio_thread *thread; |
37 | aio_thread *thread; |
22 | |
38 | |
23 | /* read/write */ |
|
|
24 | int fd; |
39 | int fd; |
25 | off_t offset; |
40 | off_t offset; |
26 | size_t length; |
41 | size_t length; |
27 | ssize_t result; |
42 | ssize_t result; |
|
|
43 | mode_t mode; /* open */ |
28 | int errorno; |
44 | int errorno; |
29 | |
45 | SV *data, *callback; |
30 | SV *data; |
|
|
31 | void *dataptr; |
46 | void *dataptr; |
32 | STRLEN dataoffset; |
47 | STRLEN dataoffset; |
|
|
48 | |
|
|
49 | struct stat64 *statdata; |
33 | } aio_cb; |
50 | } aio_cb; |
34 | |
51 | |
35 | typedef aio_cb *aio_req; |
52 | typedef aio_cb *aio_req; |
36 | |
53 | |
37 | static int started; |
54 | static int started; |
|
|
55 | static int nreqs; |
38 | static int reqpipe[2], respipe[2]; |
56 | static int reqpipe[2], respipe[2]; |
39 | |
57 | |
|
|
58 | static aio_req qs, qe; /* queue start, queue end */ |
|
|
59 | |
40 | static int aio_proc(void *arg); |
60 | static int aio_proc(void *arg); |
41 | |
61 | |
42 | static void |
62 | static void |
43 | start_thread(void) |
63 | start_thread (void) |
44 | { |
64 | { |
45 | aio_thread *thr; |
65 | aio_thread *thr; |
46 | |
66 | |
47 | New (0, thr, 1, aio_thread); |
67 | New (0, thr, 1, aio_thread); |
48 | |
68 | |
… | |
… | |
54 | else |
74 | else |
55 | Safefree (thr); |
75 | Safefree (thr); |
56 | } |
76 | } |
57 | |
77 | |
58 | static void |
78 | static void |
59 | end_thread(void) |
79 | send_reqs (void) |
60 | { |
80 | { |
61 | aio_req req = 0; |
81 | /* this write is atomic */ |
62 | write (reqpipe[1], &req, sizeof (aio_req)); |
82 | while (qs && write (reqpipe[1], &qs, sizeof qs) == sizeof qs) |
63 | started--; |
83 | { |
|
|
84 | qs = qs->next; |
|
|
85 | if (!qs) qe = 0; |
|
|
86 | } |
64 | } |
87 | } |
65 | |
88 | |
66 | static void |
89 | static void |
67 | set_errno(int errorno) |
90 | send_req (aio_req req) |
68 | { |
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 |
|
|
104 | end_thread (void) |
|
|
105 | { |
|
|
106 | aio_req req; |
|
|
107 | New (0, req, 1, aio_cb); |
|
|
108 | req->type = REQ_QUIT; |
|
|
109 | |
|
|
110 | send_req (req); |
|
|
111 | } |
|
|
112 | |
|
|
113 | static void |
|
|
114 | read_write (pTHX_ |
|
|
115 | int dowrite, int fd, off_t offset, size_t length, |
|
|
116 | SV *data, STRLEN dataoffset, SV *callback) |
|
|
117 | { |
|
|
118 | aio_req req; |
|
|
119 | STRLEN svlen; |
|
|
120 | char *svptr = SvPV (data, svlen); |
|
|
121 | |
|
|
122 | SvUPGRADE (data, SVt_PV); |
|
|
123 | SvPOK_on (data); |
|
|
124 | |
|
|
125 | if (dataoffset < 0) |
|
|
126 | dataoffset += svlen; |
|
|
127 | |
|
|
128 | if (dataoffset < 0 || dataoffset > svlen) |
|
|
129 | croak ("data offset outside of string"); |
|
|
130 | |
|
|
131 | if (dowrite) |
|
|
132 | { |
|
|
133 | /* write: check length and adjust. */ |
|
|
134 | if (length < 0 || length + dataoffset > svlen) |
|
|
135 | length = svlen - dataoffset; |
|
|
136 | } |
|
|
137 | else |
|
|
138 | { |
|
|
139 | /* read: grow scalar as necessary */ |
|
|
140 | svptr = SvGROW (data, length + dataoffset); |
|
|
141 | } |
|
|
142 | |
|
|
143 | if (length < 0) |
|
|
144 | croak ("length must not be negative"); |
|
|
145 | |
|
|
146 | Newz (0, req, 1, aio_cb); |
|
|
147 | |
|
|
148 | if (!req) |
|
|
149 | croak ("out of memory during aio_req allocation"); |
|
|
150 | |
|
|
151 | req->type = dowrite ? REQ_WRITE : REQ_READ; |
|
|
152 | req->fd = fd; |
|
|
153 | req->offset = offset; |
|
|
154 | req->length = length; |
|
|
155 | req->data = SvREFCNT_inc (data); |
|
|
156 | req->dataptr = (char *)svptr + dataoffset; |
|
|
157 | req->callback = SvREFCNT_inc (callback); |
|
|
158 | |
|
|
159 | send_req (req); |
|
|
160 | } |
|
|
161 | |
|
|
162 | static int |
|
|
163 | poll_cb (pTHX) |
|
|
164 | { |
|
|
165 | dSP; |
|
|
166 | int count = 0; |
|
|
167 | aio_req req; |
|
|
168 | |
|
|
169 | while (read (respipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
|
|
170 | { |
|
|
171 | if (req->type == REQ_QUIT) |
|
|
172 | { |
|
|
173 | Safefree (req->thread); |
|
|
174 | started--; |
|
|
175 | } |
|
|
176 | else |
|
|
177 | { |
|
|
178 | int errorno = errno; |
|
|
179 | errno = req->errorno; |
|
|
180 | |
|
|
181 | if (req->type == REQ_READ) |
|
|
182 | SvCUR_set (req->data, req->dataoffset |
|
|
183 | + req->result > 0 ? req->result : 0); |
|
|
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 | |
|
|
209 | PUSHMARK (SP); |
|
|
210 | XPUSHs (sv_2mortal (newSViv (req->result))); |
|
|
211 | PUTBACK; |
|
|
212 | call_sv (req->callback, G_VOID); |
|
|
213 | SPAGAIN; |
|
|
214 | |
|
|
215 | if (req->callback) |
|
|
216 | SvREFCNT_dec (req->callback); |
|
|
217 | |
69 | errno = errorno; |
218 | errno = errorno; |
|
|
219 | nreqs--; |
|
|
220 | count++; |
|
|
221 | } |
|
|
222 | |
|
|
223 | Safefree (req); |
|
|
224 | } |
|
|
225 | |
|
|
226 | if (qs) |
|
|
227 | send_reqs (); |
|
|
228 | |
|
|
229 | return count; |
70 | } |
230 | } |
|
|
231 | |
|
|
232 | static sigset_t fullsigset; |
71 | |
233 | |
72 | #undef errno |
234 | #undef errno |
73 | #include <asm/unistd.h> |
235 | #include <asm/unistd.h> |
74 | |
236 | |
75 | static int |
237 | static int |
76 | aio_proc(void *thr_arg) |
238 | aio_proc (void *thr_arg) |
77 | { |
239 | { |
78 | aio_thread *thr = thr_arg; |
240 | aio_thread *thr = thr_arg; |
79 | int sig; |
241 | aio_req req; |
80 | int errno; |
242 | int errno; |
81 | aio_req req; |
|
|
82 | |
243 | |
|
|
244 | /* this is very much x86 and kernel-specific :(:(:( */ |
83 | /* we rely on gcc's ability to create closures. */ |
245 | /* 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); |
246 | _syscall3(int,read,int,fd,char *,buf,size_t,count) |
86 | _syscall3(int,write,int,fd,char *,buf,off_t,count); |
247 | _syscall3(int,write,int,fd,char *,buf,size_t,count) |
87 | |
248 | |
88 | /* first get rid of any signals */ |
249 | _syscall3(int,open,char *,pathname,int,flags,mode_t,mode) |
89 | for (sig = 1; sig < _NSIG; sig++) |
250 | _syscall1(int,close,int,fd) |
90 | signal (sig, SIG_IGN); |
|
|
91 | |
251 | |
92 | signal (SIGTERM, SIG_DFL); |
252 | _syscall5(int,pread64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi) |
93 | |
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) |
|
|
258 | |
|
|
259 | sigprocmask (SIG_SETMASK, &fullsigset, 0); |
|
|
260 | |
94 | /* then loop */ |
261 | /* then loop */ |
95 | while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
262 | while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
96 | { |
263 | { |
97 | req->thread = thr; |
264 | req->thread = thr; |
|
|
265 | errno = 0; /* strictly unnecessary */ |
98 | |
266 | |
99 | if (req->type == REQ_READ || req->type == REQ_WRITE) |
267 | switch (req->type) |
100 | { |
268 | { |
|
|
269 | case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); break; |
|
|
270 | case REQ_WRITE: req->result = pwrite64(req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); break; |
|
|
271 | case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
|
|
272 | case REQ_CLOSE: req->result = close (req->fd); break; |
|
|
273 | case REQ_STAT: req->result = stat64 (req->dataptr, req->statdata); break; |
|
|
274 | case REQ_LSTAT: req->result = lstat64 (req->dataptr, req->statdata); break; |
|
|
275 | case REQ_FSTAT: req->result = fstat64 (req->fd, req->statdata); break; |
|
|
276 | |
|
|
277 | case REQ_QUIT: |
|
|
278 | default: |
|
|
279 | write (respipe[1], (void *)&req, sizeof (req)); |
101 | errno = 0; |
280 | return 0; |
102 | |
|
|
103 | if (lseek (req->fd, req->offset, SEEK_SET) == req->offset) |
|
|
104 | { |
|
|
105 | if (req->type == REQ_READ) |
|
|
106 | req->result = read (req->fd, req->dataptr, req->length); |
|
|
107 | else |
|
|
108 | req->result = write(req->fd, req->dataptr, req->length); |
|
|
109 | } |
|
|
110 | |
|
|
111 | req->errorno = errno; |
|
|
112 | } |
281 | } |
113 | else |
|
|
114 | { |
|
|
115 | write (respipe[1], (void *)&req, sizeof (req)); |
|
|
116 | break; |
|
|
117 | } |
|
|
118 | |
282 | |
|
|
283 | req->errorno = errno; |
119 | write (respipe[1], (void *)&req, sizeof (req)); |
284 | write (respipe[1], (void *)&req, sizeof (req)); |
120 | } |
285 | } |
121 | |
286 | |
122 | return 0; |
287 | return 0; |
123 | } |
288 | } |
124 | |
289 | |
125 | MODULE = Linux::AIO PACKAGE = Linux::AIO |
290 | MODULE = Linux::AIO PACKAGE = Linux::AIO |
126 | |
291 | |
127 | BOOT: |
292 | BOOT: |
128 | { |
293 | { |
|
|
294 | sigfillset (&fullsigset); |
|
|
295 | sigdelset (&fullsigset, SIGTERM); |
|
|
296 | sigdelset (&fullsigset, SIGQUIT); |
|
|
297 | sigdelset (&fullsigset, SIGABRT); |
|
|
298 | sigdelset (&fullsigset, SIGINT); |
|
|
299 | |
129 | if (pipe (reqpipe) || pipe (respipe)) |
300 | if (pipe (reqpipe) || pipe (respipe)) |
130 | 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"); |
|
|
305 | |
|
|
306 | if (fcntl (respipe[0], F_SETFL, O_NONBLOCK)) |
|
|
307 | croak ("cannot set result pipe to nonblocking mode"); |
131 | } |
308 | } |
132 | |
309 | |
133 | void |
310 | void |
134 | min_parallel(nthreads) |
311 | min_parallel(nthreads) |
135 | int nthreads |
312 | int nthreads |
|
|
313 | PROTOTYPE: $ |
136 | CODE: |
314 | CODE: |
137 | while (nthreads > started) |
315 | while (nthreads > started) |
138 | start_thread (); |
316 | start_thread (); |
139 | |
317 | |
140 | void |
318 | void |
141 | max_parallel(nthreads) |
319 | max_parallel(nthreads) |
142 | int nthreads |
320 | int nthreads |
|
|
321 | PROTOTYPE: $ |
143 | CODE: |
322 | CODE: |
|
|
323 | int cur = started; |
|
|
324 | while (cur > nthreads) |
|
|
325 | { |
|
|
326 | end_thread (); |
|
|
327 | cur--; |
|
|
328 | } |
|
|
329 | |
144 | while (started > nthreads) |
330 | while (started > nthreads) |
|
|
331 | { |
|
|
332 | fd_set rfd; |
|
|
333 | FD_ZERO(&rfd); |
|
|
334 | FD_SET(respipe[0], &rfd); |
|
|
335 | |
|
|
336 | select (respipe[0] + 1, &rfd, 0, 0, 0); |
|
|
337 | poll_cb (aTHX); |
|
|
338 | } |
|
|
339 | |
|
|
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 | |
145 | end_thread (); |
362 | send_req (req); |
146 | |
363 | |
147 | void |
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 |
148 | read(fh,offset,length,data,dataoffset,callback) |
384 | aio_read(fh,offset,length,data,dataoffset,callback) |
149 | ALIAS: |
385 | InputStream fh |
150 | write = 1 |
386 | UV offset |
|
|
387 | IV length |
|
|
388 | SV * data |
|
|
389 | IV dataoffset |
|
|
390 | SV * callback |
|
|
391 | PROTOTYPE: $$$$$$ |
|
|
392 | CODE: |
|
|
393 | read_write (aTHX_ 0, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
|
|
394 | |
|
|
395 | void |
|
|
396 | aio_write(fh,offset,length,data,dataoffset,callback) |
|
|
397 | OutputStream fh |
|
|
398 | UV offset |
|
|
399 | IV length |
|
|
400 | SV * data |
|
|
401 | IV dataoffset |
|
|
402 | SV * callback |
|
|
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 |
151 | CODE: |
414 | CODE: |
|
|
415 | aio_req req; |
|
|
416 | |
|
|
417 | Newz (0, req, 1, aio_cb); |
|
|
418 | |
|
|
419 | if (!req) |
|
|
420 | croak ("out of memory during aio_req allocation"); |
|
|
421 | |
|
|
422 | New (0, req->statdata, 1, struct stat64); |
|
|
423 | |
|
|
424 | if (!req->statdata) |
|
|
425 | croak ("out of memory during aio_req->statdata allocation"); |
|
|
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 | { |
|
|
435 | req->type = REQ_FSTAT; |
|
|
436 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
|
|
437 | } |
|
|
438 | |
|
|
439 | req->callback = SvREFCNT_inc (callback); |
|
|
440 | |
|
|
441 | send_req (req); |
|
|
442 | |
|
|
443 | int |
|
|
444 | poll_fileno() |
|
|
445 | PROTOTYPE: |
|
|
446 | CODE: |
|
|
447 | RETVAL = respipe[0]; |
|
|
448 | OUTPUT: |
|
|
449 | RETVAL |
|
|
450 | |
|
|
451 | int |
|
|
452 | poll_cb(...) |
|
|
453 | PROTOTYPE: |
|
|
454 | CODE: |
|
|
455 | RETVAL = poll_cb (aTHX); |
|
|
456 | OUTPUT: |
|
|
457 | RETVAL |
|
|
458 | |
|
|
459 | int |
|
|
460 | nreqs() |
|
|
461 | PROTOTYPE: |
|
|
462 | CODE: |
|
|
463 | RETVAL = nreqs; |
|
|
464 | OUTPUT: |
|
|
465 | RETVAL |
|
|
466 | |