… | |
… | |
5 | #include "perl.h" |
5 | #include "perl.h" |
6 | #include "XSUB.h" |
6 | #include "XSUB.h" |
7 | |
7 | |
8 | #include "autoconf/config.h" |
8 | #include "autoconf/config.h" |
9 | |
9 | |
|
|
10 | #include <pthread.h> |
|
|
11 | |
|
|
12 | #include <stddef.h> |
10 | #include <sys/types.h> |
13 | #include <sys/types.h> |
11 | #include <sys/stat.h> |
14 | #include <sys/stat.h> |
12 | |
15 | #include <limits.h> |
13 | #include <unistd.h> |
16 | #include <unistd.h> |
14 | #include <fcntl.h> |
17 | #include <fcntl.h> |
15 | #include <signal.h> |
18 | #include <signal.h> |
16 | #include <sched.h> |
19 | #include <sched.h> |
17 | |
20 | |
18 | #include <pthread.h> |
21 | #if HAVE_SENDFILE |
19 | |
22 | # if __linux |
20 | typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
23 | # include <sys/sendfile.h> |
21 | typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
24 | # elif __freebsd |
22 | typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
25 | # include <sys/socket.h> |
|
|
26 | # include <sys/uio.h> |
|
|
27 | # elif __hpux |
|
|
28 | # include <sys/socket.h> |
|
|
29 | # elif __solaris /* not yet */ |
|
|
30 | # include <sys/sendfile.h> |
|
|
31 | # else |
|
|
32 | # error sendfile support requested but not available |
|
|
33 | # endif |
|
|
34 | #endif |
23 | |
35 | |
24 | #if __ia64 |
36 | #if __ia64 |
25 | # define STACKSIZE 65536 |
37 | # define STACKSIZE 65536 |
26 | #else |
38 | #else |
27 | # define STACKSIZE 4096 |
39 | # define STACKSIZE 8192 |
28 | #endif |
40 | #endif |
29 | |
41 | |
30 | enum { |
42 | enum { |
31 | REQ_QUIT, |
43 | REQ_QUIT, |
32 | REQ_OPEN, REQ_CLOSE, |
44 | REQ_OPEN, REQ_CLOSE, |
33 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
45 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
|
|
46 | REQ_SENDFILE, |
34 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
47 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
35 | REQ_FSYNC, REQ_FDATASYNC, |
48 | REQ_FSYNC, REQ_FDATASYNC, |
36 | REQ_UNLINK, REQ_RMDIR, |
49 | REQ_UNLINK, REQ_RMDIR, |
|
|
50 | REQ_READDIR, |
37 | REQ_SYMLINK, |
51 | REQ_SYMLINK, |
38 | }; |
52 | }; |
39 | |
53 | |
40 | typedef struct aio_cb { |
54 | typedef struct aio_cb { |
41 | struct aio_cb *volatile next; |
55 | struct aio_cb *volatile next; |
42 | |
56 | |
43 | int type; |
57 | int type; |
44 | |
58 | |
|
|
59 | /* should receive a cleanup, with unions */ |
45 | int fd; |
60 | int fd, fd2; |
46 | off_t offset; |
61 | off_t offset; |
47 | size_t length; |
62 | size_t length; |
48 | ssize_t result; |
63 | ssize_t result; |
49 | mode_t mode; /* open */ |
64 | mode_t mode; /* open */ |
50 | int errorno; |
65 | int errorno; |
51 | SV *data, *callback, *fh; |
66 | SV *data, *callback; |
|
|
67 | SV *fh, *fh2; |
52 | void *dataptr, *data2ptr; |
68 | void *dataptr, *data2ptr; |
53 | STRLEN dataoffset; |
69 | STRLEN dataoffset; |
54 | |
70 | |
55 | Stat_t *statdata; |
71 | Stat_t *statdata; |
56 | } aio_cb; |
72 | } aio_cb; |
57 | |
73 | |
58 | typedef aio_cb *aio_req; |
74 | typedef aio_cb *aio_req; |
59 | |
75 | |
60 | static int started; |
76 | static int started, wanted; |
61 | static volatile int nreqs; |
77 | static volatile int nreqs; |
62 | static int max_outstanding = 1<<30; |
78 | static int max_outstanding = 1<<30; |
63 | static int respipe [2]; |
79 | static int respipe [2]; |
64 | |
80 | |
65 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
81 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
… | |
… | |
75 | SvREFCNT_dec (req->data); |
91 | SvREFCNT_dec (req->data); |
76 | |
92 | |
77 | if (req->fh) |
93 | if (req->fh) |
78 | SvREFCNT_dec (req->fh); |
94 | SvREFCNT_dec (req->fh); |
79 | |
95 | |
|
|
96 | if (req->fh2) |
|
|
97 | SvREFCNT_dec (req->fh2); |
|
|
98 | |
80 | if (req->statdata) |
99 | if (req->statdata) |
81 | Safefree (req->statdata); |
100 | Safefree (req->statdata); |
82 | |
101 | |
83 | if (req->callback) |
102 | if (req->callback) |
84 | SvREFCNT_dec (req->callback); |
103 | SvREFCNT_dec (req->callback); |
|
|
104 | |
|
|
105 | if (req->type == REQ_READDIR && req->result >= 0) |
|
|
106 | free (req->data2ptr); |
85 | |
107 | |
86 | Safefree (req); |
108 | Safefree (req); |
87 | } |
109 | } |
88 | |
110 | |
89 | static void |
111 | static void |
… | |
… | |
116 | { |
138 | { |
117 | ress = req->next; |
139 | ress = req->next; |
118 | |
140 | |
119 | if (!ress) |
141 | if (!ress) |
120 | { |
142 | { |
121 | rese = 0; |
|
|
122 | |
|
|
123 | /* read any signals sent by the worker threads */ |
143 | /* read any signals sent by the worker threads */ |
124 | char buf [32]; |
144 | char buf [32]; |
125 | while (read (respipe [0], buf, 32) == 32) |
145 | while (read (respipe [0], buf, 32) == 32) |
126 | ; |
146 | ; |
|
|
147 | |
|
|
148 | rese = 0; |
127 | } |
149 | } |
128 | } |
150 | } |
129 | |
151 | |
130 | pthread_mutex_unlock (&reslock); |
152 | pthread_mutex_unlock (&reslock); |
131 | |
153 | |
… | |
… | |
142 | errno = req->errorno; |
164 | errno = req->errorno; |
143 | |
165 | |
144 | if (req->type == REQ_READ) |
166 | if (req->type == REQ_READ) |
145 | SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); |
167 | SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); |
146 | |
168 | |
|
|
169 | if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE)) |
|
|
170 | SvREADONLY_off (req->data); |
|
|
171 | |
147 | if (req->statdata) |
172 | if (req->statdata) |
148 | { |
173 | { |
149 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
174 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
150 | PL_laststatval = req->result; |
175 | PL_laststatval = req->result; |
151 | PL_statcache = *(req->statdata); |
176 | PL_statcache = *(req->statdata); |
152 | } |
177 | } |
153 | |
178 | |
154 | ENTER; |
179 | ENTER; |
155 | PUSHMARK (SP); |
180 | PUSHMARK (SP); |
156 | XPUSHs (sv_2mortal (newSViv (req->result))); |
|
|
157 | |
181 | |
158 | if (req->type == REQ_OPEN) |
182 | if (req->type == REQ_READDIR) |
159 | { |
183 | { |
|
|
184 | SV *rv = &PL_sv_undef; |
|
|
185 | |
|
|
186 | if (req->result >= 0) |
|
|
187 | { |
|
|
188 | char *buf = req->data2ptr; |
|
|
189 | AV *av = newAV (); |
|
|
190 | |
|
|
191 | while (req->result) |
|
|
192 | { |
|
|
193 | SV *sv = newSVpv (buf, 0); |
|
|
194 | |
|
|
195 | av_push (av, sv); |
|
|
196 | buf += SvCUR (sv) + 1; |
|
|
197 | req->result--; |
|
|
198 | } |
|
|
199 | |
|
|
200 | rv = sv_2mortal (newRV_noinc ((SV *)av)); |
|
|
201 | } |
|
|
202 | |
|
|
203 | XPUSHs (rv); |
|
|
204 | } |
|
|
205 | else |
|
|
206 | { |
|
|
207 | XPUSHs (sv_2mortal (newSViv (req->result))); |
|
|
208 | |
|
|
209 | if (req->type == REQ_OPEN) |
|
|
210 | { |
160 | /* convert fd to fh */ |
211 | /* convert fd to fh */ |
161 | SV *fh; |
212 | SV *fh; |
162 | |
213 | |
163 | PUTBACK; |
214 | PUTBACK; |
164 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
215 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
165 | SPAGAIN; |
216 | SPAGAIN; |
166 | |
217 | |
167 | fh = SvREFCNT_inc (POPs); |
218 | fh = SvREFCNT_inc (POPs); |
168 | |
219 | |
169 | PUSHMARK (SP); |
220 | PUSHMARK (SP); |
170 | XPUSHs (sv_2mortal (fh)); |
221 | XPUSHs (sv_2mortal (fh)); |
|
|
222 | } |
171 | } |
223 | } |
172 | |
224 | |
173 | if (SvOK (req->callback)) |
225 | if (SvOK (req->callback)) |
174 | { |
226 | { |
175 | PUTBACK; |
227 | PUTBACK; |
… | |
… | |
218 | } |
270 | } |
219 | |
271 | |
220 | static void |
272 | static void |
221 | send_req (aio_req req) |
273 | send_req (aio_req req) |
222 | { |
274 | { |
|
|
275 | while (started < wanted && nreqs >= started) |
|
|
276 | start_thread (); |
|
|
277 | |
223 | nreqs++; |
278 | nreqs++; |
224 | |
279 | |
225 | pthread_mutex_lock (&reqlock); |
280 | pthread_mutex_lock (&reqlock); |
226 | |
281 | |
227 | req->next = 0; |
282 | req->next = 0; |
… | |
… | |
259 | send_req (req); |
314 | send_req (req); |
260 | } |
315 | } |
261 | |
316 | |
262 | static void min_parallel (int nthreads) |
317 | static void min_parallel (int nthreads) |
263 | { |
318 | { |
264 | while (nthreads > started) |
319 | if (wanted < nthreads) |
265 | start_thread (); |
320 | wanted = nthreads; |
266 | } |
321 | } |
267 | |
322 | |
268 | static void max_parallel (int nthreads) |
323 | static void max_parallel (int nthreads) |
269 | { |
324 | { |
270 | int cur = started; |
325 | int cur = started; |
271 | |
326 | |
|
|
327 | if (wanted > nthreads) |
|
|
328 | wanted = nthreads; |
|
|
329 | |
272 | while (cur > nthreads) |
330 | while (cur > wanted) |
273 | { |
331 | { |
274 | end_thread (); |
332 | end_thread (); |
275 | cur--; |
333 | cur--; |
276 | } |
334 | } |
277 | |
335 | |
278 | while (started > nthreads) |
336 | while (started > wanted) |
279 | { |
337 | { |
280 | poll_wait (); |
338 | poll_wait (); |
281 | poll_cb (); |
339 | poll_cb (); |
282 | } |
340 | } |
283 | } |
341 | } |
… | |
… | |
290 | if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)) |
348 | if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)) |
291 | croak ("cannot set result pipe to nonblocking mode"); |
349 | croak ("cannot set result pipe to nonblocking mode"); |
292 | |
350 | |
293 | if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) |
351 | if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) |
294 | croak ("cannot set result pipe to nonblocking mode"); |
352 | croak ("cannot set result pipe to nonblocking mode"); |
295 | } |
|
|
296 | |
|
|
297 | static void atfork_prepare (void) |
|
|
298 | { |
|
|
299 | for (;;) { |
|
|
300 | |
|
|
301 | for (;;) |
|
|
302 | { |
|
|
303 | poll_cb (); |
|
|
304 | |
|
|
305 | if (!nreqs) |
|
|
306 | break; |
|
|
307 | |
|
|
308 | poll_wait (); |
|
|
309 | } |
|
|
310 | |
|
|
311 | pthread_mutex_lock (&reqlock); |
|
|
312 | |
|
|
313 | if (!nreqs) |
|
|
314 | break; |
|
|
315 | |
|
|
316 | pthread_mutex_unlock (&reqlock); |
|
|
317 | } |
|
|
318 | |
|
|
319 | pthread_mutex_lock (&reslock); |
|
|
320 | |
|
|
321 | assert (!nreqs && !reqs && !ress); |
|
|
322 | } |
|
|
323 | |
|
|
324 | static void atfork_parent (void) |
|
|
325 | { |
|
|
326 | pthread_mutex_unlock (&reslock); |
|
|
327 | pthread_mutex_unlock (&reqlock); |
|
|
328 | } |
|
|
329 | |
|
|
330 | static void atfork_child (void) |
|
|
331 | { |
|
|
332 | int restart = started; |
|
|
333 | started = 0; |
|
|
334 | |
|
|
335 | atfork_parent (); |
|
|
336 | |
|
|
337 | min_parallel (restart); |
|
|
338 | } |
353 | } |
339 | |
354 | |
340 | /*****************************************************************************/ |
355 | /*****************************************************************************/ |
341 | /* work around various missing functions */ |
356 | /* work around various missing functions */ |
342 | |
357 | |
… | |
… | |
347 | /* |
362 | /* |
348 | * make our pread/pwrite safe against themselves, but not against |
363 | * make our pread/pwrite safe against themselves, but not against |
349 | * normal read/write by using a mutex. slows down execution a lot, |
364 | * normal read/write by using a mutex. slows down execution a lot, |
350 | * but that's your problem, not mine. |
365 | * but that's your problem, not mine. |
351 | */ |
366 | */ |
352 | static pthread_mutex_t iolock = PTHREAD_MUTEX_INITIALIZER; |
367 | static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER; |
353 | |
368 | |
354 | static ssize_t |
369 | static ssize_t |
355 | pread (int fd, void *buf, size_t count, off_t offset) |
370 | pread (int fd, void *buf, size_t count, off_t offset) |
356 | { |
371 | { |
357 | ssize_t res; |
372 | ssize_t res; |
358 | off_t ooffset; |
373 | off_t ooffset; |
359 | |
374 | |
360 | pthread_mutex_lock (&iolock); |
375 | pthread_mutex_lock (&preadwritelock); |
361 | ooffset = lseek (fd, 0, SEEK_CUR); |
376 | ooffset = lseek (fd, 0, SEEK_CUR); |
362 | lseek (fd, offset, SEEK_SET); |
377 | lseek (fd, offset, SEEK_SET); |
363 | res = read (fd, buf, count); |
378 | res = read (fd, buf, count); |
364 | lseek (fd, ooffset, SEEK_SET); |
379 | lseek (fd, ooffset, SEEK_SET); |
365 | pthread_mutex_unlock (&iolock); |
380 | pthread_mutex_unlock (&preadwritelock); |
366 | |
381 | |
367 | return res; |
382 | return res; |
368 | } |
383 | } |
369 | |
384 | |
370 | static ssize_t |
385 | static ssize_t |
371 | pwrite (int fd, void *buf, size_t count, off_t offset) |
386 | pwrite (int fd, void *buf, size_t count, off_t offset) |
372 | { |
387 | { |
373 | ssize_t res; |
388 | ssize_t res; |
374 | off_t ooffset; |
389 | off_t ooffset; |
375 | |
390 | |
376 | pthread_mutex_lock (&iolock); |
391 | pthread_mutex_lock (&preadwritelock); |
377 | ooffset = lseek (fd, 0, SEEK_CUR); |
392 | ooffset = lseek (fd, 0, SEEK_CUR); |
378 | lseek (fd, offset, SEEK_SET); |
393 | lseek (fd, offset, SEEK_SET); |
379 | res = write (fd, buf, count); |
394 | res = write (fd, buf, count); |
380 | lseek (fd, offset, SEEK_SET); |
395 | lseek (fd, offset, SEEK_SET); |
381 | pthread_mutex_unlock (&iolock); |
396 | pthread_mutex_unlock (&preadwritelock); |
382 | |
397 | |
383 | return res; |
398 | return res; |
384 | } |
399 | } |
385 | #endif |
400 | #endif |
386 | |
401 | |
… | |
… | |
389 | #endif |
404 | #endif |
390 | |
405 | |
391 | #if !HAVE_READAHEAD |
406 | #if !HAVE_READAHEAD |
392 | # define readahead aio_readahead |
407 | # define readahead aio_readahead |
393 | |
408 | |
394 | static char readahead_buf[4096]; |
|
|
395 | |
|
|
396 | static ssize_t |
409 | static ssize_t |
397 | readahead (int fd, off_t offset, size_t count) |
410 | readahead (int fd, off_t offset, size_t count) |
398 | { |
411 | { |
|
|
412 | char readahead_buf[4096]; |
|
|
413 | |
399 | while (count > 0) |
414 | while (count > 0) |
400 | { |
415 | { |
401 | size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); |
416 | size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); |
402 | |
417 | |
403 | pread (fd, readahead_buf, len, offset); |
418 | pread (fd, readahead_buf, len, offset); |
… | |
… | |
406 | } |
421 | } |
407 | |
422 | |
408 | errno = 0; |
423 | errno = 0; |
409 | } |
424 | } |
410 | #endif |
425 | #endif |
|
|
426 | |
|
|
427 | #if !HAVE_READDIR_R |
|
|
428 | # define readdir_r aio_readdir_r |
|
|
429 | |
|
|
430 | static pthread_mutex_t readdirlock = PTHREAD_MUTEX_INITIALIZER; |
|
|
431 | |
|
|
432 | static int |
|
|
433 | readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) |
|
|
434 | { |
|
|
435 | struct dirent *e; |
|
|
436 | int errorno; |
|
|
437 | |
|
|
438 | pthread_mutex_lock (&readdirlock); |
|
|
439 | |
|
|
440 | e = readdir (dirp); |
|
|
441 | errorno = errno; |
|
|
442 | |
|
|
443 | if (e) |
|
|
444 | { |
|
|
445 | *res = ent; |
|
|
446 | strcpy (ent->d_name, e->d_name); |
|
|
447 | } |
|
|
448 | else |
|
|
449 | *res = 0; |
|
|
450 | |
|
|
451 | pthread_mutex_unlock (&readdirlock); |
|
|
452 | |
|
|
453 | errno = errorno; |
|
|
454 | return e ? 0 : -1; |
|
|
455 | } |
|
|
456 | #endif |
|
|
457 | |
|
|
458 | /* sendfile always needs emulation */ |
|
|
459 | static ssize_t |
|
|
460 | sendfile_ (int ofd, int ifd, off_t offset, size_t count) |
|
|
461 | { |
|
|
462 | ssize_t res; |
|
|
463 | |
|
|
464 | if (!count) |
|
|
465 | return 0; |
|
|
466 | |
|
|
467 | #if HAVE_SENDFILE |
|
|
468 | # if __linux |
|
|
469 | res = sendfile (ofd, ifd, &offset, count); |
|
|
470 | |
|
|
471 | # elif __freebsd |
|
|
472 | /* |
|
|
473 | * Of course, the freebsd sendfile is a dire hack with no thoughts |
|
|
474 | * wasted on making it similar to other I/O functions. |
|
|
475 | */ |
|
|
476 | { |
|
|
477 | off_t sbytes; |
|
|
478 | res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); |
|
|
479 | |
|
|
480 | if (res < 0 && sbytes) |
|
|
481 | /* maybe only on EAGAIN only: as usual, the manpage leaves you guessing */ |
|
|
482 | res = sbytes; |
|
|
483 | } |
|
|
484 | |
|
|
485 | # elif __hpux |
|
|
486 | res = sendfile (ofd, ifd, offset, count, 0, 0); |
|
|
487 | |
|
|
488 | # elif __solaris |
|
|
489 | { |
|
|
490 | struct sendfilevec vec; |
|
|
491 | size_t sbytes; |
|
|
492 | |
|
|
493 | vec.sfv_fd = ifd; |
|
|
494 | vec.sfv_flag = 0; |
|
|
495 | vec.sfv_off = offset; |
|
|
496 | vec.sfv_len = count; |
|
|
497 | |
|
|
498 | res = sendfilev (ofd, &vec, 1, &sbytes); |
|
|
499 | |
|
|
500 | if (res < 0 && sbytes) |
|
|
501 | res = sbytes; |
|
|
502 | } |
|
|
503 | |
|
|
504 | # else |
|
|
505 | res = -1; |
|
|
506 | errno = ENOSYS; |
|
|
507 | # endif |
|
|
508 | #endif |
|
|
509 | |
|
|
510 | if (res < 0 |
|
|
511 | && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK |
|
|
512 | #if __solaris |
|
|
513 | || errno == EAFNOSUPPORT || errno == EPROTOTYPE |
|
|
514 | #endif |
|
|
515 | ) |
|
|
516 | ) |
|
|
517 | { |
|
|
518 | /* emulate sendfile. this is a major pain in the ass */ |
|
|
519 | char buf[4096]; |
|
|
520 | res = 0; |
|
|
521 | |
|
|
522 | for (;;) |
|
|
523 | { |
|
|
524 | ssize_t cnt; |
|
|
525 | |
|
|
526 | cnt = pread (ifd, buf, 4096, offset); |
|
|
527 | |
|
|
528 | if (cnt <= 0) |
|
|
529 | { |
|
|
530 | if (cnt && !res) res = -1; |
|
|
531 | break; |
|
|
532 | } |
|
|
533 | |
|
|
534 | cnt = write (ofd, buf, cnt); |
|
|
535 | |
|
|
536 | if (cnt <= 0) |
|
|
537 | { |
|
|
538 | if (cnt && !res) res = -1; |
|
|
539 | break; |
|
|
540 | } |
|
|
541 | |
|
|
542 | offset += cnt; |
|
|
543 | res += cnt; |
|
|
544 | } |
|
|
545 | } |
|
|
546 | |
|
|
547 | return res; |
|
|
548 | } |
|
|
549 | |
|
|
550 | /* read a full directory */ |
|
|
551 | static int |
|
|
552 | scandir_ (const char *path, void **namesp) |
|
|
553 | { |
|
|
554 | DIR *dirp = opendir (path); |
|
|
555 | union |
|
|
556 | { |
|
|
557 | struct dirent d; |
|
|
558 | char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; |
|
|
559 | } u; |
|
|
560 | struct dirent *entp; |
|
|
561 | char *name, *names; |
|
|
562 | int memlen = 4096; |
|
|
563 | int memofs = 0; |
|
|
564 | int res = 0; |
|
|
565 | int errorno; |
|
|
566 | |
|
|
567 | if (!dirp) |
|
|
568 | return -1; |
|
|
569 | |
|
|
570 | names = malloc (memlen); |
|
|
571 | |
|
|
572 | for (;;) |
|
|
573 | { |
|
|
574 | errno = 0, readdir_r (dirp, &u.d, &entp); |
|
|
575 | |
|
|
576 | if (!entp) |
|
|
577 | break; |
|
|
578 | |
|
|
579 | name = entp->d_name; |
|
|
580 | |
|
|
581 | if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) |
|
|
582 | { |
|
|
583 | int len = strlen (name) + 1; |
|
|
584 | |
|
|
585 | res++; |
|
|
586 | |
|
|
587 | while (memofs + len > memlen) |
|
|
588 | { |
|
|
589 | memlen *= 2; |
|
|
590 | names = realloc (names, memlen); |
|
|
591 | if (!names) |
|
|
592 | break; |
|
|
593 | } |
|
|
594 | |
|
|
595 | memcpy (names + memofs, name, len); |
|
|
596 | memofs += len; |
|
|
597 | } |
|
|
598 | } |
|
|
599 | |
|
|
600 | errorno = errno; |
|
|
601 | closedir (dirp); |
|
|
602 | |
|
|
603 | if (errorno) |
|
|
604 | { |
|
|
605 | free (names); |
|
|
606 | errno = errorno; |
|
|
607 | res = -1; |
|
|
608 | } |
|
|
609 | |
|
|
610 | *namesp = (void *)names; |
|
|
611 | return res; |
|
|
612 | } |
411 | |
613 | |
412 | /*****************************************************************************/ |
614 | /*****************************************************************************/ |
413 | |
615 | |
414 | static void * |
616 | static void * |
415 | aio_proc (void *thr_arg) |
617 | aio_proc (void *thr_arg) |
… | |
… | |
447 | { |
649 | { |
448 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
650 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
449 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
651 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
450 | |
652 | |
451 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
653 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
|
|
654 | case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length); break; |
452 | |
655 | |
453 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
656 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
454 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
657 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
455 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
658 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
456 | |
659 | |
… | |
… | |
460 | case REQ_RMDIR: req->result = rmdir (req->dataptr); break; |
663 | case REQ_RMDIR: req->result = rmdir (req->dataptr); break; |
461 | case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; |
664 | case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; |
462 | |
665 | |
463 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
666 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
464 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
667 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
|
|
668 | case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break; |
465 | |
669 | |
466 | case REQ_QUIT: |
670 | case REQ_QUIT: |
467 | break; |
671 | break; |
468 | |
672 | |
469 | default: |
673 | default: |
… | |
… | |
493 | pthread_mutex_unlock (&reslock); |
697 | pthread_mutex_unlock (&reslock); |
494 | } |
698 | } |
495 | while (type != REQ_QUIT); |
699 | while (type != REQ_QUIT); |
496 | |
700 | |
497 | return 0; |
701 | return 0; |
|
|
702 | } |
|
|
703 | |
|
|
704 | /*****************************************************************************/ |
|
|
705 | |
|
|
706 | static void atfork_prepare (void) |
|
|
707 | { |
|
|
708 | pthread_mutex_lock (&reqlock); |
|
|
709 | pthread_mutex_lock (&reslock); |
|
|
710 | #if !HAVE_PREADWRITE |
|
|
711 | pthread_mutex_lock (&preadwritelock); |
|
|
712 | #endif |
|
|
713 | #if !HAVE_READDIR_R |
|
|
714 | pthread_mutex_lock (&readdirlock); |
|
|
715 | #endif |
|
|
716 | } |
|
|
717 | |
|
|
718 | static void atfork_parent (void) |
|
|
719 | { |
|
|
720 | #if !HAVE_READDIR_R |
|
|
721 | pthread_mutex_unlock (&readdirlock); |
|
|
722 | #endif |
|
|
723 | #if !HAVE_PREADWRITE |
|
|
724 | pthread_mutex_unlock (&preadwritelock); |
|
|
725 | #endif |
|
|
726 | pthread_mutex_unlock (&reslock); |
|
|
727 | pthread_mutex_unlock (&reqlock); |
|
|
728 | } |
|
|
729 | |
|
|
730 | static void atfork_child (void) |
|
|
731 | { |
|
|
732 | aio_req prv; |
|
|
733 | |
|
|
734 | started = 0; |
|
|
735 | |
|
|
736 | while (reqs) |
|
|
737 | { |
|
|
738 | prv = reqs; |
|
|
739 | reqs = prv->next; |
|
|
740 | free_req (prv); |
|
|
741 | } |
|
|
742 | |
|
|
743 | reqs = reqe = 0; |
|
|
744 | |
|
|
745 | while (ress) |
|
|
746 | { |
|
|
747 | prv = ress; |
|
|
748 | ress = prv->next; |
|
|
749 | free_req (prv); |
|
|
750 | } |
|
|
751 | |
|
|
752 | ress = rese = 0; |
|
|
753 | |
|
|
754 | close (respipe [0]); |
|
|
755 | close (respipe [1]); |
|
|
756 | create_pipe (); |
|
|
757 | |
|
|
758 | atfork_parent (); |
498 | } |
759 | } |
499 | |
760 | |
500 | #define dREQ \ |
761 | #define dREQ \ |
501 | aio_req req; \ |
762 | aio_req req; \ |
502 | \ |
763 | \ |
… | |
… | |
579 | |
840 | |
580 | void |
841 | void |
581 | aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
842 | aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
582 | SV * fh |
843 | SV * fh |
583 | UV offset |
844 | UV offset |
584 | IV length |
845 | UV length |
585 | SV * data |
846 | SV * data |
586 | IV dataoffset |
847 | UV dataoffset |
587 | SV * callback |
848 | SV * callback |
588 | ALIAS: |
849 | ALIAS: |
589 | aio_read = REQ_READ |
850 | aio_read = REQ_READ |
590 | aio_write = REQ_WRITE |
851 | aio_write = REQ_WRITE |
591 | PROTOTYPE: $$$$$;$ |
852 | PROTOTYPE: $$$$$;$ |
… | |
… | |
629 | req->offset = offset; |
890 | req->offset = offset; |
630 | req->length = length; |
891 | req->length = length; |
631 | req->data = SvREFCNT_inc (data); |
892 | req->data = SvREFCNT_inc (data); |
632 | req->dataptr = (char *)svptr + dataoffset; |
893 | req->dataptr = (char *)svptr + dataoffset; |
633 | |
894 | |
|
|
895 | if (!SvREADONLY (data)) |
|
|
896 | { |
|
|
897 | SvREADONLY_on (data); |
|
|
898 | req->data2ptr = (void *)data; |
|
|
899 | } |
|
|
900 | |
634 | send_req (req); |
901 | send_req (req); |
635 | } |
902 | } |
|
|
903 | } |
|
|
904 | |
|
|
905 | void |
|
|
906 | aio_sendfile(out_fh,in_fh,in_offset,length,callback=&PL_sv_undef) |
|
|
907 | SV * out_fh |
|
|
908 | SV * in_fh |
|
|
909 | UV in_offset |
|
|
910 | UV length |
|
|
911 | SV * callback |
|
|
912 | PROTOTYPE: $$$$;$ |
|
|
913 | CODE: |
|
|
914 | { |
|
|
915 | dREQ; |
|
|
916 | |
|
|
917 | req->type = REQ_SENDFILE; |
|
|
918 | req->fh = newSVsv (out_fh); |
|
|
919 | req->fd = PerlIO_fileno (IoIFP (sv_2io (out_fh))); |
|
|
920 | req->fh2 = newSVsv (in_fh); |
|
|
921 | req->fd2 = PerlIO_fileno (IoIFP (sv_2io (in_fh))); |
|
|
922 | req->offset = in_offset; |
|
|
923 | req->length = length; |
|
|
924 | |
|
|
925 | send_req (req); |
636 | } |
926 | } |
637 | |
927 | |
638 | void |
928 | void |
639 | aio_readahead(fh,offset,length,callback=&PL_sv_undef) |
929 | aio_readahead(fh,offset,length,callback=&PL_sv_undef) |
640 | SV * fh |
930 | SV * fh |
… | |
… | |
724 | |
1014 | |
725 | send_req (req); |
1015 | send_req (req); |
726 | } |
1016 | } |
727 | |
1017 | |
728 | void |
1018 | void |
|
|
1019 | aio_readdir(pathname,callback=&PL_sv_undef) |
|
|
1020 | SV * pathname |
|
|
1021 | SV * callback |
|
|
1022 | CODE: |
|
|
1023 | { |
|
|
1024 | dREQ; |
|
|
1025 | |
|
|
1026 | req->type = REQ_READDIR; |
|
|
1027 | req->data = newSVsv (pathname); |
|
|
1028 | req->dataptr = SvPVbyte_nolen (req->data); |
|
|
1029 | |
|
|
1030 | send_req (req); |
|
|
1031 | } |
|
|
1032 | |
|
|
1033 | void |
729 | flush() |
1034 | flush() |
730 | PROTOTYPE: |
1035 | PROTOTYPE: |
731 | CODE: |
1036 | CODE: |
732 | while (nreqs) |
1037 | while (nreqs) |
733 | { |
1038 | { |