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