… | |
… | |
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_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 |
… | |
… | |
75 | |
82 | |
76 | #define EIO_STRUCT_STAT Stat_t |
83 | #define EIO_STRUCT_STAT Stat_t |
77 | |
84 | |
78 | /* use NV for 32 bit perls as it allows larger offsets */ |
85 | /* use NV for 32 bit perls as it allows larger offsets */ |
79 | #if IVSIZE >= 8 |
86 | #if IVSIZE >= 8 |
|
|
87 | # define VAL64 IV |
80 | # define SvVAL64 SvIV |
88 | # define SvVAL64 SvIV |
|
|
89 | # define newSVval64 newSViv |
81 | #else |
90 | #else |
|
|
91 | # define VAL64 NV |
82 | # define SvVAL64 SvNV |
92 | # define SvVAL64 SvNV |
|
|
93 | # define newSVval64 newSVnv |
83 | #endif |
94 | #endif |
84 | |
95 | |
85 | /*****************************************************************************/ |
96 | /*****************************************************************************/ |
86 | |
97 | |
87 | #if __GNUC__ >= 3 |
98 | #if __GNUC__ >= 3 |
… | |
… | |
93 | #define expect_false(expr) expect ((expr) != 0, 0) |
104 | #define expect_false(expr) expect ((expr) != 0, 0) |
94 | #define expect_true(expr) expect ((expr) != 0, 1) |
105 | #define expect_true(expr) expect ((expr) != 0, 1) |
95 | |
106 | |
96 | /*****************************************************************************/ |
107 | /*****************************************************************************/ |
97 | |
108 | |
98 | static HV *stash; |
|
|
99 | typedef SV SV8; /* byte-sv, used for argument-checking */ |
109 | typedef SV SV8; /* byte-sv, used for argument-checking */ |
|
|
110 | typedef int aio_rfd; /* read file desriptor */ |
|
|
111 | typedef int aio_wfd; /* write file descriptor */ |
100 | |
112 | |
101 | #define AIO_REQ_KLASS "IO::AIO::REQ" |
113 | static HV *aio_stash, *aio_req_stash, *aio_grp_stash; |
102 | #define AIO_GRP_KLASS "IO::AIO::GRP" |
|
|
103 | |
114 | |
104 | #define EIO_REQ_MEMBERS \ |
115 | #define EIO_REQ_MEMBERS \ |
105 | SV *callback; \ |
116 | SV *callback; \ |
106 | SV *sv1, *sv2; \ |
117 | SV *sv1, *sv2; \ |
107 | STRLEN stroffset; \ |
118 | STRLEN stroffset; \ |
… | |
… | |
109 | |
120 | |
110 | #define EIO_NO_WRAPPERS 1 |
121 | #define EIO_NO_WRAPPERS 1 |
111 | |
122 | |
112 | #include "libeio/eio.h" |
123 | #include "libeio/eio.h" |
113 | |
124 | |
|
|
125 | #ifndef POSIX_FADV_NORMAL |
|
|
126 | # define POSIX_FADV_NORMAL 0 |
|
|
127 | # define NO_FADVISE 1 |
|
|
128 | #endif |
|
|
129 | #ifndef POSIX_FADV_SEQUENTIAL |
|
|
130 | # define POSIX_FADV_SEQUENTIAL 0 |
|
|
131 | #endif |
|
|
132 | #ifndef POSIX_FADV_RANDOM |
|
|
133 | # define POSIX_FADV_RANDOM 0 |
|
|
134 | #endif |
|
|
135 | #ifndef POSIX_FADV_NOREUSE |
|
|
136 | # define POSIX_FADV_NOREUSE 0 |
|
|
137 | #endif |
|
|
138 | #ifndef POSIX_FADV_WILLNEED |
|
|
139 | # define POSIX_FADV_WILLNEED 0 |
|
|
140 | #endif |
|
|
141 | #ifndef POSIX_FADV_DONTNEED |
|
|
142 | # define POSIX_FADV_DONTNEED 0 |
|
|
143 | #endif |
|
|
144 | |
|
|
145 | #if _XOPEN_SOURCE < 600 || NO_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 | # define NO_MADVISE 1 |
|
|
152 | #endif |
|
|
153 | #ifndef POSIX_MADV_SEQUENTIAL |
|
|
154 | # define POSIX_MADV_SEQUENTIAL 0 |
|
|
155 | #endif |
|
|
156 | #ifndef POSIX_MADV_RANDOM |
|
|
157 | # define POSIX_MADV_RANDOM 0 |
|
|
158 | #endif |
|
|
159 | #ifndef POSIX_MADV_WILLNEED |
|
|
160 | # define POSIX_MADV_WILLNEED 0 |
|
|
161 | #endif |
|
|
162 | #ifndef POSIX_MADV_DONTNEED |
|
|
163 | # define POSIX_MADV_DONTNEED 0 |
|
|
164 | #endif |
|
|
165 | |
|
|
166 | #if _XOPEN_SOURCE < 600 || NO_MADVISE |
|
|
167 | # define posix_madvise(a,b,c) errno = ENOSYS /* also return ENOSYS */ |
|
|
168 | #endif |
|
|
169 | |
|
|
170 | #ifndef PROT_NONE |
|
|
171 | # define PROT_NONE 0 |
|
|
172 | #endif |
|
|
173 | #ifndef PROT_READ |
|
|
174 | # define PROT_READ 0 |
|
|
175 | #endif |
|
|
176 | #ifndef PROT_WRITE |
|
|
177 | # define PROT_READ 0 |
|
|
178 | #endif |
|
|
179 | #ifndef PROT_EXEC |
|
|
180 | # define PROT_EXEC 0 |
|
|
181 | #endif |
|
|
182 | |
|
|
183 | #ifndef ST_NODEV |
|
|
184 | # define ST_NODEV 0 |
|
|
185 | #endif |
|
|
186 | #ifndef ST_NOEXEC |
|
|
187 | # define ST_NOEXEC 0 |
|
|
188 | #endif |
|
|
189 | #ifndef ST_SYNCHRONOUS |
|
|
190 | # define ST_SYNCHRONOUS 0 |
|
|
191 | #endif |
|
|
192 | #ifndef ST_MANDLOCK |
|
|
193 | # define ST_MANDLOCK 0 |
|
|
194 | #endif |
|
|
195 | #ifndef ST_WRITE |
|
|
196 | # define ST_WRITE 0 |
|
|
197 | #endif |
|
|
198 | #ifndef ST_APPEND |
|
|
199 | # define ST_APPEND 0 |
|
|
200 | #endif |
|
|
201 | #ifndef ST_IMMUTABLE |
|
|
202 | # define ST_IMMUTABLE 0 |
|
|
203 | #endif |
|
|
204 | #ifndef ST_NOATIME |
|
|
205 | # define ST_NOATIME 0 |
|
|
206 | #endif |
|
|
207 | #ifndef ST_NODIRATIME |
|
|
208 | # define ST_NODIRATIME 0 |
|
|
209 | #endif |
|
|
210 | #ifndef ST_RELATIME |
|
|
211 | # define ST_RELATIME 0 |
|
|
212 | #endif |
|
|
213 | |
|
|
214 | #ifndef MCL_CURRENT |
|
|
215 | # define MCL_CURRENT 0 |
|
|
216 | #endif |
|
|
217 | #ifndef MCL_FUTURE |
|
|
218 | # define MCL_FUTURE 0 |
|
|
219 | #endif |
|
|
220 | |
|
|
221 | #ifndef MAP_ANONYMOUS |
|
|
222 | # ifdef MAP_ANON |
|
|
223 | # define MAP_ANONYMOUS MAP_ANON |
|
|
224 | # else |
|
|
225 | # define MAP_ANONYMOUS MAP_FIXED /* and hope this fails */ |
|
|
226 | # endif |
|
|
227 | #endif |
|
|
228 | #ifndef MAP_HUGETLB |
|
|
229 | # define MAP_HUGETLB 0 |
|
|
230 | #endif |
|
|
231 | #ifndef MAP_LOCKED |
|
|
232 | # define MAP_LOCKED 0 |
|
|
233 | #endif |
|
|
234 | #ifndef MAP_NORESERVE |
|
|
235 | # define MAP_NORESERVE 0 |
|
|
236 | #endif |
|
|
237 | #ifndef MAP_POPULATE |
|
|
238 | # define MAP_POPULATE 0 |
|
|
239 | #endif |
|
|
240 | #ifndef MAP_NONBLOCK |
|
|
241 | # define MAP_NONBLOCK 0 |
|
|
242 | #endif |
|
|
243 | |
|
|
244 | #ifndef PAGESIZE |
|
|
245 | # define PAGESIZE sysconf (_SC_PAGESIZE) |
|
|
246 | #endif |
|
|
247 | |
114 | static int req_invoke (eio_req *req); |
248 | static int req_invoke (eio_req *req); |
115 | #define EIO_FINISH(req) req_invoke (req) |
249 | #define EIO_FINISH(req) req_invoke (req) |
116 | static void aio_grp_feed (eio_req *grp); |
|
|
117 | #define EIO_FEED(req) aio_grp_feed (req) |
|
|
118 | static void req_destroy (eio_req *grp); |
250 | static void req_destroy (eio_req *grp); |
119 | #define EIO_DESTROY(req) req_destroy (req) |
251 | #define EIO_DESTROY(req) req_destroy (req) |
120 | |
252 | |
121 | enum { |
253 | enum { |
122 | FLAG_SV2_RO_OFF = 0x40, /* data was set readonly */ |
254 | FLAG_SV2_RO_OFF = 0x40, /* data was set readonly */ |
… | |
… | |
129 | |
261 | |
130 | static SV *on_next_submit; |
262 | static SV *on_next_submit; |
131 | static int next_pri = EIO_PRI_DEFAULT; |
263 | static int next_pri = EIO_PRI_DEFAULT; |
132 | static int max_outstanding; |
264 | static int max_outstanding; |
133 | |
265 | |
134 | static int respipe_osf [2], respipe [2] = { -1, -1 }; |
266 | static s_epipe respipe; |
135 | |
267 | |
136 | static void req_destroy (aio_req req); |
268 | static void req_destroy (aio_req req); |
137 | static void req_cancel (aio_req req); |
269 | static void req_cancel (aio_req req); |
138 | |
270 | |
139 | static void want_poll (void) |
271 | static void want_poll (void) |
140 | { |
272 | { |
141 | /* write a dummy byte to the pipe so fh becomes ready */ |
273 | /* write a dummy byte to the pipe so fh becomes ready */ |
142 | respipe_write (respipe_osf [1], (const void *)&respipe_osf, 1); |
274 | s_epipe_signal (&respipe); |
143 | } |
275 | } |
144 | |
276 | |
145 | static void done_poll (void) |
277 | static void done_poll (void) |
146 | { |
278 | { |
147 | /* read any signals sent by the worker threads */ |
279 | /* read any signals sent by the worker threads */ |
148 | char buf [4]; |
280 | s_epipe_drain (&respipe); |
149 | while (respipe_read (respipe [0], buf, 4) == 4) |
|
|
150 | ; |
|
|
151 | } |
281 | } |
152 | |
282 | |
153 | /* must be called at most once */ |
283 | /* must be called at most once */ |
154 | static SV *req_sv (aio_req req, const char *klass) |
284 | static SV *req_sv (aio_req req, HV *stash) |
155 | { |
285 | { |
156 | if (!req->self) |
286 | if (!req->self) |
157 | { |
287 | { |
158 | req->self = (SV *)newHV (); |
288 | req->self = (SV *)newHV (); |
159 | sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0); |
289 | sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0); |
160 | } |
290 | } |
161 | |
291 | |
162 | return sv_2mortal (sv_bless (newRV_inc (req->self), gv_stashpv (klass, 1))); |
292 | return sv_2mortal (sv_bless (newRV_inc (req->self), stash)); |
163 | } |
293 | } |
164 | |
294 | |
165 | static aio_req SvAIO_REQ (SV *sv) |
295 | static aio_req SvAIO_REQ (SV *sv) |
166 | { |
296 | { |
167 | MAGIC *mg; |
297 | MAGIC *mg; |
168 | |
298 | |
169 | if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv)) |
299 | if (!SvROK (sv) |
|
|
300 | || (SvSTASH (SvRV (sv)) != aio_grp_stash |
|
|
301 | && SvSTASH (SvRV (sv)) != aio_req_stash |
|
|
302 | && !sv_derived_from (sv, "IO::AIO::REQ"))) |
170 | croak ("object of class " AIO_REQ_KLASS " expected"); |
303 | croak ("object of class IO::AIO::REQ expected"); |
171 | |
304 | |
172 | mg = mg_find (SvRV (sv), PERL_MAGIC_ext); |
305 | mg = mg_find (SvRV (sv), PERL_MAGIC_ext); |
173 | |
306 | |
174 | return mg ? (aio_req)mg->mg_ptr : 0; |
307 | return mg ? (aio_req)mg->mg_ptr : 0; |
175 | } |
308 | } |
… | |
… | |
181 | dSP; |
314 | dSP; |
182 | |
315 | |
183 | ENTER; |
316 | ENTER; |
184 | SAVETMPS; |
317 | SAVETMPS; |
185 | PUSHMARK (SP); |
318 | PUSHMARK (SP); |
186 | XPUSHs (req_sv (grp, AIO_GRP_KLASS)); |
319 | XPUSHs (req_sv (grp, aio_grp_stash)); |
187 | PUTBACK; |
320 | PUTBACK; |
188 | call_sv (grp->sv2, G_VOID | G_EVAL | G_KEEPERR); |
321 | call_sv (grp->sv2, G_VOID | G_EVAL | G_KEEPERR); |
189 | SPAGAIN; |
322 | SPAGAIN; |
190 | FREETMPS; |
323 | FREETMPS; |
191 | LEAVE; |
324 | LEAVE; |
… | |
… | |
209 | } |
342 | } |
210 | } |
343 | } |
211 | |
344 | |
212 | static int req_invoke (eio_req *req) |
345 | static int req_invoke (eio_req *req) |
213 | { |
346 | { |
214 | dSP; |
|
|
215 | |
|
|
216 | if (req->flags & FLAG_SV2_RO_OFF) |
347 | if (req->flags & FLAG_SV2_RO_OFF) |
217 | SvREADONLY_off (req->sv2); |
348 | SvREADONLY_off (req->sv2); |
218 | |
349 | |
219 | if (!EIO_CANCELLED (req) && SvOK (req->callback)) |
350 | if (!EIO_CANCELLED (req) && req->callback) |
220 | { |
351 | { |
|
|
352 | dSP; |
|
|
353 | static SV *sv_result_cache; /* caches the result integer SV */ |
|
|
354 | SV *sv_result; |
|
|
355 | |
221 | ENTER; |
356 | ENTER; |
222 | SAVETMPS; |
357 | SAVETMPS; |
223 | PUSHMARK (SP); |
358 | PUSHMARK (SP); |
224 | EXTEND (SP, 1); |
359 | EXTEND (SP, 1); |
|
|
360 | |
|
|
361 | /* do not recreate the result IV from scratch each time */ |
|
|
362 | if (expect_true (sv_result_cache)) |
|
|
363 | { |
|
|
364 | sv_result = sv_result_cache; sv_result_cache = 0; |
|
|
365 | SvIV_set (sv_result, req->result); |
|
|
366 | } |
|
|
367 | else |
|
|
368 | { |
|
|
369 | sv_result = newSViv (req->result); |
|
|
370 | SvREADONLY_on (sv_result); |
|
|
371 | } |
225 | |
372 | |
226 | switch (req->type) |
373 | switch (req->type) |
227 | { |
374 | { |
228 | case EIO_READDIR: |
375 | case EIO_READDIR: |
229 | { |
376 | { |
230 | SV *rv = &PL_sv_undef; |
377 | SV *rv = &PL_sv_undef; |
231 | |
378 | |
232 | if (req->result >= 0) |
379 | if (req->result >= 0) |
233 | { |
380 | { |
234 | int i; |
381 | int i; |
235 | char *buf = req->ptr2; |
382 | char *names = (char *)req->ptr2; |
|
|
383 | eio_dirent *ent = (eio_dirent *)req->ptr1; /* might be 0 */ |
236 | AV *av = newAV (); |
384 | AV *av = newAV (); |
237 | |
385 | |
238 | av_extend (av, req->result - 1); |
386 | av_extend (av, req->result - 1); |
239 | |
387 | |
240 | for (i = 0; i < req->result; ++i) |
388 | for (i = 0; i < req->result; ++i) |
241 | { |
389 | { |
242 | SV *sv = newSVpv (buf, 0); |
390 | if (req->int1 & EIO_READDIR_DENTS) |
|
|
391 | { |
|
|
392 | SV *namesv = newSVpvn (names + ent->nameofs, ent->namelen); |
243 | |
393 | |
|
|
394 | if (req->int1 & EIO_READDIR_CUSTOM2) |
|
|
395 | { |
|
|
396 | static SV *sv_type [EIO_DT_MAX + 1]; /* type sv cache */ |
|
|
397 | AV *avent = newAV (); |
|
|
398 | |
|
|
399 | av_extend (avent, 2); |
|
|
400 | |
|
|
401 | if (!sv_type [ent->type]) |
|
|
402 | { |
|
|
403 | sv_type [ent->type] = newSViv (ent->type); |
|
|
404 | SvREADONLY_on (sv_type [ent->type]); |
|
|
405 | } |
|
|
406 | |
|
|
407 | av_store (avent, 0, namesv); |
|
|
408 | av_store (avent, 1, SvREFCNT_inc (sv_type [ent->type])); |
|
|
409 | av_store (avent, 2, IVSIZE >= 8 ? newSVuv (ent->inode) : newSVnv (ent->inode)); |
|
|
410 | |
|
|
411 | av_store (av, i, newRV_noinc ((SV *)avent)); |
|
|
412 | } |
|
|
413 | else |
244 | av_store (av, i, sv); |
414 | av_store (av, i, namesv); |
|
|
415 | |
|
|
416 | ++ent; |
|
|
417 | } |
|
|
418 | else |
|
|
419 | { |
|
|
420 | SV *name = newSVpv (names, 0); |
|
|
421 | av_store (av, i, name); |
245 | buf += SvCUR (sv) + 1; |
422 | names += SvCUR (name) + 1; |
|
|
423 | } |
246 | } |
424 | } |
247 | |
425 | |
248 | rv = sv_2mortal (newRV_noinc ((SV *)av)); |
426 | rv = sv_2mortal (newRV_noinc ((SV *)av)); |
249 | } |
427 | } |
250 | |
428 | |
251 | PUSHs (rv); |
429 | PUSHs (rv); |
|
|
430 | |
|
|
431 | if (req->int1 & EIO_READDIR_CUSTOM1) |
|
|
432 | XPUSHs (sv_2mortal (newSViv (req->int1 & ~(EIO_READDIR_CUSTOM1 | EIO_READDIR_CUSTOM2)))); |
252 | } |
433 | } |
253 | break; |
434 | break; |
254 | |
435 | |
255 | case EIO_OPEN: |
436 | case EIO_OPEN: |
256 | { |
437 | { |
… | |
… | |
262 | GV *gv = (GV *)sv_newmortal (); |
443 | GV *gv = (GV *)sv_newmortal (); |
263 | int flags = req->int1 & (O_RDONLY | O_WRONLY | O_RDWR); |
444 | int flags = req->int1 & (O_RDONLY | O_WRONLY | O_RDWR); |
264 | char sym [64]; |
445 | char sym [64]; |
265 | int symlen; |
446 | int symlen; |
266 | |
447 | |
267 | symlen = snprintf (sym, sizeof (sym), "fd#%d", req->result); |
448 | symlen = snprintf (sym, sizeof (sym), "fd#%d", (int)req->result); |
268 | gv_init (gv, stash, sym, symlen, 0); |
449 | gv_init (gv, aio_stash, sym, symlen, 0); |
269 | |
450 | |
270 | symlen = snprintf ( |
451 | symlen = snprintf ( |
271 | sym, |
452 | sym, |
272 | sizeof (sym), |
453 | sizeof (sym), |
273 | "%s&=%d", |
454 | "%s&=%d", |
274 | flags == O_RDONLY ? "<" : flags == O_WRONLY ? ">" : "+<", |
455 | flags == O_RDONLY ? "<" : flags == O_WRONLY ? ">" : "+<", |
275 | req->result |
456 | (int)req->result |
276 | ); |
457 | ); |
277 | |
458 | |
278 | if (do_open (gv, sym, symlen, 0, 0, 0, 0)) |
459 | if (do_open (gv, sym, symlen, 0, 0, 0, 0)) |
279 | fh = (SV *)gv; |
460 | fh = (SV *)gv; |
280 | } |
461 | } |
281 | |
462 | |
282 | PUSHs (fh); |
463 | PUSHs (fh); |
283 | } |
464 | } |
|
|
465 | break; |
|
|
466 | |
|
|
467 | case EIO_STATVFS: |
|
|
468 | case EIO_FSTATVFS: |
|
|
469 | { |
|
|
470 | SV *rv = &PL_sv_undef; |
|
|
471 | |
|
|
472 | if (req->result >= 0) |
|
|
473 | { |
|
|
474 | EIO_STRUCT_STATVFS *f = EIO_STATVFS_BUF (req); |
|
|
475 | HV *hv = newHV (); |
|
|
476 | |
|
|
477 | rv = sv_2mortal (newRV_noinc ((SV *)hv)); |
|
|
478 | |
|
|
479 | hv_store (hv, "bsize" , sizeof ("bsize" ) - 1, newSVval64 (f->f_bsize ), 0); |
|
|
480 | hv_store (hv, "frsize" , sizeof ("frsize" ) - 1, newSVval64 (f->f_frsize ), 0); |
|
|
481 | hv_store (hv, "blocks" , sizeof ("blocks" ) - 1, newSVval64 (f->f_blocks ), 0); |
|
|
482 | hv_store (hv, "bfree" , sizeof ("bfree" ) - 1, newSVval64 (f->f_bfree ), 0); |
|
|
483 | hv_store (hv, "bavail" , sizeof ("bavail" ) - 1, newSVval64 (f->f_bavail ), 0); |
|
|
484 | hv_store (hv, "files" , sizeof ("files" ) - 1, newSVval64 (f->f_files ), 0); |
|
|
485 | hv_store (hv, "ffree" , sizeof ("ffree" ) - 1, newSVval64 (f->f_ffree ), 0); |
|
|
486 | hv_store (hv, "favail" , sizeof ("favail" ) - 1, newSVval64 (f->f_favail ), 0); |
|
|
487 | hv_store (hv, "fsid" , sizeof ("fsid" ) - 1, newSVval64 (f->f_fsid ), 0); |
|
|
488 | hv_store (hv, "flag" , sizeof ("flag" ) - 1, newSVval64 (f->f_flag ), 0); |
|
|
489 | hv_store (hv, "namemax", sizeof ("namemax") - 1, newSVval64 (f->f_namemax), 0); |
|
|
490 | } |
|
|
491 | |
|
|
492 | PUSHs (rv); |
|
|
493 | } |
|
|
494 | |
284 | break; |
495 | break; |
285 | |
496 | |
286 | case EIO_GROUP: |
497 | case EIO_GROUP: |
287 | req->int1 = 2; /* mark group as finished */ |
498 | req->int1 = 2; /* mark group as finished */ |
288 | |
499 | |
… | |
… | |
310 | case EIO_LSTAT: |
521 | case EIO_LSTAT: |
311 | case EIO_FSTAT: |
522 | case EIO_FSTAT: |
312 | PL_laststype = req->type == EIO_LSTAT ? OP_LSTAT : OP_STAT; |
523 | PL_laststype = req->type == EIO_LSTAT ? OP_LSTAT : OP_STAT; |
313 | PL_laststatval = req->result; |
524 | PL_laststatval = req->result; |
314 | PL_statcache = *(EIO_STRUCT_STAT *)(req->ptr2); |
525 | PL_statcache = *(EIO_STRUCT_STAT *)(req->ptr2); |
315 | PUSHs (sv_2mortal (newSViv (req->result))); |
526 | PUSHs (sv_result); |
316 | break; |
527 | break; |
317 | |
528 | |
318 | case EIO_READ: |
529 | case EIO_READ: |
|
|
530 | { |
319 | SvCUR_set (req->sv2, req->stroffset + (req->result > 0 ? req->result : 0)); |
531 | SvCUR_set (req->sv2, req->stroffset + (req->result > 0 ? req->result : 0)); |
320 | *SvEND (req->sv2) = 0; |
532 | *SvEND (req->sv2) = 0; |
321 | PUSHs (sv_2mortal (newSViv (req->result))); |
533 | SvPOK_only (req->sv2); |
|
|
534 | SvSETMAGIC (req->sv2); |
|
|
535 | PUSHs (sv_result); |
|
|
536 | } |
322 | break; |
537 | break; |
323 | |
538 | |
324 | case EIO_DUP2: |
539 | case EIO_DUP2: /* EIO_DUP2 actually means aio_close(), su fudge result value */ |
325 | if (req->result > 0) |
540 | if (req->result > 0) |
326 | req->result = 0; |
541 | SvIV_set (sv_result, 0); |
327 | /* FALLTHROUGH */ |
542 | /* FALLTHROUGH */ |
328 | |
543 | |
329 | default: |
544 | default: |
330 | PUSHs (sv_2mortal (newSViv (req->result))); |
545 | PUSHs (sv_result); |
331 | break; |
546 | break; |
332 | } |
547 | } |
333 | |
548 | |
334 | errno = req->errorno; |
549 | errno = req->errorno; |
335 | |
550 | |
336 | PUTBACK; |
551 | PUTBACK; |
337 | call_sv (req->callback, G_VOID | G_EVAL | G_DISCARD); |
552 | call_sv (req->callback, G_VOID | G_EVAL | G_DISCARD); |
338 | SPAGAIN; |
553 | SPAGAIN; |
|
|
554 | |
|
|
555 | if (expect_false (SvREFCNT (sv_result) != 1 || sv_result_cache)) |
|
|
556 | SvREFCNT_dec (sv_result); |
|
|
557 | else |
|
|
558 | sv_result_cache = sv_result; |
339 | |
559 | |
340 | FREETMPS; |
560 | FREETMPS; |
341 | LEAVE; |
561 | LEAVE; |
342 | |
562 | |
343 | PUTBACK; |
563 | PUTBACK; |
… | |
… | |
372 | grp->sv2 = 0; |
592 | grp->sv2 = 0; |
373 | |
593 | |
374 | eio_grp_cancel (grp); |
594 | eio_grp_cancel (grp); |
375 | } |
595 | } |
376 | |
596 | |
377 | #ifdef USE_SOCKETS_AS_HANDLES |
|
|
378 | # define TO_SOCKET(x) (win32_get_osfhandle (x)) |
|
|
379 | #else |
|
|
380 | # define TO_SOCKET(x) (x) |
|
|
381 | #endif |
|
|
382 | |
|
|
383 | static void |
597 | static void |
384 | create_respipe (void) |
598 | create_respipe (void) |
385 | { |
599 | { |
386 | int old_readfd = respipe [0]; |
|
|
387 | |
|
|
388 | if (respipe [1] >= 0) |
|
|
389 | respipe_close (TO_SOCKET (respipe [1])); |
|
|
390 | |
|
|
391 | #ifdef _WIN32 |
|
|
392 | if (PerlSock_socketpair (AF_UNIX, SOCK_STREAM, 0, respipe)) |
|
|
393 | #else |
|
|
394 | if (pipe (respipe)) |
600 | if (s_epipe_renew (&respipe)) |
395 | #endif |
|
|
396 | croak ("unable to initialize result pipe"); |
601 | croak ("IO::AIO: unable to initialize result pipe"); |
397 | |
|
|
398 | if (old_readfd >= 0) |
|
|
399 | { |
|
|
400 | if (dup2 (TO_SOCKET (respipe [0]), TO_SOCKET (old_readfd)) < 0) |
|
|
401 | croak ("unable to initialize result pipe(2)"); |
|
|
402 | |
|
|
403 | respipe_close (respipe [0]); |
|
|
404 | respipe [0] = old_readfd; |
|
|
405 | } |
|
|
406 | |
|
|
407 | #ifdef _WIN32 |
|
|
408 | int arg = 1; |
|
|
409 | if (ioctlsocket (TO_SOCKET (respipe [0]), FIONBIO, &arg) |
|
|
410 | || ioctlsocket (TO_SOCKET (respipe [1]), FIONBIO, &arg)) |
|
|
411 | #else |
|
|
412 | if (fcntl (respipe [0], F_SETFL, O_NONBLOCK) |
|
|
413 | || fcntl (respipe [1], F_SETFL, O_NONBLOCK)) |
|
|
414 | #endif |
|
|
415 | croak ("unable to initialize result pipe(3)"); |
|
|
416 | |
|
|
417 | respipe_osf [0] = TO_SOCKET (respipe [0]); |
|
|
418 | respipe_osf [1] = TO_SOCKET (respipe [1]); |
|
|
419 | } |
602 | } |
420 | |
603 | |
421 | static void poll_wait (void) |
604 | static void poll_wait (void) |
422 | { |
605 | { |
423 | fd_set rfd; |
|
|
424 | |
|
|
425 | while (eio_nreqs ()) |
606 | while (eio_nreqs ()) |
426 | { |
607 | { |
427 | int size; |
608 | int size; |
428 | |
609 | |
429 | X_LOCK (reslock); |
610 | X_LOCK (reslock); |
… | |
… | |
433 | if (size) |
614 | if (size) |
434 | return; |
615 | return; |
435 | |
616 | |
436 | etp_maybe_start_thread (); |
617 | etp_maybe_start_thread (); |
437 | |
618 | |
438 | FD_ZERO (&rfd); |
619 | s_epipe_wait (&respipe); |
439 | FD_SET (respipe [0], &rfd); |
|
|
440 | |
|
|
441 | PerlSock_select (respipe [0] + 1, &rfd, 0, 0, 0); |
|
|
442 | } |
620 | } |
443 | } |
621 | } |
444 | |
622 | |
445 | static int poll_cb (void) |
623 | static int poll_cb (void) |
446 | { |
624 | { |
… | |
… | |
461 | static void atfork_child (void) |
639 | static void atfork_child (void) |
462 | { |
640 | { |
463 | create_respipe (); |
641 | create_respipe (); |
464 | } |
642 | } |
465 | |
643 | |
|
|
644 | /*****************************************************************************/ |
|
|
645 | |
|
|
646 | #if !_POSIX_MAPPED_FILES |
|
|
647 | # define mmap(addr,length,prot,flags,fd,offs) (errno = ENOSYS, -1) |
|
|
648 | # define munmap(addr,length) (errno = ENOSYS, -1) |
|
|
649 | # define mprotect(addr,len,prot) (errno = ENOSYS, -1) |
|
|
650 | #endif |
|
|
651 | |
|
|
652 | #define MMAP_MAGIC PERL_MAGIC_ext |
|
|
653 | |
|
|
654 | static int |
|
|
655 | mmap_free (pTHX_ SV *sv, MAGIC *mg) |
|
|
656 | { |
|
|
657 | int old_errno = errno; |
|
|
658 | munmap (mg->mg_ptr, (size_t)mg->mg_obj); |
|
|
659 | errno = old_errno; |
|
|
660 | |
|
|
661 | mg->mg_obj = 0; /* just in case */ |
|
|
662 | |
|
|
663 | SvREADONLY_off (sv); |
|
|
664 | |
|
|
665 | if (SvPVX (sv) != mg->mg_ptr) |
|
|
666 | croak ("ERROR: IO::AIO::mmap-mapped scalar changed location, detected"); |
|
|
667 | |
|
|
668 | SvCUR_set (sv, 0); |
|
|
669 | SvPVX (sv) = 0; |
|
|
670 | SvOK_off (sv); |
|
|
671 | |
|
|
672 | return 0; |
|
|
673 | } |
|
|
674 | |
|
|
675 | static MGVTBL mmap_vtbl = { |
|
|
676 | 0, 0, 0, 0, mmap_free |
|
|
677 | }; |
|
|
678 | |
|
|
679 | /*****************************************************************************/ |
|
|
680 | |
|
|
681 | static SV * |
|
|
682 | get_cb (SV *cb_sv) |
|
|
683 | { |
|
|
684 | SvGETMAGIC (cb_sv); |
|
|
685 | return SvOK (cb_sv) ? s_get_cv_croak (cb_sv) : 0; |
|
|
686 | } |
|
|
687 | |
466 | #define dREQ \ |
688 | #define dREQ \ |
|
|
689 | SV *cb_cv; \ |
467 | aio_req req; \ |
690 | aio_req req; \ |
468 | int req_pri = next_pri; \ |
691 | int req_pri = next_pri; \ |
469 | next_pri = EIO_PRI_DEFAULT; \ |
692 | next_pri = EIO_PRI_DEFAULT; \ |
470 | \ |
693 | \ |
471 | if (SvOK (callback) && !SvROK (callback)) \ |
694 | cb_cv = get_cb (callback); \ |
472 | croak ("callback must be undef or of reference type"); \ |
|
|
473 | \ |
695 | \ |
474 | Newz (0, req, 1, eio_req); \ |
696 | Newz (0, req, 1, eio_req); \ |
475 | if (!req) \ |
697 | if (!req) \ |
476 | croak ("out of memory during eio_req allocation"); \ |
698 | croak ("out of memory during eio_req allocation"); \ |
477 | \ |
699 | \ |
478 | req->callback = newSVsv (callback); \ |
700 | req->callback = SvREFCNT_inc (cb_cv); \ |
479 | req->pri = req_pri |
701 | req->pri = req_pri |
480 | |
702 | |
481 | #define REQ_SEND \ |
703 | #define REQ_SEND \ |
482 | PUTBACK; \ |
704 | PUTBACK; \ |
483 | req_submit (req); \ |
705 | req_submit (req); \ |
484 | SPAGAIN; \ |
706 | SPAGAIN; \ |
485 | \ |
707 | \ |
486 | if (GIMME_V != G_VOID) \ |
708 | if (GIMME_V != G_VOID) \ |
487 | XPUSHs (req_sv (req, AIO_REQ_KLASS)); |
709 | XPUSHs (req_sv (req, aio_req_stash)); |
488 | |
710 | |
489 | MODULE = IO::AIO PACKAGE = IO::AIO |
711 | MODULE = IO::AIO PACKAGE = IO::AIO |
490 | |
712 | |
491 | PROTOTYPES: ENABLE |
713 | PROTOTYPES: ENABLE |
492 | |
714 | |
493 | BOOT: |
715 | BOOT: |
494 | { |
716 | { |
495 | stash = gv_stashpv ("IO::AIO", 1); |
717 | static const struct { |
496 | |
718 | const char *name; |
497 | newCONSTSUB (stash, "EXDEV", newSViv (EXDEV)); |
719 | IV iv; |
498 | newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY)); |
720 | } *civ, const_iv[] = { |
499 | newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY)); |
721 | # define const_niv(name, value) { # name, (IV) value }, |
500 | newCONSTSUB (stash, "O_CREAT", newSViv (O_CREAT)); |
722 | # define const_iv(name) { # name, (IV) name }, |
501 | newCONSTSUB (stash, "O_TRUNC", newSViv (O_TRUNC)); |
723 | # define const_eio(name) { # name, (IV) EIO_ ## name }, |
|
|
724 | const_iv (EXDEV) |
|
|
725 | const_iv (ENOSYS) |
|
|
726 | const_iv (O_RDONLY) |
|
|
727 | const_iv (O_WRONLY) |
|
|
728 | const_iv (O_RDWR) |
|
|
729 | const_iv (O_CREAT) |
|
|
730 | const_iv (O_TRUNC) |
|
|
731 | const_iv (O_EXCL) |
|
|
732 | const_iv (O_APPEND) |
502 | #ifndef _WIN32 |
733 | #ifndef _WIN32 |
503 | newCONSTSUB (stash, "S_IFIFO", newSViv (S_IFIFO)); |
734 | const_iv (S_IFIFO) |
504 | #endif |
735 | #endif |
|
|
736 | const_niv (FADV_NORMAL , POSIX_FADV_NORMAL) |
|
|
737 | const_niv (FADV_SEQUENTIAL, POSIX_FADV_SEQUENTIAL) |
|
|
738 | const_niv (FADV_RANDOM , POSIX_FADV_RANDOM) |
|
|
739 | const_niv (FADV_NOREUSE , POSIX_FADV_NOREUSE) |
|
|
740 | const_niv (FADV_WILLNEED , POSIX_FADV_WILLNEED) |
|
|
741 | const_niv (FADV_DONTNEED , POSIX_FADV_DONTNEED) |
505 | |
742 | |
|
|
743 | const_niv (MADV_NORMAL , POSIX_MADV_NORMAL) |
|
|
744 | const_niv (MADV_SEQUENTIAL, POSIX_MADV_SEQUENTIAL) |
|
|
745 | const_niv (MADV_RANDOM , POSIX_MADV_RANDOM) |
|
|
746 | const_niv (MADV_WILLNEED , POSIX_MADV_WILLNEED) |
|
|
747 | const_niv (MADV_DONTNEED , POSIX_MADV_DONTNEED) |
|
|
748 | |
|
|
749 | const_iv (ST_RDONLY) |
|
|
750 | const_iv (ST_NOSUID) |
|
|
751 | const_iv (ST_NODEV) |
|
|
752 | const_iv (ST_NOEXEC) |
|
|
753 | const_iv (ST_SYNCHRONOUS) |
|
|
754 | const_iv (ST_MANDLOCK) |
|
|
755 | const_iv (ST_WRITE) |
|
|
756 | const_iv (ST_APPEND) |
|
|
757 | const_iv (ST_IMMUTABLE) |
|
|
758 | const_iv (ST_NOATIME) |
|
|
759 | const_iv (ST_NODIRATIME) |
|
|
760 | const_iv (ST_RELATIME) |
|
|
761 | |
|
|
762 | const_iv (PROT_NONE) |
|
|
763 | const_iv (PROT_EXEC) |
|
|
764 | const_iv (PROT_READ) |
|
|
765 | const_iv (PROT_WRITE) |
|
|
766 | |
|
|
767 | /*const_iv (MAP_FIXED)*/ |
|
|
768 | const_iv (MAP_PRIVATE) |
|
|
769 | const_iv (MAP_SHARED) |
|
|
770 | const_iv (MAP_ANONYMOUS) |
|
|
771 | |
|
|
772 | /* linuxish */ |
|
|
773 | const_iv (MAP_HUGETLB) |
|
|
774 | const_iv (MAP_LOCKED) |
|
|
775 | const_iv (MAP_NORESERVE) |
|
|
776 | const_iv (MAP_POPULATE) |
|
|
777 | const_iv (MAP_NONBLOCK) |
|
|
778 | |
|
|
779 | const_iv (MCL_FUTURE) |
|
|
780 | const_iv (MCL_CURRENT) |
|
|
781 | |
|
|
782 | const_eio (MS_ASYNC) |
|
|
783 | const_eio (MS_INVALIDATE) |
|
|
784 | const_eio (MS_SYNC) |
|
|
785 | |
|
|
786 | const_eio (MT_MODIFY) |
|
|
787 | |
|
|
788 | const_eio (SYNC_FILE_RANGE_WAIT_BEFORE) |
|
|
789 | const_eio (SYNC_FILE_RANGE_WRITE) |
|
|
790 | const_eio (SYNC_FILE_RANGE_WAIT_AFTER) |
|
|
791 | |
|
|
792 | const_eio (READDIR_DENTS) |
|
|
793 | const_eio (READDIR_DIRS_FIRST) |
|
|
794 | const_eio (READDIR_STAT_ORDER) |
|
|
795 | const_eio (READDIR_FOUND_UNKNOWN) |
|
|
796 | |
|
|
797 | const_eio (DT_UNKNOWN) |
|
|
798 | const_eio (DT_FIFO) |
|
|
799 | const_eio (DT_CHR) |
|
|
800 | const_eio (DT_DIR) |
|
|
801 | const_eio (DT_BLK) |
|
|
802 | const_eio (DT_REG) |
|
|
803 | const_eio (DT_LNK) |
|
|
804 | const_eio (DT_SOCK) |
|
|
805 | const_eio (DT_WHT) |
|
|
806 | }; |
|
|
807 | |
|
|
808 | aio_stash = gv_stashpv ("IO::AIO" , 1); |
|
|
809 | aio_req_stash = gv_stashpv ("IO::AIO::REQ", 1); |
|
|
810 | aio_grp_stash = gv_stashpv ("IO::AIO::GRP", 1); |
|
|
811 | |
|
|
812 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) |
|
|
813 | newCONSTSUB (aio_stash, (char *)civ->name, newSViv (civ->iv)); |
|
|
814 | |
|
|
815 | newCONSTSUB (aio_stash, "PAGESIZE", newSViv (PAGESIZE)); |
|
|
816 | |
506 | create_respipe (); |
817 | create_respipe (); |
507 | |
818 | |
508 | if (eio_init (want_poll, done_poll) < 0) |
819 | if (eio_init (want_poll, done_poll) < 0) |
509 | croak ("IO::AIO: unable to initialise eio library"); |
820 | croak ("IO::AIO: unable to initialise eio library"); |
510 | |
821 | |
511 | /* atfork child called in fifo order, so before eio's handler */ |
822 | /* atfork child called in fifo order, so before eio's handler */ |
512 | X_THREAD_ATFORK (0, 0, atfork_child); |
823 | X_THREAD_ATFORK (0, 0, atfork_child); |
513 | } |
824 | } |
514 | |
825 | |
515 | void |
826 | void |
516 | max_poll_reqs (int nreqs) |
827 | max_poll_reqs (int nreqs) |
517 | PROTOTYPE: $ |
828 | PROTOTYPE: $ |
… | |
… | |
570 | ALIAS: |
881 | ALIAS: |
571 | aio_fsync = EIO_FSYNC |
882 | aio_fsync = EIO_FSYNC |
572 | aio_fdatasync = EIO_FDATASYNC |
883 | aio_fdatasync = EIO_FDATASYNC |
573 | PPCODE: |
884 | PPCODE: |
574 | { |
885 | { |
|
|
886 | int fd = s_fileno_croak (fh, 0); |
575 | dREQ; |
887 | dREQ; |
576 | |
888 | |
577 | req->type = ix; |
889 | req->type = ix; |
578 | req->sv1 = newSVsv (fh); |
890 | req->sv1 = newSVsv (fh); |
579 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh))); |
891 | req->int1 = fd; |
|
|
892 | |
|
|
893 | REQ_SEND (req); |
|
|
894 | } |
|
|
895 | |
|
|
896 | void |
|
|
897 | aio_sync_file_range (SV *fh, off_t offset, size_t nbytes, UV flags, SV *callback=&PL_sv_undef) |
|
|
898 | PROTOTYPE: $$$$;$ |
|
|
899 | PPCODE: |
|
|
900 | { |
|
|
901 | int fd = s_fileno_croak (fh, 0); |
|
|
902 | dREQ; |
|
|
903 | |
|
|
904 | req->type = EIO_SYNC_FILE_RANGE; |
|
|
905 | req->sv1 = newSVsv (fh); |
|
|
906 | req->int1 = fd; |
|
|
907 | req->offs = offset; |
|
|
908 | req->size = nbytes; |
|
|
909 | req->int2 = flags; |
580 | |
910 | |
581 | REQ_SEND (req); |
911 | REQ_SEND (req); |
582 | } |
912 | } |
583 | |
913 | |
584 | void |
914 | void |
585 | aio_close (SV *fh, SV *callback=&PL_sv_undef) |
915 | aio_close (SV *fh, SV *callback=&PL_sv_undef) |
586 | PROTOTYPE: $;$ |
916 | PROTOTYPE: $;$ |
587 | PPCODE: |
917 | PPCODE: |
588 | { |
918 | { |
589 | static int close_pipe = -1; /* dummy fd to close fds via dup2 */ |
919 | static int close_pipe = -1; /* dummy fd to close fds via dup2 */ |
|
|
920 | int fd = s_fileno_croak (fh, 0); |
590 | dREQ; |
921 | dREQ; |
591 | |
922 | |
592 | if (close_pipe < 0) |
923 | if (close_pipe < 0) |
593 | { |
924 | { |
594 | int pipefd [2]; |
925 | int pipefd [2]; |
… | |
… | |
602 | } |
933 | } |
603 | |
934 | |
604 | req->type = EIO_DUP2; |
935 | req->type = EIO_DUP2; |
605 | req->int1 = close_pipe; |
936 | req->int1 = close_pipe; |
606 | req->sv2 = newSVsv (fh); |
937 | req->sv2 = newSVsv (fh); |
607 | req->int2 = PerlIO_fileno (IoIFP (sv_2io (fh))); |
938 | req->int2 = fd; |
608 | |
939 | |
609 | REQ_SEND (req); |
940 | REQ_SEND (req); |
610 | } |
941 | } |
611 | |
942 | |
612 | void |
943 | void |
… | |
… | |
616 | aio_write = EIO_WRITE |
947 | aio_write = EIO_WRITE |
617 | PROTOTYPE: $$$$$;$ |
948 | PROTOTYPE: $$$$$;$ |
618 | PPCODE: |
949 | PPCODE: |
619 | { |
950 | { |
620 | STRLEN svlen; |
951 | STRLEN svlen; |
|
|
952 | int fd = s_fileno_croak (fh, ix == EIO_WRITE); |
621 | char *svptr = SvPVbyte (data, svlen); |
953 | char *svptr = SvPVbyte (data, svlen); |
622 | UV len = SvUV (length); |
954 | UV len = SvUV (length); |
623 | |
|
|
624 | SvUPGRADE (data, SVt_PV); |
|
|
625 | SvPOK_on (data); |
|
|
626 | |
955 | |
627 | if (dataoffset < 0) |
956 | if (dataoffset < 0) |
628 | dataoffset += svlen; |
957 | dataoffset += svlen; |
629 | |
958 | |
630 | if (dataoffset < 0 || dataoffset > svlen) |
959 | if (dataoffset < 0 || dataoffset > svlen) |
… | |
… | |
636 | if (!SvOK (length) || len + dataoffset > svlen) |
965 | if (!SvOK (length) || len + dataoffset > svlen) |
637 | len = svlen - dataoffset; |
966 | len = svlen - dataoffset; |
638 | } |
967 | } |
639 | else |
968 | else |
640 | { |
969 | { |
641 | /* read: grow scalar as necessary */ |
970 | /* read: check type and grow scalar as necessary */ |
|
|
971 | SvUPGRADE (data, SVt_PV); |
642 | svptr = SvGROW (data, len + dataoffset + 1); |
972 | svptr = SvGROW (data, len + dataoffset + 1); |
643 | } |
973 | } |
644 | |
974 | |
645 | if (len < 0) |
|
|
646 | croak ("length must not be negative"); |
|
|
647 | |
|
|
648 | { |
975 | { |
649 | dREQ; |
976 | dREQ; |
650 | |
977 | |
651 | req->type = ix; |
978 | req->type = ix; |
652 | req->sv1 = newSVsv (fh); |
979 | req->sv1 = newSVsv (fh); |
653 | req->int1 = PerlIO_fileno (ix == EIO_READ ? IoIFP (sv_2io (fh)) |
980 | req->int1 = fd; |
654 | : IoOFP (sv_2io (fh))); |
|
|
655 | req->offs = SvOK (offset) ? SvVAL64 (offset) : -1; |
981 | req->offs = SvOK (offset) ? SvVAL64 (offset) : -1; |
656 | req->size = len; |
982 | req->size = len; |
657 | req->sv2 = SvREFCNT_inc (data); |
983 | req->sv2 = SvREFCNT_inc (data); |
658 | req->ptr2 = (char *)svptr + dataoffset; |
984 | req->ptr2 = (char *)svptr + dataoffset; |
659 | req->stroffset = dataoffset; |
985 | req->stroffset = dataoffset; |
… | |
… | |
682 | |
1008 | |
683 | REQ_SEND; |
1009 | REQ_SEND; |
684 | } |
1010 | } |
685 | |
1011 | |
686 | void |
1012 | void |
687 | aio_sendfile (SV *out_fh, SV *in_fh, SV *in_offset, UV length, SV *callback=&PL_sv_undef) |
1013 | aio_sendfile (SV *out_fh, SV *in_fh, off_t in_offset, size_t length, SV *callback=&PL_sv_undef) |
688 | PROTOTYPE: $$$$;$ |
1014 | PROTOTYPE: $$$$;$ |
689 | PPCODE: |
1015 | PPCODE: |
690 | { |
1016 | { |
|
|
1017 | int ifd = s_fileno_croak (in_fh , 0); |
|
|
1018 | int ofd = s_fileno_croak (out_fh, 1); |
691 | dREQ; |
1019 | dREQ; |
692 | |
1020 | |
693 | req->type = EIO_SENDFILE; |
1021 | req->type = EIO_SENDFILE; |
694 | req->sv1 = newSVsv (out_fh); |
1022 | req->sv1 = newSVsv (out_fh); |
695 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (out_fh))); |
1023 | req->int1 = ofd; |
696 | req->sv2 = newSVsv (in_fh); |
1024 | req->sv2 = newSVsv (in_fh); |
697 | req->int2 = PerlIO_fileno (IoIFP (sv_2io (in_fh))); |
1025 | req->int2 = ifd; |
698 | req->offs = SvVAL64 (in_offset); |
1026 | req->offs = in_offset; |
699 | req->size = length; |
1027 | req->size = length; |
700 | |
1028 | |
701 | REQ_SEND; |
1029 | REQ_SEND; |
702 | } |
1030 | } |
703 | |
1031 | |
704 | void |
1032 | void |
705 | aio_readahead (SV *fh, SV *offset, IV length, SV *callback=&PL_sv_undef) |
1033 | aio_readahead (SV *fh, off_t offset, size_t length, SV *callback=&PL_sv_undef) |
706 | PROTOTYPE: $$$;$ |
1034 | PROTOTYPE: $$$;$ |
707 | PPCODE: |
1035 | PPCODE: |
708 | { |
1036 | { |
|
|
1037 | int fd = s_fileno_croak (fh, 0); |
709 | dREQ; |
1038 | dREQ; |
710 | |
1039 | |
711 | req->type = EIO_READAHEAD; |
1040 | req->type = EIO_READAHEAD; |
712 | req->sv1 = newSVsv (fh); |
1041 | req->sv1 = newSVsv (fh); |
713 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh))); |
1042 | req->int1 = fd; |
714 | req->offs = SvVAL64 (offset); |
1043 | req->offs = offset; |
715 | req->size = length; |
1044 | req->size = length; |
716 | |
1045 | |
717 | REQ_SEND; |
1046 | REQ_SEND; |
718 | } |
1047 | } |
719 | |
1048 | |
720 | void |
1049 | void |
721 | aio_stat (SV8 *fh_or_path, SV *callback=&PL_sv_undef) |
1050 | aio_stat (SV8 *fh_or_path, SV *callback=&PL_sv_undef) |
722 | ALIAS: |
1051 | ALIAS: |
723 | aio_stat = EIO_STAT |
1052 | aio_stat = EIO_STAT |
724 | aio_lstat = EIO_LSTAT |
1053 | aio_lstat = EIO_LSTAT |
|
|
1054 | aio_statvfs = EIO_STATVFS |
725 | PPCODE: |
1055 | PPCODE: |
726 | { |
1056 | { |
727 | dREQ; |
1057 | dREQ; |
728 | |
1058 | |
729 | req->sv1 = newSVsv (fh_or_path); |
1059 | req->sv1 = newSVsv (fh_or_path); |
… | |
… | |
733 | req->type = ix; |
1063 | req->type = ix; |
734 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
1064 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
735 | } |
1065 | } |
736 | else |
1066 | else |
737 | { |
1067 | { |
738 | req->type = EIO_FSTAT; |
1068 | req->type = ix == EIO_STATVFS ? EIO_FSTATVFS : EIO_FSTAT; |
739 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
1069 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
740 | } |
1070 | } |
741 | |
1071 | |
742 | REQ_SEND; |
1072 | REQ_SEND; |
743 | } |
1073 | } |
… | |
… | |
838 | |
1168 | |
839 | REQ_SEND; |
1169 | REQ_SEND; |
840 | } |
1170 | } |
841 | |
1171 | |
842 | void |
1172 | void |
|
|
1173 | aio_readdirx (SV8 *pathname, IV flags, SV *callback=&PL_sv_undef) |
|
|
1174 | PPCODE: |
|
|
1175 | { |
|
|
1176 | dREQ; |
|
|
1177 | |
|
|
1178 | req->type = EIO_READDIR; |
|
|
1179 | req->sv1 = newSVsv (pathname); |
|
|
1180 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
|
|
1181 | req->int1 = flags | EIO_READDIR_DENTS | EIO_READDIR_CUSTOM1; |
|
|
1182 | |
|
|
1183 | if (flags & EIO_READDIR_DENTS) |
|
|
1184 | req->int1 |= EIO_READDIR_CUSTOM2; |
|
|
1185 | |
|
|
1186 | REQ_SEND; |
|
|
1187 | } |
|
|
1188 | |
|
|
1189 | void |
843 | aio_unlink (SV8 *pathname, SV *callback=&PL_sv_undef) |
1190 | aio_unlink (SV8 *pathname, SV *callback=&PL_sv_undef) |
844 | ALIAS: |
1191 | ALIAS: |
845 | aio_unlink = EIO_UNLINK |
1192 | aio_unlink = EIO_UNLINK |
846 | aio_rmdir = EIO_RMDIR |
1193 | aio_rmdir = EIO_RMDIR |
847 | aio_readdir = EIO_READDIR |
1194 | aio_readdir = EIO_READDIR |
… | |
… | |
889 | |
1236 | |
890 | REQ_SEND; |
1237 | REQ_SEND; |
891 | } |
1238 | } |
892 | |
1239 | |
893 | void |
1240 | void |
|
|
1241 | aio_mtouch (SV8 *data, IV offset = 0, SV *length = &PL_sv_undef, int flags = 0, SV *callback=&PL_sv_undef) |
|
|
1242 | ALIAS: |
|
|
1243 | aio_mtouch = EIO_MTOUCH |
|
|
1244 | aio_msync = EIO_MSYNC |
|
|
1245 | PROTOTYPE: $$$$;$ |
|
|
1246 | PPCODE: |
|
|
1247 | { |
|
|
1248 | STRLEN svlen; |
|
|
1249 | UV len = SvUV (length); |
|
|
1250 | char *svptr = SvPVbyte (data, svlen); |
|
|
1251 | |
|
|
1252 | if (offset < 0) |
|
|
1253 | offset += svlen; |
|
|
1254 | |
|
|
1255 | if (offset < 0 || offset > svlen) |
|
|
1256 | croak ("offset outside of scalar"); |
|
|
1257 | |
|
|
1258 | if (!SvOK (length) || len + offset > svlen) |
|
|
1259 | len = svlen - offset; |
|
|
1260 | |
|
|
1261 | { |
|
|
1262 | dREQ; |
|
|
1263 | |
|
|
1264 | req->type = ix; |
|
|
1265 | req->size = len; |
|
|
1266 | req->sv2 = SvREFCNT_inc (data); |
|
|
1267 | req->ptr2 = (char *)svptr + offset; |
|
|
1268 | req->int1 = flags; |
|
|
1269 | |
|
|
1270 | REQ_SEND; |
|
|
1271 | } |
|
|
1272 | } |
|
|
1273 | |
|
|
1274 | void |
894 | aio_busy (double delay, SV *callback=&PL_sv_undef) |
1275 | aio_busy (double delay, SV *callback=&PL_sv_undef) |
895 | PPCODE: |
1276 | PPCODE: |
896 | { |
1277 | { |
897 | dREQ; |
1278 | dREQ; |
898 | |
1279 | |
… | |
… | |
910 | dREQ; |
1291 | dREQ; |
911 | |
1292 | |
912 | req->type = EIO_GROUP; |
1293 | req->type = EIO_GROUP; |
913 | |
1294 | |
914 | req_submit (req); |
1295 | req_submit (req); |
915 | XPUSHs (req_sv (req, AIO_GRP_KLASS)); |
1296 | XPUSHs (req_sv (req, aio_grp_stash)); |
916 | } |
1297 | } |
917 | |
1298 | |
918 | void |
1299 | void |
919 | aio_nop (SV *callback=&PL_sv_undef) |
1300 | aio_nop (SV *callback=&PL_sv_undef) |
920 | ALIAS: |
1301 | ALIAS: |
… | |
… | |
972 | |
1353 | |
973 | int |
1354 | int |
974 | poll_fileno() |
1355 | poll_fileno() |
975 | PROTOTYPE: |
1356 | PROTOTYPE: |
976 | CODE: |
1357 | CODE: |
977 | RETVAL = respipe [0]; |
1358 | RETVAL = s_epipe_fd (&respipe); |
978 | OUTPUT: |
1359 | OUTPUT: |
979 | RETVAL |
1360 | RETVAL |
980 | |
1361 | |
981 | int |
1362 | int |
982 | poll_cb(...) |
1363 | poll_cb(...) |
… | |
… | |
1022 | CODE: |
1403 | CODE: |
1023 | RETVAL = eio_nthreads (); |
1404 | RETVAL = eio_nthreads (); |
1024 | OUTPUT: |
1405 | OUTPUT: |
1025 | RETVAL |
1406 | RETVAL |
1026 | |
1407 | |
|
|
1408 | int |
|
|
1409 | fadvise (aio_rfd fh, off_t offset, off_t length, IV advice) |
|
|
1410 | PROTOTYPE: $$$$ |
|
|
1411 | CODE: |
|
|
1412 | RETVAL = posix_fadvise (fh, offset, length, advice); |
|
|
1413 | OUTPUT: |
|
|
1414 | RETVAL |
|
|
1415 | |
|
|
1416 | ssize_t |
|
|
1417 | sendfile (aio_wfd ofh, aio_rfd ifh, off_t offset, size_t count) |
|
|
1418 | PROTOTYPE: $$$$ |
|
|
1419 | CODE: |
|
|
1420 | RETVAL = eio_sendfile_sync (ofh, ifh, offset, count); |
|
|
1421 | OUTPUT: |
|
|
1422 | RETVAL |
|
|
1423 | |
|
|
1424 | void |
|
|
1425 | mmap (SV *scalar, size_t length, int prot, int flags, SV *fh, off_t offset = 0) |
|
|
1426 | PROTOTYPE: $$$$$;$ |
|
|
1427 | PPCODE: |
|
|
1428 | sv_unmagic (scalar, MMAP_MAGIC); |
|
|
1429 | { |
|
|
1430 | int fd = SvOK (fh) ? s_fileno_croak (fh, flags & PROT_WRITE) : -1; |
|
|
1431 | void *addr = (void *)mmap (0, length, prot, flags, fd, offset); |
|
|
1432 | if (addr == (void *)-1) |
|
|
1433 | XSRETURN_NO; |
|
|
1434 | |
|
|
1435 | sv_force_normal (scalar); |
|
|
1436 | |
|
|
1437 | /* we store the length in mg_obj, as namlen is I32 :/ */ |
|
|
1438 | sv_magicext (scalar, 0, MMAP_MAGIC, &mmap_vtbl, (char *)addr, 0) |
|
|
1439 | ->mg_obj = (SV *)length; |
|
|
1440 | |
|
|
1441 | SvUPGRADE (scalar, SVt_PV); /* nop... */ |
|
|
1442 | |
|
|
1443 | if (!(prot & PROT_WRITE)) |
|
|
1444 | SvREADONLY_on (scalar); |
|
|
1445 | |
|
|
1446 | if (SvLEN (scalar)) |
|
|
1447 | Safefree (SvPVX (scalar)); |
|
|
1448 | |
|
|
1449 | SvPVX (scalar) = (char *)addr; |
|
|
1450 | SvCUR_set (scalar, length); |
|
|
1451 | SvLEN_set (scalar, 0); |
|
|
1452 | SvPOK_only (scalar); |
|
|
1453 | |
|
|
1454 | XSRETURN_YES; |
|
|
1455 | } |
|
|
1456 | |
|
|
1457 | void |
|
|
1458 | munmap (SV *scalar) |
|
|
1459 | PROTOTYPE: $ |
|
|
1460 | CODE: |
|
|
1461 | sv_unmagic (scalar, MMAP_MAGIC); |
|
|
1462 | |
|
|
1463 | int |
|
|
1464 | madvise (SV *scalar, off_t offset, off_t length, IV advice_or_prot) |
|
|
1465 | ALIAS: |
|
|
1466 | mprotect = 1 |
|
|
1467 | PROTOTYPE: $$$$ |
|
|
1468 | CODE: |
|
|
1469 | { |
|
|
1470 | STRLEN cur; |
|
|
1471 | char *addr = SvPVbyte (scalar, cur); |
|
|
1472 | |
|
|
1473 | if (offset > cur) |
|
|
1474 | RETVAL = errno = EFAULT; |
|
|
1475 | else |
|
|
1476 | { |
|
|
1477 | if (!SvOK (ST (2))) |
|
|
1478 | length = cur - offset; |
|
|
1479 | |
|
|
1480 | if (offset + length > cur) |
|
|
1481 | RETVAL = errno = EFAULT; |
|
|
1482 | else |
|
|
1483 | switch (ix) |
|
|
1484 | { |
|
|
1485 | case 0: RETVAL = posix_madvise (addr + offset, length, advice_or_prot); break; |
|
|
1486 | case 1: RETVAL = mprotect (addr + offset, length, advice_or_prot); break; |
|
|
1487 | } |
|
|
1488 | } |
|
|
1489 | } |
|
|
1490 | OUTPUT: |
|
|
1491 | RETVAL |
|
|
1492 | |
|
|
1493 | int |
|
|
1494 | mlockall (int flags) |
|
|
1495 | PROTOTYPE: $ |
|
|
1496 | CODE: |
|
|
1497 | #if _POSIX_MEMLOCK |
|
|
1498 | #if __GLIBC__ == 2 && __GLIBC_MINOR__ <= 7 |
|
|
1499 | extern int mallopt (int, int); |
|
|
1500 | mallopt (-6, 238); /* http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=473812 */ |
|
|
1501 | #endif |
|
|
1502 | mlockall (flags); |
|
|
1503 | #else |
|
|
1504 | RETVAL = -1; |
|
|
1505 | errno = ENOSYS; |
|
|
1506 | #endif |
|
|
1507 | OUTPUT: |
|
|
1508 | RETVAL |
|
|
1509 | |
|
|
1510 | int |
|
|
1511 | munlockall () |
|
|
1512 | PROTOTYPE: |
|
|
1513 | CODE: |
|
|
1514 | #if _POSIX_MEMLOCK |
|
|
1515 | munlockall (); |
|
|
1516 | #else |
|
|
1517 | RETVAL = -1; |
|
|
1518 | errno = ENOSYS; |
|
|
1519 | #endif |
|
|
1520 | OUTPUT: |
|
|
1521 | RETVAL |
|
|
1522 | |
1027 | void _on_next_submit (SV *cb) |
1523 | void _on_next_submit (SV *cb) |
1028 | CODE: |
1524 | CODE: |
1029 | SvREFCNT_dec (on_next_submit); |
1525 | SvREFCNT_dec (on_next_submit); |
1030 | on_next_submit = SvOK (cb) ? newSVsv (cb) : 0; |
1526 | on_next_submit = SvOK (cb) ? newSVsv (cb) : 0; |
1031 | |
1527 | |
… | |
… | |
1038 | CODE: |
1534 | CODE: |
1039 | eio_cancel (req); |
1535 | eio_cancel (req); |
1040 | |
1536 | |
1041 | void |
1537 | void |
1042 | cb (aio_req_ornot req, SV *callback=&PL_sv_undef) |
1538 | cb (aio_req_ornot req, SV *callback=&PL_sv_undef) |
1043 | CODE: |
1539 | PPCODE: |
|
|
1540 | { |
|
|
1541 | if (GIMME_V != G_VOID) |
|
|
1542 | XPUSHs (req->callback ? sv_2mortal (newRV_inc (req->callback)) : &PL_sv_undef); |
|
|
1543 | |
|
|
1544 | if (items > 1) |
|
|
1545 | { |
|
|
1546 | SV *cb_cv =get_cb (callback); |
|
|
1547 | |
1044 | SvREFCNT_dec (req->callback); |
1548 | SvREFCNT_dec (req->callback); |
1045 | req->callback = newSVsv (callback); |
1549 | req->callback = SvREFCNT_inc (cb_cv); |
|
|
1550 | } |
|
|
1551 | } |
1046 | |
1552 | |
1047 | MODULE = IO::AIO PACKAGE = IO::AIO::GRP |
1553 | MODULE = IO::AIO PACKAGE = IO::AIO::GRP |
1048 | |
1554 | |
1049 | void |
1555 | void |
1050 | add (aio_req grp, ...) |
1556 | add (aio_req grp, ...) |
… | |
… | |
1082 | AV *av; |
1588 | AV *av; |
1083 | |
1589 | |
1084 | grp->errorno = errno; |
1590 | grp->errorno = errno; |
1085 | |
1591 | |
1086 | av = newAV (); |
1592 | av = newAV (); |
|
|
1593 | av_extend (av, items - 1); |
1087 | |
1594 | |
1088 | for (i = 1; i < items; ++i ) |
1595 | for (i = 1; i < items; ++i ) |
1089 | av_push (av, newSVsv (ST (i))); |
1596 | av_push (av, newSVsv (ST (i))); |
1090 | |
1597 | |
1091 | SvREFCNT_dec (grp->sv1); |
1598 | SvREFCNT_dec (grp->sv1); |
… | |
… | |
1105 | void |
1612 | void |
1106 | feed (aio_req grp, SV *callback=&PL_sv_undef) |
1613 | feed (aio_req grp, SV *callback=&PL_sv_undef) |
1107 | CODE: |
1614 | CODE: |
1108 | { |
1615 | { |
1109 | SvREFCNT_dec (grp->sv2); |
1616 | SvREFCNT_dec (grp->sv2); |
1110 | grp->sv2 = newSVsv (callback); |
1617 | grp->sv2 = newSVsv (callback); |
|
|
1618 | grp->feed = aio_grp_feed; |
1111 | |
1619 | |
1112 | if (grp->int2 <= 0) |
1620 | if (grp->int2 <= 0) |
1113 | grp->int2 = 2; |
1621 | grp->int2 = 2; |
1114 | |
1622 | |
1115 | eio_grp_limit (grp, grp->int2); |
1623 | eio_grp_limit (grp, grp->int2); |