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 <stddef.h> |
9 | |
10 | #include <stdlib.h> |
|
|
11 | #include <errno.h> |
10 | #include <sys/types.h> |
12 | #include <sys/types.h> |
11 | #include <sys/stat.h> |
13 | #include <sys/stat.h> |
12 | |
|
|
13 | #include <unistd.h> |
14 | #include <limits.h> |
14 | #include <fcntl.h> |
15 | #include <fcntl.h> |
15 | #include <signal.h> |
|
|
16 | #include <sched.h> |
16 | #include <sched.h> |
17 | |
17 | |
18 | #include <pthread.h> |
18 | /* perl namespace pollution */ |
|
|
19 | #undef VERSION |
19 | |
20 | |
20 | typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
21 | #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 | |
22 | |
24 | #if __ia64 |
23 | # define EIO_STRUCT_DIRENT Direntry_t |
25 | # define STACKSIZE 65536 |
24 | # undef malloc |
|
|
25 | # undef free |
|
|
26 | |
|
|
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 | |
26 | #else |
59 | #else |
27 | # define STACKSIZE 4096 |
60 | |
|
|
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 | |
28 | #endif |
68 | #endif |
29 | |
69 | |
|
|
70 | /* perl stupidly overrides readdir and maybe others */ |
|
|
71 | /* with thread-unsafe versions, imagine that :( */ |
|
|
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 |
|
|
83 | #endif |
|
|
84 | |
|
|
85 | /*****************************************************************************/ |
|
|
86 | |
|
|
87 | #if __GNUC__ >= 3 |
|
|
88 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
89 | #else |
|
|
90 | # define expect(expr,value) (expr) |
|
|
91 | #endif |
|
|
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 | |
30 | enum { |
119 | enum { |
31 | REQ_QUIT, |
120 | 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 | }; |
121 | }; |
39 | |
122 | |
40 | typedef struct aio_cb { |
123 | #include "libeio/eio.c" |
41 | struct aio_cb *volatile next; |
|
|
42 | |
124 | |
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; |
125 | typedef eio_req *aio_req; |
|
|
126 | typedef eio_req *aio_req_ornot; |
59 | |
127 | |
60 | static int started, wanted; |
128 | static SV *on_next_submit; |
61 | static volatile int nreqs; |
129 | static int next_pri = EIO_PRI_DEFAULT; |
62 | static int max_outstanding = 1<<30; |
130 | static int max_outstanding; |
63 | static int respipe [2]; |
|
|
64 | |
131 | |
65 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
132 | static int respipe_osf [2], respipe [2] = { -1, -1 }; |
66 | static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; |
|
|
67 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
|
|
68 | |
133 | |
69 | static volatile aio_req reqs, reqe; /* queue start, queue end */ |
134 | 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) |
135 | static void req_cancel (aio_req req); |
73 | { |
|
|
74 | if (req->data) |
|
|
75 | SvREFCNT_dec (req->data); |
|
|
76 | |
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 | { |
77 | if (req->fh) |
349 | if (req->self) |
|
|
350 | { |
|
|
351 | sv_unmagic (req->self, PERL_MAGIC_ext); |
78 | SvREFCNT_dec (req->fh); |
352 | SvREFCNT_dec (req->self); |
|
|
353 | } |
79 | |
354 | |
80 | if (req->statdata) |
355 | SvREFCNT_dec (req->sv1); |
81 | Safefree (req->statdata); |
356 | SvREFCNT_dec (req->sv2); |
82 | |
|
|
83 | if (req->callback) |
|
|
84 | SvREFCNT_dec (req->callback); |
357 | SvREFCNT_dec (req->callback); |
85 | |
358 | |
86 | Safefree (req); |
359 | Safefree (req); |
87 | } |
360 | } |
88 | |
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 | |
89 | static void |
381 | static void |
90 | poll_wait () |
382 | create_respipe (void) |
91 | { |
383 | { |
92 | 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) |
93 | { |
397 | { |
94 | fd_set rfd; |
398 | if (dup2 (TO_SOCKET (respipe [0]), TO_SOCKET (old_readfd)) < 0) |
95 | FD_ZERO(&rfd); |
399 | croak ("unable to initialize result pipe(2)"); |
96 | FD_SET(respipe [0], &rfd); |
|
|
97 | |
400 | |
98 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
401 | respipe_close (respipe [0]); |
|
|
402 | respipe [0] = old_readfd; |
99 | } |
403 | } |
100 | } |
|
|
101 | |
404 | |
102 | static int |
405 | #ifdef _WIN32 |
103 | poll_cb () |
406 | int arg = 1; |
104 | { |
407 | if (ioctlsocket (TO_SOCKET (respipe [0]), FIONBIO, &arg) |
105 | dSP; |
408 | || ioctlsocket (TO_SOCKET (respipe [1]), FIONBIO, &arg)) |
106 | int count = 0; |
409 | #else |
107 | int do_croak = 0; |
410 | if (fcntl (respipe [0], F_SETFL, O_NONBLOCK) |
108 | aio_req req; |
411 | || fcntl (respipe [1], F_SETFL, O_NONBLOCK)) |
|
|
412 | #endif |
|
|
413 | croak ("unable to initialize result pipe(3)"); |
109 | |
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 | { |
110 | for (;;) |
445 | for (;;) |
111 | { |
446 | { |
112 | pthread_mutex_lock (&reslock); |
447 | int res = eio_poll (); |
113 | req = ress; |
|
|
114 | |
448 | |
115 | if (req) |
449 | if (res > 0) |
116 | { |
|
|
117 | ress = req->next; |
|
|
118 | |
|
|
119 | if (!ress) |
|
|
120 | { |
|
|
121 | /* read any signals sent by the worker threads */ |
|
|
122 | char buf [32]; |
|
|
123 | while (read (respipe [0], buf, 32) == 32) |
|
|
124 | ; |
|
|
125 | |
|
|
126 | rese = 0; |
|
|
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->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE)) |
|
|
148 | SvREADONLY_off (req->data); |
|
|
149 | |
|
|
150 | if (req->statdata) |
|
|
151 | { |
|
|
152 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
|
|
153 | PL_laststatval = req->result; |
|
|
154 | PL_statcache = *(req->statdata); |
|
|
155 | } |
|
|
156 | |
|
|
157 | ENTER; |
|
|
158 | PUSHMARK (SP); |
|
|
159 | XPUSHs (sv_2mortal (newSViv (req->result))); |
|
|
160 | |
|
|
161 | if (req->type == REQ_OPEN) |
|
|
162 | { |
|
|
163 | /* convert fd to fh */ |
|
|
164 | SV *fh; |
|
|
165 | |
|
|
166 | PUTBACK; |
|
|
167 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
|
|
168 | SPAGAIN; |
|
|
169 | |
|
|
170 | fh = SvREFCNT_inc (POPs); |
|
|
171 | |
|
|
172 | PUSHMARK (SP); |
|
|
173 | XPUSHs (sv_2mortal (fh)); |
|
|
174 | } |
|
|
175 | |
|
|
176 | if (SvOK (req->callback)) |
|
|
177 | { |
|
|
178 | PUTBACK; |
|
|
179 | call_sv (req->callback, G_VOID | G_EVAL); |
|
|
180 | SPAGAIN; |
|
|
181 | |
|
|
182 | if (SvTRUE (ERRSV)) |
|
|
183 | { |
|
|
184 | free_req (req); |
|
|
185 | croak (0); |
450 | croak (0); |
186 | } |
|
|
187 | } |
|
|
188 | |
451 | |
189 | LEAVE; |
452 | if (!max_outstanding || max_outstanding > eio_nreqs ()) |
|
|
453 | return res; |
190 | |
454 | |
191 | errno = errorno; |
455 | poll_wait (); |
192 | count++; |
|
|
193 | } |
|
|
194 | |
|
|
195 | free_req (req); |
|
|
196 | } |
456 | } |
197 | |
|
|
198 | return count; |
|
|
199 | } |
|
|
200 | |
|
|
201 | static void *aio_proc(void *arg); |
|
|
202 | |
|
|
203 | static void |
|
|
204 | start_thread (void) |
|
|
205 | { |
|
|
206 | sigset_t fullsigset, oldsigset; |
|
|
207 | pthread_t tid; |
|
|
208 | pthread_attr_t attr; |
|
|
209 | |
|
|
210 | pthread_attr_init (&attr); |
|
|
211 | pthread_attr_setstacksize (&attr, STACKSIZE); |
|
|
212 | // pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); |
|
|
213 | |
|
|
214 | sigfillset (&fullsigset); |
|
|
215 | sigprocmask (SIG_SETMASK, &fullsigset, &oldsigset); |
|
|
216 | |
|
|
217 | if (pthread_create (&tid, &attr, aio_proc, 0) == 0) |
|
|
218 | started++; |
|
|
219 | |
|
|
220 | sigprocmask (SIG_SETMASK, &oldsigset, 0); |
|
|
221 | } |
|
|
222 | |
|
|
223 | static void |
|
|
224 | send_req (aio_req req) |
|
|
225 | { |
|
|
226 | while (started < wanted && nreqs >= started) |
|
|
227 | start_thread (); |
|
|
228 | |
|
|
229 | nreqs++; |
|
|
230 | |
|
|
231 | pthread_mutex_lock (&reqlock); |
|
|
232 | |
|
|
233 | req->next = 0; |
|
|
234 | |
|
|
235 | if (reqe) |
|
|
236 | { |
|
|
237 | reqe->next = req; |
|
|
238 | reqe = req; |
|
|
239 | } |
|
|
240 | else |
|
|
241 | reqe = reqs = req; |
|
|
242 | |
|
|
243 | pthread_cond_signal (&reqwait); |
|
|
244 | pthread_mutex_unlock (&reqlock); |
|
|
245 | |
|
|
246 | if (nreqs > max_outstanding) |
|
|
247 | for (;;) |
|
|
248 | { |
|
|
249 | poll_cb (); |
|
|
250 | |
|
|
251 | if (nreqs <= max_outstanding) |
|
|
252 | break; |
|
|
253 | |
|
|
254 | poll_wait (); |
|
|
255 | } |
|
|
256 | } |
|
|
257 | |
|
|
258 | static void |
|
|
259 | end_thread (void) |
|
|
260 | { |
|
|
261 | aio_req req; |
|
|
262 | Newz (0, req, 1, aio_cb); |
|
|
263 | req->type = REQ_QUIT; |
|
|
264 | |
|
|
265 | send_req (req); |
|
|
266 | } |
|
|
267 | |
|
|
268 | static void min_parallel (int nthreads) |
|
|
269 | { |
|
|
270 | if (wanted < nthreads) |
|
|
271 | wanted = nthreads; |
|
|
272 | } |
|
|
273 | |
|
|
274 | static void max_parallel (int nthreads) |
|
|
275 | { |
|
|
276 | int cur = started; |
|
|
277 | |
|
|
278 | if (wanted > nthreads) |
|
|
279 | wanted = nthreads; |
|
|
280 | |
|
|
281 | while (cur > wanted) |
|
|
282 | { |
|
|
283 | end_thread (); |
|
|
284 | cur--; |
|
|
285 | } |
|
|
286 | |
|
|
287 | while (started > wanted) |
|
|
288 | { |
|
|
289 | poll_wait (); |
|
|
290 | poll_cb (); |
|
|
291 | } |
|
|
292 | } |
|
|
293 | |
|
|
294 | static void create_pipe () |
|
|
295 | { |
|
|
296 | if (pipe (respipe)) |
|
|
297 | croak ("unable to initialize result pipe"); |
|
|
298 | |
|
|
299 | if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)) |
|
|
300 | croak ("cannot set result pipe to nonblocking mode"); |
|
|
301 | |
|
|
302 | if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) |
|
|
303 | croak ("cannot set result pipe to nonblocking mode"); |
|
|
304 | } |
|
|
305 | |
|
|
306 | static void atfork_prepare (void) |
|
|
307 | { |
|
|
308 | pthread_mutex_lock (&reqlock); |
|
|
309 | pthread_mutex_lock (&reslock); |
|
|
310 | } |
|
|
311 | |
|
|
312 | static void atfork_parent (void) |
|
|
313 | { |
|
|
314 | pthread_mutex_unlock (&reslock); |
|
|
315 | pthread_mutex_unlock (&reqlock); |
|
|
316 | } |
457 | } |
317 | |
458 | |
318 | static void atfork_child (void) |
459 | static void atfork_child (void) |
319 | { |
460 | { |
320 | aio_req prv; |
|
|
321 | |
|
|
322 | started = 0; |
|
|
323 | |
|
|
324 | while (reqs) |
|
|
325 | { |
|
|
326 | prv = reqs; |
|
|
327 | reqs = prv->next; |
|
|
328 | free_req (prv); |
|
|
329 | } |
|
|
330 | |
|
|
331 | reqs = reqe = 0; |
|
|
332 | |
|
|
333 | while (ress) |
|
|
334 | { |
|
|
335 | prv = ress; |
|
|
336 | ress = prv->next; |
|
|
337 | free_req (prv); |
|
|
338 | } |
|
|
339 | |
|
|
340 | ress = rese = 0; |
|
|
341 | |
|
|
342 | close (respipe [0]); |
|
|
343 | close (respipe [1]); |
|
|
344 | create_pipe (); |
461 | create_respipe (); |
345 | |
|
|
346 | atfork_parent (); |
|
|
347 | } |
462 | } |
348 | |
463 | |
349 | /*****************************************************************************/ |
|
|
350 | /* work around various missing functions */ |
|
|
351 | |
|
|
352 | #if !HAVE_PREADWRITE |
|
|
353 | # define pread aio_pread |
|
|
354 | # define pwrite aio_pwrite |
|
|
355 | |
|
|
356 | /* |
|
|
357 | * make our pread/pwrite safe against themselves, but not against |
|
|
358 | * normal read/write by using a mutex. slows down execution a lot, |
|
|
359 | * but that's your problem, not mine. |
|
|
360 | */ |
|
|
361 | static pthread_mutex_t iolock = PTHREAD_MUTEX_INITIALIZER; |
|
|
362 | |
|
|
363 | static ssize_t |
|
|
364 | pread (int fd, void *buf, size_t count, off_t offset) |
|
|
365 | { |
|
|
366 | ssize_t res; |
|
|
367 | off_t ooffset; |
|
|
368 | |
|
|
369 | pthread_mutex_lock (&iolock); |
|
|
370 | ooffset = lseek (fd, 0, SEEK_CUR); |
|
|
371 | lseek (fd, offset, SEEK_SET); |
|
|
372 | res = read (fd, buf, count); |
|
|
373 | lseek (fd, ooffset, SEEK_SET); |
|
|
374 | pthread_mutex_unlock (&iolock); |
|
|
375 | |
|
|
376 | return res; |
|
|
377 | } |
|
|
378 | |
|
|
379 | static ssize_t |
|
|
380 | pwrite (int fd, void *buf, size_t count, off_t offset) |
|
|
381 | { |
|
|
382 | ssize_t res; |
|
|
383 | off_t ooffset; |
|
|
384 | |
|
|
385 | pthread_mutex_lock (&iolock); |
|
|
386 | ooffset = lseek (fd, 0, SEEK_CUR); |
|
|
387 | lseek (fd, offset, SEEK_SET); |
|
|
388 | res = write (fd, buf, count); |
|
|
389 | lseek (fd, offset, SEEK_SET); |
|
|
390 | pthread_mutex_unlock (&iolock); |
|
|
391 | |
|
|
392 | return res; |
|
|
393 | } |
|
|
394 | #endif |
|
|
395 | |
|
|
396 | #if !HAVE_FDATASYNC |
|
|
397 | # define fdatasync fsync |
|
|
398 | #endif |
|
|
399 | |
|
|
400 | #if !HAVE_READAHEAD |
|
|
401 | # define readahead aio_readahead |
|
|
402 | |
|
|
403 | static char readahead_buf[4096]; |
|
|
404 | |
|
|
405 | static ssize_t |
|
|
406 | readahead (int fd, off_t offset, size_t count) |
|
|
407 | { |
|
|
408 | while (count > 0) |
|
|
409 | { |
|
|
410 | size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); |
|
|
411 | |
|
|
412 | pread (fd, readahead_buf, len, offset); |
|
|
413 | offset += len; |
|
|
414 | count -= len; |
|
|
415 | } |
|
|
416 | |
|
|
417 | errno = 0; |
|
|
418 | } |
|
|
419 | #endif |
|
|
420 | |
|
|
421 | /*****************************************************************************/ |
|
|
422 | |
|
|
423 | static void * |
464 | static SV * |
424 | aio_proc (void *thr_arg) |
465 | get_cb (SV *cb_sv) |
425 | { |
466 | { |
426 | aio_req req; |
467 | HV *st; |
427 | int type; |
468 | GV *gvp; |
|
|
469 | CV *cv; |
428 | |
470 | |
429 | do |
471 | if (!SvOK (cb_sv)) |
430 | { |
|
|
431 | pthread_mutex_lock (&reqlock); |
|
|
432 | |
|
|
433 | for (;;) |
|
|
434 | { |
|
|
435 | req = reqs; |
|
|
436 | |
|
|
437 | if (reqs) |
|
|
438 | { |
|
|
439 | reqs = reqs->next; |
|
|
440 | if (!reqs) reqe = 0; |
|
|
441 | } |
|
|
442 | |
|
|
443 | if (req) |
|
|
444 | break; |
|
|
445 | |
|
|
446 | pthread_cond_wait (&reqwait, &reqlock); |
|
|
447 | } |
|
|
448 | |
|
|
449 | pthread_mutex_unlock (&reqlock); |
|
|
450 | |
|
|
451 | errno = 0; /* strictly unnecessary */ |
|
|
452 | |
|
|
453 | type = req->type; |
|
|
454 | |
|
|
455 | switch (type) |
|
|
456 | { |
|
|
457 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
|
|
458 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
|
|
459 | |
|
|
460 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
|
|
461 | |
|
|
462 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
|
|
463 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
|
|
464 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
|
|
465 | |
|
|
466 | case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
|
|
467 | case REQ_CLOSE: req->result = close (req->fd); break; |
|
|
468 | case REQ_UNLINK: req->result = unlink (req->dataptr); break; |
|
|
469 | case REQ_RMDIR: req->result = rmdir (req->dataptr); break; |
|
|
470 | case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; |
|
|
471 | |
|
|
472 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
|
|
473 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
|
|
474 | |
|
|
475 | case REQ_QUIT: |
|
|
476 | break; |
|
|
477 | |
|
|
478 | default: |
|
|
479 | req->result = ENOSYS; |
|
|
480 | break; |
|
|
481 | } |
|
|
482 | |
|
|
483 | req->errorno = errno; |
|
|
484 | |
|
|
485 | pthread_mutex_lock (&reslock); |
|
|
486 | |
|
|
487 | req->next = 0; |
|
|
488 | |
|
|
489 | if (rese) |
|
|
490 | { |
|
|
491 | rese->next = req; |
|
|
492 | rese = req; |
|
|
493 | } |
|
|
494 | else |
|
|
495 | { |
|
|
496 | rese = ress = req; |
|
|
497 | |
|
|
498 | /* write a dummy byte to the pipe so fh becomes ready */ |
|
|
499 | write (respipe [1], &respipe, 1); |
|
|
500 | } |
|
|
501 | |
|
|
502 | pthread_mutex_unlock (&reslock); |
|
|
503 | } |
|
|
504 | while (type != REQ_QUIT); |
|
|
505 | |
|
|
506 | return 0; |
472 | return 0; |
|
|
473 | |
|
|
474 | cv = sv_2cv (cb_sv, &st, &gvp, 0); |
|
|
475 | |
|
|
476 | if (!cv) |
|
|
477 | croak ("IO::AIO callback must be undef or a CODE reference"); |
|
|
478 | |
|
|
479 | return (SV *)cv; |
507 | } |
480 | } |
508 | |
481 | |
509 | #define dREQ \ |
482 | #define dREQ \ |
|
|
483 | SV *cb_cv; \ |
510 | aio_req req; \ |
484 | aio_req req; \ |
|
|
485 | int req_pri = next_pri; \ |
|
|
486 | next_pri = EIO_PRI_DEFAULT; \ |
511 | \ |
487 | \ |
512 | if (SvOK (callback) && !SvROK (callback)) \ |
488 | cb_cv = get_cb (callback); \ |
513 | croak ("clalback must be undef or of reference type"); \ |
|
|
514 | \ |
489 | \ |
515 | Newz (0, req, 1, aio_cb); \ |
490 | Newz (0, req, 1, eio_req); \ |
516 | if (!req) \ |
491 | if (!req) \ |
517 | croak ("out of memory during aio_req allocation"); \ |
492 | croak ("out of memory during eio_req allocation"); \ |
518 | \ |
493 | \ |
519 | 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)); |
520 | |
504 | |
521 | MODULE = IO::AIO PACKAGE = IO::AIO |
505 | MODULE = IO::AIO PACKAGE = IO::AIO |
522 | |
506 | |
523 | PROTOTYPES: ENABLE |
507 | PROTOTYPES: ENABLE |
524 | |
508 | |
525 | BOOT: |
509 | BOOT: |
526 | { |
510 | { |
527 | create_pipe (); |
511 | stash = gv_stashpv ("IO::AIO", 1); |
528 | pthread_atfork (atfork_prepare, atfork_parent, atfork_child); |
|
|
529 | } |
|
|
530 | |
512 | |
|
|
513 | newCONSTSUB (stash, "EXDEV", newSViv (EXDEV)); |
|
|
514 | newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY)); |
|
|
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)); |
|
|
525 | |
|
|
526 | create_respipe (); |
|
|
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 | |
531 | void |
535 | void |
532 | min_parallel(nthreads) |
536 | max_poll_reqs (int nreqs) |
533 | int nthreads |
|
|
534 | PROTOTYPE: $ |
537 | PROTOTYPE: $ |
|
|
538 | CODE: |
|
|
539 | eio_set_max_poll_reqs (nreqs); |
535 | |
540 | |
536 | void |
541 | void |
537 | max_parallel(nthreads) |
542 | max_poll_time (double nseconds) |
538 | int nthreads |
|
|
539 | PROTOTYPE: $ |
543 | PROTOTYPE: $ |
540 | |
|
|
541 | int |
|
|
542 | max_outstanding(nreqs) |
|
|
543 | int nreqs |
|
|
544 | PROTOTYPE: $ |
|
|
545 | CODE: |
544 | CODE: |
546 | 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: |
547 | max_outstanding = nreqs; |
569 | max_outstanding = maxreqs; |
548 | |
570 | |
549 | void |
571 | void |
550 | aio_open(pathname,flags,mode,callback=&PL_sv_undef) |
572 | aio_open (SV8 *pathname, int flags, int mode, SV *callback=&PL_sv_undef) |
551 | SV * pathname |
|
|
552 | int flags |
|
|
553 | int mode |
|
|
554 | SV * callback |
|
|
555 | PROTOTYPE: $$$;$ |
573 | PROTOTYPE: $$$;$ |
556 | CODE: |
574 | PPCODE: |
557 | { |
575 | { |
558 | dREQ; |
576 | dREQ; |
559 | |
577 | |
560 | req->type = REQ_OPEN; |
578 | req->type = EIO_OPEN; |
561 | req->data = newSVsv (pathname); |
579 | req->sv1 = newSVsv (pathname); |
562 | req->dataptr = SvPVbyte_nolen (req->data); |
580 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
563 | req->fd = flags; |
581 | req->int1 = flags; |
564 | req->mode = mode; |
582 | req->int2 = mode; |
565 | |
583 | |
566 | send_req (req); |
584 | REQ_SEND; |
567 | } |
585 | } |
568 | |
586 | |
569 | void |
587 | void |
570 | aio_close(fh,callback=&PL_sv_undef) |
588 | aio_fsync (SV *fh, SV *callback=&PL_sv_undef) |
571 | SV * fh |
|
|
572 | SV * callback |
|
|
573 | PROTOTYPE: $;$ |
589 | PROTOTYPE: $;$ |
574 | ALIAS: |
590 | ALIAS: |
575 | aio_close = REQ_CLOSE |
|
|
576 | aio_fsync = REQ_FSYNC |
591 | aio_fsync = EIO_FSYNC |
577 | aio_fdatasync = REQ_FDATASYNC |
592 | aio_fdatasync = EIO_FDATASYNC |
578 | CODE: |
593 | PPCODE: |
579 | { |
594 | { |
580 | dREQ; |
595 | dREQ; |
581 | |
596 | |
582 | req->type = ix; |
597 | req->type = ix; |
583 | req->fh = newSVsv (fh); |
598 | req->sv1 = newSVsv (fh); |
584 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
599 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh))); |
585 | |
600 | |
586 | send_req (req); |
601 | REQ_SEND (req); |
587 | } |
602 | } |
588 | |
603 | |
589 | 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 |
590 | 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) |
591 | SV * fh |
|
|
592 | UV offset |
|
|
593 | IV length |
|
|
594 | SV * data |
|
|
595 | IV dataoffset |
|
|
596 | SV * callback |
|
|
597 | ALIAS: |
651 | ALIAS: |
598 | aio_read = REQ_READ |
652 | aio_read = EIO_READ |
599 | aio_write = REQ_WRITE |
653 | aio_write = EIO_WRITE |
600 | PROTOTYPE: $$$$$;$ |
654 | PROTOTYPE: $$$$$;$ |
601 | CODE: |
655 | PPCODE: |
602 | { |
656 | { |
603 | aio_req req; |
|
|
604 | STRLEN svlen; |
657 | STRLEN svlen; |
605 | 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)); |
606 | |
664 | |
607 | SvUPGRADE (data, SVt_PV); |
665 | SvUPGRADE (data, SVt_PV); |
608 | SvPOK_on (data); |
666 | SvPOK_only (data); |
609 | |
667 | |
610 | if (dataoffset < 0) |
668 | if (dataoffset < 0) |
611 | dataoffset += svlen; |
669 | dataoffset += svlen; |
612 | |
670 | |
613 | if (dataoffset < 0 || dataoffset > svlen) |
671 | if (dataoffset < 0 || dataoffset > svlen) |
614 | croak ("data offset outside of string"); |
672 | croak ("dataoffset outside of data scalar"); |
615 | |
673 | |
616 | if (ix == REQ_WRITE) |
674 | if (ix == EIO_WRITE) |
617 | { |
675 | { |
618 | /* write: check length and adjust. */ |
676 | /* write: check length and adjust. */ |
619 | if (length < 0 || length + dataoffset > svlen) |
677 | if (!SvOK (length) || len + dataoffset > svlen) |
620 | length = svlen - dataoffset; |
678 | len = svlen - dataoffset; |
621 | } |
679 | } |
622 | else |
680 | else |
623 | { |
681 | { |
624 | /* read: grow scalar as necessary */ |
682 | /* read: grow scalar as necessary */ |
625 | svptr = SvGROW (data, length + dataoffset); |
683 | svptr = SvGROW (data, len + dataoffset + 1); |
626 | } |
684 | } |
627 | |
685 | |
628 | if (length < 0) |
686 | if (len < 0) |
629 | 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"); |
630 | |
694 | |
631 | { |
695 | { |
632 | dREQ; |
696 | dREQ; |
633 | |
697 | |
634 | req->type = ix; |
698 | req->type = ix; |
635 | req->fh = newSVsv (fh); |
699 | req->sv1 = newSVsv (fh); |
636 | req->fd = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh)) |
700 | req->int1 = fd; |
637 | : IoOFP (sv_2io (fh))); |
701 | req->offs = SvOK (offset) ? SvVAL64 (offset) : -1; |
638 | req->offset = offset; |
702 | req->size = len; |
639 | req->length = length; |
|
|
640 | req->data = SvREFCNT_inc (data); |
703 | req->sv2 = SvREFCNT_inc (data); |
641 | req->dataptr = (char *)svptr + dataoffset; |
704 | req->ptr2 = (char *)svptr + dataoffset; |
|
|
705 | req->stroffset = dataoffset; |
642 | |
706 | |
643 | if (!SvREADONLY (data)) |
707 | if (!SvREADONLY (data)) |
644 | { |
708 | { |
645 | SvREADONLY_on (data); |
709 | SvREADONLY_on (data); |
646 | req->data2ptr = (void *)data; |
710 | req->flags |= FLAG_SV2_RO_OFF; |
647 | } |
711 | } |
648 | |
712 | |
649 | send_req (req); |
713 | REQ_SEND; |
650 | } |
714 | } |
651 | } |
715 | } |
652 | |
716 | |
653 | 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 |
|
|
733 | aio_sendfile (SV *out_fh, SV *in_fh, SV *in_offset, UV length, SV *callback=&PL_sv_undef) |
|
|
734 | PROTOTYPE: $$$$;$ |
|
|
735 | PPCODE: |
|
|
736 | { |
|
|
737 | dREQ; |
|
|
738 | |
|
|
739 | req->type = EIO_SENDFILE; |
|
|
740 | req->sv1 = newSVsv (out_fh); |
|
|
741 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (out_fh))); |
|
|
742 | req->sv2 = newSVsv (in_fh); |
|
|
743 | req->int2 = PerlIO_fileno (IoIFP (sv_2io (in_fh))); |
|
|
744 | req->offs = SvVAL64 (in_offset); |
|
|
745 | req->size = length; |
|
|
746 | |
|
|
747 | REQ_SEND; |
|
|
748 | } |
|
|
749 | |
|
|
750 | void |
654 | aio_readahead(fh,offset,length,callback=&PL_sv_undef) |
751 | aio_readahead (SV *fh, SV *offset, IV length, SV *callback=&PL_sv_undef) |
655 | SV * fh |
|
|
656 | UV offset |
|
|
657 | IV length |
|
|
658 | SV * callback |
|
|
659 | PROTOTYPE: $$$;$ |
752 | PROTOTYPE: $$$;$ |
660 | CODE: |
753 | PPCODE: |
661 | { |
754 | { |
662 | dREQ; |
755 | dREQ; |
663 | |
756 | |
664 | req->type = REQ_READAHEAD; |
757 | req->type = EIO_READAHEAD; |
665 | req->fh = newSVsv (fh); |
758 | req->sv1 = newSVsv (fh); |
666 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
759 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh))); |
667 | req->offset = offset; |
760 | req->offs = SvVAL64 (offset); |
668 | req->length = length; |
761 | req->size = length; |
669 | |
762 | |
670 | send_req (req); |
763 | REQ_SEND; |
671 | } |
764 | } |
672 | |
765 | |
673 | void |
766 | void |
674 | aio_stat(fh_or_path,callback=&PL_sv_undef) |
767 | aio_stat (SV8 *fh_or_path, SV *callback=&PL_sv_undef) |
675 | SV * fh_or_path |
|
|
676 | SV * callback |
|
|
677 | ALIAS: |
768 | ALIAS: |
678 | aio_stat = REQ_STAT |
769 | aio_stat = EIO_STAT |
679 | aio_lstat = REQ_LSTAT |
770 | aio_lstat = EIO_LSTAT |
680 | CODE: |
771 | PPCODE: |
681 | { |
772 | { |
682 | dREQ; |
773 | dREQ; |
683 | |
774 | |
684 | New (0, req->statdata, 1, Stat_t); |
775 | req->sv1 = newSVsv (fh_or_path); |
685 | if (!req->statdata) |
|
|
686 | { |
|
|
687 | free_req (req); |
|
|
688 | croak ("out of memory during aio_req->statdata allocation"); |
|
|
689 | } |
|
|
690 | |
776 | |
691 | if (SvPOK (fh_or_path)) |
777 | if (SvPOK (req->sv1)) |
692 | { |
778 | { |
693 | req->type = ix; |
779 | req->type = ix; |
694 | req->data = newSVsv (fh_or_path); |
|
|
695 | req->dataptr = SvPVbyte_nolen (req->data); |
780 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
696 | } |
781 | } |
697 | else |
782 | else |
698 | { |
783 | { |
699 | req->type = REQ_FSTAT; |
784 | req->type = EIO_FSTAT; |
700 | req->fh = newSVsv (fh_or_path); |
|
|
701 | 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)) |
702 | } |
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 | } |
703 | |
811 | |
704 | send_req (req); |
812 | REQ_SEND; |
705 | } |
813 | } |
706 | |
814 | |
707 | 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 |
708 | aio_unlink(pathname,callback=&PL_sv_undef) |
889 | aio_unlink (SV8 *pathname, SV *callback=&PL_sv_undef) |
709 | SV * pathname |
|
|
710 | SV * callback |
|
|
711 | ALIAS: |
890 | ALIAS: |
712 | aio_unlink = REQ_UNLINK |
891 | aio_unlink = EIO_UNLINK |
713 | aio_rmdir = REQ_RMDIR |
892 | aio_rmdir = EIO_RMDIR |
|
|
893 | aio_readdir = EIO_READDIR |
714 | CODE: |
894 | PPCODE: |
715 | { |
895 | { |
716 | dREQ; |
896 | dREQ; |
717 | |
897 | |
718 | req->type = ix; |
898 | req->type = ix; |
719 | req->data = newSVsv (pathname); |
899 | req->sv1 = newSVsv (pathname); |
720 | req->dataptr = SvPVbyte_nolen (req->data); |
900 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
|
|
901 | |
|
|
902 | REQ_SEND; |
|
|
903 | } |
|
|
904 | |
|
|
905 | void |
|
|
906 | aio_link (SV8 *oldpath, SV8 *newpath, SV *callback=&PL_sv_undef) |
|
|
907 | ALIAS: |
|
|
908 | aio_link = EIO_LINK |
|
|
909 | aio_symlink = EIO_SYMLINK |
|
|
910 | aio_rename = EIO_RENAME |
|
|
911 | PPCODE: |
|
|
912 | { |
|
|
913 | dREQ; |
721 | |
914 | |
722 | send_req (req); |
915 | req->type = ix; |
723 | } |
916 | req->sv1 = newSVsv (oldpath); |
724 | |
917 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
725 | void |
918 | req->sv2 = newSVsv (newpath); |
726 | aio_symlink(oldpath,newpath,callback=&PL_sv_undef) |
919 | req->ptr2 = SvPVbyte_nolen (req->sv2); |
727 | SV * oldpath |
|
|
728 | SV * newpath |
|
|
729 | SV * callback |
|
|
730 | CODE: |
|
|
731 | { |
|
|
732 | dREQ; |
|
|
733 | |
920 | |
734 | req->type = REQ_SYMLINK; |
921 | REQ_SEND; |
735 | req->fh = newSVsv (oldpath); |
922 | } |
736 | req->data2ptr = SvPVbyte_nolen (req->fh); |
923 | |
737 | req->data = newSVsv (newpath); |
924 | void |
738 | req->dataptr = SvPVbyte_nolen (req->data); |
925 | aio_mknod (SV8 *pathname, int mode, UV dev, SV *callback=&PL_sv_undef) |
|
|
926 | PPCODE: |
|
|
927 | { |
|
|
928 | dREQ; |
739 | |
929 | |
740 | send_req (req); |
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; |
741 | } |
937 | } |
742 | |
938 | |
743 | void |
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; |
|
|
999 | |
|
|
1000 | void |
744 | flush() |
1001 | flush () |
745 | PROTOTYPE: |
1002 | PROTOTYPE: |
746 | CODE: |
1003 | CODE: |
747 | while (nreqs) |
1004 | while (eio_nreqs ()) |
748 | { |
1005 | { |
749 | poll_wait (); |
1006 | poll_wait (); |
750 | poll_cb (); |
1007 | poll_cb (); |
751 | } |
1008 | } |
752 | |
1009 | |
753 | void |
1010 | int |
754 | poll() |
1011 | poll() |
755 | PROTOTYPE: |
1012 | PROTOTYPE: |
756 | CODE: |
1013 | CODE: |
757 | if (nreqs) |
|
|
758 | { |
|
|
759 | poll_wait (); |
1014 | poll_wait (); |
760 | poll_cb (); |
1015 | RETVAL = poll_cb (); |
761 | } |
1016 | OUTPUT: |
|
|
1017 | RETVAL |
762 | |
1018 | |
763 | int |
1019 | int |
764 | poll_fileno() |
1020 | poll_fileno() |
765 | PROTOTYPE: |
1021 | PROTOTYPE: |
766 | CODE: |
1022 | CODE: |
… | |
… | |
778 | |
1034 | |
779 | void |
1035 | void |
780 | poll_wait() |
1036 | poll_wait() |
781 | PROTOTYPE: |
1037 | PROTOTYPE: |
782 | CODE: |
1038 | CODE: |
783 | if (nreqs) |
|
|
784 | poll_wait (); |
1039 | poll_wait (); |
785 | |
1040 | |
786 | int |
1041 | int |
787 | nreqs() |
1042 | nreqs() |
788 | PROTOTYPE: |
1043 | PROTOTYPE: |
789 | CODE: |
1044 | CODE: |
790 | RETVAL = nreqs; |
1045 | RETVAL = eio_nreqs (); |
791 | OUTPUT: |
1046 | OUTPUT: |
792 | RETVAL |
1047 | RETVAL |
793 | |
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 | |