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