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