… | |
… | |
3 | #include <errno.h> |
3 | #include <errno.h> |
4 | |
4 | |
5 | #include "EXTERN.h" |
5 | #include "EXTERN.h" |
6 | #include "perl.h" |
6 | #include "perl.h" |
7 | #include "XSUB.h" |
7 | #include "XSUB.h" |
|
|
8 | |
|
|
9 | #include "schmorp.h" |
8 | |
10 | |
9 | #include <stddef.h> |
11 | #include <stddef.h> |
10 | #include <stdlib.h> |
12 | #include <stdlib.h> |
11 | #include <errno.h> |
13 | #include <errno.h> |
12 | #include <sys/types.h> |
14 | #include <sys/types.h> |
13 | #include <sys/stat.h> |
15 | #include <sys/stat.h> |
|
|
16 | #include <sys/statvfs.h> |
14 | #include <limits.h> |
17 | #include <limits.h> |
15 | #include <fcntl.h> |
18 | #include <fcntl.h> |
16 | #include <sched.h> |
19 | #include <sched.h> |
|
|
20 | |
|
|
21 | #if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES |
|
|
22 | # include <sys/mman.h> |
|
|
23 | #endif |
17 | |
24 | |
18 | /* perl namespace pollution */ |
25 | /* perl namespace pollution */ |
19 | #undef VERSION |
26 | #undef VERSION |
20 | |
27 | |
21 | #ifdef _WIN32 |
28 | #ifdef _WIN32 |
… | |
… | |
97 | #define expect_false(expr) expect ((expr) != 0, 0) |
104 | #define expect_false(expr) expect ((expr) != 0, 0) |
98 | #define expect_true(expr) expect ((expr) != 0, 1) |
105 | #define expect_true(expr) expect ((expr) != 0, 1) |
99 | |
106 | |
100 | /*****************************************************************************/ |
107 | /*****************************************************************************/ |
101 | |
108 | |
102 | static HV *stash; |
|
|
103 | typedef SV SV8; /* byte-sv, used for argument-checking */ |
109 | typedef SV SV8; /* byte-sv, used for argument-checking */ |
104 | typedef int aio_rfd; /* read file desriptor */ |
110 | typedef int aio_rfd; /* read file desriptor */ |
105 | typedef int aio_wfd; /* write file descriptor */ |
111 | typedef int aio_wfd; /* write file descriptor */ |
106 | |
112 | |
107 | #define AIO_REQ_KLASS "IO::AIO::REQ" |
113 | static HV *aio_stash, *aio_req_stash, *aio_grp_stash; |
108 | #define AIO_GRP_KLASS "IO::AIO::GRP" |
|
|
109 | |
114 | |
110 | #define EIO_REQ_MEMBERS \ |
115 | #define EIO_REQ_MEMBERS \ |
111 | SV *callback; \ |
116 | SV *callback; \ |
112 | SV *sv1, *sv2; \ |
117 | SV *sv1, *sv2; \ |
113 | STRLEN stroffset; \ |
118 | STRLEN stroffset; \ |
114 | SV *self; |
119 | SV *self; |
115 | |
120 | |
116 | #define EIO_NO_WRAPPERS 1 |
121 | #define EIO_NO_WRAPPERS 1 |
117 | |
122 | |
|
|
123 | #include "libeio/config.h" |
118 | #include "libeio/eio.h" |
124 | #include "libeio/eio.h" |
119 | |
125 | |
120 | #ifndef POSIX_FADV_NORMAL |
126 | #ifndef POSIX_FADV_NORMAL |
121 | # define POSIX_FADV_NORMAL 0 |
127 | # define POSIX_FADV_NORMAL 0 |
122 | # define NO_FADVISE 1 |
|
|
123 | #endif |
128 | #endif |
124 | |
|
|
125 | #ifndef POSIX_FADV_SEQUENTIAL |
129 | #ifndef POSIX_FADV_SEQUENTIAL |
126 | # define POSIX_FADV_SEQUENTIAL 0 |
130 | # define POSIX_FADV_SEQUENTIAL 0 |
127 | #endif |
131 | #endif |
128 | |
|
|
129 | #ifndef POSIX_FADV_RANDOM |
132 | #ifndef POSIX_FADV_RANDOM |
130 | # define POSIX_FADV_RANDOM 0 |
133 | # define POSIX_FADV_RANDOM 0 |
131 | #endif |
134 | #endif |
132 | |
|
|
133 | #ifndef POSIX_FADV_NOREUSE |
135 | #ifndef POSIX_FADV_NOREUSE |
134 | # define POSIX_FADV_NOREUSE 0 |
136 | # define POSIX_FADV_NOREUSE 0 |
135 | #endif |
137 | #endif |
136 | |
|
|
137 | #ifndef POSIX_FADV_WILLNEED |
138 | #ifndef POSIX_FADV_WILLNEED |
138 | # define POSIX_FADV_WILLNEED 0 |
139 | # define POSIX_FADV_WILLNEED 0 |
139 | #endif |
140 | #endif |
140 | |
|
|
141 | #ifndef POSIX_FADV_DONTNEED |
141 | #ifndef POSIX_FADV_DONTNEED |
142 | # define POSIX_FADV_DONTNEED 0 |
142 | # define POSIX_FADV_DONTNEED 0 |
|
|
143 | #endif |
|
|
144 | |
|
|
145 | #if !HAVE_POSIX_FADVISE |
|
|
146 | # define posix_fadvise(a,b,c,d) errno = ENOSYS /* also return ENOSYS */ |
|
|
147 | #endif |
|
|
148 | |
|
|
149 | #ifndef POSIX_MADV_NORMAL |
|
|
150 | # define POSIX_MADV_NORMAL 0 |
|
|
151 | #endif |
|
|
152 | #ifndef POSIX_MADV_SEQUENTIAL |
|
|
153 | # define POSIX_MADV_SEQUENTIAL 0 |
|
|
154 | #endif |
|
|
155 | #ifndef POSIX_MADV_RANDOM |
|
|
156 | # define POSIX_MADV_RANDOM 0 |
|
|
157 | #endif |
|
|
158 | #ifndef POSIX_MADV_WILLNEED |
|
|
159 | # define POSIX_MADV_WILLNEED 0 |
|
|
160 | #endif |
|
|
161 | #ifndef POSIX_MADV_DONTNEED |
|
|
162 | # define POSIX_MADV_DONTNEED 0 |
|
|
163 | #endif |
|
|
164 | |
|
|
165 | #if !HAVE_POSIX_MADVISE |
|
|
166 | # define posix_madvise(a,b,c) errno = ENOSYS /* also return ENOSYS */ |
|
|
167 | #endif |
|
|
168 | |
|
|
169 | #ifndef PROT_NONE |
|
|
170 | # define PROT_NONE 0 |
|
|
171 | #endif |
|
|
172 | #ifndef PROT_READ |
|
|
173 | # define PROT_READ 0 |
|
|
174 | #endif |
|
|
175 | #ifndef PROT_WRITE |
|
|
176 | # define PROT_READ 0 |
|
|
177 | #endif |
|
|
178 | #ifndef PROT_EXEC |
|
|
179 | # define PROT_EXEC 0 |
|
|
180 | #endif |
|
|
181 | |
|
|
182 | #ifndef ST_NODEV |
|
|
183 | # define ST_NODEV 0 |
|
|
184 | #endif |
|
|
185 | #ifndef ST_NOEXEC |
|
|
186 | # define ST_NOEXEC 0 |
|
|
187 | #endif |
|
|
188 | #ifndef ST_SYNCHRONOUS |
|
|
189 | # define ST_SYNCHRONOUS 0 |
|
|
190 | #endif |
|
|
191 | #ifndef ST_MANDLOCK |
|
|
192 | # define ST_MANDLOCK 0 |
|
|
193 | #endif |
|
|
194 | #ifndef ST_WRITE |
|
|
195 | # define ST_WRITE 0 |
|
|
196 | #endif |
|
|
197 | #ifndef ST_APPEND |
|
|
198 | # define ST_APPEND 0 |
|
|
199 | #endif |
|
|
200 | #ifndef ST_IMMUTABLE |
|
|
201 | # define ST_IMMUTABLE 0 |
|
|
202 | #endif |
|
|
203 | #ifndef ST_NOATIME |
|
|
204 | # define ST_NOATIME 0 |
|
|
205 | #endif |
|
|
206 | #ifndef ST_NODIRATIME |
|
|
207 | # define ST_NODIRATIME 0 |
|
|
208 | #endif |
|
|
209 | #ifndef ST_RELATIME |
|
|
210 | # define ST_RELATIME 0 |
|
|
211 | #endif |
|
|
212 | |
|
|
213 | #ifndef MAP_ANONYMOUS |
|
|
214 | # ifdef MAP_ANON |
|
|
215 | # define MAP_ANONYMOUS MAP_ANON |
|
|
216 | # else |
|
|
217 | # define MAP_ANONYMOUS MAP_FIXED /* and hope this fails */ |
|
|
218 | # endif |
|
|
219 | #endif |
|
|
220 | #ifndef MAP_HUGETLB |
|
|
221 | # define MAP_HUGETLB 0 |
|
|
222 | #endif |
|
|
223 | #ifndef MAP_LOCKED |
|
|
224 | # define MAP_LOCKED 0 |
|
|
225 | #endif |
|
|
226 | #ifndef MAP_NORESERVE |
|
|
227 | # define MAP_NORESERVE 0 |
|
|
228 | #endif |
|
|
229 | #ifndef MAP_POPULATE |
|
|
230 | # define MAP_POPULATE 0 |
|
|
231 | #endif |
|
|
232 | #ifndef MAP_NONBLOCK |
|
|
233 | # define MAP_NONBLOCK 0 |
|
|
234 | #endif |
|
|
235 | |
|
|
236 | #ifndef PAGESIZE |
|
|
237 | # define PAGESIZE sysconf (_SC_PAGESIZE) |
143 | #endif |
238 | #endif |
144 | |
239 | |
145 | static int req_invoke (eio_req *req); |
240 | static int req_invoke (eio_req *req); |
146 | #define EIO_FINISH(req) req_invoke (req) |
241 | #define EIO_FINISH(req) req_invoke (req) |
147 | static void req_destroy (eio_req *grp); |
242 | static void req_destroy (eio_req *grp); |
… | |
… | |
158 | |
253 | |
159 | static SV *on_next_submit; |
254 | static SV *on_next_submit; |
160 | static int next_pri = EIO_PRI_DEFAULT; |
255 | static int next_pri = EIO_PRI_DEFAULT; |
161 | static int max_outstanding; |
256 | static int max_outstanding; |
162 | |
257 | |
163 | static int respipe_osf [2], respipe [2] = { -1, -1 }; |
258 | static s_epipe respipe; |
164 | |
259 | |
165 | static void req_destroy (aio_req req); |
260 | static void req_destroy (aio_req req); |
166 | static void req_cancel (aio_req req); |
261 | static void req_cancel (aio_req req); |
167 | |
262 | |
168 | static void want_poll (void) |
263 | static void want_poll (void) |
169 | { |
264 | { |
170 | /* write a dummy byte to the pipe so fh becomes ready */ |
265 | /* write a dummy byte to the pipe so fh becomes ready */ |
171 | respipe_write (respipe_osf [1], (const void *)&respipe_osf, 1); |
266 | s_epipe_signal (&respipe); |
172 | } |
267 | } |
173 | |
268 | |
174 | static void done_poll (void) |
269 | static void done_poll (void) |
175 | { |
270 | { |
176 | /* read any signals sent by the worker threads */ |
271 | /* read any signals sent by the worker threads */ |
177 | char buf [4]; |
272 | s_epipe_drain (&respipe); |
178 | while (respipe_read (respipe [0], buf, 4) == 4) |
|
|
179 | ; |
|
|
180 | } |
273 | } |
181 | |
274 | |
182 | /* must be called at most once */ |
275 | /* must be called at most once */ |
183 | static SV *req_sv (aio_req req, const char *klass) |
276 | static SV *req_sv (aio_req req, HV *stash) |
184 | { |
277 | { |
185 | if (!req->self) |
278 | if (!req->self) |
186 | { |
279 | { |
187 | req->self = (SV *)newHV (); |
280 | req->self = (SV *)newHV (); |
188 | sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0); |
281 | sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0); |
189 | } |
282 | } |
190 | |
283 | |
191 | return sv_2mortal (sv_bless (newRV_inc (req->self), gv_stashpv (klass, 1))); |
284 | return sv_2mortal (sv_bless (newRV_inc (req->self), stash)); |
192 | } |
285 | } |
193 | |
286 | |
194 | static aio_req SvAIO_REQ (SV *sv) |
287 | static aio_req SvAIO_REQ (SV *sv) |
195 | { |
288 | { |
196 | MAGIC *mg; |
289 | MAGIC *mg; |
197 | |
290 | |
198 | if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv)) |
291 | if (!SvROK (sv) |
|
|
292 | || (SvSTASH (SvRV (sv)) != aio_grp_stash |
|
|
293 | && SvSTASH (SvRV (sv)) != aio_req_stash |
|
|
294 | && !sv_derived_from (sv, "IO::AIO::REQ"))) |
199 | croak ("object of class " AIO_REQ_KLASS " expected"); |
295 | croak ("object of class IO::AIO::REQ expected"); |
200 | |
296 | |
201 | mg = mg_find (SvRV (sv), PERL_MAGIC_ext); |
297 | mg = mg_find (SvRV (sv), PERL_MAGIC_ext); |
202 | |
298 | |
203 | return mg ? (aio_req)mg->mg_ptr : 0; |
299 | return mg ? (aio_req)mg->mg_ptr : 0; |
204 | } |
300 | } |
… | |
… | |
210 | dSP; |
306 | dSP; |
211 | |
307 | |
212 | ENTER; |
308 | ENTER; |
213 | SAVETMPS; |
309 | SAVETMPS; |
214 | PUSHMARK (SP); |
310 | PUSHMARK (SP); |
215 | XPUSHs (req_sv (grp, AIO_GRP_KLASS)); |
311 | XPUSHs (req_sv (grp, aio_grp_stash)); |
216 | PUTBACK; |
312 | PUTBACK; |
217 | call_sv (grp->sv2, G_VOID | G_EVAL | G_KEEPERR); |
313 | call_sv (grp->sv2, G_VOID | G_EVAL | G_KEEPERR); |
218 | SPAGAIN; |
314 | SPAGAIN; |
219 | FREETMPS; |
315 | FREETMPS; |
220 | LEAVE; |
316 | LEAVE; |
… | |
… | |
238 | } |
334 | } |
239 | } |
335 | } |
240 | |
336 | |
241 | static int req_invoke (eio_req *req) |
337 | static int req_invoke (eio_req *req) |
242 | { |
338 | { |
243 | dSP; |
|
|
244 | |
|
|
245 | if (req->flags & FLAG_SV2_RO_OFF) |
339 | if (req->flags & FLAG_SV2_RO_OFF) |
246 | SvREADONLY_off (req->sv2); |
340 | SvREADONLY_off (req->sv2); |
247 | |
341 | |
248 | if (!EIO_CANCELLED (req) && req->callback) |
342 | if (!EIO_CANCELLED (req) && req->callback) |
249 | { |
343 | { |
|
|
344 | dSP; |
|
|
345 | static SV *sv_result_cache; /* caches the result integer SV */ |
|
|
346 | SV *sv_result; |
|
|
347 | |
250 | ENTER; |
348 | ENTER; |
251 | SAVETMPS; |
349 | SAVETMPS; |
252 | PUSHMARK (SP); |
350 | PUSHMARK (SP); |
253 | EXTEND (SP, 1); |
351 | EXTEND (SP, 1); |
|
|
352 | |
|
|
353 | /* do not recreate the result IV from scratch each time */ |
|
|
354 | if (expect_true (sv_result_cache)) |
|
|
355 | { |
|
|
356 | sv_result = sv_result_cache; sv_result_cache = 0; |
|
|
357 | SvIV_set (sv_result, req->result); |
|
|
358 | SvIOK_only (sv_result); |
|
|
359 | } |
|
|
360 | else |
|
|
361 | { |
|
|
362 | sv_result = newSViv (req->result); |
|
|
363 | SvREADONLY_on (sv_result); |
|
|
364 | } |
254 | |
365 | |
255 | switch (req->type) |
366 | switch (req->type) |
256 | { |
367 | { |
257 | case EIO_READDIR: |
368 | case EIO_READDIR: |
258 | { |
369 | { |
… | |
… | |
326 | int flags = req->int1 & (O_RDONLY | O_WRONLY | O_RDWR); |
437 | int flags = req->int1 & (O_RDONLY | O_WRONLY | O_RDWR); |
327 | char sym [64]; |
438 | char sym [64]; |
328 | int symlen; |
439 | int symlen; |
329 | |
440 | |
330 | symlen = snprintf (sym, sizeof (sym), "fd#%d", (int)req->result); |
441 | symlen = snprintf (sym, sizeof (sym), "fd#%d", (int)req->result); |
331 | gv_init (gv, stash, sym, symlen, 0); |
442 | gv_init (gv, aio_stash, sym, symlen, 0); |
332 | |
443 | |
333 | symlen = snprintf ( |
444 | symlen = snprintf ( |
334 | sym, |
445 | sym, |
335 | sizeof (sym), |
446 | sizeof (sym), |
336 | "%s&=%d", |
447 | "%s&=%d", |
… | |
… | |
344 | |
455 | |
345 | PUSHs (fh); |
456 | PUSHs (fh); |
346 | } |
457 | } |
347 | break; |
458 | break; |
348 | |
459 | |
|
|
460 | case EIO_STATVFS: |
|
|
461 | case EIO_FSTATVFS: |
|
|
462 | { |
|
|
463 | SV *rv = &PL_sv_undef; |
|
|
464 | |
|
|
465 | if (req->result >= 0) |
|
|
466 | { |
|
|
467 | EIO_STRUCT_STATVFS *f = EIO_STATVFS_BUF (req); |
|
|
468 | HV *hv = newHV (); |
|
|
469 | |
|
|
470 | rv = sv_2mortal (newRV_noinc ((SV *)hv)); |
|
|
471 | |
|
|
472 | hv_store (hv, "bsize" , sizeof ("bsize" ) - 1, newSVval64 (f->f_bsize ), 0); |
|
|
473 | hv_store (hv, "frsize" , sizeof ("frsize" ) - 1, newSVval64 (f->f_frsize ), 0); |
|
|
474 | hv_store (hv, "blocks" , sizeof ("blocks" ) - 1, newSVval64 (f->f_blocks ), 0); |
|
|
475 | hv_store (hv, "bfree" , sizeof ("bfree" ) - 1, newSVval64 (f->f_bfree ), 0); |
|
|
476 | hv_store (hv, "bavail" , sizeof ("bavail" ) - 1, newSVval64 (f->f_bavail ), 0); |
|
|
477 | hv_store (hv, "files" , sizeof ("files" ) - 1, newSVval64 (f->f_files ), 0); |
|
|
478 | hv_store (hv, "ffree" , sizeof ("ffree" ) - 1, newSVval64 (f->f_ffree ), 0); |
|
|
479 | hv_store (hv, "favail" , sizeof ("favail" ) - 1, newSVval64 (f->f_favail ), 0); |
|
|
480 | hv_store (hv, "fsid" , sizeof ("fsid" ) - 1, newSVval64 (f->f_fsid ), 0); |
|
|
481 | hv_store (hv, "flag" , sizeof ("flag" ) - 1, newSVval64 (f->f_flag ), 0); |
|
|
482 | hv_store (hv, "namemax", sizeof ("namemax") - 1, newSVval64 (f->f_namemax), 0); |
|
|
483 | } |
|
|
484 | |
|
|
485 | PUSHs (rv); |
|
|
486 | } |
|
|
487 | |
|
|
488 | break; |
|
|
489 | |
349 | case EIO_GROUP: |
490 | case EIO_GROUP: |
350 | req->int1 = 2; /* mark group as finished */ |
491 | req->int1 = 2; /* mark group as finished */ |
351 | |
492 | |
352 | if (req->sv1) |
493 | if (req->sv1) |
353 | { |
494 | { |
… | |
… | |
373 | case EIO_LSTAT: |
514 | case EIO_LSTAT: |
374 | case EIO_FSTAT: |
515 | case EIO_FSTAT: |
375 | PL_laststype = req->type == EIO_LSTAT ? OP_LSTAT : OP_STAT; |
516 | PL_laststype = req->type == EIO_LSTAT ? OP_LSTAT : OP_STAT; |
376 | PL_laststatval = req->result; |
517 | PL_laststatval = req->result; |
377 | PL_statcache = *(EIO_STRUCT_STAT *)(req->ptr2); |
518 | PL_statcache = *(EIO_STRUCT_STAT *)(req->ptr2); |
378 | PUSHs (sv_2mortal (newSViv (req->result))); |
519 | PUSHs (sv_result); |
379 | break; |
520 | break; |
380 | |
521 | |
381 | case EIO_READ: |
522 | case EIO_READ: |
382 | { |
523 | { |
383 | SvCUR_set (req->sv2, req->stroffset + (req->result > 0 ? req->result : 0)); |
524 | SvCUR_set (req->sv2, req->stroffset + (req->result > 0 ? req->result : 0)); |
384 | *SvEND (req->sv2) = 0; |
525 | *SvEND (req->sv2) = 0; |
385 | SvPOK_only (req->sv2); |
526 | SvPOK_only (req->sv2); |
386 | SvSETMAGIC (req->sv2); |
527 | SvSETMAGIC (req->sv2); |
387 | PUSHs (sv_2mortal (newSViv (req->result))); |
528 | PUSHs (sv_result); |
388 | } |
529 | } |
389 | break; |
530 | break; |
390 | |
531 | |
391 | case EIO_DUP2: |
532 | case EIO_DUP2: /* EIO_DUP2 actually means aio_close(), so fudge result value */ |
392 | if (req->result > 0) |
533 | if (req->result > 0) |
393 | req->result = 0; |
534 | SvIV_set (sv_result, 0); |
394 | /* FALLTHROUGH */ |
535 | /* FALLTHROUGH */ |
395 | |
536 | |
396 | default: |
537 | default: |
397 | PUSHs (sv_2mortal (newSViv (req->result))); |
538 | PUSHs (sv_result); |
398 | break; |
539 | break; |
399 | } |
540 | } |
400 | |
541 | |
401 | errno = req->errorno; |
542 | errno = req->errorno; |
402 | |
543 | |
403 | PUTBACK; |
544 | PUTBACK; |
404 | call_sv (req->callback, G_VOID | G_EVAL | G_DISCARD); |
545 | call_sv (req->callback, G_VOID | G_EVAL | G_DISCARD); |
405 | SPAGAIN; |
546 | SPAGAIN; |
|
|
547 | |
|
|
548 | if (expect_false (SvREFCNT (sv_result) != 1 || sv_result_cache)) |
|
|
549 | SvREFCNT_dec (sv_result); |
|
|
550 | else |
|
|
551 | sv_result_cache = sv_result; |
406 | |
552 | |
407 | FREETMPS; |
553 | FREETMPS; |
408 | LEAVE; |
554 | LEAVE; |
409 | |
555 | |
410 | PUTBACK; |
556 | PUTBACK; |
… | |
… | |
439 | grp->sv2 = 0; |
585 | grp->sv2 = 0; |
440 | |
586 | |
441 | eio_grp_cancel (grp); |
587 | eio_grp_cancel (grp); |
442 | } |
588 | } |
443 | |
589 | |
444 | #ifdef USE_SOCKETS_AS_HANDLES |
|
|
445 | # define TO_SOCKET(x) (win32_get_osfhandle (x)) |
|
|
446 | #else |
|
|
447 | # define TO_SOCKET(x) (x) |
|
|
448 | #endif |
|
|
449 | |
|
|
450 | static void |
590 | static void |
451 | create_respipe (void) |
591 | create_respipe (void) |
452 | { |
592 | { |
453 | int old_readfd = respipe [0]; |
|
|
454 | |
|
|
455 | if (respipe [1] >= 0) |
|
|
456 | respipe_close (TO_SOCKET (respipe [1])); |
|
|
457 | |
|
|
458 | #ifdef _WIN32 |
|
|
459 | if (PerlSock_socketpair (AF_UNIX, SOCK_STREAM, 0, respipe)) |
|
|
460 | #else |
|
|
461 | if (pipe (respipe)) |
593 | if (s_epipe_renew (&respipe)) |
462 | #endif |
|
|
463 | croak ("unable to initialize result pipe"); |
594 | croak ("IO::AIO: unable to initialize result pipe"); |
464 | |
|
|
465 | if (old_readfd >= 0) |
|
|
466 | { |
|
|
467 | if (dup2 (TO_SOCKET (respipe [0]), TO_SOCKET (old_readfd)) < 0) |
|
|
468 | croak ("unable to initialize result pipe(2)"); |
|
|
469 | |
|
|
470 | respipe_close (respipe [0]); |
|
|
471 | respipe [0] = old_readfd; |
|
|
472 | } |
|
|
473 | |
|
|
474 | #ifdef _WIN32 |
|
|
475 | int arg = 1; |
|
|
476 | if (ioctlsocket (TO_SOCKET (respipe [0]), FIONBIO, &arg) |
|
|
477 | || ioctlsocket (TO_SOCKET (respipe [1]), FIONBIO, &arg)) |
|
|
478 | #else |
|
|
479 | if (fcntl (respipe [0], F_SETFL, O_NONBLOCK) |
|
|
480 | || fcntl (respipe [1], F_SETFL, O_NONBLOCK)) |
|
|
481 | #endif |
|
|
482 | croak ("unable to initialize result pipe(3)"); |
|
|
483 | |
|
|
484 | respipe_osf [0] = TO_SOCKET (respipe [0]); |
|
|
485 | respipe_osf [1] = TO_SOCKET (respipe [1]); |
|
|
486 | } |
595 | } |
487 | |
596 | |
488 | static void poll_wait (void) |
597 | static void poll_wait (void) |
489 | { |
598 | { |
490 | fd_set rfd; |
|
|
491 | |
|
|
492 | while (eio_nreqs ()) |
599 | while (eio_nreqs ()) |
493 | { |
600 | { |
494 | int size; |
601 | int size; |
495 | |
602 | |
496 | X_LOCK (reslock); |
603 | X_LOCK (reslock); |
… | |
… | |
500 | if (size) |
607 | if (size) |
501 | return; |
608 | return; |
502 | |
609 | |
503 | etp_maybe_start_thread (); |
610 | etp_maybe_start_thread (); |
504 | |
611 | |
505 | FD_ZERO (&rfd); |
612 | s_epipe_wait (&respipe); |
506 | FD_SET (respipe [0], &rfd); |
|
|
507 | |
|
|
508 | PerlSock_select (respipe [0] + 1, &rfd, 0, 0, 0); |
|
|
509 | } |
613 | } |
510 | } |
614 | } |
511 | |
615 | |
512 | static int poll_cb (void) |
616 | static int poll_cb (void) |
513 | { |
617 | { |
… | |
… | |
528 | static void atfork_child (void) |
632 | static void atfork_child (void) |
529 | { |
633 | { |
530 | create_respipe (); |
634 | create_respipe (); |
531 | } |
635 | } |
532 | |
636 | |
|
|
637 | /*****************************************************************************/ |
|
|
638 | |
|
|
639 | #if !_POSIX_MAPPED_FILES |
|
|
640 | # define mmap(addr,length,prot,flags,fd,offs) (errno = ENOSYS, -1) |
|
|
641 | # define munmap(addr,length) (errno = ENOSYS, -1) |
|
|
642 | #endif |
|
|
643 | |
|
|
644 | #if !_POSIX_MEMORY_PROTECTION |
|
|
645 | # define mprotect(addr,len,prot) (errno = ENOSYS, -1) |
|
|
646 | # define PROT_NONE 0 |
|
|
647 | # define PROT_WRITE 0 |
|
|
648 | # define MAP_PRIVATE 0 |
|
|
649 | # define MAP_SHARED 0 |
|
|
650 | # define MAP_FIXED 0 |
|
|
651 | #endif |
|
|
652 | |
|
|
653 | #define MMAP_MAGIC PERL_MAGIC_ext |
|
|
654 | |
|
|
655 | static int |
|
|
656 | mmap_free (pTHX_ SV *sv, MAGIC *mg) |
|
|
657 | { |
|
|
658 | int old_errno = errno; |
|
|
659 | munmap (mg->mg_ptr, (size_t)mg->mg_obj); |
|
|
660 | errno = old_errno; |
|
|
661 | |
|
|
662 | mg->mg_obj = 0; /* just in case */ |
|
|
663 | |
|
|
664 | SvREADONLY_off (sv); |
|
|
665 | |
|
|
666 | if (SvPVX (sv) != mg->mg_ptr) |
|
|
667 | croak ("ERROR: IO::AIO::mmap-mapped scalar changed location, detected"); |
|
|
668 | |
|
|
669 | SvCUR_set (sv, 0); |
|
|
670 | SvPVX (sv) = 0; |
|
|
671 | SvOK_off (sv); |
|
|
672 | |
|
|
673 | return 0; |
|
|
674 | } |
|
|
675 | |
|
|
676 | static MGVTBL mmap_vtbl = { |
|
|
677 | 0, 0, 0, 0, mmap_free |
|
|
678 | }; |
|
|
679 | |
|
|
680 | /*****************************************************************************/ |
|
|
681 | |
533 | static SV * |
682 | static SV * |
534 | get_cb (SV *cb_sv) |
683 | get_cb (SV *cb_sv) |
535 | { |
684 | { |
536 | HV *st; |
685 | SvGETMAGIC (cb_sv); |
537 | GV *gvp; |
686 | return SvOK (cb_sv) ? s_get_cv_croak (cb_sv) : 0; |
538 | CV *cv; |
|
|
539 | |
|
|
540 | if (!SvOK (cb_sv)) |
|
|
541 | return 0; |
|
|
542 | |
|
|
543 | cv = sv_2cv (cb_sv, &st, &gvp, 0); |
|
|
544 | |
|
|
545 | if (!cv) |
|
|
546 | croak ("IO::AIO callback must be undef or a CODE reference"); |
|
|
547 | |
|
|
548 | return (SV *)cv; |
|
|
549 | } |
687 | } |
550 | |
688 | |
551 | #define dREQ \ |
689 | #define dREQ \ |
552 | SV *cb_cv; \ |
690 | SV *cb_cv; \ |
553 | aio_req req; \ |
691 | aio_req req; \ |
… | |
… | |
567 | PUTBACK; \ |
705 | PUTBACK; \ |
568 | req_submit (req); \ |
706 | req_submit (req); \ |
569 | SPAGAIN; \ |
707 | SPAGAIN; \ |
570 | \ |
708 | \ |
571 | if (GIMME_V != G_VOID) \ |
709 | if (GIMME_V != G_VOID) \ |
572 | XPUSHs (req_sv (req, AIO_REQ_KLASS)); |
710 | XPUSHs (req_sv (req, aio_req_stash)); |
573 | |
|
|
574 | static int |
|
|
575 | extract_fd (SV *fh, int wr) |
|
|
576 | { |
|
|
577 | int fd = PerlIO_fileno (wr ? IoOFP (sv_2io (fh)) : IoIFP (sv_2io (fh))); |
|
|
578 | |
|
|
579 | if (fd < 0) |
|
|
580 | croak ("illegal fh argument, either not an OS file or read/write mode mismatch"); |
|
|
581 | |
|
|
582 | return fd; |
|
|
583 | } |
|
|
584 | |
711 | |
585 | MODULE = IO::AIO PACKAGE = IO::AIO |
712 | MODULE = IO::AIO PACKAGE = IO::AIO |
586 | |
713 | |
587 | PROTOTYPES: ENABLE |
714 | PROTOTYPES: ENABLE |
588 | |
715 | |
… | |
… | |
590 | { |
717 | { |
591 | static const struct { |
718 | static const struct { |
592 | const char *name; |
719 | const char *name; |
593 | IV iv; |
720 | IV iv; |
594 | } *civ, const_iv[] = { |
721 | } *civ, const_iv[] = { |
595 | # define const_iv(name, value) { # name, (IV) value }, |
722 | # define const_niv(name, value) { # name, (IV) value }, |
|
|
723 | # define const_iv(name) { # name, (IV) name }, |
596 | # define const_eio(name) { # name, (IV) EIO_ ## name }, |
724 | # define const_eio(name) { # name, (IV) EIO_ ## name }, |
597 | const_iv (EXDEV , EXDEV) |
725 | const_iv (EXDEV) |
598 | const_iv (ENOSYS , ENOSYS) |
726 | const_iv (ENOSYS) |
599 | const_iv (O_RDONLY, O_RDONLY) |
727 | const_iv (O_RDONLY) |
600 | const_iv (O_WRONLY, O_WRONLY) |
728 | const_iv (O_WRONLY) |
|
|
729 | const_iv (O_RDWR) |
601 | const_iv (O_CREAT , O_CREAT) |
730 | const_iv (O_CREAT) |
602 | const_iv (O_TRUNC , O_TRUNC) |
731 | const_iv (O_TRUNC) |
|
|
732 | const_iv (O_EXCL) |
|
|
733 | const_iv (O_APPEND) |
603 | #ifndef _WIN32 |
734 | #ifndef _WIN32 |
604 | const_iv (S_IFIFO , S_IFIFO) |
735 | const_iv (S_IFIFO) |
605 | #endif |
736 | #endif |
606 | const_iv (FADV_NORMAL , POSIX_FADV_NORMAL) |
737 | const_niv (FADV_NORMAL , POSIX_FADV_NORMAL) |
607 | const_iv (FADV_SEQUENTIAL, POSIX_FADV_SEQUENTIAL) |
738 | const_niv (FADV_SEQUENTIAL, POSIX_FADV_SEQUENTIAL) |
608 | const_iv (FADV_RANDOM , POSIX_FADV_RANDOM) |
739 | const_niv (FADV_RANDOM , POSIX_FADV_RANDOM) |
609 | const_iv (FADV_NOREUSE , POSIX_FADV_NOREUSE) |
740 | const_niv (FADV_NOREUSE , POSIX_FADV_NOREUSE) |
610 | const_iv (FADV_WILLNEED , POSIX_FADV_WILLNEED) |
741 | const_niv (FADV_WILLNEED , POSIX_FADV_WILLNEED) |
611 | const_iv (FADV_DONTNEED , POSIX_FADV_DONTNEED) |
742 | const_niv (FADV_DONTNEED , POSIX_FADV_DONTNEED) |
|
|
743 | |
|
|
744 | const_niv (MADV_NORMAL , POSIX_MADV_NORMAL) |
|
|
745 | const_niv (MADV_SEQUENTIAL, POSIX_MADV_SEQUENTIAL) |
|
|
746 | const_niv (MADV_RANDOM , POSIX_MADV_RANDOM) |
|
|
747 | const_niv (MADV_WILLNEED , POSIX_MADV_WILLNEED) |
|
|
748 | const_niv (MADV_DONTNEED , POSIX_MADV_DONTNEED) |
|
|
749 | |
|
|
750 | const_iv (ST_RDONLY) |
|
|
751 | const_iv (ST_NOSUID) |
|
|
752 | const_iv (ST_NODEV) |
|
|
753 | const_iv (ST_NOEXEC) |
|
|
754 | const_iv (ST_SYNCHRONOUS) |
|
|
755 | const_iv (ST_MANDLOCK) |
|
|
756 | const_iv (ST_WRITE) |
|
|
757 | const_iv (ST_APPEND) |
|
|
758 | const_iv (ST_IMMUTABLE) |
|
|
759 | const_iv (ST_NOATIME) |
|
|
760 | const_iv (ST_NODIRATIME) |
|
|
761 | const_iv (ST_RELATIME) |
|
|
762 | |
|
|
763 | const_iv (PROT_NONE) |
|
|
764 | const_iv (PROT_EXEC) |
|
|
765 | const_iv (PROT_READ) |
|
|
766 | const_iv (PROT_WRITE) |
|
|
767 | |
|
|
768 | /*const_iv (MAP_FIXED)*/ |
|
|
769 | const_iv (MAP_PRIVATE) |
|
|
770 | const_iv (MAP_SHARED) |
|
|
771 | const_iv (MAP_ANONYMOUS) |
|
|
772 | |
|
|
773 | /* linuxish */ |
|
|
774 | const_iv (MAP_HUGETLB) |
|
|
775 | const_iv (MAP_LOCKED) |
|
|
776 | const_iv (MAP_NORESERVE) |
|
|
777 | const_iv (MAP_POPULATE) |
|
|
778 | const_iv (MAP_NONBLOCK) |
|
|
779 | |
|
|
780 | const_eio (MCL_FUTURE) |
|
|
781 | const_eio (MCL_CURRENT) |
|
|
782 | |
|
|
783 | const_eio (MS_ASYNC) |
|
|
784 | const_eio (MS_INVALIDATE) |
|
|
785 | const_eio (MS_SYNC) |
|
|
786 | |
|
|
787 | const_eio (MT_MODIFY) |
612 | |
788 | |
613 | const_eio (SYNC_FILE_RANGE_WAIT_BEFORE) |
789 | const_eio (SYNC_FILE_RANGE_WAIT_BEFORE) |
614 | const_eio (SYNC_FILE_RANGE_WRITE) |
790 | const_eio (SYNC_FILE_RANGE_WRITE) |
615 | const_eio (SYNC_FILE_RANGE_WAIT_AFTER) |
791 | const_eio (SYNC_FILE_RANGE_WAIT_AFTER) |
616 | |
792 | |
… | |
… | |
628 | const_eio (DT_LNK) |
804 | const_eio (DT_LNK) |
629 | const_eio (DT_SOCK) |
805 | const_eio (DT_SOCK) |
630 | const_eio (DT_WHT) |
806 | const_eio (DT_WHT) |
631 | }; |
807 | }; |
632 | |
808 | |
633 | stash = gv_stashpv ("IO::AIO", 1); |
809 | aio_stash = gv_stashpv ("IO::AIO" , 1); |
|
|
810 | aio_req_stash = gv_stashpv ("IO::AIO::REQ", 1); |
|
|
811 | aio_grp_stash = gv_stashpv ("IO::AIO::GRP", 1); |
634 | |
812 | |
635 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) |
813 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) |
636 | newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); |
814 | newCONSTSUB (aio_stash, (char *)civ->name, newSViv (civ->iv)); |
|
|
815 | |
|
|
816 | newCONSTSUB (aio_stash, "PAGESIZE", newSViv (PAGESIZE)); |
637 | |
817 | |
638 | create_respipe (); |
818 | create_respipe (); |
639 | |
819 | |
640 | if (eio_init (want_poll, done_poll) < 0) |
820 | if (eio_init (want_poll, done_poll) < 0) |
641 | croak ("IO::AIO: unable to initialise eio library"); |
821 | croak ("IO::AIO: unable to initialise eio library"); |
… | |
… | |
680 | CODE: |
860 | CODE: |
681 | max_outstanding = maxreqs; |
861 | max_outstanding = maxreqs; |
682 | |
862 | |
683 | void |
863 | void |
684 | aio_open (SV8 *pathname, int flags, int mode, SV *callback=&PL_sv_undef) |
864 | aio_open (SV8 *pathname, int flags, int mode, SV *callback=&PL_sv_undef) |
685 | PROTOTYPE: $$$;$ |
|
|
686 | PPCODE: |
865 | PPCODE: |
687 | { |
866 | { |
688 | dREQ; |
867 | dREQ; |
689 | |
868 | |
690 | req->type = EIO_OPEN; |
869 | req->type = EIO_OPEN; |
… | |
… | |
696 | REQ_SEND; |
875 | REQ_SEND; |
697 | } |
876 | } |
698 | |
877 | |
699 | void |
878 | void |
700 | aio_fsync (SV *fh, SV *callback=&PL_sv_undef) |
879 | aio_fsync (SV *fh, SV *callback=&PL_sv_undef) |
701 | PROTOTYPE: $;$ |
|
|
702 | ALIAS: |
880 | ALIAS: |
703 | aio_fsync = EIO_FSYNC |
881 | aio_fsync = EIO_FSYNC |
704 | aio_fdatasync = EIO_FDATASYNC |
882 | aio_fdatasync = EIO_FDATASYNC |
705 | PPCODE: |
883 | PPCODE: |
706 | { |
884 | { |
707 | int fd = extract_fd (fh, 0); |
885 | int fd = s_fileno_croak (fh, 0); |
708 | dREQ; |
886 | dREQ; |
709 | |
887 | |
710 | req->type = ix; |
888 | req->type = ix; |
711 | req->sv1 = newSVsv (fh); |
889 | req->sv1 = newSVsv (fh); |
712 | req->int1 = fd; |
890 | req->int1 = fd; |
… | |
… | |
714 | REQ_SEND (req); |
892 | REQ_SEND (req); |
715 | } |
893 | } |
716 | |
894 | |
717 | void |
895 | void |
718 | aio_sync_file_range (SV *fh, off_t offset, size_t nbytes, UV flags, SV *callback=&PL_sv_undef) |
896 | aio_sync_file_range (SV *fh, off_t offset, size_t nbytes, UV flags, SV *callback=&PL_sv_undef) |
719 | PROTOTYPE: $$$$;$ |
|
|
720 | PPCODE: |
897 | PPCODE: |
721 | { |
898 | { |
722 | int fd = extract_fd (fh, 0); |
899 | int fd = s_fileno_croak (fh, 0); |
723 | dREQ; |
900 | dREQ; |
724 | |
901 | |
725 | req->type = EIO_SYNC_FILE_RANGE; |
902 | req->type = EIO_SYNC_FILE_RANGE; |
726 | req->sv1 = newSVsv (fh); |
903 | req->sv1 = newSVsv (fh); |
727 | req->int1 = fd; |
904 | req->int1 = fd; |
… | |
… | |
732 | REQ_SEND (req); |
909 | REQ_SEND (req); |
733 | } |
910 | } |
734 | |
911 | |
735 | void |
912 | void |
736 | aio_close (SV *fh, SV *callback=&PL_sv_undef) |
913 | aio_close (SV *fh, SV *callback=&PL_sv_undef) |
737 | PROTOTYPE: $;$ |
|
|
738 | PPCODE: |
914 | PPCODE: |
739 | { |
915 | { |
740 | static int close_pipe = -1; /* dummy fd to close fds via dup2 */ |
916 | static int close_pipe = -1; /* dummy fd to close fds via dup2 */ |
741 | int fd = extract_fd (fh, 0); |
917 | int fd = s_fileno_croak (fh, 0); |
742 | dREQ; |
918 | dREQ; |
743 | |
919 | |
744 | if (close_pipe < 0) |
920 | if (close_pipe < 0) |
745 | { |
921 | { |
746 | int pipefd [2]; |
922 | int pipefd [2]; |
… | |
… | |
764 | void |
940 | void |
765 | aio_read (SV *fh, SV *offset, SV *length, SV8 *data, IV dataoffset, SV *callback=&PL_sv_undef) |
941 | aio_read (SV *fh, SV *offset, SV *length, SV8 *data, IV dataoffset, SV *callback=&PL_sv_undef) |
766 | ALIAS: |
942 | ALIAS: |
767 | aio_read = EIO_READ |
943 | aio_read = EIO_READ |
768 | aio_write = EIO_WRITE |
944 | aio_write = EIO_WRITE |
769 | PROTOTYPE: $$$$$;$ |
|
|
770 | PPCODE: |
945 | PPCODE: |
771 | { |
946 | { |
772 | STRLEN svlen; |
947 | STRLEN svlen; |
773 | int fd = extract_fd (fh, ix == EIO_WRITE); |
948 | int fd = s_fileno_croak (fh, ix == EIO_WRITE); |
774 | char *svptr = SvPVbyte (data, svlen); |
949 | char *svptr = SvPVbyte (data, svlen); |
775 | UV len = SvUV (length); |
950 | UV len = SvUV (length); |
776 | |
951 | |
777 | if (dataoffset < 0) |
952 | if (dataoffset < 0) |
778 | dataoffset += svlen; |
953 | dataoffset += svlen; |
… | |
… | |
815 | } |
990 | } |
816 | } |
991 | } |
817 | |
992 | |
818 | void |
993 | void |
819 | aio_readlink (SV8 *path, SV *callback=&PL_sv_undef) |
994 | aio_readlink (SV8 *path, SV *callback=&PL_sv_undef) |
820 | PROTOTYPE: $$;$ |
|
|
821 | PPCODE: |
995 | PPCODE: |
822 | { |
996 | { |
823 | SV *data; |
997 | SV *data; |
824 | dREQ; |
998 | dREQ; |
825 | |
999 | |
… | |
… | |
830 | REQ_SEND; |
1004 | REQ_SEND; |
831 | } |
1005 | } |
832 | |
1006 | |
833 | void |
1007 | void |
834 | aio_sendfile (SV *out_fh, SV *in_fh, off_t in_offset, size_t length, SV *callback=&PL_sv_undef) |
1008 | aio_sendfile (SV *out_fh, SV *in_fh, off_t in_offset, size_t length, SV *callback=&PL_sv_undef) |
835 | PROTOTYPE: $$$$;$ |
|
|
836 | PPCODE: |
1009 | PPCODE: |
837 | { |
1010 | { |
838 | int ifd = extract_fd (in_fh , 0); |
1011 | int ifd = s_fileno_croak (in_fh , 0); |
839 | int ofd = extract_fd (out_fh, 0); |
1012 | int ofd = s_fileno_croak (out_fh, 1); |
840 | dREQ; |
1013 | dREQ; |
841 | |
1014 | |
842 | req->type = EIO_SENDFILE; |
1015 | req->type = EIO_SENDFILE; |
843 | req->sv1 = newSVsv (out_fh); |
1016 | req->sv1 = newSVsv (out_fh); |
844 | req->int1 = ofd; |
1017 | req->int1 = ofd; |
… | |
… | |
850 | REQ_SEND; |
1023 | REQ_SEND; |
851 | } |
1024 | } |
852 | |
1025 | |
853 | void |
1026 | void |
854 | aio_readahead (SV *fh, off_t offset, size_t length, SV *callback=&PL_sv_undef) |
1027 | aio_readahead (SV *fh, off_t offset, size_t length, SV *callback=&PL_sv_undef) |
855 | PROTOTYPE: $$$;$ |
|
|
856 | PPCODE: |
1028 | PPCODE: |
857 | { |
1029 | { |
858 | int fd = extract_fd (fh, 0); |
1030 | int fd = s_fileno_croak (fh, 0); |
859 | dREQ; |
1031 | dREQ; |
860 | |
1032 | |
861 | req->type = EIO_READAHEAD; |
1033 | req->type = EIO_READAHEAD; |
862 | req->sv1 = newSVsv (fh); |
1034 | req->sv1 = newSVsv (fh); |
863 | req->int1 = fd; |
1035 | req->int1 = fd; |
… | |
… | |
868 | } |
1040 | } |
869 | |
1041 | |
870 | void |
1042 | void |
871 | aio_stat (SV8 *fh_or_path, SV *callback=&PL_sv_undef) |
1043 | aio_stat (SV8 *fh_or_path, SV *callback=&PL_sv_undef) |
872 | ALIAS: |
1044 | ALIAS: |
873 | aio_stat = EIO_STAT |
1045 | aio_stat = EIO_STAT |
874 | aio_lstat = EIO_LSTAT |
1046 | aio_lstat = EIO_LSTAT |
|
|
1047 | aio_statvfs = EIO_STATVFS |
875 | PPCODE: |
1048 | PPCODE: |
876 | { |
1049 | { |
877 | dREQ; |
1050 | dREQ; |
878 | |
1051 | |
879 | req->sv1 = newSVsv (fh_or_path); |
1052 | req->sv1 = newSVsv (fh_or_path); |
… | |
… | |
883 | req->type = ix; |
1056 | req->type = ix; |
884 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
1057 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
885 | } |
1058 | } |
886 | else |
1059 | else |
887 | { |
1060 | { |
888 | req->type = EIO_FSTAT; |
1061 | req->type = ix == EIO_STATVFS ? EIO_FSTATVFS : EIO_FSTAT; |
889 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
1062 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
890 | } |
1063 | } |
891 | |
1064 | |
892 | REQ_SEND; |
1065 | REQ_SEND; |
893 | } |
1066 | } |
… | |
… | |
1056 | |
1229 | |
1057 | REQ_SEND; |
1230 | REQ_SEND; |
1058 | } |
1231 | } |
1059 | |
1232 | |
1060 | void |
1233 | void |
|
|
1234 | aio_mtouch (SV8 *data, IV offset = 0, SV *length = &PL_sv_undef, int flags = 0, SV *callback=&PL_sv_undef) |
|
|
1235 | ALIAS: |
|
|
1236 | aio_mtouch = EIO_MTOUCH |
|
|
1237 | aio_msync = EIO_MSYNC |
|
|
1238 | PPCODE: |
|
|
1239 | { |
|
|
1240 | STRLEN svlen; |
|
|
1241 | char *svptr = SvPVbyte (data, svlen); |
|
|
1242 | UV len = SvUV (length); |
|
|
1243 | |
|
|
1244 | if (offset < 0) |
|
|
1245 | offset += svlen; |
|
|
1246 | |
|
|
1247 | if (offset < 0 || offset > svlen) |
|
|
1248 | croak ("offset outside of scalar"); |
|
|
1249 | |
|
|
1250 | if (!SvOK (length) || len + offset > svlen) |
|
|
1251 | len = svlen - offset; |
|
|
1252 | |
|
|
1253 | { |
|
|
1254 | dREQ; |
|
|
1255 | |
|
|
1256 | req->type = ix; |
|
|
1257 | req->sv2 = SvREFCNT_inc (data); |
|
|
1258 | req->ptr2 = (char *)svptr + offset; |
|
|
1259 | req->size = len; |
|
|
1260 | req->int1 = flags; |
|
|
1261 | |
|
|
1262 | REQ_SEND; |
|
|
1263 | } |
|
|
1264 | } |
|
|
1265 | |
|
|
1266 | void |
|
|
1267 | aio_mlock (SV8 *data, IV offset = 0, SV *length = &PL_sv_undef, SV *callback=&PL_sv_undef) |
|
|
1268 | PPCODE: |
|
|
1269 | { |
|
|
1270 | STRLEN svlen; |
|
|
1271 | char *svptr = SvPVbyte (data, svlen); |
|
|
1272 | UV len = SvUV (length); |
|
|
1273 | |
|
|
1274 | if (offset < 0) |
|
|
1275 | offset += svlen; |
|
|
1276 | |
|
|
1277 | if (offset < 0 || offset > svlen) |
|
|
1278 | croak ("offset outside of scalar"); |
|
|
1279 | |
|
|
1280 | if (!SvOK (length) || len + offset > svlen) |
|
|
1281 | len = svlen - offset; |
|
|
1282 | |
|
|
1283 | { |
|
|
1284 | dREQ; |
|
|
1285 | |
|
|
1286 | req->type = EIO_MLOCK; |
|
|
1287 | req->sv2 = SvREFCNT_inc (data); |
|
|
1288 | req->ptr2 = (char *)svptr + offset; |
|
|
1289 | req->size = len; |
|
|
1290 | |
|
|
1291 | REQ_SEND; |
|
|
1292 | } |
|
|
1293 | } |
|
|
1294 | |
|
|
1295 | void |
|
|
1296 | aio_mlockall (IV flags, SV *callback=&PL_sv_undef) |
|
|
1297 | PPCODE: |
|
|
1298 | { |
|
|
1299 | dREQ; |
|
|
1300 | |
|
|
1301 | req->type = EIO_MLOCKALL; |
|
|
1302 | req->int1 = flags; |
|
|
1303 | |
|
|
1304 | REQ_SEND; |
|
|
1305 | } |
|
|
1306 | |
|
|
1307 | void |
1061 | aio_busy (double delay, SV *callback=&PL_sv_undef) |
1308 | aio_busy (double delay, SV *callback=&PL_sv_undef) |
1062 | PPCODE: |
1309 | PPCODE: |
1063 | { |
1310 | { |
1064 | dREQ; |
1311 | dREQ; |
1065 | |
1312 | |
… | |
… | |
1069 | REQ_SEND; |
1316 | REQ_SEND; |
1070 | } |
1317 | } |
1071 | |
1318 | |
1072 | void |
1319 | void |
1073 | aio_group (SV *callback=&PL_sv_undef) |
1320 | aio_group (SV *callback=&PL_sv_undef) |
1074 | PROTOTYPE: ;$ |
|
|
1075 | PPCODE: |
1321 | PPCODE: |
1076 | { |
1322 | { |
1077 | dREQ; |
1323 | dREQ; |
1078 | |
1324 | |
1079 | req->type = EIO_GROUP; |
1325 | req->type = EIO_GROUP; |
1080 | |
1326 | |
1081 | req_submit (req); |
1327 | req_submit (req); |
1082 | XPUSHs (req_sv (req, AIO_GRP_KLASS)); |
1328 | XPUSHs (req_sv (req, aio_grp_stash)); |
1083 | } |
1329 | } |
1084 | |
1330 | |
1085 | void |
1331 | void |
1086 | aio_nop (SV *callback=&PL_sv_undef) |
1332 | aio_nop (SV *callback=&PL_sv_undef) |
1087 | ALIAS: |
1333 | ALIAS: |
… | |
… | |
1096 | REQ_SEND; |
1342 | REQ_SEND; |
1097 | } |
1343 | } |
1098 | |
1344 | |
1099 | int |
1345 | int |
1100 | aioreq_pri (int pri = 0) |
1346 | aioreq_pri (int pri = 0) |
1101 | PROTOTYPE: ;$ |
|
|
1102 | CODE: |
1347 | CODE: |
1103 | RETVAL = next_pri; |
1348 | RETVAL = next_pri; |
1104 | if (items > 0) |
1349 | if (items > 0) |
1105 | { |
1350 | { |
1106 | if (pri < EIO_PRI_MIN) pri = EIO_PRI_MIN; |
1351 | if (pri < EIO_PRI_MIN) pri = EIO_PRI_MIN; |
… | |
… | |
1118 | if (nice > EIO_PRI_MAX) nice = EIO_PRI_MAX; |
1363 | if (nice > EIO_PRI_MAX) nice = EIO_PRI_MAX; |
1119 | next_pri = nice; |
1364 | next_pri = nice; |
1120 | |
1365 | |
1121 | void |
1366 | void |
1122 | flush () |
1367 | flush () |
1123 | PROTOTYPE: |
|
|
1124 | CODE: |
1368 | CODE: |
1125 | while (eio_nreqs ()) |
1369 | while (eio_nreqs ()) |
1126 | { |
1370 | { |
1127 | poll_wait (); |
1371 | poll_wait (); |
1128 | poll_cb (); |
1372 | poll_cb (); |
1129 | } |
1373 | } |
1130 | |
1374 | |
1131 | int |
1375 | int |
1132 | poll() |
1376 | poll () |
1133 | PROTOTYPE: |
|
|
1134 | CODE: |
1377 | CODE: |
1135 | poll_wait (); |
1378 | poll_wait (); |
1136 | RETVAL = poll_cb (); |
1379 | RETVAL = poll_cb (); |
1137 | OUTPUT: |
1380 | OUTPUT: |
1138 | RETVAL |
1381 | RETVAL |
1139 | |
1382 | |
1140 | int |
1383 | int |
1141 | poll_fileno() |
1384 | poll_fileno () |
1142 | PROTOTYPE: |
|
|
1143 | CODE: |
1385 | CODE: |
1144 | RETVAL = respipe [0]; |
1386 | RETVAL = s_epipe_fd (&respipe); |
1145 | OUTPUT: |
1387 | OUTPUT: |
1146 | RETVAL |
1388 | RETVAL |
1147 | |
1389 | |
1148 | int |
1390 | int |
1149 | poll_cb(...) |
1391 | poll_cb (...) |
1150 | PROTOTYPE: |
1392 | PROTOTYPE: |
1151 | CODE: |
1393 | CODE: |
1152 | RETVAL = poll_cb (); |
1394 | RETVAL = poll_cb (); |
1153 | OUTPUT: |
1395 | OUTPUT: |
1154 | RETVAL |
1396 | RETVAL |
1155 | |
1397 | |
1156 | void |
1398 | void |
1157 | poll_wait() |
1399 | poll_wait () |
1158 | PROTOTYPE: |
|
|
1159 | CODE: |
1400 | CODE: |
1160 | poll_wait (); |
1401 | poll_wait (); |
1161 | |
1402 | |
1162 | int |
1403 | int |
1163 | nreqs() |
1404 | nreqs () |
1164 | PROTOTYPE: |
|
|
1165 | CODE: |
1405 | CODE: |
1166 | RETVAL = eio_nreqs (); |
1406 | RETVAL = eio_nreqs (); |
1167 | OUTPUT: |
1407 | OUTPUT: |
1168 | RETVAL |
1408 | RETVAL |
1169 | |
1409 | |
1170 | int |
1410 | int |
1171 | nready() |
1411 | nready () |
1172 | PROTOTYPE: |
|
|
1173 | CODE: |
1412 | CODE: |
1174 | RETVAL = eio_nready (); |
1413 | RETVAL = eio_nready (); |
1175 | OUTPUT: |
1414 | OUTPUT: |
1176 | RETVAL |
1415 | RETVAL |
1177 | |
1416 | |
1178 | int |
1417 | int |
1179 | npending() |
1418 | npending () |
1180 | PROTOTYPE: |
|
|
1181 | CODE: |
1419 | CODE: |
1182 | RETVAL = eio_npending (); |
1420 | RETVAL = eio_npending (); |
1183 | OUTPUT: |
1421 | OUTPUT: |
1184 | RETVAL |
1422 | RETVAL |
1185 | |
1423 | |
1186 | int |
1424 | int |
1187 | nthreads() |
1425 | nthreads () |
1188 | PROTOTYPE: |
|
|
1189 | CODE: |
1426 | CODE: |
1190 | RETVAL = eio_nthreads (); |
1427 | RETVAL = eio_nthreads (); |
1191 | OUTPUT: |
1428 | OUTPUT: |
1192 | RETVAL |
1429 | RETVAL |
1193 | |
1430 | |
1194 | int |
1431 | int |
1195 | fadvise (aio_rfd fh, off_t offset, off_t length, IV advice) |
1432 | fadvise (aio_rfd fh, off_t offset, off_t length, IV advice) |
1196 | PROTOTYPE: $$$$ |
|
|
1197 | CODE: |
1433 | CODE: |
1198 | #if _XOPEN_SOURCE >= 600 && !NO_FADVISE |
|
|
1199 | RETVAL = posix_fadvise (fh, offset, length, advice); |
1434 | RETVAL = posix_fadvise (fh, offset, length, advice); |
1200 | #else |
|
|
1201 | RETVAL = errno = ENOSYS; |
|
|
1202 | #endif |
|
|
1203 | OUTPUT: |
1435 | OUTPUT: |
1204 | RETVAL |
1436 | RETVAL |
1205 | |
1437 | |
1206 | ssize_t |
1438 | ssize_t |
1207 | sendfile (aio_wfd ofh, aio_rfd ifh, off_t offset, size_t count) |
1439 | sendfile (aio_wfd ofh, aio_rfd ifh, off_t offset, size_t count) |
1208 | PROTOTYPE: $$$$ |
|
|
1209 | CODE: |
1440 | CODE: |
1210 | eio_sendfile_sync (ofh, ifh, offset, count); |
1441 | RETVAL = eio_sendfile_sync (ofh, ifh, offset, count); |
|
|
1442 | OUTPUT: |
|
|
1443 | RETVAL |
|
|
1444 | |
|
|
1445 | void |
|
|
1446 | mmap (SV *scalar, size_t length, int prot, int flags, SV *fh, off_t offset = 0) |
|
|
1447 | PPCODE: |
|
|
1448 | sv_unmagic (scalar, MMAP_MAGIC); |
|
|
1449 | { |
|
|
1450 | int fd = SvOK (fh) ? s_fileno_croak (fh, flags & PROT_WRITE) : -1; |
|
|
1451 | void *addr = (void *)mmap (0, length, prot, flags, fd, offset); |
|
|
1452 | if (addr == (void *)-1) |
|
|
1453 | XSRETURN_NO; |
|
|
1454 | |
|
|
1455 | sv_force_normal (scalar); |
|
|
1456 | |
|
|
1457 | /* we store the length in mg_obj, as namlen is I32 :/ */ |
|
|
1458 | sv_magicext (scalar, 0, MMAP_MAGIC, &mmap_vtbl, (char *)addr, 0) |
|
|
1459 | ->mg_obj = (SV *)length; |
|
|
1460 | |
|
|
1461 | SvUPGRADE (scalar, SVt_PV); /* nop... */ |
|
|
1462 | |
|
|
1463 | if (!(prot & PROT_WRITE)) |
|
|
1464 | SvREADONLY_on (scalar); |
|
|
1465 | |
|
|
1466 | if (SvLEN (scalar)) |
|
|
1467 | Safefree (SvPVX (scalar)); |
|
|
1468 | |
|
|
1469 | SvPVX (scalar) = (char *)addr; |
|
|
1470 | SvCUR_set (scalar, length); |
|
|
1471 | SvLEN_set (scalar, 0); |
|
|
1472 | SvPOK_only (scalar); |
|
|
1473 | |
|
|
1474 | XSRETURN_YES; |
|
|
1475 | } |
|
|
1476 | |
|
|
1477 | void |
|
|
1478 | munmap (SV *scalar) |
|
|
1479 | CODE: |
|
|
1480 | sv_unmagic (scalar, MMAP_MAGIC); |
|
|
1481 | |
|
|
1482 | int |
|
|
1483 | madvise (SV *scalar, off_t offset = 0, SV *length = &PL_sv_undef, IV advice_or_prot) |
|
|
1484 | ALIAS: |
|
|
1485 | mprotect = 1 |
|
|
1486 | CODE: |
|
|
1487 | { |
|
|
1488 | STRLEN svlen; |
|
|
1489 | void *addr = SvPVbyte (scalar, svlen); |
|
|
1490 | size_t len = SvUV (length); |
|
|
1491 | |
|
|
1492 | if (offset < 0) |
|
|
1493 | offset += svlen; |
|
|
1494 | |
|
|
1495 | if (offset < 0 || offset > svlen) |
|
|
1496 | croak ("offset outside of scalar"); |
|
|
1497 | |
|
|
1498 | if (!SvOK (length) || len + offset > svlen) |
|
|
1499 | len = svlen - offset; |
|
|
1500 | |
|
|
1501 | addr = (void *)(((intptr_t)addr) + offset); |
|
|
1502 | eio_page_align (&addr, &len); |
|
|
1503 | |
|
|
1504 | switch (ix) |
|
|
1505 | { |
|
|
1506 | case 0: RETVAL = posix_madvise (addr, len, advice_or_prot); break; |
|
|
1507 | case 1: RETVAL = mprotect (addr, len, advice_or_prot); break; |
|
|
1508 | } |
|
|
1509 | } |
|
|
1510 | OUTPUT: |
|
|
1511 | RETVAL |
|
|
1512 | |
|
|
1513 | int |
|
|
1514 | munlock (SV *scalar, off_t offset = 0, SV *length = &PL_sv_undef) |
|
|
1515 | CODE: |
|
|
1516 | { |
|
|
1517 | STRLEN svlen; |
|
|
1518 | void *addr = SvPVbyte (scalar, svlen); |
|
|
1519 | size_t len = SvUV (length); |
|
|
1520 | |
|
|
1521 | if (offset < 0) |
|
|
1522 | offset += svlen; |
|
|
1523 | |
|
|
1524 | if (offset < 0 || offset > svlen) |
|
|
1525 | croak ("offset outside of scalar"); |
|
|
1526 | |
|
|
1527 | if (!SvOK (length) || len + offset > svlen) |
|
|
1528 | len = svlen - offset; |
|
|
1529 | |
|
|
1530 | addr = (void *)(((intptr_t)addr) + offset); |
|
|
1531 | eio_page_align (&addr, &len); |
|
|
1532 | #if _POSIX_MEMLOCK_RANGE |
|
|
1533 | RETVAL = munlock (addr, len); |
|
|
1534 | #else |
|
|
1535 | RETVAL = ((errno = ENOSYS), -1); |
|
|
1536 | #endif |
|
|
1537 | } |
|
|
1538 | OUTPUT: |
|
|
1539 | RETVAL |
|
|
1540 | |
|
|
1541 | int |
|
|
1542 | munlockall () |
|
|
1543 | CODE: |
|
|
1544 | #if _POSIX_MEMLOCK |
|
|
1545 | munlockall (); |
|
|
1546 | #else |
|
|
1547 | RETVAL = -1; |
|
|
1548 | errno = ENOSYS; |
|
|
1549 | #endif |
|
|
1550 | OUTPUT: |
|
|
1551 | RETVAL |
1211 | |
1552 | |
1212 | void _on_next_submit (SV *cb) |
1553 | void _on_next_submit (SV *cb) |
1213 | CODE: |
1554 | CODE: |
1214 | SvREFCNT_dec (on_next_submit); |
1555 | SvREFCNT_dec (on_next_submit); |
1215 | on_next_submit = SvOK (cb) ? newSVsv (cb) : 0; |
1556 | on_next_submit = SvOK (cb) ? newSVsv (cb) : 0; |
… | |
… | |
1230 | if (GIMME_V != G_VOID) |
1571 | if (GIMME_V != G_VOID) |
1231 | XPUSHs (req->callback ? sv_2mortal (newRV_inc (req->callback)) : &PL_sv_undef); |
1572 | XPUSHs (req->callback ? sv_2mortal (newRV_inc (req->callback)) : &PL_sv_undef); |
1232 | |
1573 | |
1233 | if (items > 1) |
1574 | if (items > 1) |
1234 | { |
1575 | { |
1235 | SV *cb_cv = get_cb (callback); |
1576 | SV *cb_cv =get_cb (callback); |
1236 | |
1577 | |
1237 | SvREFCNT_dec (req->callback); |
1578 | SvREFCNT_dec (req->callback); |
1238 | req->callback = SvREFCNT_inc (cb_cv); |
1579 | req->callback = SvREFCNT_inc (cb_cv); |
1239 | } |
1580 | } |
1240 | } |
1581 | } |