… | |
… | |
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; |
66 | static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; |
82 | static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; |
67 | static pthread_mutex_t frklock = PTHREAD_MUTEX_INITIALIZER; |
|
|
68 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
83 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
69 | |
84 | |
70 | static volatile aio_req reqs, reqe; /* queue start, queue end */ |
85 | static volatile aio_req reqs, reqe; /* queue start, queue end */ |
71 | static volatile aio_req ress, rese; /* queue start, queue end */ |
86 | static volatile aio_req ress, rese; /* queue start, queue end */ |
72 | |
87 | |
… | |
… | |
76 | SvREFCNT_dec (req->data); |
91 | SvREFCNT_dec (req->data); |
77 | |
92 | |
78 | if (req->fh) |
93 | if (req->fh) |
79 | SvREFCNT_dec (req->fh); |
94 | SvREFCNT_dec (req->fh); |
80 | |
95 | |
81 | if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) |
96 | if (req->fh2) |
|
|
97 | SvREFCNT_dec (req->fh2); |
|
|
98 | |
|
|
99 | if (req->statdata) |
82 | Safefree (req->statdata); |
100 | Safefree (req->statdata); |
83 | |
101 | |
84 | if (req->callback) |
102 | if (req->callback) |
85 | SvREFCNT_dec (req->callback); |
103 | SvREFCNT_dec (req->callback); |
|
|
104 | |
|
|
105 | if (req->type == REQ_READDIR && req->result >= 0) |
|
|
106 | free (req->data2ptr); |
86 | |
107 | |
87 | Safefree (req); |
108 | Safefree (req); |
88 | } |
109 | } |
89 | |
110 | |
90 | static void |
111 | static void |
… | |
… | |
104 | poll_cb () |
125 | poll_cb () |
105 | { |
126 | { |
106 | dSP; |
127 | dSP; |
107 | int count = 0; |
128 | int count = 0; |
108 | int do_croak = 0; |
129 | int do_croak = 0; |
109 | aio_req req, prv; |
130 | aio_req req; |
110 | |
131 | |
|
|
132 | for (;;) |
|
|
133 | { |
111 | pthread_mutex_lock (&reslock); |
134 | pthread_mutex_lock (&reslock); |
|
|
135 | req = ress; |
112 | |
136 | |
113 | { |
137 | if (req) |
|
|
138 | { |
|
|
139 | ress = req->next; |
|
|
140 | |
|
|
141 | if (!ress) |
|
|
142 | { |
114 | /* read any signals sent by the worker threads */ |
143 | /* read any signals sent by the worker threads */ |
115 | char buf [32]; |
144 | char buf [32]; |
116 | while (read (respipe [0], buf, 32) == 32) |
145 | while (read (respipe [0], buf, 32) == 32) |
|
|
146 | ; |
|
|
147 | |
|
|
148 | rese = 0; |
|
|
149 | } |
117 | ; |
150 | } |
118 | } |
|
|
119 | |
151 | |
120 | req = ress; |
|
|
121 | ress = rese = 0; |
|
|
122 | |
|
|
123 | pthread_mutex_unlock (&reslock); |
152 | pthread_mutex_unlock (&reslock); |
124 | |
153 | |
125 | while (req) |
154 | if (!req) |
126 | { |
155 | break; |
|
|
156 | |
127 | nreqs--; |
157 | nreqs--; |
128 | |
158 | |
129 | if (req->type == REQ_QUIT) |
159 | if (req->type == REQ_QUIT) |
130 | started--; |
160 | started--; |
131 | else |
161 | else |
132 | { |
162 | { |
133 | int errorno = errno; |
163 | int errorno = errno; |
134 | errno = req->errorno; |
164 | errno = req->errorno; |
135 | |
165 | |
136 | if (req->type == REQ_READ) |
166 | if (req->type == REQ_READ) |
137 | SvCUR_set (req->data, req->dataoffset |
167 | SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); |
138 | + req->result > 0 ? req->result : 0); |
|
|
139 | |
168 | |
140 | if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) |
169 | if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE)) |
|
|
170 | SvREADONLY_off (req->data); |
|
|
171 | |
|
|
172 | if (req->statdata) |
141 | { |
173 | { |
142 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
174 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
143 | PL_laststatval = req->result; |
175 | PL_laststatval = req->result; |
144 | PL_statcache = *(req->statdata); |
176 | PL_statcache = *(req->statdata); |
145 | } |
177 | } |
146 | |
178 | |
147 | ENTER; |
179 | ENTER; |
148 | PUSHMARK (SP); |
180 | PUSHMARK (SP); |
149 | XPUSHs (sv_2mortal (newSViv (req->result))); |
|
|
150 | |
181 | |
151 | if (req->type == REQ_OPEN) |
182 | if (req->type == REQ_READDIR) |
152 | { |
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 | { |
153 | /* convert fd to fh */ |
211 | /* convert fd to fh */ |
154 | SV *fh; |
212 | SV *fh; |
155 | |
213 | |
156 | PUTBACK; |
214 | PUTBACK; |
157 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
215 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
158 | SPAGAIN; |
216 | SPAGAIN; |
159 | |
217 | |
160 | fh = SvREFCNT_inc (POPs); |
218 | fh = SvREFCNT_inc (POPs); |
161 | |
219 | |
162 | PUSHMARK (SP); |
220 | PUSHMARK (SP); |
163 | XPUSHs (sv_2mortal (fh)); |
221 | XPUSHs (sv_2mortal (fh)); |
|
|
222 | } |
164 | } |
223 | } |
165 | |
224 | |
166 | if (SvOK (req->callback)) |
225 | if (SvOK (req->callback)) |
167 | { |
226 | { |
168 | PUTBACK; |
227 | PUTBACK; |
169 | call_sv (req->callback, G_VOID | G_EVAL); |
228 | call_sv (req->callback, G_VOID | G_EVAL); |
170 | SPAGAIN; |
229 | SPAGAIN; |
|
|
230 | |
|
|
231 | if (SvTRUE (ERRSV)) |
|
|
232 | { |
|
|
233 | free_req (req); |
|
|
234 | croak (0); |
|
|
235 | } |
171 | } |
236 | } |
172 | |
237 | |
173 | LEAVE; |
238 | LEAVE; |
174 | |
239 | |
175 | do_croak = SvTRUE (ERRSV); |
|
|
176 | |
|
|
177 | errno = errorno; |
240 | errno = errorno; |
178 | count++; |
241 | count++; |
179 | } |
242 | } |
180 | |
243 | |
181 | prv = req; |
|
|
182 | req = req->next; |
|
|
183 | free_req (prv); |
244 | free_req (req); |
184 | |
|
|
185 | if (do_croak) |
|
|
186 | croak (0); |
|
|
187 | } |
245 | } |
188 | |
246 | |
189 | return count; |
247 | return count; |
190 | } |
248 | } |
191 | |
249 | |
… | |
… | |
212 | } |
270 | } |
213 | |
271 | |
214 | static void |
272 | static void |
215 | send_req (aio_req req) |
273 | send_req (aio_req req) |
216 | { |
274 | { |
|
|
275 | while (started < wanted && nreqs >= started) |
|
|
276 | start_thread (); |
|
|
277 | |
217 | nreqs++; |
278 | nreqs++; |
218 | |
279 | |
219 | pthread_mutex_lock (&reqlock); |
280 | pthread_mutex_lock (&reqlock); |
220 | |
281 | |
221 | req->next = 0; |
282 | req->next = 0; |
… | |
… | |
229 | reqe = reqs = req; |
290 | reqe = reqs = req; |
230 | |
291 | |
231 | pthread_cond_signal (&reqwait); |
292 | pthread_cond_signal (&reqwait); |
232 | pthread_mutex_unlock (&reqlock); |
293 | pthread_mutex_unlock (&reqlock); |
233 | |
294 | |
234 | while (nreqs > max_outstanding) |
295 | if (nreqs > max_outstanding) |
|
|
296 | for (;;) |
235 | { |
297 | { |
|
|
298 | poll_cb (); |
|
|
299 | |
|
|
300 | if (nreqs <= max_outstanding) |
|
|
301 | break; |
|
|
302 | |
236 | poll_wait (); |
303 | poll_wait (); |
237 | poll_cb (); |
|
|
238 | } |
304 | } |
239 | } |
305 | } |
240 | |
306 | |
241 | static void |
307 | static void |
242 | end_thread (void) |
308 | end_thread (void) |
243 | { |
309 | { |
… | |
… | |
246 | req->type = REQ_QUIT; |
312 | req->type = REQ_QUIT; |
247 | |
313 | |
248 | send_req (req); |
314 | send_req (req); |
249 | } |
315 | } |
250 | |
316 | |
251 | |
|
|
252 | static void min_parallel (int nthreads) |
317 | static void min_parallel (int nthreads) |
253 | { |
318 | { |
254 | while (nthreads > started) |
319 | if (wanted < nthreads) |
255 | start_thread (); |
320 | wanted = nthreads; |
256 | } |
321 | } |
257 | |
322 | |
258 | static void max_parallel (int nthreads) |
323 | static void max_parallel (int nthreads) |
259 | { |
324 | { |
260 | int cur = started; |
325 | int cur = started; |
|
|
326 | |
|
|
327 | if (wanted > nthreads) |
|
|
328 | wanted = nthreads; |
|
|
329 | |
261 | while (cur > nthreads) |
330 | while (cur > wanted) |
262 | { |
331 | { |
263 | end_thread (); |
332 | end_thread (); |
264 | cur--; |
333 | cur--; |
265 | } |
334 | } |
266 | |
335 | |
267 | while (started > nthreads) |
336 | while (started > wanted) |
268 | { |
337 | { |
269 | poll_wait (); |
338 | poll_wait (); |
270 | poll_cb (); |
339 | poll_cb (); |
271 | } |
340 | } |
272 | } |
341 | } |
… | |
… | |
279 | if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)) |
348 | if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)) |
280 | croak ("cannot set result pipe to nonblocking mode"); |
349 | croak ("cannot set result pipe to nonblocking mode"); |
281 | |
350 | |
282 | if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) |
351 | if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) |
283 | croak ("cannot set result pipe to nonblocking mode"); |
352 | croak ("cannot set result pipe to nonblocking mode"); |
284 | } |
|
|
285 | |
|
|
286 | static void atfork_prepare (void) |
|
|
287 | { |
|
|
288 | pthread_mutex_lock (&frklock); |
|
|
289 | pthread_mutex_lock (&reqlock); |
|
|
290 | pthread_mutex_lock (&reslock); |
|
|
291 | } |
|
|
292 | |
|
|
293 | static void atfork_parent (void) |
|
|
294 | { |
|
|
295 | pthread_mutex_unlock (&reslock); |
|
|
296 | pthread_mutex_unlock (&reqlock); |
|
|
297 | pthread_mutex_unlock (&frklock); |
|
|
298 | } |
|
|
299 | |
|
|
300 | static void atfork_child (void) |
|
|
301 | { |
|
|
302 | int restart = started; |
|
|
303 | started = 0; |
|
|
304 | |
|
|
305 | while (reqs) |
|
|
306 | { |
|
|
307 | free_req (reqs); |
|
|
308 | reqs = reqs->next; |
|
|
309 | } |
|
|
310 | |
|
|
311 | reqs = reqe = 0; |
|
|
312 | |
|
|
313 | while (ress) |
|
|
314 | { |
|
|
315 | free_req (ress); |
|
|
316 | ress = ress->next; |
|
|
317 | } |
|
|
318 | |
|
|
319 | ress = rese = 0; |
|
|
320 | |
|
|
321 | close (respipe [0]); |
|
|
322 | close (respipe [1]); |
|
|
323 | |
|
|
324 | create_pipe (); |
|
|
325 | |
|
|
326 | atfork_parent (); |
|
|
327 | |
|
|
328 | min_parallel (restart); |
|
|
329 | } |
353 | } |
330 | |
354 | |
331 | /*****************************************************************************/ |
355 | /*****************************************************************************/ |
332 | /* work around various missing functions */ |
356 | /* work around various missing functions */ |
333 | |
357 | |
… | |
… | |
338 | /* |
362 | /* |
339 | * make our pread/pwrite safe against themselves, but not against |
363 | * make our pread/pwrite safe against themselves, but not against |
340 | * 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, |
341 | * but that's your problem, not mine. |
365 | * but that's your problem, not mine. |
342 | */ |
366 | */ |
343 | static pthread_mutex_t iolock = PTHREAD_MUTEX_INITIALIZER; |
367 | static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER; |
344 | |
368 | |
345 | static ssize_t |
369 | static ssize_t |
346 | pread (int fd, void *buf, size_t count, off_t offset) |
370 | pread (int fd, void *buf, size_t count, off_t offset) |
347 | { |
371 | { |
348 | ssize_t res; |
372 | ssize_t res; |
349 | off_t ooffset; |
373 | off_t ooffset; |
350 | |
374 | |
351 | pthread_mutex_lock (&iolock); |
375 | pthread_mutex_lock (&preadwritelock); |
352 | ooffset = lseek (fd, 0, SEEK_CUR); |
376 | ooffset = lseek (fd, 0, SEEK_CUR); |
353 | lseek (fd, offset, SEEK_SET); |
377 | lseek (fd, offset, SEEK_SET); |
354 | res = read (fd, buf, count); |
378 | res = read (fd, buf, count); |
355 | lseek (fd, ooffset, SEEK_SET); |
379 | lseek (fd, ooffset, SEEK_SET); |
356 | pthread_mutex_unlock (&iolock); |
380 | pthread_mutex_unlock (&preadwritelock); |
357 | |
381 | |
358 | return res; |
382 | return res; |
359 | } |
383 | } |
360 | |
384 | |
361 | static ssize_t |
385 | static ssize_t |
362 | pwrite (int fd, void *buf, size_t count, off_t offset) |
386 | pwrite (int fd, void *buf, size_t count, off_t offset) |
363 | { |
387 | { |
364 | ssize_t res; |
388 | ssize_t res; |
365 | off_t ooffset; |
389 | off_t ooffset; |
366 | |
390 | |
367 | pthread_mutex_lock (&iolock); |
391 | pthread_mutex_lock (&preadwritelock); |
368 | ooffset = lseek (fd, 0, SEEK_CUR); |
392 | ooffset = lseek (fd, 0, SEEK_CUR); |
369 | lseek (fd, offset, SEEK_SET); |
393 | lseek (fd, offset, SEEK_SET); |
370 | res = write (fd, buf, count); |
394 | res = write (fd, buf, count); |
371 | lseek (fd, offset, SEEK_SET); |
395 | lseek (fd, offset, SEEK_SET); |
372 | pthread_mutex_unlock (&iolock); |
396 | pthread_mutex_unlock (&preadwritelock); |
373 | |
397 | |
374 | return res; |
398 | return res; |
375 | } |
399 | } |
376 | #endif |
400 | #endif |
377 | |
401 | |
… | |
… | |
380 | #endif |
404 | #endif |
381 | |
405 | |
382 | #if !HAVE_READAHEAD |
406 | #if !HAVE_READAHEAD |
383 | # define readahead aio_readahead |
407 | # define readahead aio_readahead |
384 | |
408 | |
385 | static char readahead_buf[4096]; |
|
|
386 | |
|
|
387 | static ssize_t |
409 | static ssize_t |
388 | readahead (int fd, off_t offset, size_t count) |
410 | readahead (int fd, off_t offset, size_t count) |
389 | { |
411 | { |
|
|
412 | char readahead_buf[4096]; |
|
|
413 | |
390 | while (count > 0) |
414 | while (count > 0) |
391 | { |
415 | { |
392 | size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); |
416 | size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); |
393 | |
417 | |
394 | pread (fd, readahead_buf, len, offset); |
418 | pread (fd, readahead_buf, len, offset); |
… | |
… | |
397 | } |
421 | } |
398 | |
422 | |
399 | errno = 0; |
423 | errno = 0; |
400 | } |
424 | } |
401 | #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 | # endif |
|
|
505 | #else |
|
|
506 | res = -1; |
|
|
507 | errno = ENOSYS; |
|
|
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 | while (count) |
|
|
523 | { |
|
|
524 | ssize_t cnt; |
|
|
525 | |
|
|
526 | cnt = pread (ifd, buf, count > 4096 ? 4096 : count, 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 | count -= cnt; |
|
|
545 | } |
|
|
546 | } |
|
|
547 | |
|
|
548 | return res; |
|
|
549 | } |
|
|
550 | |
|
|
551 | /* read a full directory */ |
|
|
552 | static int |
|
|
553 | scandir_ (const char *path, void **namesp) |
|
|
554 | { |
|
|
555 | DIR *dirp = opendir (path); |
|
|
556 | union |
|
|
557 | { |
|
|
558 | struct dirent d; |
|
|
559 | char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; |
|
|
560 | } u; |
|
|
561 | struct dirent *entp; |
|
|
562 | char *name, *names; |
|
|
563 | int memlen = 4096; |
|
|
564 | int memofs = 0; |
|
|
565 | int res = 0; |
|
|
566 | int errorno; |
|
|
567 | |
|
|
568 | if (!dirp) |
|
|
569 | return -1; |
|
|
570 | |
|
|
571 | names = malloc (memlen); |
|
|
572 | |
|
|
573 | for (;;) |
|
|
574 | { |
|
|
575 | errno = 0, readdir_r (dirp, &u.d, &entp); |
|
|
576 | |
|
|
577 | if (!entp) |
|
|
578 | break; |
|
|
579 | |
|
|
580 | name = entp->d_name; |
|
|
581 | |
|
|
582 | if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) |
|
|
583 | { |
|
|
584 | int len = strlen (name) + 1; |
|
|
585 | |
|
|
586 | res++; |
|
|
587 | |
|
|
588 | while (memofs + len > memlen) |
|
|
589 | { |
|
|
590 | memlen *= 2; |
|
|
591 | names = realloc (names, memlen); |
|
|
592 | if (!names) |
|
|
593 | break; |
|
|
594 | } |
|
|
595 | |
|
|
596 | memcpy (names + memofs, name, len); |
|
|
597 | memofs += len; |
|
|
598 | } |
|
|
599 | } |
|
|
600 | |
|
|
601 | errorno = errno; |
|
|
602 | closedir (dirp); |
|
|
603 | |
|
|
604 | if (errorno) |
|
|
605 | { |
|
|
606 | free (names); |
|
|
607 | errno = errorno; |
|
|
608 | res = -1; |
|
|
609 | } |
|
|
610 | |
|
|
611 | *namesp = (void *)names; |
|
|
612 | return res; |
|
|
613 | } |
402 | |
614 | |
403 | /*****************************************************************************/ |
615 | /*****************************************************************************/ |
404 | |
616 | |
405 | static void * |
617 | static void * |
406 | aio_proc (void *thr_arg) |
618 | aio_proc (void *thr_arg) |
… | |
… | |
438 | { |
650 | { |
439 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
651 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
440 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
652 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
441 | |
653 | |
442 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
654 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
|
|
655 | case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length); break; |
443 | |
656 | |
444 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
657 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
445 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
658 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
446 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
659 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
447 | |
660 | |
… | |
… | |
451 | case REQ_RMDIR: req->result = rmdir (req->dataptr); break; |
664 | case REQ_RMDIR: req->result = rmdir (req->dataptr); break; |
452 | case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; |
665 | case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; |
453 | |
666 | |
454 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
667 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
455 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
668 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
|
|
669 | case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break; |
456 | |
670 | |
457 | case REQ_QUIT: |
671 | case REQ_QUIT: |
458 | break; |
672 | break; |
459 | |
673 | |
460 | default: |
674 | default: |
… | |
… | |
484 | pthread_mutex_unlock (&reslock); |
698 | pthread_mutex_unlock (&reslock); |
485 | } |
699 | } |
486 | while (type != REQ_QUIT); |
700 | while (type != REQ_QUIT); |
487 | |
701 | |
488 | return 0; |
702 | return 0; |
|
|
703 | } |
|
|
704 | |
|
|
705 | /*****************************************************************************/ |
|
|
706 | |
|
|
707 | static void atfork_prepare (void) |
|
|
708 | { |
|
|
709 | pthread_mutex_lock (&reqlock); |
|
|
710 | pthread_mutex_lock (&reslock); |
|
|
711 | #if !HAVE_PREADWRITE |
|
|
712 | pthread_mutex_lock (&preadwritelock); |
|
|
713 | #endif |
|
|
714 | #if !HAVE_READDIR_R |
|
|
715 | pthread_mutex_lock (&readdirlock); |
|
|
716 | #endif |
|
|
717 | } |
|
|
718 | |
|
|
719 | static void atfork_parent (void) |
|
|
720 | { |
|
|
721 | #if !HAVE_READDIR_R |
|
|
722 | pthread_mutex_unlock (&readdirlock); |
|
|
723 | #endif |
|
|
724 | #if !HAVE_PREADWRITE |
|
|
725 | pthread_mutex_unlock (&preadwritelock); |
|
|
726 | #endif |
|
|
727 | pthread_mutex_unlock (&reslock); |
|
|
728 | pthread_mutex_unlock (&reqlock); |
|
|
729 | } |
|
|
730 | |
|
|
731 | static void atfork_child (void) |
|
|
732 | { |
|
|
733 | aio_req prv; |
|
|
734 | |
|
|
735 | started = 0; |
|
|
736 | |
|
|
737 | while (reqs) |
|
|
738 | { |
|
|
739 | prv = reqs; |
|
|
740 | reqs = prv->next; |
|
|
741 | free_req (prv); |
|
|
742 | } |
|
|
743 | |
|
|
744 | reqs = reqe = 0; |
|
|
745 | |
|
|
746 | while (ress) |
|
|
747 | { |
|
|
748 | prv = ress; |
|
|
749 | ress = prv->next; |
|
|
750 | free_req (prv); |
|
|
751 | } |
|
|
752 | |
|
|
753 | ress = rese = 0; |
|
|
754 | |
|
|
755 | close (respipe [0]); |
|
|
756 | close (respipe [1]); |
|
|
757 | create_pipe (); |
|
|
758 | |
|
|
759 | atfork_parent (); |
489 | } |
760 | } |
490 | |
761 | |
491 | #define dREQ \ |
762 | #define dREQ \ |
492 | aio_req req; \ |
763 | aio_req req; \ |
493 | \ |
764 | \ |
… | |
… | |
496 | \ |
767 | \ |
497 | Newz (0, req, 1, aio_cb); \ |
768 | Newz (0, req, 1, aio_cb); \ |
498 | if (!req) \ |
769 | if (!req) \ |
499 | croak ("out of memory during aio_req allocation"); \ |
770 | croak ("out of memory during aio_req allocation"); \ |
500 | \ |
771 | \ |
501 | req->callback = SvREFCNT_inc (callback); |
772 | req->callback = newSVsv (callback); |
502 | |
773 | |
503 | MODULE = IO::AIO PACKAGE = IO::AIO |
774 | MODULE = IO::AIO PACKAGE = IO::AIO |
504 | |
775 | |
505 | PROTOTYPES: ENABLE |
776 | PROTOTYPES: ENABLE |
506 | |
777 | |
… | |
… | |
570 | |
841 | |
571 | void |
842 | void |
572 | aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
843 | aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
573 | SV * fh |
844 | SV * fh |
574 | UV offset |
845 | UV offset |
575 | IV length |
846 | UV length |
576 | SV * data |
847 | SV * data |
577 | IV dataoffset |
848 | UV dataoffset |
578 | SV * callback |
849 | SV * callback |
579 | ALIAS: |
850 | ALIAS: |
580 | aio_read = REQ_READ |
851 | aio_read = REQ_READ |
581 | aio_write = REQ_WRITE |
852 | aio_write = REQ_WRITE |
582 | PROTOTYPE: $$$$$;$ |
853 | PROTOTYPE: $$$$$;$ |
… | |
… | |
619 | : IoOFP (sv_2io (fh))); |
890 | : IoOFP (sv_2io (fh))); |
620 | req->offset = offset; |
891 | req->offset = offset; |
621 | req->length = length; |
892 | req->length = length; |
622 | req->data = SvREFCNT_inc (data); |
893 | req->data = SvREFCNT_inc (data); |
623 | req->dataptr = (char *)svptr + dataoffset; |
894 | req->dataptr = (char *)svptr + dataoffset; |
624 | req->callback = SvREFCNT_inc (callback); |
895 | |
|
|
896 | if (!SvREADONLY (data)) |
|
|
897 | { |
|
|
898 | SvREADONLY_on (data); |
|
|
899 | req->data2ptr = (void *)data; |
|
|
900 | } |
625 | |
901 | |
626 | send_req (req); |
902 | send_req (req); |
627 | } |
903 | } |
|
|
904 | } |
|
|
905 | |
|
|
906 | void |
|
|
907 | aio_sendfile(out_fh,in_fh,in_offset,length,callback=&PL_sv_undef) |
|
|
908 | SV * out_fh |
|
|
909 | SV * in_fh |
|
|
910 | UV in_offset |
|
|
911 | UV length |
|
|
912 | SV * callback |
|
|
913 | PROTOTYPE: $$$$;$ |
|
|
914 | CODE: |
|
|
915 | { |
|
|
916 | dREQ; |
|
|
917 | |
|
|
918 | req->type = REQ_SENDFILE; |
|
|
919 | req->fh = newSVsv (out_fh); |
|
|
920 | req->fd = PerlIO_fileno (IoIFP (sv_2io (out_fh))); |
|
|
921 | req->fh2 = newSVsv (in_fh); |
|
|
922 | req->fd2 = PerlIO_fileno (IoIFP (sv_2io (in_fh))); |
|
|
923 | req->offset = in_offset; |
|
|
924 | req->length = length; |
|
|
925 | |
|
|
926 | send_req (req); |
628 | } |
927 | } |
629 | |
928 | |
630 | void |
929 | void |
631 | aio_readahead(fh,offset,length,callback=&PL_sv_undef) |
930 | aio_readahead(fh,offset,length,callback=&PL_sv_undef) |
632 | SV * fh |
931 | SV * fh |
… | |
… | |
658 | { |
957 | { |
659 | dREQ; |
958 | dREQ; |
660 | |
959 | |
661 | New (0, req->statdata, 1, Stat_t); |
960 | New (0, req->statdata, 1, Stat_t); |
662 | if (!req->statdata) |
961 | if (!req->statdata) |
|
|
962 | { |
|
|
963 | free_req (req); |
663 | croak ("out of memory during aio_req->statdata allocation (sorry, i just leaked memory, too)"); |
964 | croak ("out of memory during aio_req->statdata allocation"); |
|
|
965 | } |
664 | |
966 | |
665 | if (SvPOK (fh_or_path)) |
967 | if (SvPOK (fh_or_path)) |
666 | { |
968 | { |
667 | req->type = ix; |
969 | req->type = ix; |
668 | req->data = newSVsv (fh_or_path); |
970 | req->data = newSVsv (fh_or_path); |
… | |
… | |
713 | |
1015 | |
714 | send_req (req); |
1016 | send_req (req); |
715 | } |
1017 | } |
716 | |
1018 | |
717 | void |
1019 | void |
|
|
1020 | aio_readdir(pathname,callback=&PL_sv_undef) |
|
|
1021 | SV * pathname |
|
|
1022 | SV * callback |
|
|
1023 | CODE: |
|
|
1024 | { |
|
|
1025 | dREQ; |
|
|
1026 | |
|
|
1027 | req->type = REQ_READDIR; |
|
|
1028 | req->data = newSVsv (pathname); |
|
|
1029 | req->dataptr = SvPVbyte_nolen (req->data); |
|
|
1030 | |
|
|
1031 | send_req (req); |
|
|
1032 | } |
|
|
1033 | |
|
|
1034 | void |
718 | flush() |
1035 | flush() |
719 | PROTOTYPE: |
1036 | PROTOTYPE: |
720 | CODE: |
1037 | CODE: |
721 | while (nreqs) |
1038 | while (nreqs) |
722 | { |
1039 | { |