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> |
7 | #include <fcntl.h> |
8 | #include <fcntl.h> |
8 | #include <signal.h> |
9 | #include <signal.h> |
9 | #include <sched.h> |
10 | #include <sched.h> |
10 | |
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 | |
11 | #define STACKSIZE 1024 /* yeah */ |
23 | #define STACKSIZE 1024 /* yeah */ |
12 | |
24 | |
13 | enum { REQ_QUIT, REQ_READ, REQ_WRITE, REQ_OPEN, REQ_CLOSE }; |
25 | enum { REQ_QUIT, REQ_OPEN, REQ_CLOSE, REQ_READ, REQ_WRITE, REQ_STAT, REQ_LSTAT, REQ_FSTAT}; |
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; |
28 | mode_t mode; /* open */ |
39 | mode_t mode; /* open */ |
29 | int errorno; |
40 | int errorno; |
30 | SV *data, *callback; |
41 | SV *data, *callback; |
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; |
… | |
… | |
77 | SV *data, STRLEN dataoffset, SV*callback) |
90 | SV *data, STRLEN dataoffset, SV*callback) |
78 | { |
91 | { |
79 | aio_req req; |
92 | aio_req req; |
80 | STRLEN svlen; |
93 | STRLEN svlen; |
81 | char *svptr = SvPV (data, svlen); |
94 | char *svptr = SvPV (data, svlen); |
|
|
95 | |
|
|
96 | SvUPGRADE (data, SVt_PV); |
|
|
97 | SvPOK_on (data); |
82 | |
98 | |
83 | if (dataoffset < 0) |
99 | if (dataoffset < 0) |
84 | dataoffset += svlen; |
100 | dataoffset += svlen; |
85 | |
101 | |
86 | if (dataoffset < 0 || dataoffset > svlen) |
102 | if (dataoffset < 0 || dataoffset > svlen) |
… | |
… | |
99 | } |
115 | } |
100 | |
116 | |
101 | if (length < 0) |
117 | if (length < 0) |
102 | croak ("length must not be negative"); |
118 | croak ("length must not be negative"); |
103 | |
119 | |
104 | New (0, req, 1, aio_cb); |
120 | Newz (0, req, 1, aio_cb); |
105 | |
121 | |
106 | if (!req) |
122 | if (!req) |
107 | croak ("out of memory during aio_req allocation"); |
123 | croak ("out of memory during aio_req allocation"); |
108 | |
124 | |
109 | req->type = dowrite ? REQ_WRITE : REQ_READ; |
125 | req->type = dowrite ? REQ_WRITE : REQ_READ; |
… | |
… | |
138 | |
154 | |
139 | if (req->type == REQ_READ) |
155 | if (req->type == REQ_READ) |
140 | SvCUR_set (req->data, req->dataoffset |
156 | SvCUR_set (req->data, req->dataoffset |
141 | + req->result > 0 ? req->result : 0); |
157 | + req->result > 0 ? req->result : 0); |
142 | |
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 | |
143 | PUSHMARK (SP); |
183 | PUSHMARK (SP); |
144 | XPUSHs (sv_2mortal (newSViv (req->result))); |
184 | XPUSHs (sv_2mortal (newSViv (req->result))); |
145 | PUTBACK; |
185 | PUTBACK; |
146 | call_sv (req->callback, G_VOID); |
186 | call_sv (req->callback, G_VOID); |
147 | SPAGAIN; |
187 | SPAGAIN; |
148 | |
188 | |
149 | SvREFCNT_dec (req->data); |
189 | if (req->callback) |
150 | SvREFCNT_dec (req->callback); |
190 | SvREFCNT_dec (req->callback); |
151 | |
191 | |
152 | errno = errorno; |
192 | errno = errorno; |
153 | nreqs--; |
193 | nreqs--; |
154 | count++; |
194 | count++; |
155 | } |
195 | } |
… | |
… | |
170 | { |
210 | { |
171 | aio_thread *thr = thr_arg; |
211 | aio_thread *thr = thr_arg; |
172 | aio_req req; |
212 | aio_req req; |
173 | int errno; |
213 | int errno; |
174 | |
214 | |
|
|
215 | /* this is very much x86 and kernel-specific :(:(:( */ |
175 | /* we rely on gcc's ability to create closures. */ |
216 | /* we rely on gcc's ability to create closures. */ |
176 | _syscall3(int,lseek,int,fd,off_t,offset,int,whence) |
|
|
177 | _syscall3(int,read,int,fd,char *,buf,off_t,count) |
217 | _syscall3(int,read,int,fd,char *,buf,size_t,count) |
178 | _syscall3(int,write,int,fd,char *,buf,off_t,count) |
218 | _syscall3(int,write,int,fd,char *,buf,size_t,count) |
|
|
219 | |
179 | _syscall3(int,open,char *,pathname,int,flags,mode_t,mode) |
220 | _syscall3(int,open,char *,pathname,int,flags,mode_t,mode) |
180 | _syscall1(int,close,int,fd) |
221 | _syscall1(int,close,int,fd) |
|
|
222 | |
|
|
223 | _syscall5(int,pread64,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi) |
|
|
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) |
181 | |
229 | |
182 | sigprocmask (SIG_SETMASK, &fullsigset, 0); |
230 | sigprocmask (SIG_SETMASK, &fullsigset, 0); |
183 | |
231 | |
184 | /* then loop */ |
232 | /* then loop */ |
185 | while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
233 | while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
186 | { |
234 | { |
187 | req->thread = thr; |
235 | req->thread = thr; |
188 | errno = 0; |
236 | errno = 0; /* strictly unnecessary */ |
189 | |
237 | |
190 | if (req->type == REQ_READ || req->type == REQ_WRITE) |
|
|
191 | { |
|
|
192 | if (lseek (req->fd, req->offset, SEEK_SET) == req->offset) |
|
|
193 | { |
|
|
194 | if (req->type == REQ_READ) |
238 | if (req->type == REQ_READ) |
195 | 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); |
196 | else |
240 | else if (req->type == REQ_WRITE) |
197 | 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); |
198 | } |
|
|
199 | } |
|
|
200 | else if (req->type == REQ_OPEN) |
242 | else if (req->type == REQ_OPEN) |
201 | { |
|
|
202 | req->result = open (req->dataptr, req->fd, req->mode); |
243 | req->result = open (req->dataptr, req->fd, req->mode); |
203 | } |
|
|
204 | else if (req->type == REQ_CLOSE) |
244 | else if (req->type == REQ_CLOSE) |
205 | { |
|
|
206 | req->result = close (req->fd); |
245 | req->result = close (req->fd); |
207 | } |
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); |
208 | else |
252 | else |
209 | { |
253 | { |
210 | write (respipe[1], (void *)&req, sizeof (req)); |
254 | write (respipe[1], (void *)&req, sizeof (req)); |
211 | break; |
255 | break; |
212 | } |
256 | } |
… | |
… | |
253 | { |
297 | { |
254 | end_thread (); |
298 | end_thread (); |
255 | cur--; |
299 | cur--; |
256 | } |
300 | } |
257 | |
301 | |
258 | poll_cb (); |
|
|
259 | while (started > nthreads) |
302 | while (started > nthreads) |
260 | { |
303 | { |
261 | sched_yield (); |
304 | fd_set rfd; |
|
|
305 | FD_ZERO(&rfd); |
|
|
306 | FD_SET(respipe[0], &rfd); |
|
|
307 | |
|
|
308 | select (respipe[0] + 1, &rfd, 0, 0, 0); |
262 | poll_cb (); |
309 | poll_cb (); |
263 | } |
310 | } |
264 | |
311 | |
265 | void |
312 | void |
266 | aio_read(fh,offset,length,data,dataoffset,callback) |
|
|
267 | PerlIO * fh |
|
|
268 | UV offset |
|
|
269 | STRLEN length |
|
|
270 | SV * data |
|
|
271 | STRLEN dataoffset |
|
|
272 | SV * callback |
|
|
273 | PROTOTYPE: $$$$$$ |
|
|
274 | ALIAS: |
|
|
275 | aio_write = 1 |
|
|
276 | CODE: |
|
|
277 | SvUPGRADE (data, SVt_PV); |
|
|
278 | SvPOK_on (data); |
|
|
279 | read_write (aTHX_ ix, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
|
|
280 | |
|
|
281 | void |
|
|
282 | aio_open(pathname,flags,mode,callback) |
313 | aio_open(pathname,flags,mode,callback) |
283 | char * pathname |
314 | SV * pathname |
284 | int flags |
315 | int flags |
285 | int mode |
316 | int mode |
286 | SV * callback |
317 | SV * callback |
287 | PROTOTYPE: $$$$ |
318 | PROTOTYPE: $$$$ |
288 | CODE: |
319 | CODE: |
289 | aio_req req; |
320 | aio_req req; |
290 | |
321 | |
291 | New (0, req, 1, aio_cb); |
322 | Newz (0, req, 1, aio_cb); |
292 | |
323 | |
293 | if (!req) |
324 | if (!req) |
294 | croak ("out of memory during aio_req allocation"); |
325 | croak ("out of memory during aio_req allocation"); |
295 | |
326 | |
296 | req->type = REQ_OPEN; |
327 | req->type = REQ_OPEN; |
297 | req->dataptr = pathname; |
328 | req->data = newSVsv (pathname); |
|
|
329 | req->dataptr = SvPV_nolen (req->data); |
298 | req->fd = flags; |
330 | req->fd = flags; |
299 | req->mode = mode; |
331 | req->mode = mode; |
300 | req->callback = SvREFCNT_inc (callback); |
332 | req->callback = SvREFCNT_inc (callback); |
301 | |
333 | |
302 | send_req (req); |
334 | send_req (req); |
303 | |
335 | |
304 | void |
336 | void |
305 | aio_close(fh,callback) |
337 | aio_close(fh,callback) |
306 | PerlIO * fh |
338 | InputStream fh |
307 | SV * callback |
339 | SV * callback |
308 | PROTOTYPE: $ |
340 | PROTOTYPE: $$ |
309 | CODE: |
341 | CODE: |
310 | aio_req req; |
342 | aio_req req; |
311 | |
343 | |
312 | New (0, req, 1, aio_cb); |
344 | Newz (0, req, 1, aio_cb); |
313 | |
345 | |
314 | if (!req) |
346 | if (!req) |
315 | croak ("out of memory during aio_req allocation"); |
347 | croak ("out of memory during aio_req allocation"); |
316 | |
348 | |
317 | req->type = REQ_CLOSE; |
349 | req->type = REQ_CLOSE; |
318 | req->fd = PerlIO_fileno (fh); |
350 | req->fd = PerlIO_fileno (fh); |
319 | req->callback = SvREFCNT_inc (callback); |
351 | req->callback = SvREFCNT_inc (callback); |
320 | |
352 | |
321 | send_req (req); |
353 | send_req (req); |
322 | |
354 | |
|
|
355 | void |
|
|
356 | aio_read(fh,offset,length,data,dataoffset,callback) |
|
|
357 | InputStream fh |
|
|
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 |
|
|
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 | |
323 | int |
415 | int |
324 | poll_fileno() |
416 | poll_fileno() |
325 | PROTOTYPE: |
417 | PROTOTYPE: |
326 | CODE: |
418 | CODE: |
327 | RETVAL = respipe[0]; |
419 | RETVAL = respipe[0]; |