1 | #define _REENTRANT 1 |
1 | #include "libeio/xthread.h" |
|
|
2 | |
2 | #include <errno.h> |
3 | #include <errno.h> |
3 | |
4 | |
4 | #include "EXTERN.h" |
5 | #include "EXTERN.h" |
5 | #include "perl.h" |
6 | #include "perl.h" |
6 | #include "XSUB.h" |
7 | #include "XSUB.h" |
7 | |
8 | |
8 | #include "autoconf/config.h" |
9 | #include "schmorp.h" |
9 | |
10 | |
|
|
11 | #include <stddef.h> |
|
|
12 | #include <stdlib.h> |
|
|
13 | #include <errno.h> |
10 | #include <sys/types.h> |
14 | #include <sys/types.h> |
11 | #include <sys/stat.h> |
15 | #include <sys/stat.h> |
12 | |
|
|
13 | #include <unistd.h> |
16 | #include <limits.h> |
14 | #include <fcntl.h> |
17 | #include <fcntl.h> |
15 | #include <signal.h> |
|
|
16 | #include <sched.h> |
18 | #include <sched.h> |
17 | |
19 | |
18 | #include <pthread.h> |
20 | /* perl namespace pollution */ |
|
|
21 | #undef VERSION |
19 | |
22 | |
20 | typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
23 | #ifdef _WIN32 |
21 | typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
|
|
22 | typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
|
|
23 | |
24 | |
24 | #if __ia64 |
25 | # define EIO_STRUCT_DIRENT Direntry_t |
25 | # define STACKSIZE 65536 |
26 | # undef malloc |
|
|
27 | # undef free |
|
|
28 | |
|
|
29 | // perl overrides all those nice win32 functions |
|
|
30 | # undef open |
|
|
31 | # undef read |
|
|
32 | # undef write |
|
|
33 | # undef send |
|
|
34 | # undef recv |
|
|
35 | # undef stat |
|
|
36 | # undef fstat |
|
|
37 | # define lstat stat |
|
|
38 | # undef truncate |
|
|
39 | # undef ftruncate |
|
|
40 | # undef open |
|
|
41 | # undef close |
|
|
42 | # undef unlink |
|
|
43 | # undef rmdir |
|
|
44 | # undef rename |
|
|
45 | # undef lseek |
|
|
46 | |
|
|
47 | # define chown(a,b,c) (errno = ENOSYS, -1) |
|
|
48 | # define fchown(a,b,c) (errno = ENOSYS, -1) |
|
|
49 | # define fchmod(a,b) (errno = ENOSYS, -1) |
|
|
50 | # define symlink(a,b) (errno = ENOSYS, -1) |
|
|
51 | # define readlink(a,b,c) (errno = ENOSYS, -1) |
|
|
52 | # define mknod(a,b,c) (errno = ENOSYS, -1) |
|
|
53 | # define truncate(a,b) (errno = ENOSYS, -1) |
|
|
54 | # define ftruncate(fd,o) chsize ((fd), (o)) |
|
|
55 | # define fsync(fd) _commit (fd) |
|
|
56 | # define opendir(fd) (errno = ENOSYS, 0) |
|
|
57 | # define readdir(fd) (errno = ENOSYS, -1) |
|
|
58 | # define closedir(fd) (errno = ENOSYS, -1) |
|
|
59 | # define mkdir(a,b) mkdir (a) |
|
|
60 | |
26 | #else |
61 | #else |
27 | # define STACKSIZE 4096 |
62 | |
|
|
63 | # include <sys/time.h> |
|
|
64 | # include <sys/select.h> |
|
|
65 | # include <unistd.h> |
|
|
66 | # include <utime.h> |
|
|
67 | # include <signal.h> |
|
|
68 | # define EIO_STRUCT_DIRENT struct dirent |
|
|
69 | |
28 | #endif |
70 | #endif |
29 | |
71 | |
|
|
72 | /* perl stupidly overrides readdir and maybe others */ |
|
|
73 | /* with thread-unsafe versions, imagine that :( */ |
|
|
74 | #undef readdir |
|
|
75 | #undef opendir |
|
|
76 | #undef closedir |
|
|
77 | |
|
|
78 | #define EIO_STRUCT_STAT Stat_t |
|
|
79 | |
|
|
80 | /* use NV for 32 bit perls as it allows larger offsets */ |
|
|
81 | #if IVSIZE >= 8 |
|
|
82 | # define VAL64 IV |
|
|
83 | # define SvVAL64 SvIV |
|
|
84 | # define newSVval64 newSViv |
|
|
85 | #else |
|
|
86 | # define VAL64 NV |
|
|
87 | # define SvVAL64 SvNV |
|
|
88 | # define newSVval64 newSVnv |
|
|
89 | #endif |
|
|
90 | |
|
|
91 | /*****************************************************************************/ |
|
|
92 | |
|
|
93 | #if __GNUC__ >= 3 |
|
|
94 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
95 | #else |
|
|
96 | # define expect(expr,value) (expr) |
|
|
97 | #endif |
|
|
98 | |
|
|
99 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
100 | #define expect_true(expr) expect ((expr) != 0, 1) |
|
|
101 | |
|
|
102 | /*****************************************************************************/ |
|
|
103 | |
|
|
104 | typedef SV SV8; /* byte-sv, used for argument-checking */ |
|
|
105 | typedef int aio_rfd; /* read file desriptor */ |
|
|
106 | typedef int aio_wfd; /* write file descriptor */ |
|
|
107 | |
|
|
108 | static HV *aio_stash, *aio_req_stash, *aio_grp_stash; |
|
|
109 | |
|
|
110 | #define EIO_REQ_MEMBERS \ |
|
|
111 | SV *callback; \ |
|
|
112 | SV *sv1, *sv2; \ |
|
|
113 | STRLEN stroffset; \ |
|
|
114 | SV *self; |
|
|
115 | |
|
|
116 | #define EIO_NO_WRAPPERS 1 |
|
|
117 | |
|
|
118 | #include "libeio/eio.h" |
|
|
119 | |
|
|
120 | #ifndef POSIX_FADV_NORMAL |
|
|
121 | # define POSIX_FADV_NORMAL 0 |
|
|
122 | # define NO_FADVISE 1 |
|
|
123 | #endif |
|
|
124 | |
|
|
125 | #ifndef POSIX_FADV_SEQUENTIAL |
|
|
126 | # define POSIX_FADV_SEQUENTIAL 0 |
|
|
127 | #endif |
|
|
128 | |
|
|
129 | #ifndef POSIX_FADV_RANDOM |
|
|
130 | # define POSIX_FADV_RANDOM 0 |
|
|
131 | #endif |
|
|
132 | |
|
|
133 | #ifndef POSIX_FADV_NOREUSE |
|
|
134 | # define POSIX_FADV_NOREUSE 0 |
|
|
135 | #endif |
|
|
136 | |
|
|
137 | #ifndef POSIX_FADV_WILLNEED |
|
|
138 | # define POSIX_FADV_WILLNEED 0 |
|
|
139 | #endif |
|
|
140 | |
|
|
141 | #ifndef POSIX_FADV_DONTNEED |
|
|
142 | # define POSIX_FADV_DONTNEED 0 |
|
|
143 | #endif |
|
|
144 | |
|
|
145 | static int req_invoke (eio_req *req); |
|
|
146 | #define EIO_FINISH(req) req_invoke (req) |
|
|
147 | static void req_destroy (eio_req *grp); |
|
|
148 | #define EIO_DESTROY(req) req_destroy (req) |
|
|
149 | |
30 | enum { |
150 | enum { |
31 | REQ_QUIT, |
151 | FLAG_SV2_RO_OFF = 0x40, /* data was set readonly */ |
32 | REQ_OPEN, REQ_CLOSE, |
|
|
33 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
|
|
34 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
|
|
35 | REQ_FSYNC, REQ_FDATASYNC, |
|
|
36 | REQ_UNLINK, REQ_RMDIR, |
|
|
37 | REQ_SYMLINK, |
|
|
38 | }; |
152 | }; |
39 | |
153 | |
40 | typedef struct aio_cb { |
154 | #include "libeio/eio.c" |
41 | struct aio_cb *volatile next; |
|
|
42 | |
155 | |
43 | int type; |
|
|
44 | |
|
|
45 | int fd; |
|
|
46 | off_t offset; |
|
|
47 | size_t length; |
|
|
48 | ssize_t result; |
|
|
49 | mode_t mode; /* open */ |
|
|
50 | int errorno; |
|
|
51 | SV *data, *callback, *fh; |
|
|
52 | void *dataptr, *data2ptr; |
|
|
53 | STRLEN dataoffset; |
|
|
54 | |
|
|
55 | Stat_t *statdata; |
|
|
56 | } aio_cb; |
|
|
57 | |
|
|
58 | typedef aio_cb *aio_req; |
156 | typedef eio_req *aio_req; |
|
|
157 | typedef eio_req *aio_req_ornot; |
59 | |
158 | |
60 | static int started; |
159 | static SV *on_next_submit; |
61 | static volatile int nreqs; |
160 | static int next_pri = EIO_PRI_DEFAULT; |
62 | static int max_outstanding = 1<<30; |
161 | static int max_outstanding; |
63 | static int respipe [2]; |
|
|
64 | |
162 | |
65 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
163 | static s_epipe respipe; |
66 | static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; |
|
|
67 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
|
|
68 | |
164 | |
69 | static volatile aio_req reqs, reqe; /* queue start, queue end */ |
165 | static void req_destroy (aio_req req); |
70 | static volatile aio_req ress, rese; /* queue start, queue end */ |
|
|
71 | |
|
|
72 | static void free_req (aio_req req) |
166 | static void req_cancel (aio_req req); |
73 | { |
|
|
74 | if (req->data) |
|
|
75 | SvREFCNT_dec (req->data); |
|
|
76 | |
167 | |
|
|
168 | static void want_poll (void) |
|
|
169 | { |
|
|
170 | /* write a dummy byte to the pipe so fh becomes ready */ |
|
|
171 | s_epipe_signal (&respipe); |
|
|
172 | } |
|
|
173 | |
|
|
174 | static void done_poll (void) |
|
|
175 | { |
|
|
176 | /* read any signals sent by the worker threads */ |
|
|
177 | s_epipe_drain (&respipe); |
|
|
178 | } |
|
|
179 | |
|
|
180 | /* must be called at most once */ |
|
|
181 | static SV *req_sv (aio_req req, HV *stash) |
|
|
182 | { |
|
|
183 | if (!req->self) |
|
|
184 | { |
|
|
185 | req->self = (SV *)newHV (); |
|
|
186 | sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0); |
|
|
187 | } |
|
|
188 | |
|
|
189 | return sv_2mortal (sv_bless (newRV_inc (req->self), stash)); |
|
|
190 | } |
|
|
191 | |
|
|
192 | static aio_req SvAIO_REQ (SV *sv) |
|
|
193 | { |
|
|
194 | MAGIC *mg; |
|
|
195 | |
|
|
196 | if (!SvROK (sv) |
|
|
197 | || (SvSTASH (SvRV (sv)) != aio_grp_stash |
|
|
198 | && SvSTASH (SvRV (sv)) != aio_req_stash |
|
|
199 | && !sv_derived_from (sv, "IO::AIO::REQ"))) |
|
|
200 | croak ("object of class IO::AIO::REQ expected"); |
|
|
201 | |
|
|
202 | mg = mg_find (SvRV (sv), PERL_MAGIC_ext); |
|
|
203 | |
|
|
204 | return mg ? (aio_req)mg->mg_ptr : 0; |
|
|
205 | } |
|
|
206 | |
|
|
207 | static void aio_grp_feed (aio_req grp) |
|
|
208 | { |
|
|
209 | if (grp->sv2 && SvOK (grp->sv2)) |
|
|
210 | { |
|
|
211 | dSP; |
|
|
212 | |
|
|
213 | ENTER; |
|
|
214 | SAVETMPS; |
|
|
215 | PUSHMARK (SP); |
|
|
216 | XPUSHs (req_sv (grp, aio_grp_stash)); |
|
|
217 | PUTBACK; |
|
|
218 | call_sv (grp->sv2, G_VOID | G_EVAL | G_KEEPERR); |
|
|
219 | SPAGAIN; |
|
|
220 | FREETMPS; |
|
|
221 | LEAVE; |
|
|
222 | } |
|
|
223 | } |
|
|
224 | |
|
|
225 | static void req_submit (eio_req *req) |
|
|
226 | { |
|
|
227 | eio_submit (req); |
|
|
228 | |
|
|
229 | if (expect_false (on_next_submit)) |
|
|
230 | { |
|
|
231 | dSP; |
|
|
232 | SV *cb = sv_2mortal (on_next_submit); |
|
|
233 | |
|
|
234 | on_next_submit = 0; |
|
|
235 | |
|
|
236 | PUSHMARK (SP); |
|
|
237 | PUTBACK; |
|
|
238 | call_sv (cb, G_DISCARD | G_EVAL); |
|
|
239 | } |
|
|
240 | } |
|
|
241 | |
|
|
242 | static int req_invoke (eio_req *req) |
|
|
243 | { |
|
|
244 | dSP; |
|
|
245 | |
|
|
246 | if (req->flags & FLAG_SV2_RO_OFF) |
|
|
247 | SvREADONLY_off (req->sv2); |
|
|
248 | |
|
|
249 | if (!EIO_CANCELLED (req) && req->callback) |
|
|
250 | { |
|
|
251 | ENTER; |
|
|
252 | SAVETMPS; |
|
|
253 | PUSHMARK (SP); |
|
|
254 | EXTEND (SP, 1); |
|
|
255 | |
|
|
256 | switch (req->type) |
|
|
257 | { |
|
|
258 | case EIO_READDIR: |
|
|
259 | { |
|
|
260 | SV *rv = &PL_sv_undef; |
|
|
261 | |
|
|
262 | if (req->result >= 0) |
|
|
263 | { |
|
|
264 | int i; |
|
|
265 | char *names = (char *)req->ptr2; |
|
|
266 | eio_dirent *ent = (eio_dirent *)req->ptr1; /* might be 0 */ |
|
|
267 | AV *av = newAV (); |
|
|
268 | |
|
|
269 | av_extend (av, req->result - 1); |
|
|
270 | |
|
|
271 | for (i = 0; i < req->result; ++i) |
|
|
272 | { |
|
|
273 | if (req->int1 & EIO_READDIR_DENTS) |
|
|
274 | { |
|
|
275 | SV *namesv = newSVpvn (names + ent->nameofs, ent->namelen); |
|
|
276 | |
|
|
277 | if (req->int1 & EIO_READDIR_CUSTOM2) |
|
|
278 | { |
|
|
279 | static SV *sv_type [EIO_DT_MAX + 1]; /* type sv cache */ |
|
|
280 | AV *avent = newAV (); |
|
|
281 | |
|
|
282 | av_extend (avent, 2); |
|
|
283 | |
|
|
284 | if (!sv_type [ent->type]) |
|
|
285 | { |
|
|
286 | sv_type [ent->type] = newSViv (ent->type); |
|
|
287 | SvREADONLY_on (sv_type [ent->type]); |
|
|
288 | } |
|
|
289 | |
|
|
290 | av_store (avent, 0, namesv); |
|
|
291 | av_store (avent, 1, SvREFCNT_inc (sv_type [ent->type])); |
|
|
292 | av_store (avent, 2, IVSIZE >= 8 ? newSVuv (ent->inode) : newSVnv (ent->inode)); |
|
|
293 | |
|
|
294 | av_store (av, i, newRV_noinc ((SV *)avent)); |
|
|
295 | } |
|
|
296 | else |
|
|
297 | av_store (av, i, namesv); |
|
|
298 | |
|
|
299 | ++ent; |
|
|
300 | } |
|
|
301 | else |
|
|
302 | { |
|
|
303 | SV *name = newSVpv (names, 0); |
|
|
304 | av_store (av, i, name); |
|
|
305 | names += SvCUR (name) + 1; |
|
|
306 | } |
|
|
307 | } |
|
|
308 | |
|
|
309 | rv = sv_2mortal (newRV_noinc ((SV *)av)); |
|
|
310 | } |
|
|
311 | |
|
|
312 | PUSHs (rv); |
|
|
313 | |
|
|
314 | if (req->int1 & EIO_READDIR_CUSTOM1) |
|
|
315 | XPUSHs (sv_2mortal (newSViv (req->int1 & ~(EIO_READDIR_CUSTOM1 | EIO_READDIR_CUSTOM2)))); |
|
|
316 | } |
|
|
317 | break; |
|
|
318 | |
|
|
319 | case EIO_OPEN: |
|
|
320 | { |
|
|
321 | /* convert fd to fh */ |
|
|
322 | SV *fh = &PL_sv_undef; |
|
|
323 | |
|
|
324 | if (req->result >= 0) |
|
|
325 | { |
|
|
326 | GV *gv = (GV *)sv_newmortal (); |
|
|
327 | int flags = req->int1 & (O_RDONLY | O_WRONLY | O_RDWR); |
|
|
328 | char sym [64]; |
|
|
329 | int symlen; |
|
|
330 | |
|
|
331 | symlen = snprintf (sym, sizeof (sym), "fd#%d", (int)req->result); |
|
|
332 | gv_init (gv, aio_stash, sym, symlen, 0); |
|
|
333 | |
|
|
334 | symlen = snprintf ( |
|
|
335 | sym, |
|
|
336 | sizeof (sym), |
|
|
337 | "%s&=%d", |
|
|
338 | flags == O_RDONLY ? "<" : flags == O_WRONLY ? ">" : "+<", |
|
|
339 | (int)req->result |
|
|
340 | ); |
|
|
341 | |
|
|
342 | if (do_open (gv, sym, symlen, 0, 0, 0, 0)) |
|
|
343 | fh = (SV *)gv; |
|
|
344 | } |
|
|
345 | |
|
|
346 | PUSHs (fh); |
|
|
347 | } |
|
|
348 | break; |
|
|
349 | |
|
|
350 | case EIO_GROUP: |
|
|
351 | req->int1 = 2; /* mark group as finished */ |
|
|
352 | |
|
|
353 | if (req->sv1) |
|
|
354 | { |
|
|
355 | int i; |
|
|
356 | AV *av = (AV *)req->sv1; |
|
|
357 | |
|
|
358 | EXTEND (SP, AvFILL (av) + 1); |
|
|
359 | for (i = 0; i <= AvFILL (av); ++i) |
|
|
360 | PUSHs (*av_fetch (av, i, 0)); |
|
|
361 | } |
|
|
362 | break; |
|
|
363 | |
|
|
364 | case EIO_NOP: |
|
|
365 | case EIO_BUSY: |
|
|
366 | break; |
|
|
367 | |
|
|
368 | case EIO_READLINK: |
|
|
369 | if (req->result > 0) |
|
|
370 | PUSHs (sv_2mortal (newSVpvn (req->ptr2, req->result))); |
|
|
371 | break; |
|
|
372 | |
|
|
373 | case EIO_STAT: |
|
|
374 | case EIO_LSTAT: |
|
|
375 | case EIO_FSTAT: |
|
|
376 | PL_laststype = req->type == EIO_LSTAT ? OP_LSTAT : OP_STAT; |
|
|
377 | PL_laststatval = req->result; |
|
|
378 | PL_statcache = *(EIO_STRUCT_STAT *)(req->ptr2); |
|
|
379 | PUSHs (sv_2mortal (newSViv (req->result))); |
|
|
380 | break; |
|
|
381 | |
|
|
382 | case EIO_READ: |
|
|
383 | { |
|
|
384 | SvCUR_set (req->sv2, req->stroffset + (req->result > 0 ? req->result : 0)); |
|
|
385 | *SvEND (req->sv2) = 0; |
|
|
386 | SvPOK_only (req->sv2); |
|
|
387 | SvSETMAGIC (req->sv2); |
|
|
388 | PUSHs (sv_2mortal (newSViv (req->result))); |
|
|
389 | } |
|
|
390 | break; |
|
|
391 | |
|
|
392 | case EIO_DUP2: |
|
|
393 | if (req->result > 0) |
|
|
394 | req->result = 0; |
|
|
395 | /* FALLTHROUGH */ |
|
|
396 | |
|
|
397 | default: |
|
|
398 | PUSHs (sv_2mortal (newSViv (req->result))); |
|
|
399 | break; |
|
|
400 | } |
|
|
401 | |
|
|
402 | errno = req->errorno; |
|
|
403 | |
|
|
404 | PUTBACK; |
|
|
405 | call_sv (req->callback, G_VOID | G_EVAL | G_DISCARD); |
|
|
406 | SPAGAIN; |
|
|
407 | |
|
|
408 | FREETMPS; |
|
|
409 | LEAVE; |
|
|
410 | |
|
|
411 | PUTBACK; |
|
|
412 | } |
|
|
413 | |
|
|
414 | return !!SvTRUE (ERRSV); |
|
|
415 | } |
|
|
416 | |
|
|
417 | static void req_destroy (aio_req req) |
|
|
418 | { |
77 | if (req->fh) |
419 | if (req->self) |
|
|
420 | { |
|
|
421 | sv_unmagic (req->self, PERL_MAGIC_ext); |
78 | SvREFCNT_dec (req->fh); |
422 | SvREFCNT_dec (req->self); |
|
|
423 | } |
79 | |
424 | |
80 | if (req->statdata) |
425 | SvREFCNT_dec (req->sv1); |
81 | Safefree (req->statdata); |
426 | SvREFCNT_dec (req->sv2); |
82 | |
|
|
83 | if (req->callback) |
|
|
84 | SvREFCNT_dec (req->callback); |
427 | SvREFCNT_dec (req->callback); |
85 | |
428 | |
86 | Safefree (req); |
429 | Safefree (req); |
87 | } |
430 | } |
88 | |
431 | |
|
|
432 | static void req_cancel_subs (aio_req grp) |
|
|
433 | { |
|
|
434 | aio_req sub; |
|
|
435 | |
|
|
436 | if (grp->type != EIO_GROUP) |
|
|
437 | return; |
|
|
438 | |
|
|
439 | SvREFCNT_dec (grp->sv2); |
|
|
440 | grp->sv2 = 0; |
|
|
441 | |
|
|
442 | eio_grp_cancel (grp); |
|
|
443 | } |
|
|
444 | |
89 | static void |
445 | static void |
90 | poll_wait () |
446 | create_respipe (void) |
91 | { |
447 | { |
92 | if (nreqs && !ress) |
448 | if (s_epipe_renew (&respipe)) |
|
|
449 | croak ("IO::AIO: unable to initialize result pipe"); |
|
|
450 | } |
|
|
451 | |
|
|
452 | static void poll_wait (void) |
|
|
453 | { |
|
|
454 | while (eio_nreqs ()) |
93 | { |
455 | { |
94 | fd_set rfd; |
456 | int size; |
95 | FD_ZERO(&rfd); |
|
|
96 | FD_SET(respipe [0], &rfd); |
|
|
97 | |
457 | |
98 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
458 | X_LOCK (reslock); |
|
|
459 | size = res_queue.size; |
|
|
460 | X_UNLOCK (reslock); |
|
|
461 | |
|
|
462 | if (size) |
|
|
463 | return; |
|
|
464 | |
|
|
465 | etp_maybe_start_thread (); |
|
|
466 | |
|
|
467 | s_epipe_wait (&respipe); |
99 | } |
468 | } |
100 | } |
469 | } |
101 | |
470 | |
102 | static int |
471 | static int poll_cb (void) |
103 | poll_cb () |
|
|
104 | { |
472 | { |
105 | dSP; |
|
|
106 | int count = 0; |
|
|
107 | int do_croak = 0; |
|
|
108 | aio_req req; |
|
|
109 | |
|
|
110 | for (;;) |
473 | for (;;) |
111 | { |
474 | { |
112 | pthread_mutex_lock (&reslock); |
475 | int res = eio_poll (); |
113 | req = ress; |
|
|
114 | |
476 | |
115 | if (req) |
477 | if (res > 0) |
116 | { |
|
|
117 | ress = req->next; |
|
|
118 | |
|
|
119 | if (!ress) |
|
|
120 | { |
|
|
121 | rese = 0; |
|
|
122 | |
|
|
123 | /* read any signals sent by the worker threads */ |
|
|
124 | char buf [32]; |
|
|
125 | while (read (respipe [0], buf, 32) == 32) |
|
|
126 | ; |
|
|
127 | } |
|
|
128 | } |
|
|
129 | |
|
|
130 | pthread_mutex_unlock (&reslock); |
|
|
131 | |
|
|
132 | if (!req) |
|
|
133 | break; |
|
|
134 | |
|
|
135 | nreqs--; |
|
|
136 | |
|
|
137 | if (req->type == REQ_QUIT) |
|
|
138 | started--; |
|
|
139 | else |
|
|
140 | { |
|
|
141 | int errorno = errno; |
|
|
142 | errno = req->errorno; |
|
|
143 | |
|
|
144 | if (req->type == REQ_READ) |
|
|
145 | SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); |
|
|
146 | |
|
|
147 | if (req->statdata) |
|
|
148 | { |
|
|
149 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
|
|
150 | PL_laststatval = req->result; |
|
|
151 | PL_statcache = *(req->statdata); |
|
|
152 | } |
|
|
153 | |
|
|
154 | ENTER; |
|
|
155 | PUSHMARK (SP); |
|
|
156 | XPUSHs (sv_2mortal (newSViv (req->result))); |
|
|
157 | |
|
|
158 | if (req->type == REQ_OPEN) |
|
|
159 | { |
|
|
160 | /* convert fd to fh */ |
|
|
161 | SV *fh; |
|
|
162 | |
|
|
163 | PUTBACK; |
|
|
164 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
|
|
165 | SPAGAIN; |
|
|
166 | |
|
|
167 | fh = SvREFCNT_inc (POPs); |
|
|
168 | |
|
|
169 | PUSHMARK (SP); |
|
|
170 | XPUSHs (sv_2mortal (fh)); |
|
|
171 | } |
|
|
172 | |
|
|
173 | if (SvOK (req->callback)) |
|
|
174 | { |
|
|
175 | PUTBACK; |
|
|
176 | call_sv (req->callback, G_VOID | G_EVAL); |
|
|
177 | SPAGAIN; |
|
|
178 | |
|
|
179 | if (SvTRUE (ERRSV)) |
|
|
180 | { |
|
|
181 | free_req (req); |
|
|
182 | croak (0); |
478 | croak (0); |
183 | } |
|
|
184 | } |
|
|
185 | |
479 | |
186 | LEAVE; |
480 | if (!max_outstanding || max_outstanding > eio_nreqs ()) |
|
|
481 | return res; |
187 | |
482 | |
188 | errno = errorno; |
483 | poll_wait (); |
189 | count++; |
|
|
190 | } |
|
|
191 | |
|
|
192 | free_req (req); |
|
|
193 | } |
484 | } |
194 | |
|
|
195 | return count; |
|
|
196 | } |
|
|
197 | |
|
|
198 | static void *aio_proc(void *arg); |
|
|
199 | |
|
|
200 | static void |
|
|
201 | start_thread (void) |
|
|
202 | { |
|
|
203 | sigset_t fullsigset, oldsigset; |
|
|
204 | pthread_t tid; |
|
|
205 | pthread_attr_t attr; |
|
|
206 | |
|
|
207 | pthread_attr_init (&attr); |
|
|
208 | pthread_attr_setstacksize (&attr, STACKSIZE); |
|
|
209 | pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); |
|
|
210 | |
|
|
211 | sigfillset (&fullsigset); |
|
|
212 | sigprocmask (SIG_SETMASK, &fullsigset, &oldsigset); |
|
|
213 | |
|
|
214 | if (pthread_create (&tid, &attr, aio_proc, 0) == 0) |
|
|
215 | started++; |
|
|
216 | |
|
|
217 | sigprocmask (SIG_SETMASK, &oldsigset, 0); |
|
|
218 | } |
|
|
219 | |
|
|
220 | static void |
|
|
221 | send_req (aio_req req) |
|
|
222 | { |
|
|
223 | nreqs++; |
|
|
224 | |
|
|
225 | pthread_mutex_lock (&reqlock); |
|
|
226 | |
|
|
227 | req->next = 0; |
|
|
228 | |
|
|
229 | if (reqe) |
|
|
230 | { |
|
|
231 | reqe->next = req; |
|
|
232 | reqe = req; |
|
|
233 | } |
|
|
234 | else |
|
|
235 | reqe = reqs = req; |
|
|
236 | |
|
|
237 | pthread_cond_signal (&reqwait); |
|
|
238 | pthread_mutex_unlock (&reqlock); |
|
|
239 | |
|
|
240 | if (nreqs > max_outstanding) |
|
|
241 | for (;;) |
|
|
242 | { |
|
|
243 | poll_cb (); |
|
|
244 | |
|
|
245 | if (nreqs <= max_outstanding) |
|
|
246 | break; |
|
|
247 | |
|
|
248 | poll_wait (); |
|
|
249 | } |
|
|
250 | } |
|
|
251 | |
|
|
252 | static void |
|
|
253 | end_thread (void) |
|
|
254 | { |
|
|
255 | aio_req req; |
|
|
256 | Newz (0, req, 1, aio_cb); |
|
|
257 | req->type = REQ_QUIT; |
|
|
258 | |
|
|
259 | send_req (req); |
|
|
260 | } |
|
|
261 | |
|
|
262 | static void min_parallel (int nthreads) |
|
|
263 | { |
|
|
264 | while (nthreads > started) |
|
|
265 | start_thread (); |
|
|
266 | } |
|
|
267 | |
|
|
268 | static void max_parallel (int nthreads) |
|
|
269 | { |
|
|
270 | int cur = started; |
|
|
271 | |
|
|
272 | while (cur > nthreads) |
|
|
273 | { |
|
|
274 | end_thread (); |
|
|
275 | cur--; |
|
|
276 | } |
|
|
277 | |
|
|
278 | while (started > nthreads) |
|
|
279 | { |
|
|
280 | poll_wait (); |
|
|
281 | poll_cb (); |
|
|
282 | } |
|
|
283 | } |
|
|
284 | |
|
|
285 | static void create_pipe () |
|
|
286 | { |
|
|
287 | if (pipe (respipe)) |
|
|
288 | croak ("unable to initialize result pipe"); |
|
|
289 | |
|
|
290 | if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)) |
|
|
291 | croak ("cannot set result pipe to nonblocking mode"); |
|
|
292 | |
|
|
293 | if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) |
|
|
294 | croak ("cannot set result pipe to nonblocking mode"); |
|
|
295 | } |
|
|
296 | |
|
|
297 | static void atfork_prepare (void) |
|
|
298 | { |
|
|
299 | for (;;) { |
|
|
300 | |
|
|
301 | for (;;) |
|
|
302 | { |
|
|
303 | poll_cb (); |
|
|
304 | |
|
|
305 | if (!nreqs) |
|
|
306 | break; |
|
|
307 | |
|
|
308 | poll_wait (); |
|
|
309 | } |
|
|
310 | |
|
|
311 | pthread_mutex_lock (&reqlock); |
|
|
312 | |
|
|
313 | if (!nreqs) |
|
|
314 | break; |
|
|
315 | |
|
|
316 | pthread_mutex_unlock (&reqlock); |
|
|
317 | } |
|
|
318 | |
|
|
319 | pthread_mutex_lock (&reslock); |
|
|
320 | |
|
|
321 | assert (!nreqs && !reqs && !ress); |
|
|
322 | } |
|
|
323 | |
|
|
324 | static void atfork_parent (void) |
|
|
325 | { |
|
|
326 | pthread_mutex_unlock (&reslock); |
|
|
327 | pthread_mutex_unlock (&reqlock); |
|
|
328 | } |
485 | } |
329 | |
486 | |
330 | static void atfork_child (void) |
487 | static void atfork_child (void) |
331 | { |
488 | { |
332 | int restart = started; |
489 | create_respipe (); |
333 | started = 0; |
|
|
334 | |
|
|
335 | atfork_parent (); |
|
|
336 | |
|
|
337 | min_parallel (restart); |
|
|
338 | } |
490 | } |
339 | |
491 | |
340 | /*****************************************************************************/ |
492 | static SV * |
341 | /* work around various missing functions */ |
493 | get_cb (SV *cb_sv) |
342 | |
|
|
343 | #if !HAVE_PREADWRITE |
|
|
344 | # define pread aio_pread |
|
|
345 | # define pwrite aio_pwrite |
|
|
346 | |
|
|
347 | /* |
|
|
348 | * make our pread/pwrite safe against themselves, but not against |
|
|
349 | * normal read/write by using a mutex. slows down execution a lot, |
|
|
350 | * but that's your problem, not mine. |
|
|
351 | */ |
|
|
352 | static pthread_mutex_t iolock = PTHREAD_MUTEX_INITIALIZER; |
|
|
353 | |
|
|
354 | static ssize_t |
|
|
355 | pread (int fd, void *buf, size_t count, off_t offset) |
|
|
356 | { |
494 | { |
357 | ssize_t res; |
495 | SvGETMAGIC (cb_sv); |
358 | off_t ooffset; |
496 | return SvOK (cb_sv) ? s_get_cv_croak (cb_sv) : 0; |
359 | |
|
|
360 | pthread_mutex_lock (&iolock); |
|
|
361 | ooffset = lseek (fd, 0, SEEK_CUR); |
|
|
362 | lseek (fd, offset, SEEK_SET); |
|
|
363 | res = read (fd, buf, count); |
|
|
364 | lseek (fd, ooffset, SEEK_SET); |
|
|
365 | pthread_mutex_unlock (&iolock); |
|
|
366 | |
|
|
367 | return res; |
|
|
368 | } |
497 | } |
369 | |
498 | |
370 | static ssize_t |
499 | #define dREQ \ |
371 | pwrite (int fd, void *buf, size_t count, off_t offset) |
500 | SV *cb_cv; \ |
372 | { |
501 | aio_req req; \ |
373 | ssize_t res; |
502 | int req_pri = next_pri; \ |
374 | off_t ooffset; |
503 | next_pri = EIO_PRI_DEFAULT; \ |
|
|
504 | \ |
|
|
505 | cb_cv = get_cb (callback); \ |
|
|
506 | \ |
|
|
507 | Newz (0, req, 1, eio_req); \ |
|
|
508 | if (!req) \ |
|
|
509 | croak ("out of memory during eio_req allocation"); \ |
|
|
510 | \ |
|
|
511 | req->callback = SvREFCNT_inc (cb_cv); \ |
|
|
512 | req->pri = req_pri |
375 | |
513 | |
376 | pthread_mutex_lock (&iolock); |
514 | #define REQ_SEND \ |
377 | ooffset = lseek (fd, 0, SEEK_CUR); |
515 | PUTBACK; \ |
378 | lseek (fd, offset, SEEK_SET); |
516 | req_submit (req); \ |
379 | res = write (fd, buf, count); |
517 | SPAGAIN; \ |
380 | lseek (fd, offset, SEEK_SET); |
518 | \ |
381 | pthread_mutex_unlock (&iolock); |
519 | if (GIMME_V != G_VOID) \ |
|
|
520 | XPUSHs (req_sv (req, aio_req_stash)); |
382 | |
521 | |
383 | return res; |
522 | MODULE = IO::AIO PACKAGE = IO::AIO |
384 | } |
523 | |
|
|
524 | PROTOTYPES: ENABLE |
|
|
525 | |
|
|
526 | BOOT: |
|
|
527 | { |
|
|
528 | static const struct { |
|
|
529 | const char *name; |
|
|
530 | IV iv; |
|
|
531 | } *civ, const_iv[] = { |
|
|
532 | # define const_iv(name, value) { # name, (IV) value }, |
|
|
533 | # define const_eio(name) { # name, (IV) EIO_ ## name }, |
|
|
534 | const_iv (EXDEV , EXDEV) |
|
|
535 | const_iv (ENOSYS , ENOSYS) |
|
|
536 | const_iv (O_RDONLY, O_RDONLY) |
|
|
537 | const_iv (O_WRONLY, O_WRONLY) |
|
|
538 | const_iv (O_CREAT , O_CREAT) |
|
|
539 | const_iv (O_TRUNC , O_TRUNC) |
|
|
540 | #ifndef _WIN32 |
|
|
541 | const_iv (S_IFIFO , S_IFIFO) |
385 | #endif |
542 | #endif |
|
|
543 | const_iv (FADV_NORMAL , POSIX_FADV_NORMAL) |
|
|
544 | const_iv (FADV_SEQUENTIAL, POSIX_FADV_SEQUENTIAL) |
|
|
545 | const_iv (FADV_RANDOM , POSIX_FADV_RANDOM) |
|
|
546 | const_iv (FADV_NOREUSE , POSIX_FADV_NOREUSE) |
|
|
547 | const_iv (FADV_WILLNEED , POSIX_FADV_WILLNEED) |
|
|
548 | const_iv (FADV_DONTNEED , POSIX_FADV_DONTNEED) |
386 | |
549 | |
387 | #if !HAVE_FDATASYNC |
550 | const_eio (SYNC_FILE_RANGE_WAIT_BEFORE) |
388 | # define fdatasync fsync |
551 | const_eio (SYNC_FILE_RANGE_WRITE) |
389 | #endif |
552 | const_eio (SYNC_FILE_RANGE_WAIT_AFTER) |
390 | |
553 | |
391 | #if !HAVE_READAHEAD |
554 | const_eio (READDIR_DENTS) |
392 | # define readahead aio_readahead |
555 | const_eio (READDIR_DIRS_FIRST) |
|
|
556 | const_eio (READDIR_STAT_ORDER) |
|
|
557 | const_eio (READDIR_FOUND_UNKNOWN) |
393 | |
558 | |
394 | static char readahead_buf[4096]; |
559 | const_eio (DT_UNKNOWN) |
|
|
560 | const_eio (DT_FIFO) |
|
|
561 | const_eio (DT_CHR) |
|
|
562 | const_eio (DT_DIR) |
|
|
563 | const_eio (DT_BLK) |
|
|
564 | const_eio (DT_REG) |
|
|
565 | const_eio (DT_LNK) |
|
|
566 | const_eio (DT_SOCK) |
|
|
567 | const_eio (DT_WHT) |
|
|
568 | }; |
395 | |
569 | |
396 | static ssize_t |
570 | aio_stash = gv_stashpv ("IO::AIO" , 1); |
397 | readahead (int fd, off_t offset, size_t count) |
571 | aio_req_stash = gv_stashpv ("IO::AIO::REQ", 1); |
398 | { |
572 | aio_grp_stash = gv_stashpv ("IO::AIO::GRP", 1); |
399 | while (count > 0) |
|
|
400 | { |
|
|
401 | size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); |
|
|
402 | |
573 | |
403 | pread (fd, readahead_buf, len, offset); |
574 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) |
404 | offset += len; |
575 | newCONSTSUB (aio_stash, (char *)civ->name, newSViv (civ->iv)); |
405 | count -= len; |
|
|
406 | } |
|
|
407 | |
576 | |
408 | errno = 0; |
|
|
409 | } |
|
|
410 | #endif |
|
|
411 | |
|
|
412 | /*****************************************************************************/ |
|
|
413 | |
|
|
414 | static void * |
|
|
415 | aio_proc (void *thr_arg) |
|
|
416 | { |
|
|
417 | aio_req req; |
|
|
418 | int type; |
|
|
419 | |
|
|
420 | do |
|
|
421 | { |
|
|
422 | pthread_mutex_lock (&reqlock); |
|
|
423 | |
|
|
424 | for (;;) |
|
|
425 | { |
|
|
426 | req = reqs; |
|
|
427 | |
|
|
428 | if (reqs) |
|
|
429 | { |
|
|
430 | reqs = reqs->next; |
|
|
431 | if (!reqs) reqe = 0; |
|
|
432 | } |
|
|
433 | |
|
|
434 | if (req) |
|
|
435 | break; |
|
|
436 | |
|
|
437 | pthread_cond_wait (&reqwait, &reqlock); |
|
|
438 | } |
|
|
439 | |
|
|
440 | pthread_mutex_unlock (&reqlock); |
|
|
441 | |
|
|
442 | errno = 0; /* strictly unnecessary */ |
|
|
443 | |
|
|
444 | type = req->type; |
|
|
445 | |
|
|
446 | switch (type) |
|
|
447 | { |
|
|
448 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
|
|
449 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
|
|
450 | |
|
|
451 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
|
|
452 | |
|
|
453 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
|
|
454 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
|
|
455 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
|
|
456 | |
|
|
457 | case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
|
|
458 | case REQ_CLOSE: req->result = close (req->fd); break; |
|
|
459 | case REQ_UNLINK: req->result = unlink (req->dataptr); break; |
|
|
460 | case REQ_RMDIR: req->result = rmdir (req->dataptr); break; |
|
|
461 | case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; |
|
|
462 | |
|
|
463 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
|
|
464 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
|
|
465 | |
|
|
466 | case REQ_QUIT: |
|
|
467 | break; |
|
|
468 | |
|
|
469 | default: |
|
|
470 | req->result = ENOSYS; |
|
|
471 | break; |
|
|
472 | } |
|
|
473 | |
|
|
474 | req->errorno = errno; |
|
|
475 | |
|
|
476 | pthread_mutex_lock (&reslock); |
|
|
477 | |
|
|
478 | req->next = 0; |
|
|
479 | |
|
|
480 | if (rese) |
|
|
481 | { |
|
|
482 | rese->next = req; |
|
|
483 | rese = req; |
|
|
484 | } |
|
|
485 | else |
|
|
486 | { |
|
|
487 | rese = ress = req; |
|
|
488 | |
|
|
489 | /* write a dummy byte to the pipe so fh becomes ready */ |
|
|
490 | write (respipe [1], &respipe, 1); |
|
|
491 | } |
|
|
492 | |
|
|
493 | pthread_mutex_unlock (&reslock); |
|
|
494 | } |
|
|
495 | while (type != REQ_QUIT); |
|
|
496 | |
|
|
497 | return 0; |
|
|
498 | } |
|
|
499 | |
|
|
500 | #define dREQ \ |
|
|
501 | aio_req req; \ |
|
|
502 | \ |
|
|
503 | if (SvOK (callback) && !SvROK (callback)) \ |
|
|
504 | croak ("clalback must be undef or of reference type"); \ |
|
|
505 | \ |
|
|
506 | Newz (0, req, 1, aio_cb); \ |
|
|
507 | if (!req) \ |
|
|
508 | croak ("out of memory during aio_req allocation"); \ |
|
|
509 | \ |
|
|
510 | req->callback = newSVsv (callback); |
|
|
511 | |
|
|
512 | MODULE = IO::AIO PACKAGE = IO::AIO |
|
|
513 | |
|
|
514 | PROTOTYPES: ENABLE |
|
|
515 | |
|
|
516 | BOOT: |
|
|
517 | { |
|
|
518 | create_pipe (); |
577 | create_respipe (); |
519 | pthread_atfork (atfork_prepare, atfork_parent, atfork_child); |
|
|
520 | } |
|
|
521 | |
578 | |
|
|
579 | if (eio_init (want_poll, done_poll) < 0) |
|
|
580 | croak ("IO::AIO: unable to initialise eio library"); |
|
|
581 | |
|
|
582 | /* atfork child called in fifo order, so before eio's handler */ |
|
|
583 | X_THREAD_ATFORK (0, 0, atfork_child); |
|
|
584 | } |
|
|
585 | |
522 | void |
586 | void |
523 | min_parallel(nthreads) |
587 | max_poll_reqs (int nreqs) |
524 | int nthreads |
|
|
525 | PROTOTYPE: $ |
588 | PROTOTYPE: $ |
|
|
589 | CODE: |
|
|
590 | eio_set_max_poll_reqs (nreqs); |
526 | |
591 | |
527 | void |
592 | void |
528 | max_parallel(nthreads) |
593 | max_poll_time (double nseconds) |
529 | int nthreads |
|
|
530 | PROTOTYPE: $ |
594 | PROTOTYPE: $ |
531 | |
|
|
532 | int |
|
|
533 | max_outstanding(nreqs) |
|
|
534 | int nreqs |
|
|
535 | PROTOTYPE: $ |
|
|
536 | CODE: |
595 | CODE: |
537 | RETVAL = max_outstanding; |
596 | eio_set_max_poll_time (nseconds); |
|
|
597 | |
|
|
598 | void |
|
|
599 | min_parallel (int nthreads) |
|
|
600 | PROTOTYPE: $ |
|
|
601 | CODE: |
|
|
602 | eio_set_min_parallel (nthreads); |
|
|
603 | |
|
|
604 | void |
|
|
605 | max_parallel (int nthreads) |
|
|
606 | PROTOTYPE: $ |
|
|
607 | CODE: |
|
|
608 | eio_set_max_parallel (nthreads); |
|
|
609 | |
|
|
610 | void |
|
|
611 | max_idle (int nthreads) |
|
|
612 | PROTOTYPE: $ |
|
|
613 | CODE: |
|
|
614 | eio_set_max_idle (nthreads); |
|
|
615 | |
|
|
616 | void |
|
|
617 | max_outstanding (int maxreqs) |
|
|
618 | PROTOTYPE: $ |
|
|
619 | CODE: |
538 | max_outstanding = nreqs; |
620 | max_outstanding = maxreqs; |
539 | |
621 | |
540 | void |
622 | void |
541 | aio_open(pathname,flags,mode,callback=&PL_sv_undef) |
623 | aio_open (SV8 *pathname, int flags, int mode, SV *callback=&PL_sv_undef) |
542 | SV * pathname |
|
|
543 | int flags |
|
|
544 | int mode |
|
|
545 | SV * callback |
|
|
546 | PROTOTYPE: $$$;$ |
624 | PROTOTYPE: $$$;$ |
547 | CODE: |
625 | PPCODE: |
548 | { |
626 | { |
549 | dREQ; |
627 | dREQ; |
550 | |
628 | |
551 | req->type = REQ_OPEN; |
629 | req->type = EIO_OPEN; |
552 | req->data = newSVsv (pathname); |
630 | req->sv1 = newSVsv (pathname); |
553 | req->dataptr = SvPVbyte_nolen (req->data); |
631 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
554 | req->fd = flags; |
632 | req->int1 = flags; |
555 | req->mode = mode; |
633 | req->int2 = mode; |
556 | |
634 | |
557 | send_req (req); |
635 | REQ_SEND; |
558 | } |
636 | } |
559 | |
637 | |
560 | void |
638 | void |
561 | aio_close(fh,callback=&PL_sv_undef) |
639 | aio_fsync (SV *fh, SV *callback=&PL_sv_undef) |
562 | SV * fh |
|
|
563 | SV * callback |
|
|
564 | PROTOTYPE: $;$ |
640 | PROTOTYPE: $;$ |
565 | ALIAS: |
641 | ALIAS: |
566 | aio_close = REQ_CLOSE |
|
|
567 | aio_fsync = REQ_FSYNC |
642 | aio_fsync = EIO_FSYNC |
568 | aio_fdatasync = REQ_FDATASYNC |
643 | aio_fdatasync = EIO_FDATASYNC |
569 | CODE: |
644 | PPCODE: |
570 | { |
645 | { |
|
|
646 | int fd = s_fileno_croak (fh, 0); |
571 | dREQ; |
647 | dREQ; |
572 | |
648 | |
573 | req->type = ix; |
649 | req->type = ix; |
574 | req->fh = newSVsv (fh); |
650 | req->sv1 = newSVsv (fh); |
575 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
651 | req->int1 = fd; |
576 | |
652 | |
577 | send_req (req); |
653 | REQ_SEND (req); |
578 | } |
654 | } |
579 | |
655 | |
580 | void |
656 | void |
|
|
657 | aio_sync_file_range (SV *fh, off_t offset, size_t nbytes, UV flags, SV *callback=&PL_sv_undef) |
|
|
658 | PROTOTYPE: $$$$;$ |
|
|
659 | PPCODE: |
|
|
660 | { |
|
|
661 | int fd = s_fileno_croak (fh, 0); |
|
|
662 | dREQ; |
|
|
663 | |
|
|
664 | req->type = EIO_SYNC_FILE_RANGE; |
|
|
665 | req->sv1 = newSVsv (fh); |
|
|
666 | req->int1 = fd; |
|
|
667 | req->offs = offset; |
|
|
668 | req->size = nbytes; |
|
|
669 | req->int2 = flags; |
|
|
670 | |
|
|
671 | REQ_SEND (req); |
|
|
672 | } |
|
|
673 | |
|
|
674 | void |
|
|
675 | aio_close (SV *fh, SV *callback=&PL_sv_undef) |
|
|
676 | PROTOTYPE: $;$ |
|
|
677 | PPCODE: |
|
|
678 | { |
|
|
679 | static int close_pipe = -1; /* dummy fd to close fds via dup2 */ |
|
|
680 | int fd = s_fileno_croak (fh, 0); |
|
|
681 | dREQ; |
|
|
682 | |
|
|
683 | if (close_pipe < 0) |
|
|
684 | { |
|
|
685 | int pipefd [2]; |
|
|
686 | |
|
|
687 | if (pipe (pipefd) < 0 |
|
|
688 | || close (pipefd [1]) < 0 |
|
|
689 | || fcntl (pipefd [0], F_SETFD, FD_CLOEXEC) < 0) |
|
|
690 | abort (); /*D*/ |
|
|
691 | |
|
|
692 | close_pipe = pipefd [0]; |
|
|
693 | } |
|
|
694 | |
|
|
695 | req->type = EIO_DUP2; |
|
|
696 | req->int1 = close_pipe; |
|
|
697 | req->sv2 = newSVsv (fh); |
|
|
698 | req->int2 = fd; |
|
|
699 | |
|
|
700 | REQ_SEND (req); |
|
|
701 | } |
|
|
702 | |
|
|
703 | void |
581 | aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
704 | aio_read (SV *fh, SV *offset, SV *length, SV8 *data, IV dataoffset, SV *callback=&PL_sv_undef) |
582 | SV * fh |
|
|
583 | UV offset |
|
|
584 | IV length |
|
|
585 | SV * data |
|
|
586 | IV dataoffset |
|
|
587 | SV * callback |
|
|
588 | ALIAS: |
705 | ALIAS: |
589 | aio_read = REQ_READ |
706 | aio_read = EIO_READ |
590 | aio_write = REQ_WRITE |
707 | aio_write = EIO_WRITE |
591 | PROTOTYPE: $$$$$;$ |
708 | PROTOTYPE: $$$$$;$ |
592 | CODE: |
709 | PPCODE: |
593 | { |
710 | { |
594 | aio_req req; |
|
|
595 | STRLEN svlen; |
711 | STRLEN svlen; |
|
|
712 | int fd = s_fileno_croak (fh, ix == EIO_WRITE); |
596 | char *svptr = SvPVbyte (data, svlen); |
713 | char *svptr = SvPVbyte (data, svlen); |
597 | |
714 | UV len = SvUV (length); |
598 | SvUPGRADE (data, SVt_PV); |
|
|
599 | SvPOK_on (data); |
|
|
600 | |
715 | |
601 | if (dataoffset < 0) |
716 | if (dataoffset < 0) |
602 | dataoffset += svlen; |
717 | dataoffset += svlen; |
603 | |
718 | |
604 | if (dataoffset < 0 || dataoffset > svlen) |
719 | if (dataoffset < 0 || dataoffset > svlen) |
605 | croak ("data offset outside of string"); |
720 | croak ("dataoffset outside of data scalar"); |
606 | |
721 | |
607 | if (ix == REQ_WRITE) |
722 | if (ix == EIO_WRITE) |
608 | { |
723 | { |
609 | /* write: check length and adjust. */ |
724 | /* write: check length and adjust. */ |
610 | if (length < 0 || length + dataoffset > svlen) |
725 | if (!SvOK (length) || len + dataoffset > svlen) |
611 | length = svlen - dataoffset; |
726 | len = svlen - dataoffset; |
612 | } |
727 | } |
613 | else |
728 | else |
614 | { |
729 | { |
615 | /* read: grow scalar as necessary */ |
730 | /* read: check type and grow scalar as necessary */ |
|
|
731 | SvUPGRADE (data, SVt_PV); |
616 | svptr = SvGROW (data, length + dataoffset); |
732 | svptr = SvGROW (data, len + dataoffset + 1); |
617 | } |
733 | } |
618 | |
|
|
619 | if (length < 0) |
|
|
620 | croak ("length must not be negative"); |
|
|
621 | |
734 | |
622 | { |
735 | { |
623 | dREQ; |
736 | dREQ; |
624 | |
737 | |
625 | req->type = ix; |
738 | req->type = ix; |
626 | req->fh = newSVsv (fh); |
739 | req->sv1 = newSVsv (fh); |
627 | req->fd = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh)) |
740 | req->int1 = fd; |
628 | : IoOFP (sv_2io (fh))); |
741 | req->offs = SvOK (offset) ? SvVAL64 (offset) : -1; |
629 | req->offset = offset; |
742 | req->size = len; |
630 | req->length = length; |
|
|
631 | req->data = SvREFCNT_inc (data); |
743 | req->sv2 = SvREFCNT_inc (data); |
632 | req->dataptr = (char *)svptr + dataoffset; |
744 | req->ptr2 = (char *)svptr + dataoffset; |
|
|
745 | req->stroffset = dataoffset; |
633 | |
746 | |
634 | send_req (req); |
747 | if (!SvREADONLY (data)) |
|
|
748 | { |
|
|
749 | SvREADONLY_on (data); |
|
|
750 | req->flags |= FLAG_SV2_RO_OFF; |
|
|
751 | } |
|
|
752 | |
|
|
753 | REQ_SEND; |
635 | } |
754 | } |
636 | } |
755 | } |
637 | |
756 | |
638 | void |
757 | void |
|
|
758 | aio_readlink (SV8 *path, SV *callback=&PL_sv_undef) |
|
|
759 | PROTOTYPE: $$;$ |
|
|
760 | PPCODE: |
|
|
761 | { |
|
|
762 | SV *data; |
|
|
763 | dREQ; |
|
|
764 | |
|
|
765 | req->type = EIO_READLINK; |
|
|
766 | req->sv1 = newSVsv (path); |
|
|
767 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
|
|
768 | |
|
|
769 | REQ_SEND; |
|
|
770 | } |
|
|
771 | |
|
|
772 | void |
|
|
773 | aio_sendfile (SV *out_fh, SV *in_fh, off_t in_offset, size_t length, SV *callback=&PL_sv_undef) |
|
|
774 | PROTOTYPE: $$$$;$ |
|
|
775 | PPCODE: |
|
|
776 | { |
|
|
777 | int ifd = s_fileno_croak (in_fh , 0); |
|
|
778 | int ofd = s_fileno_croak (out_fh, 1); |
|
|
779 | dREQ; |
|
|
780 | |
|
|
781 | req->type = EIO_SENDFILE; |
|
|
782 | req->sv1 = newSVsv (out_fh); |
|
|
783 | req->int1 = ofd; |
|
|
784 | req->sv2 = newSVsv (in_fh); |
|
|
785 | req->int2 = ifd; |
|
|
786 | req->offs = in_offset; |
|
|
787 | req->size = length; |
|
|
788 | |
|
|
789 | REQ_SEND; |
|
|
790 | } |
|
|
791 | |
|
|
792 | void |
639 | aio_readahead(fh,offset,length,callback=&PL_sv_undef) |
793 | aio_readahead (SV *fh, off_t offset, size_t length, SV *callback=&PL_sv_undef) |
640 | SV * fh |
|
|
641 | UV offset |
|
|
642 | IV length |
|
|
643 | SV * callback |
|
|
644 | PROTOTYPE: $$$;$ |
794 | PROTOTYPE: $$$;$ |
645 | CODE: |
795 | PPCODE: |
646 | { |
796 | { |
|
|
797 | int fd = s_fileno_croak (fh, 0); |
647 | dREQ; |
798 | dREQ; |
648 | |
799 | |
649 | req->type = REQ_READAHEAD; |
800 | req->type = EIO_READAHEAD; |
650 | req->fh = newSVsv (fh); |
801 | req->sv1 = newSVsv (fh); |
651 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
802 | req->int1 = fd; |
652 | req->offset = offset; |
803 | req->offs = offset; |
653 | req->length = length; |
804 | req->size = length; |
654 | |
805 | |
655 | send_req (req); |
806 | REQ_SEND; |
656 | } |
807 | } |
657 | |
808 | |
658 | void |
809 | void |
659 | aio_stat(fh_or_path,callback=&PL_sv_undef) |
810 | aio_stat (SV8 *fh_or_path, SV *callback=&PL_sv_undef) |
660 | SV * fh_or_path |
|
|
661 | SV * callback |
|
|
662 | ALIAS: |
811 | ALIAS: |
663 | aio_stat = REQ_STAT |
812 | aio_stat = EIO_STAT |
664 | aio_lstat = REQ_LSTAT |
813 | aio_lstat = EIO_LSTAT |
665 | CODE: |
814 | PPCODE: |
666 | { |
815 | { |
667 | dREQ; |
816 | dREQ; |
668 | |
817 | |
669 | New (0, req->statdata, 1, Stat_t); |
818 | req->sv1 = newSVsv (fh_or_path); |
670 | if (!req->statdata) |
|
|
671 | { |
|
|
672 | free_req (req); |
|
|
673 | croak ("out of memory during aio_req->statdata allocation"); |
|
|
674 | } |
|
|
675 | |
819 | |
676 | if (SvPOK (fh_or_path)) |
820 | if (SvPOK (req->sv1)) |
677 | { |
821 | { |
678 | req->type = ix; |
822 | req->type = ix; |
679 | req->data = newSVsv (fh_or_path); |
|
|
680 | req->dataptr = SvPVbyte_nolen (req->data); |
823 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
681 | } |
824 | } |
682 | else |
825 | else |
683 | { |
826 | { |
684 | req->type = REQ_FSTAT; |
827 | req->type = EIO_FSTAT; |
685 | req->fh = newSVsv (fh_or_path); |
|
|
686 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
828 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
|
|
829 | } |
|
|
830 | |
|
|
831 | REQ_SEND; |
|
|
832 | } |
|
|
833 | |
|
|
834 | void |
|
|
835 | aio_utime (SV8 *fh_or_path, SV *atime, SV *mtime, SV *callback=&PL_sv_undef) |
|
|
836 | PPCODE: |
|
|
837 | { |
|
|
838 | dREQ; |
|
|
839 | |
|
|
840 | req->nv1 = SvOK (atime) ? SvNV (atime) : -1.; |
|
|
841 | req->nv2 = SvOK (mtime) ? SvNV (mtime) : -1.; |
|
|
842 | req->sv1 = newSVsv (fh_or_path); |
|
|
843 | |
|
|
844 | if (SvPOK (req->sv1)) |
687 | } |
845 | { |
|
|
846 | req->type = EIO_UTIME; |
|
|
847 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
|
|
848 | } |
|
|
849 | else |
|
|
850 | { |
|
|
851 | req->type = EIO_FUTIME; |
|
|
852 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
|
|
853 | } |
688 | |
854 | |
689 | send_req (req); |
855 | REQ_SEND; |
690 | } |
856 | } |
691 | |
857 | |
692 | void |
858 | void |
|
|
859 | aio_truncate (SV8 *fh_or_path, SV *offset, SV *callback=&PL_sv_undef) |
|
|
860 | PPCODE: |
|
|
861 | { |
|
|
862 | dREQ; |
|
|
863 | |
|
|
864 | req->sv1 = newSVsv (fh_or_path); |
|
|
865 | req->offs = SvOK (offset) ? SvVAL64 (offset) : -1; |
|
|
866 | |
|
|
867 | if (SvPOK (req->sv1)) |
|
|
868 | { |
|
|
869 | req->type = EIO_TRUNCATE; |
|
|
870 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
|
|
871 | } |
|
|
872 | else |
|
|
873 | { |
|
|
874 | req->type = EIO_FTRUNCATE; |
|
|
875 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
|
|
876 | } |
|
|
877 | |
|
|
878 | REQ_SEND; |
|
|
879 | } |
|
|
880 | |
|
|
881 | void |
|
|
882 | aio_chmod (SV8 *fh_or_path, int mode, SV *callback=&PL_sv_undef) |
|
|
883 | ALIAS: |
|
|
884 | aio_chmod = EIO_CHMOD |
|
|
885 | aio_mkdir = EIO_MKDIR |
|
|
886 | PPCODE: |
|
|
887 | { |
|
|
888 | dREQ; |
|
|
889 | |
|
|
890 | req->int2 = mode; |
|
|
891 | req->sv1 = newSVsv (fh_or_path); |
|
|
892 | |
|
|
893 | if (SvPOK (req->sv1)) |
|
|
894 | { |
|
|
895 | req->type = ix; |
|
|
896 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
|
|
897 | } |
|
|
898 | else |
|
|
899 | { |
|
|
900 | req->type = EIO_FCHMOD; |
|
|
901 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
|
|
902 | } |
|
|
903 | |
|
|
904 | REQ_SEND; |
|
|
905 | } |
|
|
906 | |
|
|
907 | void |
|
|
908 | aio_chown (SV8 *fh_or_path, SV *uid, SV *gid, SV *callback=&PL_sv_undef) |
|
|
909 | PPCODE: |
|
|
910 | { |
|
|
911 | dREQ; |
|
|
912 | |
|
|
913 | req->int2 = SvOK (uid) ? SvIV (uid) : -1; |
|
|
914 | req->int3 = SvOK (gid) ? SvIV (gid) : -1; |
|
|
915 | req->sv1 = newSVsv (fh_or_path); |
|
|
916 | |
|
|
917 | if (SvPOK (req->sv1)) |
|
|
918 | { |
|
|
919 | req->type = EIO_CHOWN; |
|
|
920 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
|
|
921 | } |
|
|
922 | else |
|
|
923 | { |
|
|
924 | req->type = EIO_FCHOWN; |
|
|
925 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
|
|
926 | } |
|
|
927 | |
|
|
928 | REQ_SEND; |
|
|
929 | } |
|
|
930 | |
|
|
931 | void |
|
|
932 | aio_readdirx (SV8 *pathname, IV flags, SV *callback=&PL_sv_undef) |
|
|
933 | PPCODE: |
|
|
934 | { |
|
|
935 | dREQ; |
|
|
936 | |
|
|
937 | req->type = EIO_READDIR; |
|
|
938 | req->sv1 = newSVsv (pathname); |
|
|
939 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
|
|
940 | req->int1 = flags | EIO_READDIR_DENTS | EIO_READDIR_CUSTOM1; |
|
|
941 | |
|
|
942 | if (flags & EIO_READDIR_DENTS) |
|
|
943 | req->int1 |= EIO_READDIR_CUSTOM2; |
|
|
944 | |
|
|
945 | REQ_SEND; |
|
|
946 | } |
|
|
947 | |
|
|
948 | void |
693 | aio_unlink(pathname,callback=&PL_sv_undef) |
949 | aio_unlink (SV8 *pathname, SV *callback=&PL_sv_undef) |
694 | SV * pathname |
|
|
695 | SV * callback |
|
|
696 | ALIAS: |
950 | ALIAS: |
697 | aio_unlink = REQ_UNLINK |
951 | aio_unlink = EIO_UNLINK |
698 | aio_rmdir = REQ_RMDIR |
952 | aio_rmdir = EIO_RMDIR |
|
|
953 | aio_readdir = EIO_READDIR |
699 | CODE: |
954 | PPCODE: |
700 | { |
955 | { |
701 | dREQ; |
956 | dREQ; |
702 | |
957 | |
703 | req->type = ix; |
958 | req->type = ix; |
704 | req->data = newSVsv (pathname); |
959 | req->sv1 = newSVsv (pathname); |
705 | req->dataptr = SvPVbyte_nolen (req->data); |
960 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
|
|
961 | |
|
|
962 | REQ_SEND; |
|
|
963 | } |
|
|
964 | |
|
|
965 | void |
|
|
966 | aio_link (SV8 *oldpath, SV8 *newpath, SV *callback=&PL_sv_undef) |
|
|
967 | ALIAS: |
|
|
968 | aio_link = EIO_LINK |
|
|
969 | aio_symlink = EIO_SYMLINK |
|
|
970 | aio_rename = EIO_RENAME |
|
|
971 | PPCODE: |
|
|
972 | { |
|
|
973 | dREQ; |
706 | |
974 | |
707 | send_req (req); |
975 | req->type = ix; |
708 | } |
976 | req->sv1 = newSVsv (oldpath); |
709 | |
977 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
710 | void |
978 | req->sv2 = newSVsv (newpath); |
711 | aio_symlink(oldpath,newpath,callback=&PL_sv_undef) |
979 | req->ptr2 = SvPVbyte_nolen (req->sv2); |
712 | SV * oldpath |
|
|
713 | SV * newpath |
|
|
714 | SV * callback |
|
|
715 | CODE: |
|
|
716 | { |
|
|
717 | dREQ; |
|
|
718 | |
980 | |
719 | req->type = REQ_SYMLINK; |
981 | REQ_SEND; |
720 | req->fh = newSVsv (oldpath); |
982 | } |
721 | req->data2ptr = SvPVbyte_nolen (req->fh); |
983 | |
722 | req->data = newSVsv (newpath); |
984 | void |
723 | req->dataptr = SvPVbyte_nolen (req->data); |
985 | aio_mknod (SV8 *pathname, int mode, UV dev, SV *callback=&PL_sv_undef) |
|
|
986 | PPCODE: |
|
|
987 | { |
|
|
988 | dREQ; |
724 | |
989 | |
725 | send_req (req); |
990 | req->type = EIO_MKNOD; |
|
|
991 | req->sv1 = newSVsv (pathname); |
|
|
992 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
|
|
993 | req->int2 = (mode_t)mode; |
|
|
994 | req->offs = dev; |
|
|
995 | |
|
|
996 | REQ_SEND; |
726 | } |
997 | } |
727 | |
998 | |
728 | void |
999 | void |
|
|
1000 | aio_busy (double delay, SV *callback=&PL_sv_undef) |
|
|
1001 | PPCODE: |
|
|
1002 | { |
|
|
1003 | dREQ; |
|
|
1004 | |
|
|
1005 | req->type = EIO_BUSY; |
|
|
1006 | req->nv1 = delay < 0. ? 0. : delay; |
|
|
1007 | |
|
|
1008 | REQ_SEND; |
|
|
1009 | } |
|
|
1010 | |
|
|
1011 | void |
|
|
1012 | aio_group (SV *callback=&PL_sv_undef) |
|
|
1013 | PROTOTYPE: ;$ |
|
|
1014 | PPCODE: |
|
|
1015 | { |
|
|
1016 | dREQ; |
|
|
1017 | |
|
|
1018 | req->type = EIO_GROUP; |
|
|
1019 | |
|
|
1020 | req_submit (req); |
|
|
1021 | XPUSHs (req_sv (req, aio_grp_stash)); |
|
|
1022 | } |
|
|
1023 | |
|
|
1024 | void |
|
|
1025 | aio_nop (SV *callback=&PL_sv_undef) |
|
|
1026 | ALIAS: |
|
|
1027 | aio_nop = EIO_NOP |
|
|
1028 | aio_sync = EIO_SYNC |
|
|
1029 | PPCODE: |
|
|
1030 | { |
|
|
1031 | dREQ; |
|
|
1032 | |
|
|
1033 | req->type = ix; |
|
|
1034 | |
|
|
1035 | REQ_SEND; |
|
|
1036 | } |
|
|
1037 | |
|
|
1038 | int |
|
|
1039 | aioreq_pri (int pri = 0) |
|
|
1040 | PROTOTYPE: ;$ |
|
|
1041 | CODE: |
|
|
1042 | RETVAL = next_pri; |
|
|
1043 | if (items > 0) |
|
|
1044 | { |
|
|
1045 | if (pri < EIO_PRI_MIN) pri = EIO_PRI_MIN; |
|
|
1046 | if (pri > EIO_PRI_MAX) pri = EIO_PRI_MAX; |
|
|
1047 | next_pri = pri; |
|
|
1048 | } |
|
|
1049 | OUTPUT: |
|
|
1050 | RETVAL |
|
|
1051 | |
|
|
1052 | void |
|
|
1053 | aioreq_nice (int nice = 0) |
|
|
1054 | CODE: |
|
|
1055 | nice = next_pri - nice; |
|
|
1056 | if (nice < EIO_PRI_MIN) nice = EIO_PRI_MIN; |
|
|
1057 | if (nice > EIO_PRI_MAX) nice = EIO_PRI_MAX; |
|
|
1058 | next_pri = nice; |
|
|
1059 | |
|
|
1060 | void |
729 | flush() |
1061 | flush () |
730 | PROTOTYPE: |
1062 | PROTOTYPE: |
731 | CODE: |
1063 | CODE: |
732 | while (nreqs) |
1064 | while (eio_nreqs ()) |
733 | { |
1065 | { |
734 | poll_wait (); |
1066 | poll_wait (); |
735 | poll_cb (); |
1067 | poll_cb (); |
736 | } |
1068 | } |
737 | |
1069 | |
738 | void |
1070 | int |
739 | poll() |
1071 | poll() |
740 | PROTOTYPE: |
1072 | PROTOTYPE: |
741 | CODE: |
1073 | CODE: |
742 | if (nreqs) |
|
|
743 | { |
|
|
744 | poll_wait (); |
1074 | poll_wait (); |
745 | poll_cb (); |
1075 | RETVAL = poll_cb (); |
746 | } |
1076 | OUTPUT: |
|
|
1077 | RETVAL |
747 | |
1078 | |
748 | int |
1079 | int |
749 | poll_fileno() |
1080 | poll_fileno() |
750 | PROTOTYPE: |
1081 | PROTOTYPE: |
751 | CODE: |
1082 | CODE: |
752 | RETVAL = respipe [0]; |
1083 | RETVAL = s_epipe_fd (&respipe); |
753 | OUTPUT: |
1084 | OUTPUT: |
754 | RETVAL |
1085 | RETVAL |
755 | |
1086 | |
756 | int |
1087 | int |
757 | poll_cb(...) |
1088 | poll_cb(...) |
… | |
… | |
763 | |
1094 | |
764 | void |
1095 | void |
765 | poll_wait() |
1096 | poll_wait() |
766 | PROTOTYPE: |
1097 | PROTOTYPE: |
767 | CODE: |
1098 | CODE: |
768 | if (nreqs) |
|
|
769 | poll_wait (); |
1099 | poll_wait (); |
770 | |
1100 | |
771 | int |
1101 | int |
772 | nreqs() |
1102 | nreqs() |
773 | PROTOTYPE: |
1103 | PROTOTYPE: |
774 | CODE: |
1104 | CODE: |
775 | RETVAL = nreqs; |
1105 | RETVAL = eio_nreqs (); |
776 | OUTPUT: |
1106 | OUTPUT: |
777 | RETVAL |
1107 | RETVAL |
778 | |
1108 | |
|
|
1109 | int |
|
|
1110 | nready() |
|
|
1111 | PROTOTYPE: |
|
|
1112 | CODE: |
|
|
1113 | RETVAL = eio_nready (); |
|
|
1114 | OUTPUT: |
|
|
1115 | RETVAL |
|
|
1116 | |
|
|
1117 | int |
|
|
1118 | npending() |
|
|
1119 | PROTOTYPE: |
|
|
1120 | CODE: |
|
|
1121 | RETVAL = eio_npending (); |
|
|
1122 | OUTPUT: |
|
|
1123 | RETVAL |
|
|
1124 | |
|
|
1125 | int |
|
|
1126 | nthreads() |
|
|
1127 | PROTOTYPE: |
|
|
1128 | CODE: |
|
|
1129 | RETVAL = eio_nthreads (); |
|
|
1130 | OUTPUT: |
|
|
1131 | RETVAL |
|
|
1132 | |
|
|
1133 | int |
|
|
1134 | fadvise (aio_rfd fh, off_t offset, off_t length, IV advice) |
|
|
1135 | PROTOTYPE: $$$$ |
|
|
1136 | CODE: |
|
|
1137 | #if _XOPEN_SOURCE >= 600 && !NO_FADVISE |
|
|
1138 | RETVAL = posix_fadvise (fh, offset, length, advice); |
|
|
1139 | #else |
|
|
1140 | RETVAL = errno = ENOSYS; |
|
|
1141 | #endif |
|
|
1142 | OUTPUT: |
|
|
1143 | RETVAL |
|
|
1144 | |
|
|
1145 | ssize_t |
|
|
1146 | sendfile (aio_wfd ofh, aio_rfd ifh, off_t offset, size_t count) |
|
|
1147 | PROTOTYPE: $$$$ |
|
|
1148 | CODE: |
|
|
1149 | eio_sendfile_sync (ofh, ifh, offset, count); |
|
|
1150 | |
|
|
1151 | void _on_next_submit (SV *cb) |
|
|
1152 | CODE: |
|
|
1153 | SvREFCNT_dec (on_next_submit); |
|
|
1154 | on_next_submit = SvOK (cb) ? newSVsv (cb) : 0; |
|
|
1155 | |
|
|
1156 | PROTOTYPES: DISABLE |
|
|
1157 | |
|
|
1158 | MODULE = IO::AIO PACKAGE = IO::AIO::REQ |
|
|
1159 | |
|
|
1160 | void |
|
|
1161 | cancel (aio_req_ornot req) |
|
|
1162 | CODE: |
|
|
1163 | eio_cancel (req); |
|
|
1164 | |
|
|
1165 | void |
|
|
1166 | cb (aio_req_ornot req, SV *callback=&PL_sv_undef) |
|
|
1167 | PPCODE: |
|
|
1168 | { |
|
|
1169 | if (GIMME_V != G_VOID) |
|
|
1170 | XPUSHs (req->callback ? sv_2mortal (newRV_inc (req->callback)) : &PL_sv_undef); |
|
|
1171 | |
|
|
1172 | if (items > 1) |
|
|
1173 | { |
|
|
1174 | SV *cb_cv =get_cb (callback); |
|
|
1175 | |
|
|
1176 | SvREFCNT_dec (req->callback); |
|
|
1177 | req->callback = SvREFCNT_inc (cb_cv); |
|
|
1178 | } |
|
|
1179 | } |
|
|
1180 | |
|
|
1181 | MODULE = IO::AIO PACKAGE = IO::AIO::GRP |
|
|
1182 | |
|
|
1183 | void |
|
|
1184 | add (aio_req grp, ...) |
|
|
1185 | PPCODE: |
|
|
1186 | { |
|
|
1187 | int i; |
|
|
1188 | |
|
|
1189 | if (grp->int1 == 2) |
|
|
1190 | croak ("cannot add requests to IO::AIO::GRP after the group finished"); |
|
|
1191 | |
|
|
1192 | for (i = 1; i < items; ++i ) |
|
|
1193 | { |
|
|
1194 | aio_req req; |
|
|
1195 | |
|
|
1196 | if (GIMME_V != G_VOID) |
|
|
1197 | XPUSHs (sv_2mortal (newSVsv (ST (i)))); |
|
|
1198 | |
|
|
1199 | req = SvAIO_REQ (ST (i)); |
|
|
1200 | |
|
|
1201 | if (req) |
|
|
1202 | eio_grp_add (grp, req); |
|
|
1203 | } |
|
|
1204 | } |
|
|
1205 | |
|
|
1206 | void |
|
|
1207 | cancel_subs (aio_req_ornot req) |
|
|
1208 | CODE: |
|
|
1209 | req_cancel_subs (req); |
|
|
1210 | |
|
|
1211 | void |
|
|
1212 | result (aio_req grp, ...) |
|
|
1213 | CODE: |
|
|
1214 | { |
|
|
1215 | int i; |
|
|
1216 | AV *av; |
|
|
1217 | |
|
|
1218 | grp->errorno = errno; |
|
|
1219 | |
|
|
1220 | av = newAV (); |
|
|
1221 | av_extend (av, items - 1); |
|
|
1222 | |
|
|
1223 | for (i = 1; i < items; ++i ) |
|
|
1224 | av_push (av, newSVsv (ST (i))); |
|
|
1225 | |
|
|
1226 | SvREFCNT_dec (grp->sv1); |
|
|
1227 | grp->sv1 = (SV *)av; |
|
|
1228 | } |
|
|
1229 | |
|
|
1230 | void |
|
|
1231 | errno (aio_req grp, int errorno = errno) |
|
|
1232 | CODE: |
|
|
1233 | grp->errorno = errorno; |
|
|
1234 | |
|
|
1235 | void |
|
|
1236 | limit (aio_req grp, int limit) |
|
|
1237 | CODE: |
|
|
1238 | eio_grp_limit (grp, limit); |
|
|
1239 | |
|
|
1240 | void |
|
|
1241 | feed (aio_req grp, SV *callback=&PL_sv_undef) |
|
|
1242 | CODE: |
|
|
1243 | { |
|
|
1244 | SvREFCNT_dec (grp->sv2); |
|
|
1245 | grp->sv2 = newSVsv (callback); |
|
|
1246 | grp->feed = aio_grp_feed; |
|
|
1247 | |
|
|
1248 | if (grp->int2 <= 0) |
|
|
1249 | grp->int2 = 2; |
|
|
1250 | |
|
|
1251 | eio_grp_limit (grp, grp->int2); |
|
|
1252 | } |
|
|
1253 | |