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