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