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