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