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