1 | #define _REENTRANT 1 |
1 | #include "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 | #include "autoconf/config.h" |
9 | |
10 | |
10 | #include <pthread.h> |
|
|
11 | |
|
|
12 | #include <stddef.h> |
11 | #include <stddef.h> |
|
|
12 | #include <stdlib.h> |
13 | #include <errno.h> |
13 | #include <errno.h> |
14 | #include <sys/time.h> |
14 | #include <sys/time.h> |
15 | #include <sys/select.h> |
15 | #include <sys/select.h> |
16 | #include <sys/types.h> |
16 | #include <sys/types.h> |
17 | #include <sys/stat.h> |
17 | #include <sys/stat.h> |
18 | #include <limits.h> |
18 | #include <limits.h> |
19 | #include <unistd.h> |
19 | #include <unistd.h> |
20 | #include <fcntl.h> |
20 | #include <fcntl.h> |
21 | #include <signal.h> |
21 | #include <signal.h> |
22 | #include <sched.h> |
22 | #include <sched.h> |
|
|
23 | #include <utime.h> |
23 | |
24 | |
24 | #if HAVE_SENDFILE |
25 | #if HAVE_SENDFILE |
25 | # if __linux |
26 | # if __linux |
26 | # include <sys/sendfile.h> |
27 | # include <sys/sendfile.h> |
27 | # elif __freebsd |
28 | # elif __freebsd |
… | |
… | |
34 | # else |
35 | # else |
35 | # error sendfile support requested but not available |
36 | # error sendfile support requested but not available |
36 | # endif |
37 | # endif |
37 | #endif |
38 | #endif |
38 | |
39 | |
|
|
40 | /* number of seconds after which idle threads exit */ |
|
|
41 | #define IDLE_TIMEOUT 10 |
|
|
42 | |
39 | /* used for struct dirent, AIX doesn't provide it */ |
43 | /* used for struct dirent, AIX doesn't provide it */ |
40 | #ifndef NAME_MAX |
44 | #ifndef NAME_MAX |
41 | # define NAME_MAX 4096 |
45 | # define NAME_MAX 4096 |
42 | #endif |
46 | #endif |
43 | |
47 | |
44 | #if __ia64 |
48 | /* buffer size for various temporary buffers */ |
45 | # define STACKSIZE 65536 |
49 | #define AIO_BUFSIZE 65536 |
|
|
50 | |
|
|
51 | /* use NV for 32 bit perls as it allows larger offsets */ |
|
|
52 | #if IVSIZE >= 8 |
|
|
53 | # define SvVAL64 SvIV |
46 | #else |
54 | #else |
47 | # define STACKSIZE 8192 |
55 | # define SvVAL64 SvNV |
48 | #endif |
56 | #endif |
|
|
57 | |
|
|
58 | #define dBUF \ |
|
|
59 | char *aio_buf; \ |
|
|
60 | LOCK (wrklock); \ |
|
|
61 | self->dbuf = aio_buf = malloc (AIO_BUFSIZE); \ |
|
|
62 | UNLOCK (wrklock); \ |
|
|
63 | if (!aio_buf) \ |
|
|
64 | return -1; |
|
|
65 | |
|
|
66 | typedef SV SV8; /* byte-sv, used for argument-checking */ |
49 | |
67 | |
50 | enum { |
68 | enum { |
51 | REQ_QUIT, |
69 | REQ_QUIT, |
52 | REQ_OPEN, REQ_CLOSE, |
70 | REQ_OPEN, REQ_CLOSE, |
53 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
71 | REQ_READ, REQ_WRITE, |
54 | REQ_SENDFILE, |
72 | REQ_READAHEAD, REQ_SENDFILE, |
55 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
73 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
|
|
74 | REQ_UTIME, REQ_FUTIME, |
|
|
75 | REQ_CHMOD, REQ_FCHMOD, |
|
|
76 | REQ_CHOWN, REQ_FCHOWN, |
56 | REQ_FSYNC, REQ_FDATASYNC, |
77 | REQ_FSYNC, REQ_FDATASYNC, |
57 | REQ_UNLINK, REQ_RMDIR, REQ_RENAME, |
78 | REQ_UNLINK, REQ_RMDIR, REQ_MKDIR, REQ_RENAME, |
58 | REQ_READDIR, |
79 | REQ_MKNOD, REQ_READDIR, |
59 | REQ_LINK, REQ_SYMLINK, |
80 | REQ_LINK, REQ_SYMLINK, REQ_READLINK, |
60 | REQ_SLEEP, |
81 | REQ_GROUP, REQ_NOP, |
61 | REQ_GROUP, |
82 | REQ_BUSY, |
62 | }; |
83 | }; |
63 | |
84 | |
64 | #define AIO_REQ_KLASS "IO::AIO::REQ" |
85 | #define AIO_REQ_KLASS "IO::AIO::REQ" |
65 | #define AIO_GRP_KLASS "IO::AIO::GRP" |
86 | #define AIO_GRP_KLASS "IO::AIO::GRP" |
66 | |
87 | |
67 | typedef struct aio_cb |
88 | typedef struct aio_cb |
68 | { |
89 | { |
69 | struct aio_cb *volatile next; |
90 | struct aio_cb *volatile next; |
70 | |
91 | |
|
|
92 | SV *callback; |
|
|
93 | SV *sv1, *sv2; |
|
|
94 | void *ptr1, *ptr2; |
|
|
95 | off_t offs; |
|
|
96 | size_t size; |
|
|
97 | ssize_t result; |
|
|
98 | double nv1, nv2; |
|
|
99 | |
|
|
100 | STRLEN stroffset; |
|
|
101 | int type; |
|
|
102 | int int1, int2, int3; |
|
|
103 | int errorno; |
|
|
104 | mode_t mode; /* open */ |
|
|
105 | |
|
|
106 | unsigned char flags; |
|
|
107 | unsigned char pri; |
|
|
108 | |
|
|
109 | SV *self; /* the perl counterpart of this request, if any */ |
71 | struct aio_cb *grp, *grp_prev, *grp_next, *grp_first; |
110 | 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; |
|
|
76 | SV *fh, *fh2; |
|
|
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; |
|
|
87 | mode_t mode; /* open */ |
|
|
88 | unsigned char cancelled; |
|
|
89 | } aio_cb; |
111 | } aio_cb; |
|
|
112 | |
|
|
113 | enum { |
|
|
114 | FLAG_CANCELLED = 0x01, /* request was cancelled */ |
|
|
115 | FLAG_SV2_RO_OFF = 0x40, /* data was set readonly */ |
|
|
116 | FLAG_PTR2_FREE = 0x80, /* need to free(ptr2) */ |
|
|
117 | }; |
90 | |
118 | |
91 | typedef aio_cb *aio_req; |
119 | typedef aio_cb *aio_req; |
92 | typedef aio_cb *aio_req_ornot; |
120 | typedef aio_cb *aio_req_ornot; |
93 | |
121 | |
|
|
122 | enum { |
|
|
123 | PRI_MIN = -4, |
|
|
124 | PRI_MAX = 4, |
|
|
125 | |
|
|
126 | DEFAULT_PRI = 0, |
|
|
127 | PRI_BIAS = -PRI_MIN, |
|
|
128 | NUM_PRI = PRI_MAX + PRI_BIAS + 1, |
|
|
129 | }; |
|
|
130 | |
|
|
131 | #define AIO_TICKS ((1000000 + 1023) >> 10) |
|
|
132 | |
|
|
133 | static unsigned int max_poll_time = 0; |
|
|
134 | static unsigned int max_poll_reqs = 0; |
|
|
135 | |
|
|
136 | /* calculcate time difference in ~1/AIO_TICKS of a second */ |
|
|
137 | static int tvdiff (struct timeval *tv1, struct timeval *tv2) |
|
|
138 | { |
|
|
139 | return (tv2->tv_sec - tv1->tv_sec ) * AIO_TICKS |
|
|
140 | + ((tv2->tv_usec - tv1->tv_usec) >> 10); |
|
|
141 | } |
|
|
142 | |
|
|
143 | static thread_t main_tid; |
|
|
144 | static int main_sig; |
|
|
145 | static int block_sig_level; |
|
|
146 | |
|
|
147 | void block_sig () |
|
|
148 | { |
|
|
149 | sigset_t ss; |
|
|
150 | |
|
|
151 | if (block_sig_level++) |
|
|
152 | return; |
|
|
153 | |
|
|
154 | if (!main_sig) |
|
|
155 | return; |
|
|
156 | |
|
|
157 | sigemptyset (&ss); |
|
|
158 | sigaddset (&ss, main_sig); |
|
|
159 | pthread_sigmask (SIG_BLOCK, &ss, 0); |
|
|
160 | } |
|
|
161 | |
|
|
162 | void unblock_sig () |
|
|
163 | { |
|
|
164 | sigset_t ss; |
|
|
165 | |
|
|
166 | if (--block_sig_level) |
|
|
167 | return; |
|
|
168 | |
|
|
169 | if (!main_sig) |
|
|
170 | return; |
|
|
171 | |
|
|
172 | sigemptyset (&ss); |
|
|
173 | sigaddset (&ss, main_sig); |
|
|
174 | pthread_sigmask (SIG_UNBLOCK, &ss, 0); |
|
|
175 | } |
|
|
176 | |
|
|
177 | static int next_pri = DEFAULT_PRI + PRI_BIAS; |
|
|
178 | |
94 | static int started, wanted; |
179 | static unsigned int started, idle, wanted; |
95 | static volatile int nreqs; |
180 | |
96 | static int max_outstanding = 1<<30; |
181 | /* worker threads management */ |
|
|
182 | static mutex_t wrklock = MUTEX_INIT; |
|
|
183 | |
|
|
184 | typedef struct worker { |
|
|
185 | /* locked by wrklock */ |
|
|
186 | struct worker *prev, *next; |
|
|
187 | |
|
|
188 | thread_t tid; |
|
|
189 | |
|
|
190 | /* locked by reslock, reqlock or wrklock */ |
|
|
191 | aio_req req; /* currently processed request */ |
|
|
192 | void *dbuf; |
|
|
193 | DIR *dirp; |
|
|
194 | } worker; |
|
|
195 | |
|
|
196 | static worker wrk_first = { &wrk_first, &wrk_first, 0 }; |
|
|
197 | |
|
|
198 | static void worker_clear (worker *wrk) |
|
|
199 | { |
|
|
200 | if (wrk->dirp) |
|
|
201 | { |
|
|
202 | closedir (wrk->dirp); |
|
|
203 | wrk->dirp = 0; |
|
|
204 | } |
|
|
205 | |
|
|
206 | if (wrk->dbuf) |
|
|
207 | { |
|
|
208 | free (wrk->dbuf); |
|
|
209 | wrk->dbuf = 0; |
|
|
210 | } |
|
|
211 | } |
|
|
212 | |
|
|
213 | static void worker_free (worker *wrk) |
|
|
214 | { |
|
|
215 | wrk->next->prev = wrk->prev; |
|
|
216 | wrk->prev->next = wrk->next; |
|
|
217 | |
|
|
218 | free (wrk); |
|
|
219 | } |
|
|
220 | |
|
|
221 | static volatile unsigned int nreqs, nready, npending; |
|
|
222 | static volatile unsigned int max_idle = 4; |
|
|
223 | static volatile unsigned int max_outstanding = 0xffffffff; |
97 | static int respipe [2]; |
224 | static int respipe [2]; |
98 | |
225 | |
99 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
226 | static mutex_t reslock = MUTEX_INIT; |
100 | static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; |
227 | static mutex_t reqlock = MUTEX_INIT; |
101 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
228 | static cond_t reqwait = COND_INIT; |
102 | |
229 | |
103 | static volatile aio_req reqs, reqe; /* queue start, queue end */ |
230 | #if WORDACCESS_UNSAFE |
104 | static volatile aio_req ress, rese; /* queue start, queue end */ |
|
|
105 | |
231 | |
|
|
232 | static unsigned int get_nready () |
|
|
233 | { |
|
|
234 | unsigned int retval; |
|
|
235 | |
|
|
236 | LOCK (reqlock); |
|
|
237 | retval = nready; |
|
|
238 | UNLOCK (reqlock); |
|
|
239 | |
|
|
240 | return retval; |
|
|
241 | } |
|
|
242 | |
|
|
243 | static unsigned int get_npending () |
|
|
244 | { |
|
|
245 | unsigned int retval; |
|
|
246 | |
|
|
247 | LOCK (reslock); |
|
|
248 | retval = npending; |
|
|
249 | UNLOCK (reslock); |
|
|
250 | |
|
|
251 | return retval; |
|
|
252 | } |
|
|
253 | |
|
|
254 | static unsigned int get_nthreads () |
|
|
255 | { |
|
|
256 | unsigned int retval; |
|
|
257 | |
|
|
258 | LOCK (wrklock); |
|
|
259 | retval = started; |
|
|
260 | UNLOCK (wrklock); |
|
|
261 | |
|
|
262 | return retval; |
|
|
263 | } |
|
|
264 | |
|
|
265 | #else |
|
|
266 | |
|
|
267 | # define get_nready() nready |
|
|
268 | # define get_npending() npending |
|
|
269 | # define get_nthreads() started |
|
|
270 | |
|
|
271 | #endif |
|
|
272 | |
|
|
273 | /* |
|
|
274 | * a somewhat faster data structure might be nice, but |
|
|
275 | * with 8 priorities this actually needs <20 insns |
|
|
276 | * per shift, the most expensive operation. |
|
|
277 | */ |
|
|
278 | typedef struct { |
|
|
279 | aio_req qs[NUM_PRI], qe[NUM_PRI]; /* qstart, qend */ |
|
|
280 | int size; |
|
|
281 | } reqq; |
|
|
282 | |
|
|
283 | static reqq req_queue; |
|
|
284 | static reqq res_queue; |
|
|
285 | |
|
|
286 | int reqq_push (reqq *q, aio_req req) |
|
|
287 | { |
|
|
288 | int pri = req->pri; |
|
|
289 | req->next = 0; |
|
|
290 | |
|
|
291 | if (q->qe[pri]) |
|
|
292 | { |
|
|
293 | q->qe[pri]->next = req; |
|
|
294 | q->qe[pri] = req; |
|
|
295 | } |
|
|
296 | else |
|
|
297 | q->qe[pri] = q->qs[pri] = req; |
|
|
298 | |
|
|
299 | return q->size++; |
|
|
300 | } |
|
|
301 | |
|
|
302 | aio_req reqq_shift (reqq *q) |
|
|
303 | { |
|
|
304 | int pri; |
|
|
305 | |
|
|
306 | if (!q->size) |
|
|
307 | return 0; |
|
|
308 | |
|
|
309 | --q->size; |
|
|
310 | |
|
|
311 | for (pri = NUM_PRI; pri--; ) |
|
|
312 | { |
|
|
313 | aio_req req = q->qs[pri]; |
|
|
314 | |
|
|
315 | if (req) |
|
|
316 | { |
|
|
317 | if (!(q->qs[pri] = req->next)) |
|
|
318 | q->qe[pri] = 0; |
|
|
319 | |
|
|
320 | return req; |
|
|
321 | } |
|
|
322 | } |
|
|
323 | |
|
|
324 | abort (); |
|
|
325 | } |
|
|
326 | |
|
|
327 | static int poll_cb (); |
|
|
328 | static int req_invoke (aio_req req); |
|
|
329 | static void req_destroy (aio_req req); |
106 | static void req_free (aio_req req); |
330 | static void req_cancel (aio_req req); |
107 | |
331 | |
108 | /* must be called at most once */ |
332 | /* must be called at most once */ |
109 | static SV *req_sv (aio_req req, const char *klass) |
333 | static SV *req_sv (aio_req req, const char *klass) |
110 | { |
334 | { |
111 | if (!req->self) |
335 | if (!req->self) |
… | |
… | |
117 | return sv_2mortal (sv_bless (newRV_inc (req->self), gv_stashpv (klass, 1))); |
341 | return sv_2mortal (sv_bless (newRV_inc (req->self), gv_stashpv (klass, 1))); |
118 | } |
342 | } |
119 | |
343 | |
120 | static aio_req SvAIO_REQ (SV *sv) |
344 | static aio_req SvAIO_REQ (SV *sv) |
121 | { |
345 | { |
|
|
346 | MAGIC *mg; |
|
|
347 | |
122 | if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv)) |
348 | if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv)) |
123 | croak ("object of class " AIO_REQ_KLASS " expected"); |
349 | croak ("object of class " AIO_REQ_KLASS " expected"); |
124 | |
350 | |
125 | MAGIC *mg = mg_find (SvRV (sv), PERL_MAGIC_ext); |
351 | mg = mg_find (SvRV (sv), PERL_MAGIC_ext); |
126 | |
352 | |
127 | return mg ? (aio_req)mg->mg_ptr : 0; |
353 | return mg ? (aio_req)mg->mg_ptr : 0; |
128 | } |
354 | } |
129 | |
355 | |
130 | static void aio_grp_feed (aio_req grp) |
356 | static void aio_grp_feed (aio_req grp) |
131 | { |
357 | { |
132 | while (grp->length < grp->fd2) |
358 | block_sig (); |
|
|
359 | |
|
|
360 | while (grp->size < grp->int2 && !(grp->flags & FLAG_CANCELLED)) |
133 | { |
361 | { |
134 | int old_len = grp->length; |
362 | int old_len = grp->size; |
135 | |
363 | |
136 | if (grp->fh2 && SvOK (grp->fh2)) |
364 | if (grp->sv2 && SvOK (grp->sv2)) |
137 | { |
365 | { |
138 | dSP; |
366 | dSP; |
139 | |
367 | |
140 | ENTER; |
368 | ENTER; |
141 | SAVETMPS; |
369 | SAVETMPS; |
142 | PUSHMARK (SP); |
370 | PUSHMARK (SP); |
143 | XPUSHs (req_sv (grp, AIO_GRP_KLASS)); |
371 | XPUSHs (req_sv (grp, AIO_GRP_KLASS)); |
144 | PUTBACK; |
372 | PUTBACK; |
145 | call_sv (grp->fh2, G_VOID | G_EVAL); |
373 | call_sv (grp->sv2, G_VOID | G_EVAL | G_KEEPERR); |
146 | SPAGAIN; |
374 | SPAGAIN; |
147 | FREETMPS; |
375 | FREETMPS; |
148 | LEAVE; |
376 | LEAVE; |
149 | } |
377 | } |
150 | |
378 | |
151 | /* stop if no progress has been made */ |
379 | /* stop if no progress has been made */ |
152 | if (old_len == grp->length) |
380 | if (old_len == grp->size) |
153 | { |
381 | { |
154 | SvREFCNT_dec (grp->fh2); |
382 | SvREFCNT_dec (grp->sv2); |
155 | grp->fh2 = 0; |
383 | grp->sv2 = 0; |
156 | break; |
384 | break; |
157 | } |
385 | } |
158 | } |
386 | } |
159 | } |
|
|
160 | |
387 | |
161 | static void poll_wait () |
388 | unblock_sig (); |
|
|
389 | } |
|
|
390 | |
|
|
391 | static void aio_grp_dec (aio_req grp) |
162 | { |
392 | { |
163 | if (nreqs && !ress) |
393 | --grp->size; |
|
|
394 | |
|
|
395 | /* call feeder, if applicable */ |
|
|
396 | aio_grp_feed (grp); |
|
|
397 | |
|
|
398 | /* finish, if done */ |
|
|
399 | if (!grp->size && grp->int1) |
164 | { |
400 | { |
165 | fd_set rfd; |
401 | block_sig (); |
166 | FD_ZERO(&rfd); |
|
|
167 | FD_SET(respipe [0], &rfd); |
|
|
168 | |
402 | |
169 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
403 | if (!req_invoke (grp)) |
|
|
404 | { |
|
|
405 | req_destroy (grp); |
|
|
406 | unblock_sig (); |
|
|
407 | croak (0); |
|
|
408 | } |
|
|
409 | |
|
|
410 | req_destroy (grp); |
|
|
411 | unblock_sig (); |
170 | } |
412 | } |
171 | } |
413 | } |
172 | |
414 | |
173 | static void req_invoke (aio_req req) |
415 | static int req_invoke (aio_req req) |
174 | { |
416 | { |
175 | dSP; |
417 | dSP; |
176 | int errorno = errno; |
|
|
177 | |
418 | |
178 | if (req->cancelled || !SvOK (req->callback)) |
419 | if (req->flags & FLAG_SV2_RO_OFF) |
179 | return; |
420 | SvREADONLY_off (req->sv2); |
180 | |
421 | |
181 | errno = req->errorno; |
422 | if (!(req->flags & FLAG_CANCELLED) && SvOK (req->callback)) |
182 | |
|
|
183 | ENTER; |
|
|
184 | SAVETMPS; |
|
|
185 | PUSHMARK (SP); |
|
|
186 | EXTEND (SP, 1); |
|
|
187 | |
|
|
188 | switch (req->type) |
|
|
189 | { |
423 | { |
190 | case REQ_READDIR: |
424 | ENTER; |
|
|
425 | SAVETMPS; |
|
|
426 | PUSHMARK (SP); |
|
|
427 | EXTEND (SP, 1); |
|
|
428 | |
|
|
429 | switch (req->type) |
191 | { |
430 | { |
192 | SV *rv = &PL_sv_undef; |
431 | case REQ_READDIR: |
193 | |
|
|
194 | if (req->result >= 0) |
|
|
195 | { |
432 | { |
196 | char *buf = req->data2ptr; |
433 | SV *rv = &PL_sv_undef; |
197 | AV *av = newAV (); |
|
|
198 | |
434 | |
199 | while (req->result) |
435 | if (req->result >= 0) |
200 | { |
436 | { |
|
|
437 | int i; |
|
|
438 | char *buf = req->ptr2; |
|
|
439 | AV *av = newAV (); |
|
|
440 | |
|
|
441 | av_extend (av, req->result - 1); |
|
|
442 | |
|
|
443 | for (i = 0; i < req->result; ++i) |
|
|
444 | { |
201 | SV *sv = newSVpv (buf, 0); |
445 | SV *sv = newSVpv (buf, 0); |
202 | |
446 | |
203 | av_push (av, sv); |
447 | av_store (av, i, sv); |
204 | buf += SvCUR (sv) + 1; |
448 | buf += SvCUR (sv) + 1; |
205 | req->result--; |
449 | } |
|
|
450 | |
|
|
451 | rv = sv_2mortal (newRV_noinc ((SV *)av)); |
206 | } |
452 | } |
207 | |
453 | |
208 | rv = sv_2mortal (newRV_noinc ((SV *)av)); |
454 | PUSHs (rv); |
209 | } |
455 | } |
|
|
456 | break; |
210 | |
457 | |
|
|
458 | case REQ_OPEN: |
|
|
459 | { |
|
|
460 | /* convert fd to fh */ |
|
|
461 | SV *fh; |
|
|
462 | |
|
|
463 | PUSHs (sv_2mortal (newSViv (req->result))); |
|
|
464 | PUTBACK; |
|
|
465 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
|
|
466 | SPAGAIN; |
|
|
467 | |
|
|
468 | fh = POPs; |
|
|
469 | PUSHMARK (SP); |
211 | PUSHs (rv); |
470 | XPUSHs (fh); |
|
|
471 | } |
|
|
472 | break; |
|
|
473 | |
|
|
474 | case REQ_GROUP: |
|
|
475 | req->int1 = 2; /* mark group as finished */ |
|
|
476 | |
|
|
477 | if (req->sv1) |
|
|
478 | { |
|
|
479 | int i; |
|
|
480 | AV *av = (AV *)req->sv1; |
|
|
481 | |
|
|
482 | EXTEND (SP, AvFILL (av) + 1); |
|
|
483 | for (i = 0; i <= AvFILL (av); ++i) |
|
|
484 | PUSHs (*av_fetch (av, i, 0)); |
|
|
485 | } |
|
|
486 | break; |
|
|
487 | |
|
|
488 | case REQ_NOP: |
|
|
489 | case REQ_BUSY: |
|
|
490 | break; |
|
|
491 | |
|
|
492 | case REQ_READLINK: |
|
|
493 | if (req->result > 0) |
|
|
494 | { |
|
|
495 | SvCUR_set (req->sv2, req->result); |
|
|
496 | *SvEND (req->sv2) = 0; |
|
|
497 | PUSHs (req->sv2); |
|
|
498 | } |
|
|
499 | break; |
|
|
500 | |
|
|
501 | case REQ_STAT: |
|
|
502 | case REQ_LSTAT: |
|
|
503 | case REQ_FSTAT: |
|
|
504 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
|
|
505 | PL_laststatval = req->result; |
|
|
506 | PL_statcache = *(Stat_t *)(req->ptr2); |
|
|
507 | PUSHs (sv_2mortal (newSViv (req->result))); |
|
|
508 | break; |
|
|
509 | |
|
|
510 | case REQ_READ: |
|
|
511 | SvCUR_set (req->sv2, req->stroffset + (req->result > 0 ? req->result : 0)); |
|
|
512 | *SvEND (req->sv2) = 0; |
|
|
513 | PUSHs (sv_2mortal (newSViv (req->result))); |
|
|
514 | break; |
|
|
515 | |
|
|
516 | default: |
|
|
517 | PUSHs (sv_2mortal (newSViv (req->result))); |
|
|
518 | break; |
212 | } |
519 | } |
213 | break; |
|
|
214 | |
520 | |
215 | case REQ_OPEN: |
521 | errno = req->errorno; |
216 | { |
|
|
217 | /* convert fd to fh */ |
|
|
218 | SV *fh; |
|
|
219 | |
522 | |
220 | PUSHs (sv_2mortal (newSViv (req->result))); |
|
|
221 | PUTBACK; |
523 | PUTBACK; |
222 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
524 | call_sv (req->callback, G_VOID | G_EVAL); |
223 | SPAGAIN; |
525 | SPAGAIN; |
224 | |
526 | |
225 | fh = SvREFCNT_inc (POPs); |
527 | FREETMPS; |
226 | |
528 | LEAVE; |
227 | PUSHMARK (SP); |
|
|
228 | XPUSHs (sv_2mortal (fh)); |
|
|
229 | } |
|
|
230 | break; |
|
|
231 | |
|
|
232 | case REQ_GROUP: |
|
|
233 | req->fd = 2; /* mark group as finished */ |
|
|
234 | |
|
|
235 | if (req->data) |
|
|
236 | { |
|
|
237 | int i; |
|
|
238 | AV *av = (AV *)req->data; |
|
|
239 | |
|
|
240 | EXTEND (SP, AvFILL (av) + 1); |
|
|
241 | for (i = 0; i <= AvFILL (av); ++i) |
|
|
242 | PUSHs (*av_fetch (av, i, 0)); |
|
|
243 | } |
|
|
244 | break; |
|
|
245 | |
|
|
246 | case REQ_SLEEP: |
|
|
247 | break; |
|
|
248 | |
|
|
249 | default: |
|
|
250 | PUSHs (sv_2mortal (newSViv (req->result))); |
|
|
251 | break; |
|
|
252 | } |
529 | } |
253 | |
530 | |
254 | |
|
|
255 | PUTBACK; |
|
|
256 | call_sv (req->callback, G_VOID | G_EVAL); |
|
|
257 | SPAGAIN; |
|
|
258 | |
|
|
259 | if (SvTRUE (ERRSV)) |
|
|
260 | { |
|
|
261 | req_free (req); |
|
|
262 | croak (0); |
|
|
263 | } |
|
|
264 | |
|
|
265 | FREETMPS; |
|
|
266 | LEAVE; |
|
|
267 | |
|
|
268 | errno = errorno; |
|
|
269 | } |
|
|
270 | |
|
|
271 | static void req_free (aio_req req) |
|
|
272 | { |
|
|
273 | if (req->grp) |
531 | if (req->grp) |
274 | { |
532 | { |
275 | aio_req grp = req->grp; |
533 | aio_req grp = req->grp; |
276 | |
534 | |
277 | /* unlink request */ |
535 | /* unlink request */ |
… | |
… | |
279 | if (req->grp_prev) req->grp_prev->grp_next = req->grp_next; |
537 | if (req->grp_prev) req->grp_prev->grp_next = req->grp_next; |
280 | |
538 | |
281 | if (grp->grp_first == req) |
539 | if (grp->grp_first == req) |
282 | grp->grp_first = req->grp_next; |
540 | grp->grp_first = req->grp_next; |
283 | |
541 | |
284 | --grp->length; |
|
|
285 | |
|
|
286 | /* call feeder, if applicable */ |
|
|
287 | aio_grp_feed (grp); |
542 | aio_grp_dec (grp); |
288 | |
|
|
289 | /* finish, if done */ |
|
|
290 | if (!grp->length && grp->fd) |
|
|
291 | { |
|
|
292 | req_invoke (grp); |
|
|
293 | req_free (grp); |
|
|
294 | } |
|
|
295 | } |
543 | } |
296 | |
544 | |
|
|
545 | return !SvTRUE (ERRSV); |
|
|
546 | } |
|
|
547 | |
|
|
548 | static void req_destroy (aio_req req) |
|
|
549 | { |
297 | if (req->self) |
550 | if (req->self) |
298 | { |
551 | { |
299 | sv_unmagic (req->self, PERL_MAGIC_ext); |
552 | sv_unmagic (req->self, PERL_MAGIC_ext); |
300 | SvREFCNT_dec (req->self); |
553 | SvREFCNT_dec (req->self); |
301 | } |
554 | } |
302 | |
555 | |
303 | SvREFCNT_dec (req->data); |
|
|
304 | SvREFCNT_dec (req->fh); |
556 | SvREFCNT_dec (req->sv1); |
305 | SvREFCNT_dec (req->fh2); |
557 | SvREFCNT_dec (req->sv2); |
306 | SvREFCNT_dec (req->callback); |
558 | SvREFCNT_dec (req->callback); |
307 | Safefree (req->statdata); |
|
|
308 | |
559 | |
309 | if (req->type == REQ_READDIR && req->result >= 0) |
560 | if (req->flags & FLAG_PTR2_FREE) |
310 | free (req->data2ptr); |
561 | free (req->ptr2); |
311 | |
562 | |
312 | Safefree (req); |
563 | Safefree (req); |
313 | } |
564 | } |
314 | |
565 | |
|
|
566 | static void req_cancel_subs (aio_req grp) |
|
|
567 | { |
|
|
568 | aio_req sub; |
|
|
569 | |
|
|
570 | if (grp->type != REQ_GROUP) |
|
|
571 | return; |
|
|
572 | |
|
|
573 | SvREFCNT_dec (grp->sv2); |
|
|
574 | grp->sv2 = 0; |
|
|
575 | |
|
|
576 | for (sub = grp->grp_first; sub; sub = sub->grp_next) |
|
|
577 | req_cancel (sub); |
|
|
578 | } |
|
|
579 | |
315 | static void req_cancel (aio_req req) |
580 | static void req_cancel (aio_req req) |
316 | { |
581 | { |
317 | req->cancelled = 1; |
582 | req->flags |= FLAG_CANCELLED; |
318 | |
583 | |
319 | if (req->type == REQ_GROUP) |
584 | req_cancel_subs (req); |
|
|
585 | } |
|
|
586 | |
|
|
587 | static void *aio_proc (void *arg); |
|
|
588 | |
|
|
589 | static void start_thread (void) |
|
|
590 | { |
|
|
591 | worker *wrk = calloc (1, sizeof (worker)); |
|
|
592 | |
|
|
593 | if (!wrk) |
|
|
594 | croak ("unable to allocate worker thread data"); |
|
|
595 | |
|
|
596 | LOCK (wrklock); |
|
|
597 | |
|
|
598 | if (thread_create (&wrk->tid, aio_proc, (void *)wrk)) |
320 | { |
599 | { |
321 | aio_req sub; |
600 | wrk->prev = &wrk_first; |
|
|
601 | wrk->next = wrk_first.next; |
|
|
602 | wrk_first.next->prev = wrk; |
|
|
603 | wrk_first.next = wrk; |
|
|
604 | ++started; |
|
|
605 | } |
|
|
606 | else |
|
|
607 | free (wrk); |
322 | |
608 | |
323 | for (sub = req->grp_first; sub; sub = sub->grp_next) |
609 | UNLOCK (wrklock); |
324 | req_cancel (sub); |
610 | } |
|
|
611 | |
|
|
612 | static void maybe_start_thread () |
|
|
613 | { |
|
|
614 | if (get_nthreads () >= wanted) |
|
|
615 | return; |
|
|
616 | |
|
|
617 | /* todo: maybe use idle here, but might be less exact */ |
|
|
618 | if (0 <= (int)get_nthreads () + (int)get_npending () - (int)nreqs) |
|
|
619 | return; |
|
|
620 | |
|
|
621 | start_thread (); |
|
|
622 | } |
|
|
623 | |
|
|
624 | static void req_send (aio_req req) |
|
|
625 | { |
|
|
626 | block_sig (); |
|
|
627 | |
|
|
628 | ++nreqs; |
|
|
629 | |
|
|
630 | LOCK (reqlock); |
|
|
631 | ++nready; |
|
|
632 | reqq_push (&req_queue, req); |
|
|
633 | COND_SIGNAL (reqwait); |
|
|
634 | UNLOCK (reqlock); |
|
|
635 | |
|
|
636 | unblock_sig (); |
|
|
637 | |
|
|
638 | maybe_start_thread (); |
|
|
639 | } |
|
|
640 | |
|
|
641 | static void end_thread (void) |
|
|
642 | { |
|
|
643 | aio_req req; |
|
|
644 | |
|
|
645 | Newz (0, req, 1, aio_cb); |
|
|
646 | |
|
|
647 | req->type = REQ_QUIT; |
|
|
648 | req->pri = PRI_MAX + PRI_BIAS; |
|
|
649 | |
|
|
650 | LOCK (reqlock); |
|
|
651 | reqq_push (&req_queue, req); |
|
|
652 | COND_SIGNAL (reqwait); |
|
|
653 | UNLOCK (reqlock); |
|
|
654 | |
|
|
655 | LOCK (wrklock); |
|
|
656 | --started; |
|
|
657 | UNLOCK (wrklock); |
|
|
658 | } |
|
|
659 | |
|
|
660 | static void set_max_idle (int nthreads) |
|
|
661 | { |
|
|
662 | if (WORDACCESS_UNSAFE) LOCK (reqlock); |
|
|
663 | max_idle = nthreads <= 0 ? 1 : nthreads; |
|
|
664 | if (WORDACCESS_UNSAFE) UNLOCK (reqlock); |
|
|
665 | } |
|
|
666 | |
|
|
667 | static void min_parallel (int nthreads) |
|
|
668 | { |
|
|
669 | if (wanted < nthreads) |
|
|
670 | wanted = nthreads; |
|
|
671 | } |
|
|
672 | |
|
|
673 | static void max_parallel (int nthreads) |
|
|
674 | { |
|
|
675 | if (wanted > nthreads) |
|
|
676 | wanted = nthreads; |
|
|
677 | |
|
|
678 | while (started > wanted) |
|
|
679 | end_thread (); |
|
|
680 | } |
|
|
681 | |
|
|
682 | static void poll_wait () |
|
|
683 | { |
|
|
684 | fd_set rfd; |
|
|
685 | |
|
|
686 | while (nreqs) |
|
|
687 | { |
|
|
688 | int size; |
|
|
689 | if (WORDACCESS_UNSAFE) LOCK (reslock); |
|
|
690 | size = res_queue.size; |
|
|
691 | if (WORDACCESS_UNSAFE) UNLOCK (reslock); |
|
|
692 | |
|
|
693 | if (size) |
|
|
694 | return; |
|
|
695 | |
|
|
696 | maybe_start_thread (); |
|
|
697 | |
|
|
698 | FD_ZERO(&rfd); |
|
|
699 | FD_SET(respipe [0], &rfd); |
|
|
700 | |
|
|
701 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
325 | } |
702 | } |
326 | } |
703 | } |
327 | |
704 | |
328 | static int poll_cb () |
705 | static int poll_cb () |
329 | { |
706 | { |
330 | dSP; |
707 | dSP; |
331 | int count = 0; |
708 | int count = 0; |
|
|
709 | int maxreqs = max_poll_reqs; |
332 | int do_croak = 0; |
710 | int do_croak = 0; |
|
|
711 | struct timeval tv_start, tv_now; |
333 | aio_req req; |
712 | aio_req req; |
|
|
713 | |
|
|
714 | if (max_poll_time) |
|
|
715 | gettimeofday (&tv_start, 0); |
|
|
716 | |
|
|
717 | block_sig (); |
334 | |
718 | |
335 | for (;;) |
719 | for (;;) |
336 | { |
720 | { |
337 | pthread_mutex_lock (&reslock); |
721 | for (;;) |
338 | req = ress; |
|
|
339 | |
|
|
340 | if (req) |
|
|
341 | { |
722 | { |
342 | ress = req->next; |
723 | maybe_start_thread (); |
343 | |
724 | |
|
|
725 | LOCK (reslock); |
|
|
726 | req = reqq_shift (&res_queue); |
|
|
727 | |
344 | if (!ress) |
728 | if (req) |
345 | { |
729 | { |
|
|
730 | --npending; |
|
|
731 | |
|
|
732 | if (!res_queue.size) |
|
|
733 | { |
346 | /* read any signals sent by the worker threads */ |
734 | /* read any signals sent by the worker threads */ |
347 | char buf [32]; |
735 | char buf [4]; |
348 | while (read (respipe [0], buf, 32) == 32) |
736 | while (read (respipe [0], buf, 4) == 4) |
|
|
737 | ; |
349 | ; |
738 | } |
|
|
739 | } |
350 | |
740 | |
|
|
741 | UNLOCK (reslock); |
|
|
742 | |
|
|
743 | if (!req) |
|
|
744 | break; |
|
|
745 | |
|
|
746 | --nreqs; |
|
|
747 | |
|
|
748 | if (req->type == REQ_GROUP && req->size) |
|
|
749 | { |
|
|
750 | req->int1 = 1; /* mark request as delayed */ |
|
|
751 | continue; |
|
|
752 | } |
|
|
753 | else |
|
|
754 | { |
|
|
755 | if (!req_invoke (req)) |
|
|
756 | { |
|
|
757 | req_destroy (req); |
|
|
758 | unblock_sig (); |
|
|
759 | croak (0); |
|
|
760 | } |
|
|
761 | |
|
|
762 | count++; |
|
|
763 | } |
|
|
764 | |
|
|
765 | req_destroy (req); |
|
|
766 | |
|
|
767 | if (maxreqs && !--maxreqs) |
|
|
768 | break; |
|
|
769 | |
|
|
770 | if (max_poll_time) |
|
|
771 | { |
|
|
772 | gettimeofday (&tv_now, 0); |
|
|
773 | |
|
|
774 | if (tvdiff (&tv_start, &tv_now) >= max_poll_time) |
351 | rese = 0; |
775 | break; |
352 | } |
776 | } |
353 | } |
777 | } |
354 | |
778 | |
355 | pthread_mutex_unlock (&reslock); |
779 | if (nreqs <= max_outstanding) |
356 | |
|
|
357 | if (!req) |
|
|
358 | break; |
780 | break; |
359 | |
781 | |
360 | nreqs--; |
782 | poll_wait (); |
361 | |
783 | |
362 | if (req->type == REQ_QUIT) |
784 | ++maxreqs; |
363 | started--; |
|
|
364 | else if (req->type == REQ_GROUP && req->length) |
|
|
365 | { |
|
|
366 | req->fd = 1; /* mark request as delayed */ |
|
|
367 | continue; |
|
|
368 | } |
|
|
369 | else |
|
|
370 | { |
|
|
371 | if (req->type == REQ_READ) |
|
|
372 | SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); |
|
|
373 | |
|
|
374 | if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE)) |
|
|
375 | SvREADONLY_off (req->data); |
|
|
376 | |
|
|
377 | if (req->statdata) |
|
|
378 | { |
|
|
379 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
|
|
380 | PL_laststatval = req->result; |
|
|
381 | PL_statcache = *(req->statdata); |
|
|
382 | } |
|
|
383 | |
|
|
384 | req_invoke (req); |
|
|
385 | |
|
|
386 | count++; |
|
|
387 | } |
|
|
388 | |
|
|
389 | req_free (req); |
|
|
390 | } |
785 | } |
391 | |
786 | |
|
|
787 | unblock_sig (); |
392 | return count; |
788 | return count; |
393 | } |
|
|
394 | |
|
|
395 | static void *aio_proc(void *arg); |
|
|
396 | |
|
|
397 | static void start_thread (void) |
|
|
398 | { |
|
|
399 | sigset_t fullsigset, oldsigset; |
|
|
400 | pthread_t tid; |
|
|
401 | pthread_attr_t attr; |
|
|
402 | |
|
|
403 | pthread_attr_init (&attr); |
|
|
404 | pthread_attr_setstacksize (&attr, STACKSIZE); |
|
|
405 | pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); |
|
|
406 | |
|
|
407 | sigfillset (&fullsigset); |
|
|
408 | sigprocmask (SIG_SETMASK, &fullsigset, &oldsigset); |
|
|
409 | |
|
|
410 | if (pthread_create (&tid, &attr, aio_proc, 0) == 0) |
|
|
411 | started++; |
|
|
412 | |
|
|
413 | sigprocmask (SIG_SETMASK, &oldsigset, 0); |
|
|
414 | } |
|
|
415 | |
|
|
416 | static void req_send (aio_req req) |
|
|
417 | { |
|
|
418 | while (started < wanted && nreqs >= started) |
|
|
419 | start_thread (); |
|
|
420 | |
|
|
421 | nreqs++; |
|
|
422 | |
|
|
423 | pthread_mutex_lock (&reqlock); |
|
|
424 | |
|
|
425 | req->next = 0; |
|
|
426 | |
|
|
427 | if (reqe) |
|
|
428 | { |
|
|
429 | reqe->next = req; |
|
|
430 | reqe = req; |
|
|
431 | } |
|
|
432 | else |
|
|
433 | reqe = reqs = req; |
|
|
434 | |
|
|
435 | pthread_cond_signal (&reqwait); |
|
|
436 | pthread_mutex_unlock (&reqlock); |
|
|
437 | |
|
|
438 | if (nreqs > max_outstanding) |
|
|
439 | for (;;) |
|
|
440 | { |
|
|
441 | poll_cb (); |
|
|
442 | |
|
|
443 | if (nreqs <= max_outstanding) |
|
|
444 | break; |
|
|
445 | |
|
|
446 | poll_wait (); |
|
|
447 | } |
|
|
448 | } |
|
|
449 | |
|
|
450 | static void end_thread (void) |
|
|
451 | { |
|
|
452 | aio_req req; |
|
|
453 | Newz (0, req, 1, aio_cb); |
|
|
454 | req->type = REQ_QUIT; |
|
|
455 | |
|
|
456 | req_send (req); |
|
|
457 | } |
|
|
458 | |
|
|
459 | static void min_parallel (int nthreads) |
|
|
460 | { |
|
|
461 | if (wanted < nthreads) |
|
|
462 | wanted = nthreads; |
|
|
463 | } |
|
|
464 | |
|
|
465 | static void max_parallel (int nthreads) |
|
|
466 | { |
|
|
467 | int cur = started; |
|
|
468 | |
|
|
469 | if (wanted > nthreads) |
|
|
470 | wanted = nthreads; |
|
|
471 | |
|
|
472 | while (cur > wanted) |
|
|
473 | { |
|
|
474 | end_thread (); |
|
|
475 | cur--; |
|
|
476 | } |
|
|
477 | |
|
|
478 | while (started > wanted) |
|
|
479 | { |
|
|
480 | poll_wait (); |
|
|
481 | poll_cb (); |
|
|
482 | } |
|
|
483 | } |
789 | } |
484 | |
790 | |
485 | static void create_pipe () |
791 | static void create_pipe () |
486 | { |
792 | { |
487 | if (pipe (respipe)) |
793 | if (pipe (respipe)) |
… | |
… | |
504 | /* |
810 | /* |
505 | * make our pread/pwrite safe against themselves, but not against |
811 | * make our pread/pwrite safe against themselves, but not against |
506 | * normal read/write by using a mutex. slows down execution a lot, |
812 | * normal read/write by using a mutex. slows down execution a lot, |
507 | * but that's your problem, not mine. |
813 | * but that's your problem, not mine. |
508 | */ |
814 | */ |
509 | static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER; |
815 | static mutex_t preadwritelock = MUTEX_INIT; |
510 | |
816 | |
511 | static ssize_t pread (int fd, void *buf, size_t count, off_t offset) |
817 | static ssize_t pread (int fd, void *buf, size_t count, off_t offset) |
512 | { |
818 | { |
513 | ssize_t res; |
819 | ssize_t res; |
514 | off_t ooffset; |
820 | off_t ooffset; |
515 | |
821 | |
516 | pthread_mutex_lock (&preadwritelock); |
822 | LOCK (preadwritelock); |
517 | ooffset = lseek (fd, 0, SEEK_CUR); |
823 | ooffset = lseek (fd, 0, SEEK_CUR); |
518 | lseek (fd, offset, SEEK_SET); |
824 | lseek (fd, offset, SEEK_SET); |
519 | res = read (fd, buf, count); |
825 | res = read (fd, buf, count); |
520 | lseek (fd, ooffset, SEEK_SET); |
826 | lseek (fd, ooffset, SEEK_SET); |
521 | pthread_mutex_unlock (&preadwritelock); |
827 | UNLOCK (preadwritelock); |
522 | |
828 | |
523 | return res; |
829 | return res; |
524 | } |
830 | } |
525 | |
831 | |
526 | static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset) |
832 | static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset) |
527 | { |
833 | { |
528 | ssize_t res; |
834 | ssize_t res; |
529 | off_t ooffset; |
835 | off_t ooffset; |
530 | |
836 | |
531 | pthread_mutex_lock (&preadwritelock); |
837 | LOCK (preadwritelock); |
532 | ooffset = lseek (fd, 0, SEEK_CUR); |
838 | ooffset = lseek (fd, 0, SEEK_CUR); |
533 | lseek (fd, offset, SEEK_SET); |
839 | lseek (fd, offset, SEEK_SET); |
534 | res = write (fd, buf, count); |
840 | res = write (fd, buf, count); |
535 | lseek (fd, offset, SEEK_SET); |
841 | lseek (fd, offset, SEEK_SET); |
536 | pthread_mutex_unlock (&preadwritelock); |
842 | UNLOCK (preadwritelock); |
537 | |
843 | |
538 | return res; |
844 | return res; |
539 | } |
845 | } |
|
|
846 | #endif |
|
|
847 | |
|
|
848 | #ifndef HAVE_FUTIMES |
|
|
849 | |
|
|
850 | # define utimes(path,times) aio_utimes (path, times) |
|
|
851 | # define futimes(fd,times) aio_futimes (fd, times) |
|
|
852 | |
|
|
853 | int aio_utimes (const char *filename, const struct timeval times[2]) |
|
|
854 | { |
|
|
855 | if (times) |
|
|
856 | { |
|
|
857 | struct utimbuf buf; |
|
|
858 | |
|
|
859 | buf.actime = times[0].tv_sec; |
|
|
860 | buf.modtime = times[1].tv_sec; |
|
|
861 | |
|
|
862 | return utime (filename, &buf); |
|
|
863 | } |
|
|
864 | else |
|
|
865 | return utime (filename, 0); |
|
|
866 | } |
|
|
867 | |
|
|
868 | int aio_futimes (int fd, const struct timeval tv[2]) |
|
|
869 | { |
|
|
870 | errno = ENOSYS; |
|
|
871 | return -1; |
|
|
872 | } |
|
|
873 | |
540 | #endif |
874 | #endif |
541 | |
875 | |
542 | #if !HAVE_FDATASYNC |
876 | #if !HAVE_FDATASYNC |
543 | # define fdatasync fsync |
877 | # define fdatasync fsync |
544 | #endif |
878 | #endif |
545 | |
879 | |
546 | #if !HAVE_READAHEAD |
880 | #if !HAVE_READAHEAD |
547 | # define readahead aio_readahead |
881 | # define readahead(fd,offset,count) aio_readahead (fd, offset, count, self) |
548 | |
882 | |
549 | static ssize_t readahead (int fd, off_t offset, size_t count) |
883 | static ssize_t aio_readahead (int fd, off_t offset, size_t count, worker *self) |
550 | { |
884 | { |
551 | char readahead_buf[4096]; |
885 | dBUF; |
552 | |
886 | |
553 | while (count > 0) |
887 | while (count > 0) |
554 | { |
888 | { |
555 | size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); |
889 | size_t len = count < AIO_BUFSIZE ? count : AIO_BUFSIZE; |
556 | |
890 | |
557 | pread (fd, readahead_buf, len, offset); |
891 | pread (fd, aio_buf, len, offset); |
558 | offset += len; |
892 | offset += len; |
559 | count -= len; |
893 | count -= len; |
560 | } |
894 | } |
561 | |
895 | |
562 | errno = 0; |
896 | errno = 0; |
563 | } |
897 | } |
|
|
898 | |
564 | #endif |
899 | #endif |
565 | |
900 | |
566 | #if !HAVE_READDIR_R |
901 | #if !HAVE_READDIR_R |
567 | # define readdir_r aio_readdir_r |
902 | # define readdir_r aio_readdir_r |
568 | |
903 | |
569 | static pthread_mutex_t readdirlock = PTHREAD_MUTEX_INITIALIZER; |
904 | static mutex_t readdirlock = MUTEX_INIT; |
570 | |
905 | |
571 | static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) |
906 | static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) |
572 | { |
907 | { |
573 | struct dirent *e; |
908 | struct dirent *e; |
574 | int errorno; |
909 | int errorno; |
575 | |
910 | |
576 | pthread_mutex_lock (&readdirlock); |
911 | LOCK (readdirlock); |
577 | |
912 | |
578 | e = readdir (dirp); |
913 | e = readdir (dirp); |
579 | errorno = errno; |
914 | errorno = errno; |
580 | |
915 | |
581 | if (e) |
916 | if (e) |
… | |
… | |
584 | strcpy (ent->d_name, e->d_name); |
919 | strcpy (ent->d_name, e->d_name); |
585 | } |
920 | } |
586 | else |
921 | else |
587 | *res = 0; |
922 | *res = 0; |
588 | |
923 | |
589 | pthread_mutex_unlock (&readdirlock); |
924 | UNLOCK (readdirlock); |
590 | |
925 | |
591 | errno = errorno; |
926 | errno = errorno; |
592 | return e ? 0 : -1; |
927 | return e ? 0 : -1; |
593 | } |
928 | } |
594 | #endif |
929 | #endif |
595 | |
930 | |
596 | /* sendfile always needs emulation */ |
931 | /* sendfile always needs emulation */ |
597 | static ssize_t sendfile_ (int ofd, int ifd, off_t offset, size_t count) |
932 | static ssize_t sendfile_ (int ofd, int ifd, off_t offset, size_t count, worker *self) |
598 | { |
933 | { |
599 | ssize_t res; |
934 | ssize_t res; |
600 | |
935 | |
601 | if (!count) |
936 | if (!count) |
602 | return 0; |
937 | return 0; |
… | |
… | |
613 | { |
948 | { |
614 | off_t sbytes; |
949 | off_t sbytes; |
615 | res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); |
950 | res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); |
616 | |
951 | |
617 | if (res < 0 && sbytes) |
952 | if (res < 0 && sbytes) |
618 | /* maybe only on EAGAIN only: as usual, the manpage leaves you guessing */ |
953 | /* maybe only on EAGAIN: as usual, the manpage leaves you guessing */ |
619 | res = sbytes; |
954 | res = sbytes; |
620 | } |
955 | } |
621 | |
956 | |
622 | # elif __hpux |
957 | # elif __hpux |
623 | res = sendfile (ofd, ifd, offset, count, 0, 0); |
958 | res = sendfile (ofd, ifd, offset, count, 0, 0); |
… | |
… | |
651 | #endif |
986 | #endif |
652 | ) |
987 | ) |
653 | ) |
988 | ) |
654 | { |
989 | { |
655 | /* emulate sendfile. this is a major pain in the ass */ |
990 | /* emulate sendfile. this is a major pain in the ass */ |
656 | char buf[4096]; |
991 | dBUF; |
|
|
992 | |
657 | res = 0; |
993 | res = 0; |
658 | |
994 | |
659 | while (count) |
995 | while (count) |
660 | { |
996 | { |
661 | ssize_t cnt; |
997 | ssize_t cnt; |
662 | |
998 | |
663 | cnt = pread (ifd, buf, count > 4096 ? 4096 : count, offset); |
999 | cnt = pread (ifd, aio_buf, count > AIO_BUFSIZE ? AIO_BUFSIZE : count, offset); |
664 | |
1000 | |
665 | if (cnt <= 0) |
1001 | if (cnt <= 0) |
666 | { |
1002 | { |
667 | if (cnt && !res) res = -1; |
1003 | if (cnt && !res) res = -1; |
668 | break; |
1004 | break; |
669 | } |
1005 | } |
670 | |
1006 | |
671 | cnt = write (ofd, buf, cnt); |
1007 | cnt = write (ofd, aio_buf, cnt); |
672 | |
1008 | |
673 | if (cnt <= 0) |
1009 | if (cnt <= 0) |
674 | { |
1010 | { |
675 | if (cnt && !res) res = -1; |
1011 | if (cnt && !res) res = -1; |
676 | break; |
1012 | break; |
… | |
… | |
684 | |
1020 | |
685 | return res; |
1021 | return res; |
686 | } |
1022 | } |
687 | |
1023 | |
688 | /* read a full directory */ |
1024 | /* read a full directory */ |
689 | static int scandir_ (const char *path, void **namesp) |
1025 | static void scandir_ (aio_req req, worker *self) |
690 | { |
1026 | { |
691 | DIR *dirp = opendir (path); |
1027 | DIR *dirp; |
692 | union |
1028 | union |
693 | { |
1029 | { |
694 | struct dirent d; |
1030 | struct dirent d; |
695 | char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; |
1031 | char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; |
696 | } u; |
1032 | } *u; |
697 | struct dirent *entp; |
1033 | struct dirent *entp; |
698 | char *name, *names; |
1034 | char *name, *names; |
699 | int memlen = 4096; |
1035 | int memlen = 4096; |
700 | int memofs = 0; |
1036 | int memofs = 0; |
701 | int res = 0; |
1037 | int res = 0; |
702 | int errorno; |
1038 | int errorno; |
703 | |
1039 | |
704 | if (!dirp) |
1040 | LOCK (wrklock); |
705 | return -1; |
1041 | self->dirp = dirp = opendir (req->ptr1); |
706 | |
1042 | self->dbuf = u = malloc (sizeof (*u)); |
|
|
1043 | req->flags |= FLAG_PTR2_FREE; |
707 | names = malloc (memlen); |
1044 | req->ptr2 = names = malloc (memlen); |
|
|
1045 | UNLOCK (wrklock); |
|
|
1046 | |
|
|
1047 | if (dirp && u && names) |
|
|
1048 | for (;;) |
|
|
1049 | { |
|
|
1050 | errno = 0; |
|
|
1051 | readdir_r (dirp, &u->d, &entp); |
|
|
1052 | |
|
|
1053 | if (!entp) |
|
|
1054 | break; |
|
|
1055 | |
|
|
1056 | name = entp->d_name; |
|
|
1057 | |
|
|
1058 | if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) |
|
|
1059 | { |
|
|
1060 | int len = strlen (name) + 1; |
|
|
1061 | |
|
|
1062 | res++; |
|
|
1063 | |
|
|
1064 | while (memofs + len > memlen) |
|
|
1065 | { |
|
|
1066 | memlen *= 2; |
|
|
1067 | LOCK (wrklock); |
|
|
1068 | req->ptr2 = names = realloc (names, memlen); |
|
|
1069 | UNLOCK (wrklock); |
|
|
1070 | |
|
|
1071 | if (!names) |
|
|
1072 | break; |
|
|
1073 | } |
|
|
1074 | |
|
|
1075 | memcpy (names + memofs, name, len); |
|
|
1076 | memofs += len; |
|
|
1077 | } |
|
|
1078 | } |
|
|
1079 | |
|
|
1080 | if (errno) |
|
|
1081 | res = -1; |
|
|
1082 | |
|
|
1083 | req->result = res; |
|
|
1084 | } |
|
|
1085 | |
|
|
1086 | /*****************************************************************************/ |
|
|
1087 | |
|
|
1088 | static void *aio_proc (void *thr_arg) |
|
|
1089 | { |
|
|
1090 | aio_req req; |
|
|
1091 | struct timespec ts; |
|
|
1092 | worker *self = (worker *)thr_arg; |
|
|
1093 | |
|
|
1094 | /* try to distribute timeouts somewhat evenly */ |
|
|
1095 | ts.tv_nsec = (((unsigned long)self + (unsigned long)ts.tv_sec) & 1023UL) |
|
|
1096 | * (1000000000UL / 1024UL); |
708 | |
1097 | |
709 | for (;;) |
1098 | for (;;) |
710 | { |
1099 | { |
711 | errno = 0, readdir_r (dirp, &u.d, &entp); |
1100 | ts.tv_sec = time (0) + IDLE_TIMEOUT; |
712 | |
1101 | |
713 | if (!entp) |
1102 | LOCK (reqlock); |
714 | break; |
|
|
715 | |
|
|
716 | name = entp->d_name; |
|
|
717 | |
|
|
718 | if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) |
|
|
719 | { |
|
|
720 | int len = strlen (name) + 1; |
|
|
721 | |
|
|
722 | res++; |
|
|
723 | |
|
|
724 | while (memofs + len > memlen) |
|
|
725 | { |
|
|
726 | memlen *= 2; |
|
|
727 | names = realloc (names, memlen); |
|
|
728 | if (!names) |
|
|
729 | break; |
|
|
730 | } |
|
|
731 | |
|
|
732 | memcpy (names + memofs, name, len); |
|
|
733 | memofs += len; |
|
|
734 | } |
|
|
735 | } |
|
|
736 | |
|
|
737 | errorno = errno; |
|
|
738 | closedir (dirp); |
|
|
739 | |
|
|
740 | if (errorno) |
|
|
741 | { |
|
|
742 | free (names); |
|
|
743 | errno = errorno; |
|
|
744 | res = -1; |
|
|
745 | } |
|
|
746 | |
|
|
747 | *namesp = (void *)names; |
|
|
748 | return res; |
|
|
749 | } |
|
|
750 | |
|
|
751 | /*****************************************************************************/ |
|
|
752 | |
|
|
753 | static void *aio_proc (void *thr_arg) |
|
|
754 | { |
|
|
755 | aio_req req; |
|
|
756 | int type; |
|
|
757 | |
|
|
758 | do |
|
|
759 | { |
|
|
760 | pthread_mutex_lock (&reqlock); |
|
|
761 | |
1103 | |
762 | for (;;) |
1104 | for (;;) |
763 | { |
1105 | { |
764 | req = reqs; |
1106 | self->req = req = reqq_shift (&req_queue); |
765 | |
|
|
766 | if (reqs) |
|
|
767 | { |
|
|
768 | reqs = reqs->next; |
|
|
769 | if (!reqs) reqe = 0; |
|
|
770 | } |
|
|
771 | |
1107 | |
772 | if (req) |
1108 | if (req) |
773 | break; |
1109 | break; |
774 | |
1110 | |
775 | pthread_cond_wait (&reqwait, &reqlock); |
1111 | ++idle; |
|
|
1112 | |
|
|
1113 | if (COND_TIMEDWAIT (reqwait, reqlock, ts) |
|
|
1114 | == ETIMEDOUT) |
|
|
1115 | { |
|
|
1116 | if (idle > max_idle) |
|
|
1117 | { |
|
|
1118 | --idle; |
|
|
1119 | UNLOCK (reqlock); |
|
|
1120 | LOCK (wrklock); |
|
|
1121 | --started; |
|
|
1122 | UNLOCK (wrklock); |
|
|
1123 | goto quit; |
|
|
1124 | } |
|
|
1125 | |
|
|
1126 | /* we are allowed to idle, so do so without any timeout */ |
|
|
1127 | COND_WAIT (reqwait, reqlock); |
|
|
1128 | ts.tv_sec = time (0) + IDLE_TIMEOUT; |
|
|
1129 | } |
|
|
1130 | |
|
|
1131 | --idle; |
776 | } |
1132 | } |
777 | |
1133 | |
778 | pthread_mutex_unlock (&reqlock); |
1134 | --nready; |
|
|
1135 | |
|
|
1136 | UNLOCK (reqlock); |
779 | |
1137 | |
780 | errno = 0; /* strictly unnecessary */ |
1138 | errno = 0; /* strictly unnecessary */ |
781 | |
1139 | |
782 | if (!req->cancelled) |
1140 | if (!(req->flags & FLAG_CANCELLED)) |
783 | switch (req->type) |
1141 | switch (req->type) |
784 | { |
1142 | { |
785 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
1143 | case REQ_READ: req->result = req->offs >= 0 |
786 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
1144 | ? pread (req->int1, req->ptr1, req->size, req->offs) |
|
|
1145 | : read (req->int1, req->ptr1, req->size); break; |
|
|
1146 | case REQ_WRITE: req->result = req->offs >= 0 |
|
|
1147 | ? pwrite (req->int1, req->ptr1, req->size, req->offs) |
|
|
1148 | : write (req->int1, req->ptr1, req->size); break; |
787 | |
1149 | |
788 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
1150 | case REQ_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break; |
789 | case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length); break; |
1151 | case REQ_SENDFILE: req->result = sendfile_ (req->int1, req->int2, req->offs, req->size, self); break; |
790 | |
1152 | |
791 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
1153 | case REQ_STAT: req->result = stat (req->ptr1, (Stat_t *)req->ptr2); break; |
792 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
1154 | case REQ_LSTAT: req->result = lstat (req->ptr1, (Stat_t *)req->ptr2); break; |
793 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
1155 | case REQ_FSTAT: req->result = fstat (req->int1, (Stat_t *)req->ptr2); break; |
794 | |
1156 | |
|
|
1157 | case REQ_CHOWN: req->result = chown (req->ptr1, req->int2, req->int3); break; |
|
|
1158 | case REQ_FCHOWN: req->result = fchown (req->int1, req->int2, req->int3); break; |
|
|
1159 | case REQ_CHMOD: req->result = chmod (req->ptr1, req->mode); break; |
|
|
1160 | case REQ_FCHMOD: req->result = fchmod (req->int1, req->mode); break; |
|
|
1161 | |
795 | case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
1162 | case REQ_OPEN: req->result = open (req->ptr1, req->int1, req->mode); break; |
796 | case REQ_CLOSE: req->result = close (req->fd); break; |
1163 | case REQ_CLOSE: req->result = close (req->int1); break; |
797 | case REQ_UNLINK: req->result = unlink (req->dataptr); break; |
1164 | case REQ_UNLINK: req->result = unlink (req->ptr1); break; |
798 | case REQ_RMDIR: req->result = rmdir (req->dataptr); break; |
1165 | case REQ_RMDIR: req->result = rmdir (req->ptr1); break; |
|
|
1166 | case REQ_MKDIR: req->result = mkdir (req->ptr1, req->mode); break; |
799 | case REQ_RENAME: req->result = rename (req->data2ptr, req->dataptr); break; |
1167 | case REQ_RENAME: req->result = rename (req->ptr2, req->ptr1); break; |
800 | case REQ_LINK: req->result = link (req->data2ptr, req->dataptr); break; |
1168 | case REQ_LINK: req->result = link (req->ptr2, req->ptr1); break; |
801 | case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; |
1169 | case REQ_SYMLINK: req->result = symlink (req->ptr2, req->ptr1); break; |
|
|
1170 | case REQ_MKNOD: req->result = mknod (req->ptr2, req->mode, (dev_t)req->offs); break; |
|
|
1171 | case REQ_READLINK: req->result = readlink (req->ptr2, req->ptr1, NAME_MAX); break; |
802 | |
1172 | |
803 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
1173 | case REQ_FDATASYNC: req->result = fdatasync (req->int1); break; |
804 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
1174 | case REQ_FSYNC: req->result = fsync (req->int1); break; |
805 | case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break; |
1175 | case REQ_READDIR: scandir_ (req, self); break; |
806 | |
1176 | |
807 | case REQ_SLEEP: |
1177 | case REQ_BUSY: |
808 | { |
1178 | { |
809 | struct timeval tv; |
1179 | struct timeval tv; |
810 | |
1180 | |
811 | tv.tv_sec = req->fd; |
1181 | tv.tv_sec = req->nv1; |
812 | tv.tv_usec = req->fd2; |
1182 | tv.tv_usec = (req->nv1 - tv.tv_usec) * 1000000.; |
813 | |
1183 | |
814 | req->result = select (0, 0, 0, 0, &tv); |
1184 | req->result = select (0, 0, 0, 0, &tv); |
815 | } |
1185 | } |
816 | |
1186 | |
|
|
1187 | case REQ_UTIME: |
|
|
1188 | case REQ_FUTIME: |
|
|
1189 | { |
|
|
1190 | struct timeval tv[2]; |
|
|
1191 | struct timeval *times; |
|
|
1192 | |
|
|
1193 | if (req->nv1 != -1. || req->nv2 != -1.) |
|
|
1194 | { |
|
|
1195 | tv[0].tv_sec = req->nv1; |
|
|
1196 | tv[0].tv_usec = (req->nv1 - tv[0].tv_sec) * 1000000.; |
|
|
1197 | tv[1].tv_sec = req->nv2; |
|
|
1198 | tv[1].tv_usec = (req->nv2 - tv[1].tv_sec) * 1000000.; |
|
|
1199 | |
|
|
1200 | times = tv; |
|
|
1201 | } |
|
|
1202 | else |
|
|
1203 | times = 0; |
|
|
1204 | |
|
|
1205 | |
|
|
1206 | req->result = req->type == REQ_FUTIME |
|
|
1207 | ? futimes (req->int1, times) |
|
|
1208 | : utimes (req->ptr1, times); |
|
|
1209 | } |
|
|
1210 | |
|
|
1211 | case REQ_GROUP: |
|
|
1212 | case REQ_NOP: |
|
|
1213 | break; |
|
|
1214 | |
817 | case REQ_QUIT: |
1215 | case REQ_QUIT: |
818 | break; |
1216 | goto quit; |
819 | |
1217 | |
820 | default: |
1218 | default: |
821 | req->result = ENOSYS; |
1219 | req->result = -1; |
822 | break; |
1220 | break; |
823 | } |
1221 | } |
824 | |
1222 | |
825 | req->errorno = errno; |
1223 | req->errorno = errno; |
826 | |
1224 | |
827 | pthread_mutex_lock (&reslock); |
1225 | LOCK (reslock); |
828 | |
1226 | |
829 | req->next = 0; |
1227 | ++npending; |
830 | |
1228 | |
831 | if (rese) |
1229 | if (!reqq_push (&res_queue, req)) |
832 | { |
1230 | { |
833 | rese->next = req; |
|
|
834 | rese = req; |
|
|
835 | } |
|
|
836 | else |
|
|
837 | { |
|
|
838 | rese = ress = req; |
|
|
839 | |
|
|
840 | /* write a dummy byte to the pipe so fh becomes ready */ |
1231 | /* write a dummy byte to the pipe so fh becomes ready */ |
841 | write (respipe [1], &respipe, 1); |
1232 | write (respipe [1], &respipe, 1); |
|
|
1233 | |
|
|
1234 | /* optionally signal the main thread asynchronously */ |
|
|
1235 | if (main_sig) |
|
|
1236 | pthread_kill (main_tid, main_sig); |
842 | } |
1237 | } |
843 | |
1238 | |
844 | pthread_mutex_unlock (&reslock); |
1239 | self->req = 0; |
|
|
1240 | worker_clear (self); |
|
|
1241 | |
|
|
1242 | UNLOCK (reslock); |
845 | } |
1243 | } |
846 | while (type != REQ_QUIT); |
1244 | |
|
|
1245 | quit: |
|
|
1246 | LOCK (wrklock); |
|
|
1247 | worker_free (self); |
|
|
1248 | UNLOCK (wrklock); |
847 | |
1249 | |
848 | return 0; |
1250 | return 0; |
849 | } |
1251 | } |
850 | |
1252 | |
851 | /*****************************************************************************/ |
1253 | /*****************************************************************************/ |
852 | |
1254 | |
853 | static void atfork_prepare (void) |
1255 | static void atfork_prepare (void) |
854 | { |
1256 | { |
855 | pthread_mutex_lock (&reqlock); |
1257 | LOCK (wrklock); |
856 | pthread_mutex_lock (&reslock); |
1258 | LOCK (reqlock); |
|
|
1259 | LOCK (reslock); |
857 | #if !HAVE_PREADWRITE |
1260 | #if !HAVE_PREADWRITE |
858 | pthread_mutex_lock (&preadwritelock); |
1261 | LOCK (preadwritelock); |
859 | #endif |
1262 | #endif |
860 | #if !HAVE_READDIR_R |
1263 | #if !HAVE_READDIR_R |
861 | pthread_mutex_lock (&readdirlock); |
1264 | LOCK (readdirlock); |
862 | #endif |
1265 | #endif |
863 | } |
1266 | } |
864 | |
1267 | |
865 | static void atfork_parent (void) |
1268 | static void atfork_parent (void) |
866 | { |
1269 | { |
867 | #if !HAVE_READDIR_R |
1270 | #if !HAVE_READDIR_R |
868 | pthread_mutex_unlock (&readdirlock); |
1271 | UNLOCK (readdirlock); |
869 | #endif |
1272 | #endif |
870 | #if !HAVE_PREADWRITE |
1273 | #if !HAVE_PREADWRITE |
871 | pthread_mutex_unlock (&preadwritelock); |
1274 | UNLOCK (preadwritelock); |
872 | #endif |
1275 | #endif |
873 | pthread_mutex_unlock (&reslock); |
1276 | UNLOCK (reslock); |
874 | pthread_mutex_unlock (&reqlock); |
1277 | UNLOCK (reqlock); |
|
|
1278 | UNLOCK (wrklock); |
875 | } |
1279 | } |
876 | |
1280 | |
877 | static void atfork_child (void) |
1281 | static void atfork_child (void) |
878 | { |
1282 | { |
879 | aio_req prv; |
1283 | aio_req prv; |
880 | |
1284 | |
881 | started = 0; |
1285 | while (prv = reqq_shift (&req_queue)) |
|
|
1286 | req_destroy (prv); |
882 | |
1287 | |
883 | while (reqs) |
1288 | while (prv = reqq_shift (&res_queue)) |
|
|
1289 | req_destroy (prv); |
|
|
1290 | |
|
|
1291 | while (wrk_first.next != &wrk_first) |
884 | { |
1292 | { |
885 | prv = reqs; |
1293 | worker *wrk = wrk_first.next; |
886 | reqs = prv->next; |
1294 | |
|
|
1295 | if (wrk->req) |
|
|
1296 | req_destroy (wrk->req); |
|
|
1297 | |
|
|
1298 | worker_clear (wrk); |
887 | req_free (prv); |
1299 | worker_free (wrk); |
888 | } |
1300 | } |
889 | |
1301 | |
890 | reqs = reqe = 0; |
1302 | started = 0; |
891 | |
1303 | idle = 0; |
892 | while (ress) |
1304 | nreqs = 0; |
893 | { |
1305 | nready = 0; |
894 | prv = ress; |
1306 | npending = 0; |
895 | ress = prv->next; |
|
|
896 | req_free (prv); |
|
|
897 | } |
|
|
898 | |
|
|
899 | ress = rese = 0; |
|
|
900 | |
1307 | |
901 | close (respipe [0]); |
1308 | close (respipe [0]); |
902 | close (respipe [1]); |
1309 | close (respipe [1]); |
903 | create_pipe (); |
1310 | create_pipe (); |
904 | |
1311 | |
905 | atfork_parent (); |
1312 | atfork_parent (); |
906 | } |
1313 | } |
907 | |
1314 | |
908 | #define dREQ \ |
1315 | #define dREQ \ |
909 | aio_req req; \ |
1316 | aio_req req; \ |
|
|
1317 | int req_pri = next_pri; \ |
|
|
1318 | next_pri = DEFAULT_PRI + PRI_BIAS; \ |
910 | \ |
1319 | \ |
911 | if (SvOK (callback) && !SvROK (callback)) \ |
1320 | if (SvOK (callback) && !SvROK (callback)) \ |
912 | croak ("callback must be undef or of reference type"); \ |
1321 | croak ("callback must be undef or of reference type"); \ |
913 | \ |
1322 | \ |
914 | Newz (0, req, 1, aio_cb); \ |
1323 | Newz (0, req, 1, aio_cb); \ |
915 | if (!req) \ |
1324 | if (!req) \ |
916 | croak ("out of memory during aio_req allocation"); \ |
1325 | croak ("out of memory during aio_req allocation"); \ |
917 | \ |
1326 | \ |
918 | req->callback = newSVsv (callback) |
1327 | req->callback = newSVsv (callback); \ |
|
|
1328 | req->pri = req_pri |
919 | |
1329 | |
920 | #define REQ_SEND \ |
1330 | #define REQ_SEND \ |
921 | req_send (req); \ |
1331 | req_send (req); \ |
922 | \ |
1332 | \ |
923 | if (GIMME_V != G_VOID) \ |
1333 | if (GIMME_V != G_VOID) \ |
… | |
… | |
928 | PROTOTYPES: ENABLE |
1338 | PROTOTYPES: ENABLE |
929 | |
1339 | |
930 | BOOT: |
1340 | BOOT: |
931 | { |
1341 | { |
932 | HV *stash = gv_stashpv ("IO::AIO", 1); |
1342 | HV *stash = gv_stashpv ("IO::AIO", 1); |
|
|
1343 | |
933 | newCONSTSUB (stash, "EXDEV", newSViv (EXDEV)); |
1344 | newCONSTSUB (stash, "EXDEV", newSViv (EXDEV)); |
934 | newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY)); |
1345 | newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY)); |
935 | newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY)); |
1346 | newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY)); |
|
|
1347 | newCONSTSUB (stash, "O_CREAT", newSViv (O_CREAT)); |
|
|
1348 | newCONSTSUB (stash, "O_TRUNC", newSViv (O_TRUNC)); |
|
|
1349 | newCONSTSUB (stash, "S_IFIFO", newSViv (S_IFIFO)); |
|
|
1350 | newCONSTSUB (stash, "SIGIO", newSViv (SIGIO)); |
936 | |
1351 | |
937 | create_pipe (); |
1352 | create_pipe (); |
938 | pthread_atfork (atfork_prepare, atfork_parent, atfork_child); |
1353 | ATFORK (atfork_prepare, atfork_parent, atfork_child); |
939 | } |
1354 | } |
940 | |
1355 | |
941 | void |
1356 | void |
942 | min_parallel (nthreads) |
1357 | max_poll_reqs (int nreqs) |
943 | int nthreads |
|
|
944 | PROTOTYPE: $ |
1358 | PROTOTYPE: $ |
|
|
1359 | CODE: |
|
|
1360 | max_poll_reqs = nreqs; |
945 | |
1361 | |
946 | void |
1362 | void |
947 | max_parallel (nthreads) |
1363 | max_poll_time (double nseconds) |
948 | int nthreads |
|
|
949 | PROTOTYPE: $ |
1364 | PROTOTYPE: $ |
|
|
1365 | CODE: |
|
|
1366 | max_poll_time = nseconds * AIO_TICKS; |
|
|
1367 | |
|
|
1368 | void |
|
|
1369 | min_parallel (int nthreads) |
|
|
1370 | PROTOTYPE: $ |
|
|
1371 | |
|
|
1372 | void |
|
|
1373 | max_parallel (int nthreads) |
|
|
1374 | PROTOTYPE: $ |
|
|
1375 | |
|
|
1376 | void |
|
|
1377 | max_idle (int nthreads) |
|
|
1378 | PROTOTYPE: $ |
|
|
1379 | CODE: |
|
|
1380 | set_max_idle (nthreads); |
950 | |
1381 | |
951 | int |
1382 | int |
952 | max_outstanding (nreqs) |
1383 | max_outstanding (int maxreqs) |
953 | int nreqs |
1384 | PROTOTYPE: $ |
954 | PROTOTYPE: $ |
|
|
955 | CODE: |
1385 | CODE: |
956 | RETVAL = max_outstanding; |
1386 | RETVAL = max_outstanding; |
957 | max_outstanding = nreqs; |
1387 | max_outstanding = maxreqs; |
|
|
1388 | OUTPUT: |
|
|
1389 | RETVAL |
958 | |
1390 | |
959 | void |
1391 | void |
960 | aio_open (pathname,flags,mode,callback=&PL_sv_undef) |
1392 | aio_open (SV8 *pathname, int flags, int mode, SV *callback=&PL_sv_undef) |
961 | SV * pathname |
|
|
962 | int flags |
|
|
963 | int mode |
|
|
964 | SV * callback |
|
|
965 | PROTOTYPE: $$$;$ |
1393 | PROTOTYPE: $$$;$ |
966 | PPCODE: |
1394 | PPCODE: |
967 | { |
1395 | { |
968 | dREQ; |
1396 | dREQ; |
969 | |
1397 | |
970 | req->type = REQ_OPEN; |
1398 | req->type = REQ_OPEN; |
971 | req->data = newSVsv (pathname); |
1399 | req->sv1 = newSVsv (pathname); |
972 | req->dataptr = SvPVbyte_nolen (req->data); |
1400 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
973 | req->fd = flags; |
1401 | req->int1 = flags; |
974 | req->mode = mode; |
1402 | req->mode = mode; |
975 | |
1403 | |
976 | REQ_SEND; |
1404 | REQ_SEND; |
977 | } |
1405 | } |
978 | |
1406 | |
979 | void |
1407 | void |
980 | aio_close (fh,callback=&PL_sv_undef) |
1408 | aio_close (SV *fh, SV *callback=&PL_sv_undef) |
981 | SV * fh |
|
|
982 | SV * callback |
|
|
983 | PROTOTYPE: $;$ |
1409 | PROTOTYPE: $;$ |
984 | ALIAS: |
1410 | ALIAS: |
985 | aio_close = REQ_CLOSE |
1411 | aio_close = REQ_CLOSE |
986 | aio_fsync = REQ_FSYNC |
1412 | aio_fsync = REQ_FSYNC |
987 | aio_fdatasync = REQ_FDATASYNC |
1413 | aio_fdatasync = REQ_FDATASYNC |
988 | PPCODE: |
1414 | PPCODE: |
989 | { |
1415 | { |
990 | dREQ; |
1416 | dREQ; |
991 | |
1417 | |
992 | req->type = ix; |
1418 | req->type = ix; |
993 | req->fh = newSVsv (fh); |
1419 | req->sv1 = newSVsv (fh); |
994 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
1420 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh))); |
995 | |
1421 | |
996 | REQ_SEND (req); |
1422 | REQ_SEND (req); |
997 | } |
1423 | } |
998 | |
1424 | |
999 | void |
1425 | void |
1000 | aio_read (fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
1426 | aio_read (SV *fh, SV *offset, UV length, SV8 *data, UV dataoffset, SV *callback=&PL_sv_undef) |
1001 | SV * fh |
|
|
1002 | UV offset |
|
|
1003 | UV length |
|
|
1004 | SV * data |
|
|
1005 | UV dataoffset |
|
|
1006 | SV * callback |
|
|
1007 | ALIAS: |
1427 | ALIAS: |
1008 | aio_read = REQ_READ |
1428 | aio_read = REQ_READ |
1009 | aio_write = REQ_WRITE |
1429 | aio_write = REQ_WRITE |
1010 | PROTOTYPE: $$$$$;$ |
1430 | PROTOTYPE: $$$$$;$ |
1011 | PPCODE: |
1431 | PPCODE: |
1012 | { |
1432 | { |
1013 | aio_req req; |
|
|
1014 | STRLEN svlen; |
1433 | STRLEN svlen; |
1015 | char *svptr = SvPVbyte (data, svlen); |
1434 | char *svptr = SvPVbyte (data, svlen); |
1016 | |
1435 | |
1017 | SvUPGRADE (data, SVt_PV); |
1436 | SvUPGRADE (data, SVt_PV); |
1018 | SvPOK_on (data); |
1437 | SvPOK_on (data); |
… | |
… | |
1030 | length = svlen - dataoffset; |
1449 | length = svlen - dataoffset; |
1031 | } |
1450 | } |
1032 | else |
1451 | else |
1033 | { |
1452 | { |
1034 | /* read: grow scalar as necessary */ |
1453 | /* read: grow scalar as necessary */ |
1035 | svptr = SvGROW (data, length + dataoffset); |
1454 | svptr = SvGROW (data, length + dataoffset + 1); |
1036 | } |
1455 | } |
1037 | |
1456 | |
1038 | if (length < 0) |
1457 | if (length < 0) |
1039 | croak ("length must not be negative"); |
1458 | croak ("length must not be negative"); |
1040 | |
1459 | |
1041 | { |
1460 | { |
1042 | dREQ; |
1461 | dREQ; |
1043 | |
1462 | |
1044 | req->type = ix; |
1463 | req->type = ix; |
1045 | req->fh = newSVsv (fh); |
1464 | req->sv1 = newSVsv (fh); |
1046 | req->fd = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh)) |
1465 | req->int1 = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh)) |
1047 | : IoOFP (sv_2io (fh))); |
1466 | : IoOFP (sv_2io (fh))); |
1048 | req->offset = offset; |
1467 | req->offs = SvOK (offset) ? SvVAL64 (offset) : -1; |
1049 | req->length = length; |
1468 | req->size = length; |
1050 | req->data = SvREFCNT_inc (data); |
1469 | req->sv2 = SvREFCNT_inc (data); |
1051 | req->dataptr = (char *)svptr + dataoffset; |
1470 | req->ptr1 = (char *)svptr + dataoffset; |
|
|
1471 | req->stroffset = dataoffset; |
1052 | |
1472 | |
1053 | if (!SvREADONLY (data)) |
1473 | if (!SvREADONLY (data)) |
1054 | { |
1474 | { |
1055 | SvREADONLY_on (data); |
1475 | SvREADONLY_on (data); |
1056 | req->data2ptr = (void *)data; |
1476 | req->flags |= FLAG_SV2_RO_OFF; |
1057 | } |
1477 | } |
1058 | |
1478 | |
1059 | REQ_SEND; |
1479 | REQ_SEND; |
1060 | } |
1480 | } |
1061 | } |
1481 | } |
1062 | |
1482 | |
1063 | void |
1483 | void |
|
|
1484 | aio_readlink (SV8 *path, SV *callback=&PL_sv_undef) |
|
|
1485 | PROTOTYPE: $$;$ |
|
|
1486 | PPCODE: |
|
|
1487 | { |
|
|
1488 | SV *data; |
|
|
1489 | dREQ; |
|
|
1490 | |
|
|
1491 | data = newSV (NAME_MAX); |
|
|
1492 | SvPOK_on (data); |
|
|
1493 | |
|
|
1494 | req->type = REQ_READLINK; |
|
|
1495 | req->sv1 = newSVsv (path); |
|
|
1496 | req->ptr2 = SvPVbyte_nolen (req->sv1); |
|
|
1497 | req->sv2 = data; |
|
|
1498 | req->ptr1 = SvPVbyte_nolen (data); |
|
|
1499 | |
|
|
1500 | REQ_SEND; |
|
|
1501 | } |
|
|
1502 | |
|
|
1503 | void |
1064 | aio_sendfile (out_fh,in_fh,in_offset,length,callback=&PL_sv_undef) |
1504 | aio_sendfile (SV *out_fh, SV *in_fh, SV *in_offset, UV length, SV *callback=&PL_sv_undef) |
1065 | SV * out_fh |
|
|
1066 | SV * in_fh |
|
|
1067 | UV in_offset |
|
|
1068 | UV length |
|
|
1069 | SV * callback |
|
|
1070 | PROTOTYPE: $$$$;$ |
1505 | PROTOTYPE: $$$$;$ |
1071 | PPCODE: |
1506 | PPCODE: |
1072 | { |
1507 | { |
1073 | dREQ; |
1508 | dREQ; |
1074 | |
1509 | |
1075 | req->type = REQ_SENDFILE; |
1510 | req->type = REQ_SENDFILE; |
1076 | req->fh = newSVsv (out_fh); |
1511 | req->sv1 = newSVsv (out_fh); |
1077 | req->fd = PerlIO_fileno (IoIFP (sv_2io (out_fh))); |
1512 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (out_fh))); |
1078 | req->fh2 = newSVsv (in_fh); |
1513 | req->sv2 = newSVsv (in_fh); |
1079 | req->fd2 = PerlIO_fileno (IoIFP (sv_2io (in_fh))); |
1514 | req->int2 = PerlIO_fileno (IoIFP (sv_2io (in_fh))); |
1080 | req->offset = in_offset; |
1515 | req->offs = SvVAL64 (in_offset); |
1081 | req->length = length; |
1516 | req->size = length; |
1082 | |
1517 | |
1083 | REQ_SEND; |
1518 | REQ_SEND; |
1084 | } |
1519 | } |
1085 | |
1520 | |
1086 | void |
1521 | void |
1087 | aio_readahead (fh,offset,length,callback=&PL_sv_undef) |
1522 | aio_readahead (SV *fh, SV *offset, IV length, SV *callback=&PL_sv_undef) |
1088 | SV * fh |
|
|
1089 | UV offset |
|
|
1090 | IV length |
|
|
1091 | SV * callback |
|
|
1092 | PROTOTYPE: $$$;$ |
1523 | PROTOTYPE: $$$;$ |
1093 | PPCODE: |
1524 | PPCODE: |
1094 | { |
1525 | { |
1095 | dREQ; |
1526 | dREQ; |
1096 | |
1527 | |
1097 | req->type = REQ_READAHEAD; |
1528 | req->type = REQ_READAHEAD; |
1098 | req->fh = newSVsv (fh); |
1529 | req->sv1 = newSVsv (fh); |
1099 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
1530 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh))); |
1100 | req->offset = offset; |
1531 | req->offs = SvVAL64 (offset); |
1101 | req->length = length; |
1532 | req->size = length; |
1102 | |
1533 | |
1103 | REQ_SEND; |
1534 | REQ_SEND; |
1104 | } |
1535 | } |
1105 | |
1536 | |
1106 | void |
1537 | void |
1107 | aio_stat (fh_or_path,callback=&PL_sv_undef) |
1538 | aio_stat (SV8 *fh_or_path, SV *callback=&PL_sv_undef) |
1108 | SV * fh_or_path |
|
|
1109 | SV * callback |
|
|
1110 | ALIAS: |
1539 | ALIAS: |
1111 | aio_stat = REQ_STAT |
1540 | aio_stat = REQ_STAT |
1112 | aio_lstat = REQ_LSTAT |
1541 | aio_lstat = REQ_LSTAT |
1113 | PPCODE: |
1542 | PPCODE: |
1114 | { |
1543 | { |
1115 | dREQ; |
1544 | dREQ; |
1116 | |
1545 | |
1117 | New (0, req->statdata, 1, Stat_t); |
1546 | req->ptr2 = malloc (sizeof (Stat_t)); |
1118 | if (!req->statdata) |
1547 | if (!req->ptr2) |
1119 | { |
1548 | { |
1120 | req_free (req); |
1549 | req_destroy (req); |
1121 | croak ("out of memory during aio_req->statdata allocation"); |
1550 | croak ("out of memory during aio_stat statdata allocation"); |
1122 | } |
1551 | } |
|
|
1552 | |
|
|
1553 | req->flags |= FLAG_PTR2_FREE; |
|
|
1554 | req->sv1 = newSVsv (fh_or_path); |
1123 | |
1555 | |
1124 | if (SvPOK (fh_or_path)) |
1556 | if (SvPOK (fh_or_path)) |
1125 | { |
1557 | { |
1126 | req->type = ix; |
1558 | req->type = ix; |
1127 | req->data = newSVsv (fh_or_path); |
|
|
1128 | req->dataptr = SvPVbyte_nolen (req->data); |
1559 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
1129 | } |
1560 | } |
1130 | else |
1561 | else |
1131 | { |
1562 | { |
1132 | req->type = REQ_FSTAT; |
1563 | req->type = REQ_FSTAT; |
1133 | req->fh = newSVsv (fh_or_path); |
|
|
1134 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
1564 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
1135 | } |
1565 | } |
1136 | |
1566 | |
1137 | REQ_SEND; |
1567 | REQ_SEND; |
1138 | } |
1568 | } |
1139 | |
1569 | |
1140 | void |
1570 | void |
|
|
1571 | aio_utime (SV8 *fh_or_path, SV *atime, SV *mtime, SV *callback=&PL_sv_undef) |
|
|
1572 | PPCODE: |
|
|
1573 | { |
|
|
1574 | dREQ; |
|
|
1575 | |
|
|
1576 | req->nv1 = SvOK (atime) ? SvNV (atime) : -1.; |
|
|
1577 | req->nv2 = SvOK (mtime) ? SvNV (mtime) : -1.; |
|
|
1578 | req->sv1 = newSVsv (fh_or_path); |
|
|
1579 | |
|
|
1580 | if (SvPOK (fh_or_path)) |
|
|
1581 | { |
|
|
1582 | req->type = REQ_UTIME; |
|
|
1583 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
|
|
1584 | } |
|
|
1585 | else |
|
|
1586 | { |
|
|
1587 | req->type = REQ_FUTIME; |
|
|
1588 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
|
|
1589 | } |
|
|
1590 | |
|
|
1591 | REQ_SEND; |
|
|
1592 | } |
|
|
1593 | |
|
|
1594 | void |
|
|
1595 | aio_chmod (SV8 *fh_or_path, int mode, SV *callback=&PL_sv_undef) |
|
|
1596 | PPCODE: |
|
|
1597 | { |
|
|
1598 | dREQ; |
|
|
1599 | |
|
|
1600 | req->mode = mode; |
|
|
1601 | req->sv1 = newSVsv (fh_or_path); |
|
|
1602 | |
|
|
1603 | if (SvPOK (fh_or_path)) |
|
|
1604 | { |
|
|
1605 | req->type = REQ_CHMOD; |
|
|
1606 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
|
|
1607 | } |
|
|
1608 | else |
|
|
1609 | { |
|
|
1610 | req->type = REQ_FCHMOD; |
|
|
1611 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
|
|
1612 | } |
|
|
1613 | |
|
|
1614 | REQ_SEND; |
|
|
1615 | } |
|
|
1616 | |
|
|
1617 | void |
|
|
1618 | aio_chown (SV8 *fh_or_path, SV *uid, SV *gid, SV *callback=&PL_sv_undef) |
|
|
1619 | PPCODE: |
|
|
1620 | { |
|
|
1621 | dREQ; |
|
|
1622 | |
|
|
1623 | req->int2 = SvOK (uid) ? SvIV (uid) : -1; |
|
|
1624 | req->int3 = SvOK (gid) ? SvIV (gid) : -1; |
|
|
1625 | req->sv1 = newSVsv (fh_or_path); |
|
|
1626 | |
|
|
1627 | if (SvPOK (fh_or_path)) |
|
|
1628 | { |
|
|
1629 | req->type = REQ_CHOWN; |
|
|
1630 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
|
|
1631 | } |
|
|
1632 | else |
|
|
1633 | { |
|
|
1634 | req->type = REQ_FCHOWN; |
|
|
1635 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
|
|
1636 | } |
|
|
1637 | |
|
|
1638 | REQ_SEND; |
|
|
1639 | } |
|
|
1640 | |
|
|
1641 | void |
1141 | aio_unlink (pathname,callback=&PL_sv_undef) |
1642 | aio_unlink (SV8 *pathname, SV *callback=&PL_sv_undef) |
1142 | SV * pathname |
|
|
1143 | SV * callback |
|
|
1144 | ALIAS: |
1643 | ALIAS: |
1145 | aio_unlink = REQ_UNLINK |
1644 | aio_unlink = REQ_UNLINK |
1146 | aio_rmdir = REQ_RMDIR |
1645 | aio_rmdir = REQ_RMDIR |
1147 | aio_readdir = REQ_READDIR |
1646 | aio_readdir = REQ_READDIR |
1148 | PPCODE: |
1647 | PPCODE: |
1149 | { |
1648 | { |
1150 | dREQ; |
1649 | dREQ; |
1151 | |
1650 | |
1152 | req->type = ix; |
1651 | req->type = ix; |
1153 | req->data = newSVsv (pathname); |
1652 | req->sv1 = newSVsv (pathname); |
1154 | req->dataptr = SvPVbyte_nolen (req->data); |
1653 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
|
|
1654 | |
|
|
1655 | REQ_SEND; |
|
|
1656 | } |
|
|
1657 | |
|
|
1658 | void |
|
|
1659 | aio_mkdir (SV8 *pathname, int mode, SV *callback=&PL_sv_undef) |
|
|
1660 | PPCODE: |
|
|
1661 | { |
|
|
1662 | dREQ; |
1155 | |
1663 | |
|
|
1664 | req->type = REQ_MKDIR; |
|
|
1665 | req->sv1 = newSVsv (pathname); |
|
|
1666 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
|
|
1667 | req->mode = mode; |
|
|
1668 | |
1156 | REQ_SEND; |
1669 | REQ_SEND; |
1157 | } |
1670 | } |
1158 | |
1671 | |
1159 | void |
1672 | void |
1160 | aio_link (oldpath,newpath,callback=&PL_sv_undef) |
1673 | aio_link (SV8 *oldpath, SV8 *newpath, SV *callback=&PL_sv_undef) |
1161 | SV * oldpath |
|
|
1162 | SV * newpath |
|
|
1163 | SV * callback |
|
|
1164 | ALIAS: |
1674 | ALIAS: |
1165 | aio_link = REQ_LINK |
1675 | aio_link = REQ_LINK |
1166 | aio_symlink = REQ_SYMLINK |
1676 | aio_symlink = REQ_SYMLINK |
1167 | aio_rename = REQ_RENAME |
1677 | aio_rename = REQ_RENAME |
1168 | PPCODE: |
1678 | PPCODE: |
1169 | { |
1679 | { |
1170 | dREQ; |
1680 | dREQ; |
1171 | |
1681 | |
1172 | req->type = ix; |
1682 | req->type = ix; |
1173 | req->fh = newSVsv (oldpath); |
1683 | req->sv2 = newSVsv (oldpath); |
1174 | req->data2ptr = SvPVbyte_nolen (req->fh); |
1684 | req->ptr2 = SvPVbyte_nolen (req->sv2); |
1175 | req->data = newSVsv (newpath); |
1685 | req->sv1 = newSVsv (newpath); |
1176 | req->dataptr = SvPVbyte_nolen (req->data); |
1686 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
1177 | |
1687 | |
1178 | REQ_SEND; |
1688 | REQ_SEND; |
1179 | } |
1689 | } |
1180 | |
1690 | |
1181 | void |
1691 | void |
1182 | aio_sleep (delay,callback=&PL_sv_undef) |
1692 | aio_mknod (SV8 *pathname, int mode, UV dev, SV *callback=&PL_sv_undef) |
1183 | double delay |
|
|
1184 | SV * callback |
|
|
1185 | PPCODE: |
1693 | PPCODE: |
1186 | { |
1694 | { |
1187 | dREQ; |
1695 | dREQ; |
1188 | |
1696 | |
1189 | req->type = REQ_SLEEP; |
1697 | req->type = REQ_MKNOD; |
1190 | req->fd = delay < 0. ? 0 : delay; |
1698 | req->sv1 = newSVsv (pathname); |
1191 | req->fd2 = delay < 0. ? 0 : 1000. * (delay - req->fd); |
1699 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
1192 | |
1700 | req->mode = (mode_t)mode; |
|
|
1701 | req->offs = dev; |
|
|
1702 | |
1193 | REQ_SEND; |
1703 | REQ_SEND; |
1194 | } |
1704 | } |
1195 | |
1705 | |
1196 | void |
1706 | void |
|
|
1707 | aio_busy (double delay, SV *callback=&PL_sv_undef) |
|
|
1708 | PPCODE: |
|
|
1709 | { |
|
|
1710 | dREQ; |
|
|
1711 | |
|
|
1712 | req->type = REQ_BUSY; |
|
|
1713 | req->nv1 = delay < 0. ? 0. : delay; |
|
|
1714 | |
|
|
1715 | REQ_SEND; |
|
|
1716 | } |
|
|
1717 | |
|
|
1718 | void |
1197 | aio_group (callback=&PL_sv_undef) |
1719 | aio_group (SV *callback=&PL_sv_undef) |
1198 | SV * callback |
|
|
1199 | PROTOTYPE: ;$ |
1720 | PROTOTYPE: ;$ |
1200 | PPCODE: |
1721 | PPCODE: |
1201 | { |
1722 | { |
1202 | dREQ; |
1723 | dREQ; |
|
|
1724 | |
1203 | req->type = REQ_GROUP; |
1725 | req->type = REQ_GROUP; |
|
|
1726 | |
1204 | req_send (req); |
1727 | req_send (req); |
1205 | XPUSHs (req_sv (req, AIO_GRP_KLASS)); |
1728 | XPUSHs (req_sv (req, AIO_GRP_KLASS)); |
1206 | } |
1729 | } |
|
|
1730 | |
|
|
1731 | void |
|
|
1732 | aio_nop (SV *callback=&PL_sv_undef) |
|
|
1733 | PPCODE: |
|
|
1734 | { |
|
|
1735 | dREQ; |
|
|
1736 | |
|
|
1737 | req->type = REQ_NOP; |
|
|
1738 | |
|
|
1739 | REQ_SEND; |
|
|
1740 | } |
|
|
1741 | |
|
|
1742 | int |
|
|
1743 | aioreq_pri (int pri = 0) |
|
|
1744 | PROTOTYPE: ;$ |
|
|
1745 | CODE: |
|
|
1746 | RETVAL = next_pri - PRI_BIAS; |
|
|
1747 | if (items > 0) |
|
|
1748 | { |
|
|
1749 | if (pri < PRI_MIN) pri = PRI_MIN; |
|
|
1750 | if (pri > PRI_MAX) pri = PRI_MAX; |
|
|
1751 | next_pri = pri + PRI_BIAS; |
|
|
1752 | } |
|
|
1753 | OUTPUT: |
|
|
1754 | RETVAL |
|
|
1755 | |
|
|
1756 | void |
|
|
1757 | aioreq_nice (int nice = 0) |
|
|
1758 | CODE: |
|
|
1759 | nice = next_pri - nice; |
|
|
1760 | if (nice < PRI_MIN) nice = PRI_MIN; |
|
|
1761 | if (nice > PRI_MAX) nice = PRI_MAX; |
|
|
1762 | next_pri = nice + PRI_BIAS; |
1207 | |
1763 | |
1208 | void |
1764 | void |
1209 | flush () |
1765 | flush () |
1210 | PROTOTYPE: |
1766 | PROTOTYPE: |
1211 | CODE: |
1767 | CODE: |
… | |
… | |
1213 | { |
1769 | { |
1214 | poll_wait (); |
1770 | poll_wait (); |
1215 | poll_cb (); |
1771 | poll_cb (); |
1216 | } |
1772 | } |
1217 | |
1773 | |
1218 | void |
1774 | int |
1219 | poll() |
1775 | poll() |
1220 | PROTOTYPE: |
1776 | PROTOTYPE: |
1221 | CODE: |
1777 | CODE: |
1222 | if (nreqs) |
|
|
1223 | { |
|
|
1224 | poll_wait (); |
1778 | poll_wait (); |
1225 | poll_cb (); |
1779 | RETVAL = poll_cb (); |
1226 | } |
1780 | OUTPUT: |
|
|
1781 | RETVAL |
1227 | |
1782 | |
1228 | int |
1783 | int |
1229 | poll_fileno() |
1784 | poll_fileno() |
1230 | PROTOTYPE: |
1785 | PROTOTYPE: |
1231 | CODE: |
1786 | CODE: |
… | |
… | |
1243 | |
1798 | |
1244 | void |
1799 | void |
1245 | poll_wait() |
1800 | poll_wait() |
1246 | PROTOTYPE: |
1801 | PROTOTYPE: |
1247 | CODE: |
1802 | CODE: |
1248 | if (nreqs) |
|
|
1249 | poll_wait (); |
1803 | poll_wait (); |
|
|
1804 | |
|
|
1805 | void |
|
|
1806 | setsig (int signum = SIGIO) |
|
|
1807 | PROTOTYPE: ;$ |
|
|
1808 | CODE: |
|
|
1809 | { |
|
|
1810 | if (block_sig_level) |
|
|
1811 | croak ("cannot call IO::AIO::setsig from within aio_block/callback"); |
|
|
1812 | |
|
|
1813 | LOCK (reslock); |
|
|
1814 | main_tid = pthread_self (); |
|
|
1815 | main_sig = signum; |
|
|
1816 | UNLOCK (reslock); |
|
|
1817 | |
|
|
1818 | if (main_sig && npending) |
|
|
1819 | pthread_kill (main_tid, main_sig); |
|
|
1820 | } |
|
|
1821 | |
|
|
1822 | void |
|
|
1823 | aio_block (SV *cb) |
|
|
1824 | PROTOTYPE: & |
|
|
1825 | PPCODE: |
|
|
1826 | { |
|
|
1827 | int count; |
|
|
1828 | |
|
|
1829 | block_sig (); |
|
|
1830 | PUSHMARK (SP); |
|
|
1831 | PUTBACK; |
|
|
1832 | count = call_sv (cb, GIMME_V | G_NOARGS | G_EVAL); |
|
|
1833 | SPAGAIN; |
|
|
1834 | unblock_sig (); |
|
|
1835 | |
|
|
1836 | if (SvTRUE (ERRSV)) |
|
|
1837 | croak (0); |
|
|
1838 | |
|
|
1839 | XSRETURN (count); |
|
|
1840 | } |
1250 | |
1841 | |
1251 | int |
1842 | int |
1252 | nreqs() |
1843 | nreqs() |
1253 | PROTOTYPE: |
1844 | PROTOTYPE: |
1254 | CODE: |
1845 | CODE: |
1255 | RETVAL = nreqs; |
1846 | RETVAL = nreqs; |
1256 | OUTPUT: |
1847 | OUTPUT: |
1257 | RETVAL |
1848 | RETVAL |
1258 | |
1849 | |
1259 | PROTOTYPES: DISABLE |
1850 | int |
1260 | |
1851 | nready() |
1261 | MODULE = IO::AIO PACKAGE = IO::AIO::REQ |
|
|
1262 | |
|
|
1263 | void |
|
|
1264 | cancel (aio_req_ornot req) |
|
|
1265 | PROTOTYPE: |
1852 | PROTOTYPE: |
1266 | CODE: |
1853 | CODE: |
|
|
1854 | RETVAL = get_nready (); |
|
|
1855 | OUTPUT: |
|
|
1856 | RETVAL |
|
|
1857 | |
|
|
1858 | int |
|
|
1859 | npending() |
|
|
1860 | PROTOTYPE: |
|
|
1861 | CODE: |
|
|
1862 | RETVAL = get_npending (); |
|
|
1863 | OUTPUT: |
|
|
1864 | RETVAL |
|
|
1865 | |
|
|
1866 | int |
|
|
1867 | nthreads() |
|
|
1868 | PROTOTYPE: |
|
|
1869 | CODE: |
|
|
1870 | if (WORDACCESS_UNSAFE) LOCK (wrklock); |
|
|
1871 | RETVAL = started; |
|
|
1872 | if (WORDACCESS_UNSAFE) UNLOCK (wrklock); |
|
|
1873 | OUTPUT: |
|
|
1874 | RETVAL |
|
|
1875 | |
|
|
1876 | PROTOTYPES: DISABLE |
|
|
1877 | |
|
|
1878 | MODULE = IO::AIO PACKAGE = IO::AIO::REQ |
|
|
1879 | |
|
|
1880 | void |
|
|
1881 | cancel (aio_req_ornot req) |
|
|
1882 | CODE: |
1267 | req_cancel (req); |
1883 | req_cancel (req); |
|
|
1884 | |
|
|
1885 | void |
|
|
1886 | cb (aio_req_ornot req, SV *callback=&PL_sv_undef) |
|
|
1887 | CODE: |
|
|
1888 | SvREFCNT_dec (req->callback); |
|
|
1889 | req->callback = newSVsv (callback); |
1268 | |
1890 | |
1269 | MODULE = IO::AIO PACKAGE = IO::AIO::GRP |
1891 | MODULE = IO::AIO PACKAGE = IO::AIO::GRP |
1270 | |
1892 | |
1271 | void |
1893 | void |
1272 | add (aio_req grp, ...) |
1894 | add (aio_req grp, ...) |
1273 | PPCODE: |
1895 | PPCODE: |
1274 | { |
1896 | { |
1275 | int i; |
1897 | int i; |
|
|
1898 | aio_req req; |
1276 | |
1899 | |
|
|
1900 | if (main_sig && !block_sig_level) |
|
|
1901 | croak ("aio_group->add called outside aio_block/callback context while IO::AIO::setsig is in use"); |
|
|
1902 | |
1277 | if (grp->fd == 2) |
1903 | if (grp->int1 == 2) |
1278 | croak ("cannot add requests to IO::AIO::GRP after the group finished"); |
1904 | croak ("cannot add requests to IO::AIO::GRP after the group finished"); |
1279 | |
1905 | |
1280 | for (i = 1; i < items; ++i ) |
1906 | for (i = 1; i < items; ++i ) |
1281 | { |
1907 | { |
1282 | if (GIMME_V != G_VOID) |
1908 | if (GIMME_V != G_VOID) |
1283 | XPUSHs (sv_2mortal (newSVsv (ST (i)))); |
1909 | XPUSHs (sv_2mortal (newSVsv (ST (i)))); |
1284 | |
1910 | |
1285 | aio_req req = SvAIO_REQ (ST (i)); |
1911 | req = SvAIO_REQ (ST (i)); |
1286 | |
1912 | |
1287 | if (req) |
1913 | if (req) |
1288 | { |
1914 | { |
1289 | ++grp->length; |
1915 | ++grp->size; |
1290 | req->grp = grp; |
1916 | req->grp = grp; |
1291 | |
1917 | |
1292 | req->grp_prev = 0; |
1918 | req->grp_prev = 0; |
1293 | req->grp_next = grp->grp_first; |
1919 | req->grp_next = grp->grp_first; |
1294 | |
1920 | |
… | |
… | |
1299 | } |
1925 | } |
1300 | } |
1926 | } |
1301 | } |
1927 | } |
1302 | |
1928 | |
1303 | void |
1929 | void |
|
|
1930 | cancel_subs (aio_req_ornot req) |
|
|
1931 | CODE: |
|
|
1932 | req_cancel_subs (req); |
|
|
1933 | |
|
|
1934 | void |
1304 | result (aio_req grp, ...) |
1935 | result (aio_req grp, ...) |
|
|
1936 | CODE: |
|
|
1937 | { |
|
|
1938 | int i; |
|
|
1939 | AV *av; |
|
|
1940 | |
|
|
1941 | grp->errorno = errno; |
|
|
1942 | |
|
|
1943 | av = newAV (); |
|
|
1944 | |
|
|
1945 | for (i = 1; i < items; ++i ) |
|
|
1946 | av_push (av, newSVsv (ST (i))); |
|
|
1947 | |
|
|
1948 | SvREFCNT_dec (grp->sv1); |
|
|
1949 | grp->sv1 = (SV *)av; |
|
|
1950 | } |
|
|
1951 | |
|
|
1952 | void |
|
|
1953 | errno (aio_req grp, int errorno = errno) |
|
|
1954 | CODE: |
|
|
1955 | grp->errorno = errorno; |
|
|
1956 | |
|
|
1957 | void |
|
|
1958 | limit (aio_req grp, int limit) |
1305 | CODE: |
1959 | CODE: |
1306 | { |
1960 | grp->int2 = limit; |
1307 | int i; |
1961 | aio_grp_feed (grp); |
1308 | AV *av = newAV (); |
|
|
1309 | |
1962 | |
1310 | for (i = 1; i < items; ++i ) |
|
|
1311 | av_push (av, newSVsv (ST (i))); |
|
|
1312 | |
|
|
1313 | SvREFCNT_dec (grp->data); |
|
|
1314 | grp->data = (SV *)av; |
|
|
1315 | } |
|
|
1316 | |
|
|
1317 | void |
1963 | void |
1318 | feeder_limit (aio_req grp, int limit) |
1964 | feed (aio_req grp, SV *callback=&PL_sv_undef) |
1319 | CODE: |
1965 | CODE: |
1320 | grp->fd2 = limit; |
1966 | { |
|
|
1967 | SvREFCNT_dec (grp->sv2); |
|
|
1968 | grp->sv2 = newSVsv (callback); |
|
|
1969 | |
|
|
1970 | if (grp->int2 <= 0) |
|
|
1971 | grp->int2 = 2; |
|
|
1972 | |
1321 | aio_grp_feed (grp); |
1973 | aio_grp_feed (grp); |
1322 | |
|
|
1323 | void |
|
|
1324 | set_feeder (aio_req grp, SV *callback=&PL_sv_undef) |
|
|
1325 | CODE: |
|
|
1326 | { |
|
|
1327 | SvREFCNT_dec (grp->fh2); |
|
|
1328 | grp->fh2 = newSVsv (callback); |
|
|
1329 | |
|
|
1330 | if (grp->fd2 <= 0) |
|
|
1331 | grp->fd2 = 2; |
|
|
1332 | |
|
|
1333 | aio_grp_feed (grp); |
|
|
1334 | } |
1974 | } |
1335 | |
1975 | |