1 | #define _XOPEN_SOURCE 500 |
|
|
2 | |
|
|
3 | #include "EXTERN.h" |
1 | #include "EXTERN.h" |
4 | #include "perl.h" |
2 | #include "perl.h" |
5 | #include "XSUB.h" |
3 | #include "XSUB.h" |
|
|
4 | |
|
|
5 | #include "autoconf/config.h" |
6 | |
6 | |
7 | #include <sys/types.h> |
7 | #include <sys/types.h> |
8 | #include <sys/stat.h> |
8 | #include <sys/stat.h> |
9 | |
9 | |
10 | #include <unistd.h> |
10 | #include <unistd.h> |
11 | #include <fcntl.h> |
11 | #include <fcntl.h> |
12 | #include <signal.h> |
12 | #include <signal.h> |
13 | #include <sched.h> |
13 | #include <sched.h> |
14 | #if __linux |
|
|
15 | #include <sys/syscall.h> |
|
|
16 | #endif |
|
|
17 | |
14 | |
18 | #include <pthread.h> |
15 | #include <pthread.h> |
19 | |
16 | |
20 | typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
17 | typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
21 | typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
18 | typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
… | |
… | |
44 | off_t offset; |
41 | off_t offset; |
45 | size_t length; |
42 | size_t length; |
46 | ssize_t result; |
43 | ssize_t result; |
47 | mode_t mode; /* open */ |
44 | mode_t mode; /* open */ |
48 | int errorno; |
45 | int errorno; |
49 | SV *data, *callback; |
46 | SV *data, *callback, *fh; |
50 | void *dataptr; |
47 | void *dataptr; |
51 | STRLEN dataoffset; |
48 | STRLEN dataoffset; |
52 | |
49 | |
53 | Stat_t *statdata; |
50 | Stat_t *statdata; |
54 | } aio_cb; |
51 | } aio_cb; |
… | |
… | |
84 | poll_cb () |
81 | poll_cb () |
85 | { |
82 | { |
86 | dSP; |
83 | dSP; |
87 | int count = 0; |
84 | int count = 0; |
88 | aio_req req, prv; |
85 | aio_req req, prv; |
89 | |
|
|
90 | static int rl;//D |
|
|
91 | //printf ("%d ENTER\n", ++rl);//D |
|
|
92 | |
86 | |
93 | pthread_mutex_lock (&reslock); |
87 | pthread_mutex_lock (&reslock); |
94 | |
88 | |
95 | { |
89 | { |
96 | /* read any signals sent by the worker threads */ |
90 | /* read any signals sent by the worker threads */ |
… | |
… | |
105 | pthread_mutex_unlock (&reslock); |
99 | pthread_mutex_unlock (&reslock); |
106 | |
100 | |
107 | while (req) |
101 | while (req) |
108 | { |
102 | { |
109 | nreqs--; |
103 | nreqs--; |
110 | //printf ("%d count %d %p->%p\n", rl, count, req, req->next);//D |
|
|
111 | |
104 | |
112 | if (req->type == REQ_QUIT) |
105 | if (req->type == REQ_QUIT) |
113 | started--; |
106 | started--; |
114 | else |
107 | else |
115 | { |
108 | { |
… | |
… | |
121 | + req->result > 0 ? req->result : 0); |
114 | + req->result > 0 ? req->result : 0); |
122 | |
115 | |
123 | if (req->data) |
116 | if (req->data) |
124 | SvREFCNT_dec (req->data); |
117 | SvREFCNT_dec (req->data); |
125 | |
118 | |
|
|
119 | if (req->fh) |
|
|
120 | SvREFCNT_dec (req->fh); |
|
|
121 | |
126 | if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) |
122 | if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) |
127 | { |
123 | { |
128 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
124 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
129 | PL_laststatval = req->result; |
125 | PL_laststatval = req->result; |
130 | PL_statcache = *(req->statdata); |
126 | PL_statcache = *(req->statdata); |
131 | |
127 | |
132 | Safefree (req->statdata); |
128 | Safefree (req->statdata); |
133 | } |
129 | } |
134 | |
130 | |
|
|
131 | ENTER; |
135 | PUSHMARK (SP); |
132 | PUSHMARK (SP); |
136 | XPUSHs (sv_2mortal (newSViv (req->result))); |
133 | XPUSHs (sv_2mortal (newSViv (req->result))); |
137 | |
134 | |
138 | if (req->type == REQ_OPEN) |
135 | if (req->type == REQ_OPEN) |
139 | { |
136 | { |
… | |
… | |
142 | |
139 | |
143 | PUTBACK; |
140 | PUTBACK; |
144 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
141 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
145 | SPAGAIN; |
142 | SPAGAIN; |
146 | |
143 | |
147 | fh = POPs; |
144 | fh = SvREFCNT_inc (POPs); |
148 | |
145 | |
149 | PUSHMARK (SP); |
146 | PUSHMARK (SP); |
150 | XPUSHs (fh); |
147 | XPUSHs (sv_2mortal (fh)); |
151 | } |
148 | } |
152 | |
149 | |
153 | if (SvOK (req->callback)) |
150 | if (SvOK (req->callback)) |
154 | { |
151 | { |
155 | PUTBACK; |
152 | PUTBACK; |
156 | call_sv (req->callback, G_VOID | G_EVAL); |
153 | call_sv (req->callback, G_VOID | G_EVAL); |
157 | SPAGAIN; |
154 | SPAGAIN; |
158 | } |
155 | } |
|
|
156 | |
|
|
157 | LEAVE; |
159 | |
158 | |
160 | if (req->callback) |
159 | if (req->callback) |
161 | SvREFCNT_dec (req->callback); |
160 | SvREFCNT_dec (req->callback); |
162 | |
161 | |
163 | errno = errorno; |
162 | errno = errorno; |
… | |
… | |
169 | Safefree (prv); |
168 | Safefree (prv); |
170 | |
169 | |
171 | /* TODO: croak on errors? */ |
170 | /* TODO: croak on errors? */ |
172 | } |
171 | } |
173 | |
172 | |
174 | //printf ("%d LEAVE %p %p\n", rl--, ress, rese);//D |
|
|
175 | return count; |
173 | return count; |
176 | } |
174 | } |
177 | |
175 | |
178 | static void *aio_proc(void *arg); |
176 | static void *aio_proc(void *arg); |
179 | |
177 | |
… | |
… | |
232 | req->type = REQ_QUIT; |
230 | req->type = REQ_QUIT; |
233 | |
231 | |
234 | send_req (req); |
232 | send_req (req); |
235 | } |
233 | } |
236 | |
234 | |
237 | static void |
|
|
238 | read_write (int dowrite, int fd, off_t offset, size_t length, |
|
|
239 | SV *data, STRLEN dataoffset, SV *callback) |
|
|
240 | { |
|
|
241 | aio_req req; |
|
|
242 | STRLEN svlen; |
|
|
243 | char *svptr = SvPV (data, svlen); |
|
|
244 | |
|
|
245 | SvUPGRADE (data, SVt_PV); |
|
|
246 | SvPOK_on (data); |
|
|
247 | |
|
|
248 | if (dataoffset < 0) |
|
|
249 | dataoffset += svlen; |
|
|
250 | |
|
|
251 | if (dataoffset < 0 || dataoffset > svlen) |
|
|
252 | croak ("data offset outside of string"); |
|
|
253 | |
|
|
254 | if (dowrite) |
|
|
255 | { |
|
|
256 | /* write: check length and adjust. */ |
|
|
257 | if (length < 0 || length + dataoffset > svlen) |
|
|
258 | length = svlen - dataoffset; |
|
|
259 | } |
|
|
260 | else |
|
|
261 | { |
|
|
262 | /* read: grow scalar as necessary */ |
|
|
263 | svptr = SvGROW (data, length + dataoffset); |
|
|
264 | } |
|
|
265 | |
|
|
266 | if (length < 0) |
|
|
267 | croak ("length must not be negative"); |
|
|
268 | |
|
|
269 | Newz (0, req, 1, aio_cb); |
|
|
270 | |
|
|
271 | if (!req) |
|
|
272 | croak ("out of memory during aio_req allocation"); |
|
|
273 | |
|
|
274 | req->type = dowrite ? REQ_WRITE : REQ_READ; |
|
|
275 | req->fd = fd; |
|
|
276 | req->offset = offset; |
|
|
277 | req->length = length; |
|
|
278 | req->data = SvREFCNT_inc (data); |
|
|
279 | req->dataptr = (char *)svptr + dataoffset; |
|
|
280 | req->callback = SvREFCNT_inc (callback); |
|
|
281 | |
|
|
282 | send_req (req); |
|
|
283 | } |
|
|
284 | |
|
|
285 | static void * |
235 | static void * |
286 | aio_proc (void *thr_arg) |
236 | aio_proc (void *thr_arg) |
287 | { |
237 | { |
288 | aio_req req; |
238 | aio_req req; |
289 | int type; |
239 | int type; |
… | |
… | |
314 | |
264 | |
315 | type = req->type; |
265 | type = req->type; |
316 | |
266 | |
317 | switch (type) |
267 | switch (type) |
318 | { |
268 | { |
|
|
269 | #if HAVE_PREADWRITE |
319 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
270 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
320 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
271 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
321 | #if SYS_readahead |
272 | #else |
|
|
273 | # error "pread/pwrite cannot be emulated, fix your os" |
|
|
274 | #endif |
|
|
275 | |
|
|
276 | #if HAVE_READAHEAD |
322 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
277 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
323 | #else |
278 | #else |
|
|
279 | // TODO: emulate |
324 | case REQ_READAHEAD: req->result = -1; errno = ENOSYS; break; |
280 | case REQ_READAHEAD: req->result = -1; errno = ENOSYS; break; |
325 | #endif |
281 | #endif |
326 | |
282 | |
327 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
283 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
328 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
284 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
… | |
… | |
330 | |
286 | |
331 | case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
287 | case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
332 | case REQ_CLOSE: req->result = close (req->fd); break; |
288 | case REQ_CLOSE: req->result = close (req->fd); break; |
333 | case REQ_UNLINK: req->result = unlink (req->dataptr); break; |
289 | case REQ_UNLINK: req->result = unlink (req->dataptr); break; |
334 | |
290 | |
|
|
291 | case REQ_FDATASYNC: |
|
|
292 | #if HAVE_FDATASYNC |
|
|
293 | req->result = fdatasync (req->fd); break; |
|
|
294 | #endif |
335 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
295 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
336 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
|
|
337 | |
296 | |
338 | case REQ_QUIT: |
297 | case REQ_QUIT: |
339 | break; |
298 | break; |
340 | |
299 | |
341 | default: |
300 | default: |
… | |
… | |
447 | send_req (req); |
406 | send_req (req); |
448 | } |
407 | } |
449 | |
408 | |
450 | void |
409 | void |
451 | aio_close(fh,callback=&PL_sv_undef) |
410 | aio_close(fh,callback=&PL_sv_undef) |
452 | InputStream fh |
411 | SV * fh |
453 | SV * callback |
412 | SV * callback |
454 | PROTOTYPE: $;$ |
413 | PROTOTYPE: $;$ |
455 | ALIAS: |
414 | ALIAS: |
456 | aio_close = REQ_CLOSE |
415 | aio_close = REQ_CLOSE |
457 | aio_fsync = REQ_FSYNC |
416 | aio_fsync = REQ_FSYNC |
458 | aio_fdatasync = REQ_FDATASYNC |
417 | aio_fdatasync = REQ_FDATASYNC |
… | |
… | |
464 | |
423 | |
465 | if (!req) |
424 | if (!req) |
466 | croak ("out of memory during aio_req allocation"); |
425 | croak ("out of memory during aio_req allocation"); |
467 | |
426 | |
468 | req->type = ix; |
427 | req->type = ix; |
|
|
428 | req->fh = newSVsv (fh); |
469 | req->fd = PerlIO_fileno (fh); |
429 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
470 | req->callback = SvREFCNT_inc (callback); |
430 | req->callback = SvREFCNT_inc (callback); |
471 | |
431 | |
472 | send_req (req); |
432 | send_req (req); |
473 | } |
433 | } |
474 | |
434 | |
475 | void |
435 | void |
476 | aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
436 | aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
477 | InputStream fh |
437 | SV * fh |
478 | UV offset |
438 | UV offset |
479 | IV length |
439 | IV length |
480 | SV * data |
440 | SV * data |
481 | IV dataoffset |
441 | IV dataoffset |
482 | SV * callback |
442 | SV * callback |
|
|
443 | ALIAS: |
|
|
444 | aio_read = REQ_READ |
|
|
445 | aio_write = REQ_WRITE |
483 | PROTOTYPE: $$$$$;$ |
446 | PROTOTYPE: $$$$$;$ |
484 | CODE: |
447 | CODE: |
485 | read_write (0, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
448 | { |
|
|
449 | aio_req req; |
|
|
450 | STRLEN svlen; |
|
|
451 | char *svptr = SvPV (data, svlen); |
486 | |
452 | |
487 | void |
453 | SvUPGRADE (data, SVt_PV); |
488 | aio_write(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
454 | SvPOK_on (data); |
489 | OutputStream fh |
455 | |
490 | UV offset |
456 | if (dataoffset < 0) |
491 | IV length |
457 | dataoffset += svlen; |
492 | SV * data |
458 | |
493 | IV dataoffset |
459 | if (dataoffset < 0 || dataoffset > svlen) |
494 | SV * callback |
460 | croak ("data offset outside of string"); |
495 | PROTOTYPE: $$$$$;$ |
461 | |
496 | CODE: |
462 | if (ix == REQ_WRITE) |
497 | read_write (1, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
463 | { |
|
|
464 | /* write: check length and adjust. */ |
|
|
465 | if (length < 0 || length + dataoffset > svlen) |
|
|
466 | length = svlen - dataoffset; |
|
|
467 | } |
|
|
468 | else |
|
|
469 | { |
|
|
470 | /* read: grow scalar as necessary */ |
|
|
471 | svptr = SvGROW (data, length + dataoffset); |
|
|
472 | } |
|
|
473 | |
|
|
474 | if (length < 0) |
|
|
475 | croak ("length must not be negative"); |
|
|
476 | |
|
|
477 | Newz (0, req, 1, aio_cb); |
|
|
478 | |
|
|
479 | if (!req) |
|
|
480 | croak ("out of memory during aio_req allocation"); |
|
|
481 | |
|
|
482 | req->type = ix; |
|
|
483 | req->fh = newSVsv (fh); |
|
|
484 | req->fd = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh)) |
|
|
485 | : IoOFP (sv_2io (fh))); |
|
|
486 | req->offset = offset; |
|
|
487 | req->length = length; |
|
|
488 | req->data = SvREFCNT_inc (data); |
|
|
489 | req->dataptr = (char *)svptr + dataoffset; |
|
|
490 | req->callback = SvREFCNT_inc (callback); |
|
|
491 | |
|
|
492 | send_req (req); |
|
|
493 | } |
498 | |
494 | |
499 | void |
495 | void |
500 | aio_readahead(fh,offset,length,callback=&PL_sv_undef) |
496 | aio_readahead(fh,offset,length,callback=&PL_sv_undef) |
501 | InputStream fh |
497 | SV * fh |
502 | UV offset |
498 | UV offset |
503 | IV length |
499 | IV length |
504 | SV * callback |
500 | SV * callback |
505 | PROTOTYPE: $$$;$ |
501 | PROTOTYPE: $$$;$ |
506 | CODE: |
502 | CODE: |
507 | { |
503 | { |
508 | aio_req req; |
504 | aio_req req; |
509 | |
505 | |
… | |
… | |
514 | |
510 | |
515 | if (!req) |
511 | if (!req) |
516 | croak ("out of memory during aio_req allocation"); |
512 | croak ("out of memory during aio_req allocation"); |
517 | |
513 | |
518 | req->type = REQ_READAHEAD; |
514 | req->type = REQ_READAHEAD; |
|
|
515 | req->fh = newSVsv (fh); |
519 | req->fd = PerlIO_fileno (fh); |
516 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
520 | req->offset = offset; |
517 | req->offset = offset; |
521 | req->length = length; |
518 | req->length = length; |
522 | req->callback = SvREFCNT_inc (callback); |
519 | req->callback = SvREFCNT_inc (callback); |
523 | |
520 | |
524 | send_req (req); |
521 | send_req (req); |
… | |
… | |
552 | req->dataptr = SvPV_nolen (req->data); |
549 | req->dataptr = SvPV_nolen (req->data); |
553 | } |
550 | } |
554 | else |
551 | else |
555 | { |
552 | { |
556 | req->type = REQ_FSTAT; |
553 | req->type = REQ_FSTAT; |
|
|
554 | req->fh = newSVsv (fh_or_path); |
557 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
555 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
558 | } |
556 | } |
559 | |
557 | |
560 | req->callback = SvREFCNT_inc (callback); |
558 | req->callback = SvREFCNT_inc (callback); |
561 | |
559 | |