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