|
|
1 | #define _REENTRANT 1 |
|
|
2 | #include <errno.h> |
|
|
3 | |
1 | #include "EXTERN.h" |
4 | #include "EXTERN.h" |
2 | #include "perl.h" |
5 | #include "perl.h" |
3 | #include "XSUB.h" |
6 | #include "XSUB.h" |
4 | |
7 | |
|
|
8 | #include "autoconf/config.h" |
|
|
9 | |
|
|
10 | #include <pthread.h> |
|
|
11 | |
|
|
12 | #include <stddef.h> |
|
|
13 | #include <errno.h> |
5 | #include <sys/types.h> |
14 | #include <sys/types.h> |
6 | #include <sys/stat.h> |
15 | #include <sys/stat.h> |
|
|
16 | #include <limits.h> |
7 | #include <unistd.h> |
17 | #include <unistd.h> |
8 | #include <fcntl.h> |
18 | #include <fcntl.h> |
9 | #include <signal.h> |
19 | #include <signal.h> |
10 | #include <sched.h> |
20 | #include <sched.h> |
11 | #include <endian.h> |
|
|
12 | |
21 | |
13 | #include <pthread.h> |
22 | #if HAVE_SENDFILE |
|
|
23 | # if __linux |
|
|
24 | # include <sys/sendfile.h> |
|
|
25 | # elif __freebsd |
14 | #include <sys/syscall.h> |
26 | # include <sys/socket.h> |
|
|
27 | # include <sys/uio.h> |
|
|
28 | # elif __hpux |
|
|
29 | # include <sys/socket.h> |
|
|
30 | # elif __solaris /* not yet */ |
|
|
31 | # include <sys/sendfile.h> |
|
|
32 | # else |
|
|
33 | # error sendfile support requested but not available |
|
|
34 | # endif |
|
|
35 | #endif |
15 | |
36 | |
16 | typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
37 | /* used for struct dirent, AIX doesn't provide it */ |
17 | typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
38 | #ifndef NAME_MAX |
18 | typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
39 | # define NAME_MAX 4096 |
|
|
40 | #endif |
19 | |
41 | |
20 | #if __ia64 |
42 | #if __ia64 |
21 | # define STACKSIZE 65536 |
43 | # define STACKSIZE 65536 |
22 | #else |
44 | #else |
23 | # define STACKSIZE 4096 |
45 | # define STACKSIZE 8192 |
24 | #endif |
46 | #endif |
25 | |
47 | |
26 | enum { |
48 | enum { |
27 | REQ_QUIT, |
49 | REQ_QUIT, |
28 | REQ_OPEN, REQ_CLOSE, |
50 | REQ_OPEN, REQ_CLOSE, |
29 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
51 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
|
|
52 | REQ_SENDFILE, |
30 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_UNLINK, |
53 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
31 | REQ_FSYNC, REQ_FDATASYNC, |
54 | REQ_FSYNC, REQ_FDATASYNC, |
|
|
55 | REQ_UNLINK, REQ_RMDIR, REQ_RENAME, |
|
|
56 | REQ_READDIR, |
|
|
57 | REQ_LINK, REQ_SYMLINK, |
32 | }; |
58 | }; |
33 | |
59 | |
34 | typedef struct aio_cb { |
60 | typedef struct aio_cb { |
35 | struct aio_cb *volatile next; |
61 | struct aio_cb *volatile next; |
36 | |
62 | |
37 | int type; |
63 | int type; |
38 | |
64 | |
|
|
65 | /* should receive a cleanup, with unions */ |
39 | int fd; |
66 | int fd, fd2; |
40 | off_t offset; |
67 | off_t offset; |
41 | size_t length; |
68 | size_t length; |
42 | ssize_t result; |
69 | ssize_t result; |
43 | mode_t mode; /* open */ |
70 | mode_t mode; /* open */ |
44 | int errorno; |
71 | int errorno; |
45 | SV *data, *callback; |
72 | SV *data, *callback; |
46 | void *dataptr; |
73 | SV *fh, *fh2; |
|
|
74 | void *dataptr, *data2ptr; |
47 | STRLEN dataoffset; |
75 | STRLEN dataoffset; |
48 | |
76 | |
49 | Stat_t *statdata; |
77 | Stat_t *statdata; |
50 | } aio_cb; |
78 | } aio_cb; |
51 | |
79 | |
52 | typedef aio_cb *aio_req; |
80 | typedef aio_cb *aio_req; |
53 | |
81 | |
54 | static int started; |
82 | static int started, wanted; |
55 | static int nreqs; |
83 | static volatile int nreqs; |
56 | static int max_outstanding = 1<<30; |
84 | static int max_outstanding = 1<<30; |
57 | static int respipe [2]; |
85 | static int respipe [2]; |
58 | |
86 | |
59 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
87 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
60 | static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; |
88 | static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; |
61 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
89 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
62 | |
90 | |
63 | static volatile aio_req reqs, reqe; /* queue start, queue end */ |
91 | static volatile aio_req reqs, reqe; /* queue start, queue end */ |
64 | static volatile aio_req ress, rese; /* queue start, queue end */ |
92 | static volatile aio_req ress, rese; /* queue start, queue end */ |
65 | |
93 | |
|
|
94 | static void free_req (aio_req req) |
|
|
95 | { |
|
|
96 | if (req->data) |
|
|
97 | SvREFCNT_dec (req->data); |
|
|
98 | |
|
|
99 | if (req->fh) |
|
|
100 | SvREFCNT_dec (req->fh); |
|
|
101 | |
|
|
102 | if (req->fh2) |
|
|
103 | SvREFCNT_dec (req->fh2); |
|
|
104 | |
|
|
105 | if (req->statdata) |
|
|
106 | Safefree (req->statdata); |
|
|
107 | |
|
|
108 | if (req->callback) |
|
|
109 | SvREFCNT_dec (req->callback); |
|
|
110 | |
|
|
111 | if (req->type == REQ_READDIR && req->result >= 0) |
|
|
112 | free (req->data2ptr); |
|
|
113 | |
|
|
114 | Safefree (req); |
|
|
115 | } |
|
|
116 | |
66 | static void |
117 | static void |
67 | poll_wait () |
118 | poll_wait () |
68 | { |
119 | { |
69 | if (!nreqs) |
120 | if (nreqs && !ress) |
70 | return; |
121 | { |
71 | |
|
|
72 | fd_set rfd; |
122 | fd_set rfd; |
73 | FD_ZERO(&rfd); |
123 | FD_ZERO(&rfd); |
74 | FD_SET(respipe [0], &rfd); |
124 | FD_SET(respipe [0], &rfd); |
75 | |
125 | |
76 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
126 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
|
|
127 | } |
77 | } |
128 | } |
78 | |
129 | |
79 | static int |
130 | static int |
80 | poll_cb () |
131 | poll_cb () |
81 | { |
132 | { |
82 | dSP; |
133 | dSP; |
83 | int count = 0; |
134 | int count = 0; |
|
|
135 | int do_croak = 0; |
84 | aio_req req; |
136 | aio_req req; |
85 | |
|
|
86 | { |
|
|
87 | /* read and signals sent by the worker threads */ |
|
|
88 | char buf [32]; |
|
|
89 | while (read (respipe [0], buf, 32) > 0) |
|
|
90 | ; |
|
|
91 | } |
|
|
92 | |
137 | |
93 | for (;;) |
138 | for (;;) |
94 | { |
139 | { |
95 | pthread_mutex_lock (&reslock); |
140 | pthread_mutex_lock (&reslock); |
96 | |
|
|
97 | req = ress; |
141 | req = ress; |
98 | |
142 | |
99 | if (ress) |
143 | if (req) |
100 | { |
144 | { |
101 | ress = ress->next; |
145 | ress = req->next; |
|
|
146 | |
102 | if (!ress) rese = 0; |
147 | if (!ress) |
|
|
148 | { |
|
|
149 | /* read any signals sent by the worker threads */ |
|
|
150 | char buf [32]; |
|
|
151 | while (read (respipe [0], buf, 32) == 32) |
|
|
152 | ; |
|
|
153 | |
|
|
154 | rese = 0; |
|
|
155 | } |
103 | } |
156 | } |
104 | |
157 | |
105 | pthread_mutex_unlock (&reslock); |
158 | pthread_mutex_unlock (&reslock); |
106 | |
159 | |
107 | if (!req) |
160 | if (!req) |
… | |
… | |
115 | { |
168 | { |
116 | int errorno = errno; |
169 | int errorno = errno; |
117 | errno = req->errorno; |
170 | errno = req->errorno; |
118 | |
171 | |
119 | if (req->type == REQ_READ) |
172 | if (req->type == REQ_READ) |
120 | SvCUR_set (req->data, req->dataoffset |
173 | SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); |
121 | + req->result > 0 ? req->result : 0); |
|
|
122 | |
174 | |
|
|
175 | if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE)) |
|
|
176 | SvREADONLY_off (req->data); |
|
|
177 | |
123 | if (req->data) |
178 | if (req->statdata) |
124 | SvREFCNT_dec (req->data); |
|
|
125 | |
|
|
126 | if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) |
|
|
127 | { |
179 | { |
128 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
180 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
129 | PL_laststatval = req->result; |
181 | PL_laststatval = req->result; |
130 | PL_statcache = *(req->statdata); |
182 | PL_statcache = *(req->statdata); |
131 | |
|
|
132 | Safefree (req->statdata); |
|
|
133 | } |
183 | } |
134 | |
184 | |
|
|
185 | ENTER; |
135 | PUSHMARK (SP); |
186 | PUSHMARK (SP); |
136 | XPUSHs (sv_2mortal (newSViv (req->result))); |
|
|
137 | |
187 | |
138 | if (req->type == REQ_OPEN) |
188 | if (req->type == REQ_READDIR) |
139 | { |
189 | { |
|
|
190 | SV *rv = &PL_sv_undef; |
|
|
191 | |
|
|
192 | if (req->result >= 0) |
|
|
193 | { |
|
|
194 | char *buf = req->data2ptr; |
|
|
195 | AV *av = newAV (); |
|
|
196 | |
|
|
197 | while (req->result) |
|
|
198 | { |
|
|
199 | SV *sv = newSVpv (buf, 0); |
|
|
200 | |
|
|
201 | av_push (av, sv); |
|
|
202 | buf += SvCUR (sv) + 1; |
|
|
203 | req->result--; |
|
|
204 | } |
|
|
205 | |
|
|
206 | rv = sv_2mortal (newRV_noinc ((SV *)av)); |
|
|
207 | } |
|
|
208 | |
|
|
209 | XPUSHs (rv); |
|
|
210 | } |
|
|
211 | else |
|
|
212 | { |
|
|
213 | XPUSHs (sv_2mortal (newSViv (req->result))); |
|
|
214 | |
|
|
215 | if (req->type == REQ_OPEN) |
|
|
216 | { |
140 | /* convert fd to fh */ |
217 | /* convert fd to fh */ |
141 | SV *fh; |
218 | SV *fh; |
142 | |
219 | |
|
|
220 | PUTBACK; |
|
|
221 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
|
|
222 | SPAGAIN; |
|
|
223 | |
|
|
224 | fh = SvREFCNT_inc (POPs); |
|
|
225 | |
|
|
226 | PUSHMARK (SP); |
|
|
227 | XPUSHs (sv_2mortal (fh)); |
|
|
228 | } |
|
|
229 | } |
|
|
230 | |
|
|
231 | if (SvOK (req->callback)) |
|
|
232 | { |
143 | PUTBACK; |
233 | PUTBACK; |
144 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
234 | call_sv (req->callback, G_VOID | G_EVAL); |
145 | SPAGAIN; |
235 | SPAGAIN; |
146 | |
236 | |
147 | fh = POPs; |
237 | if (SvTRUE (ERRSV)) |
148 | |
238 | { |
149 | PUSHMARK (SP); |
239 | free_req (req); |
150 | XPUSHs (fh); |
240 | croak (0); |
|
|
241 | } |
151 | } |
242 | } |
152 | |
243 | |
153 | PUTBACK; |
|
|
154 | call_sv (req->callback, G_VOID | G_EVAL); |
|
|
155 | SPAGAIN; |
|
|
156 | |
244 | LEAVE; |
157 | if (req->callback) |
|
|
158 | SvREFCNT_dec (req->callback); |
|
|
159 | |
245 | |
160 | errno = errorno; |
246 | errno = errorno; |
161 | count++; |
247 | count++; |
162 | } |
248 | } |
163 | |
249 | |
164 | Safefree (req); |
250 | free_req (req); |
165 | } |
251 | } |
166 | |
252 | |
167 | return count; |
253 | return count; |
168 | } |
254 | } |
169 | |
255 | |
… | |
… | |
190 | } |
276 | } |
191 | |
277 | |
192 | static void |
278 | static void |
193 | send_req (aio_req req) |
279 | send_req (aio_req req) |
194 | { |
280 | { |
|
|
281 | while (started < wanted && nreqs >= started) |
|
|
282 | start_thread (); |
|
|
283 | |
195 | nreqs++; |
284 | nreqs++; |
196 | |
285 | |
197 | pthread_mutex_lock (&reqlock); |
286 | pthread_mutex_lock (&reqlock); |
198 | |
287 | |
199 | req->next = 0; |
288 | req->next = 0; |
… | |
… | |
207 | reqe = reqs = req; |
296 | reqe = reqs = req; |
208 | |
297 | |
209 | pthread_cond_signal (&reqwait); |
298 | pthread_cond_signal (&reqwait); |
210 | pthread_mutex_unlock (&reqlock); |
299 | pthread_mutex_unlock (&reqlock); |
211 | |
300 | |
212 | while (nreqs > max_outstanding) |
301 | if (nreqs > max_outstanding) |
|
|
302 | for (;;) |
|
|
303 | { |
|
|
304 | poll_cb (); |
|
|
305 | |
|
|
306 | if (nreqs <= max_outstanding) |
|
|
307 | break; |
|
|
308 | |
|
|
309 | poll_wait (); |
|
|
310 | } |
|
|
311 | } |
|
|
312 | |
|
|
313 | static void |
|
|
314 | end_thread (void) |
|
|
315 | { |
|
|
316 | aio_req req; |
|
|
317 | Newz (0, req, 1, aio_cb); |
|
|
318 | req->type = REQ_QUIT; |
|
|
319 | |
|
|
320 | send_req (req); |
|
|
321 | } |
|
|
322 | |
|
|
323 | static void min_parallel (int nthreads) |
|
|
324 | { |
|
|
325 | if (wanted < nthreads) |
|
|
326 | wanted = nthreads; |
|
|
327 | } |
|
|
328 | |
|
|
329 | static void max_parallel (int nthreads) |
|
|
330 | { |
|
|
331 | int cur = started; |
|
|
332 | |
|
|
333 | if (wanted > nthreads) |
|
|
334 | wanted = nthreads; |
|
|
335 | |
|
|
336 | while (cur > wanted) |
|
|
337 | { |
|
|
338 | end_thread (); |
|
|
339 | cur--; |
|
|
340 | } |
|
|
341 | |
|
|
342 | while (started > wanted) |
213 | { |
343 | { |
214 | poll_wait (); |
344 | poll_wait (); |
215 | poll_cb (); |
345 | poll_cb (); |
216 | } |
346 | } |
217 | } |
347 | } |
218 | |
348 | |
219 | static void |
349 | static void create_pipe () |
220 | end_thread (void) |
|
|
221 | { |
350 | { |
222 | aio_req req; |
351 | if (pipe (respipe)) |
223 | New (0, req, 1, aio_cb); |
352 | croak ("unable to initialize result pipe"); |
224 | req->type = REQ_QUIT; |
|
|
225 | |
353 | |
226 | send_req (req); |
354 | if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)) |
227 | } |
355 | croak ("cannot set result pipe to nonblocking mode"); |
228 | |
356 | |
229 | static void |
357 | if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) |
230 | read_write (int dowrite, int fd, off_t offset, size_t length, |
358 | croak ("cannot set result pipe to nonblocking mode"); |
231 | SV *data, STRLEN dataoffset, SV *callback) |
359 | } |
232 | { |
|
|
233 | aio_req req; |
|
|
234 | STRLEN svlen; |
|
|
235 | char *svptr = SvPV (data, svlen); |
|
|
236 | |
360 | |
237 | SvUPGRADE (data, SVt_PV); |
361 | /*****************************************************************************/ |
238 | SvPOK_on (data); |
362 | /* work around various missing functions */ |
239 | |
363 | |
240 | if (dataoffset < 0) |
364 | #if !HAVE_PREADWRITE |
|
|
365 | # define pread aio_pread |
|
|
366 | # define pwrite aio_pwrite |
|
|
367 | |
|
|
368 | /* |
|
|
369 | * make our pread/pwrite safe against themselves, but not against |
|
|
370 | * normal read/write by using a mutex. slows down execution a lot, |
|
|
371 | * but that's your problem, not mine. |
|
|
372 | */ |
|
|
373 | static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER; |
|
|
374 | |
|
|
375 | static ssize_t |
|
|
376 | pread (int fd, void *buf, size_t count, off_t offset) |
|
|
377 | { |
|
|
378 | ssize_t res; |
|
|
379 | off_t ooffset; |
|
|
380 | |
|
|
381 | pthread_mutex_lock (&preadwritelock); |
|
|
382 | ooffset = lseek (fd, 0, SEEK_CUR); |
|
|
383 | lseek (fd, offset, SEEK_SET); |
|
|
384 | res = read (fd, buf, count); |
|
|
385 | lseek (fd, ooffset, SEEK_SET); |
|
|
386 | pthread_mutex_unlock (&preadwritelock); |
|
|
387 | |
|
|
388 | return res; |
|
|
389 | } |
|
|
390 | |
|
|
391 | static ssize_t |
|
|
392 | pwrite (int fd, void *buf, size_t count, off_t offset) |
|
|
393 | { |
|
|
394 | ssize_t res; |
|
|
395 | off_t ooffset; |
|
|
396 | |
|
|
397 | pthread_mutex_lock (&preadwritelock); |
|
|
398 | ooffset = lseek (fd, 0, SEEK_CUR); |
|
|
399 | lseek (fd, offset, SEEK_SET); |
|
|
400 | res = write (fd, buf, count); |
|
|
401 | lseek (fd, offset, SEEK_SET); |
|
|
402 | pthread_mutex_unlock (&preadwritelock); |
|
|
403 | |
|
|
404 | return res; |
|
|
405 | } |
|
|
406 | #endif |
|
|
407 | |
|
|
408 | #if !HAVE_FDATASYNC |
|
|
409 | # define fdatasync fsync |
|
|
410 | #endif |
|
|
411 | |
|
|
412 | #if !HAVE_READAHEAD |
|
|
413 | # define readahead aio_readahead |
|
|
414 | |
|
|
415 | static ssize_t |
|
|
416 | readahead (int fd, off_t offset, size_t count) |
|
|
417 | { |
|
|
418 | char readahead_buf[4096]; |
|
|
419 | |
|
|
420 | while (count > 0) |
|
|
421 | { |
|
|
422 | size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); |
|
|
423 | |
|
|
424 | pread (fd, readahead_buf, len, offset); |
241 | dataoffset += svlen; |
425 | offset += len; |
242 | |
426 | count -= len; |
243 | if (dataoffset < 0 || dataoffset > svlen) |
|
|
244 | croak ("data offset outside of string"); |
|
|
245 | |
|
|
246 | if (dowrite) |
|
|
247 | { |
427 | } |
248 | /* write: check length and adjust. */ |
428 | |
249 | if (length < 0 || length + dataoffset > svlen) |
429 | errno = 0; |
250 | length = svlen - dataoffset; |
430 | } |
|
|
431 | #endif |
|
|
432 | |
|
|
433 | #if !HAVE_READDIR_R |
|
|
434 | # define readdir_r aio_readdir_r |
|
|
435 | |
|
|
436 | static pthread_mutex_t readdirlock = PTHREAD_MUTEX_INITIALIZER; |
|
|
437 | |
|
|
438 | static int |
|
|
439 | readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) |
|
|
440 | { |
|
|
441 | struct dirent *e; |
|
|
442 | int errorno; |
|
|
443 | |
|
|
444 | pthread_mutex_lock (&readdirlock); |
|
|
445 | |
|
|
446 | e = readdir (dirp); |
|
|
447 | errorno = errno; |
|
|
448 | |
|
|
449 | if (e) |
|
|
450 | { |
|
|
451 | *res = ent; |
|
|
452 | strcpy (ent->d_name, e->d_name); |
251 | } |
453 | } |
252 | else |
454 | else |
|
|
455 | *res = 0; |
|
|
456 | |
|
|
457 | pthread_mutex_unlock (&readdirlock); |
|
|
458 | |
|
|
459 | errno = errorno; |
|
|
460 | return e ? 0 : -1; |
|
|
461 | } |
|
|
462 | #endif |
|
|
463 | |
|
|
464 | /* sendfile always needs emulation */ |
|
|
465 | static ssize_t |
|
|
466 | sendfile_ (int ofd, int ifd, off_t offset, size_t count) |
|
|
467 | { |
|
|
468 | ssize_t res; |
|
|
469 | |
|
|
470 | if (!count) |
|
|
471 | return 0; |
|
|
472 | |
|
|
473 | #if HAVE_SENDFILE |
|
|
474 | # if __linux |
|
|
475 | res = sendfile (ofd, ifd, &offset, count); |
|
|
476 | |
|
|
477 | # elif __freebsd |
|
|
478 | /* |
|
|
479 | * Of course, the freebsd sendfile is a dire hack with no thoughts |
|
|
480 | * wasted on making it similar to other I/O functions. |
|
|
481 | */ |
253 | { |
482 | { |
254 | /* read: grow scalar as necessary */ |
483 | off_t sbytes; |
255 | svptr = SvGROW (data, length + dataoffset); |
484 | res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); |
|
|
485 | |
|
|
486 | if (res < 0 && sbytes) |
|
|
487 | /* maybe only on EAGAIN only: as usual, the manpage leaves you guessing */ |
|
|
488 | res = sbytes; |
|
|
489 | } |
|
|
490 | |
|
|
491 | # elif __hpux |
|
|
492 | res = sendfile (ofd, ifd, offset, count, 0, 0); |
|
|
493 | |
|
|
494 | # elif __solaris |
|
|
495 | { |
|
|
496 | struct sendfilevec vec; |
|
|
497 | size_t sbytes; |
|
|
498 | |
|
|
499 | vec.sfv_fd = ifd; |
|
|
500 | vec.sfv_flag = 0; |
|
|
501 | vec.sfv_off = offset; |
|
|
502 | vec.sfv_len = count; |
|
|
503 | |
|
|
504 | res = sendfilev (ofd, &vec, 1, &sbytes); |
|
|
505 | |
|
|
506 | if (res < 0 && sbytes) |
|
|
507 | res = sbytes; |
|
|
508 | } |
|
|
509 | |
|
|
510 | # endif |
|
|
511 | #else |
|
|
512 | res = -1; |
|
|
513 | errno = ENOSYS; |
|
|
514 | #endif |
|
|
515 | |
|
|
516 | if (res < 0 |
|
|
517 | && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK |
|
|
518 | #if __solaris |
|
|
519 | || errno == EAFNOSUPPORT || errno == EPROTOTYPE |
|
|
520 | #endif |
|
|
521 | ) |
|
|
522 | ) |
256 | } |
523 | { |
|
|
524 | /* emulate sendfile. this is a major pain in the ass */ |
|
|
525 | char buf[4096]; |
|
|
526 | res = 0; |
257 | |
527 | |
258 | if (length < 0) |
528 | while (count) |
259 | croak ("length must not be negative"); |
529 | { |
|
|
530 | ssize_t cnt; |
|
|
531 | |
|
|
532 | cnt = pread (ifd, buf, count > 4096 ? 4096 : count, offset); |
260 | |
533 | |
261 | Newz (0, req, 1, aio_cb); |
534 | if (cnt <= 0) |
|
|
535 | { |
|
|
536 | if (cnt && !res) res = -1; |
|
|
537 | break; |
|
|
538 | } |
262 | |
539 | |
|
|
540 | cnt = write (ofd, buf, cnt); |
|
|
541 | |
|
|
542 | if (cnt <= 0) |
|
|
543 | { |
|
|
544 | if (cnt && !res) res = -1; |
|
|
545 | break; |
|
|
546 | } |
|
|
547 | |
|
|
548 | offset += cnt; |
|
|
549 | res += cnt; |
|
|
550 | count -= cnt; |
|
|
551 | } |
|
|
552 | } |
|
|
553 | |
|
|
554 | return res; |
|
|
555 | } |
|
|
556 | |
|
|
557 | /* read a full directory */ |
|
|
558 | static int |
|
|
559 | scandir_ (const char *path, void **namesp) |
|
|
560 | { |
|
|
561 | DIR *dirp = opendir (path); |
|
|
562 | union |
|
|
563 | { |
|
|
564 | struct dirent d; |
|
|
565 | char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; |
|
|
566 | } u; |
|
|
567 | struct dirent *entp; |
|
|
568 | char *name, *names; |
|
|
569 | int memlen = 4096; |
|
|
570 | int memofs = 0; |
|
|
571 | int res = 0; |
|
|
572 | int errorno; |
|
|
573 | |
263 | if (!req) |
574 | if (!dirp) |
264 | croak ("out of memory during aio_req allocation"); |
575 | return -1; |
265 | |
576 | |
266 | req->type = dowrite ? REQ_WRITE : REQ_READ; |
577 | names = malloc (memlen); |
267 | req->fd = fd; |
|
|
268 | req->offset = offset; |
|
|
269 | req->length = length; |
|
|
270 | req->data = SvREFCNT_inc (data); |
|
|
271 | req->dataptr = (char *)svptr + dataoffset; |
|
|
272 | req->callback = SvREFCNT_inc (callback); |
|
|
273 | |
578 | |
274 | send_req (req); |
579 | for (;;) |
|
|
580 | { |
|
|
581 | errno = 0, readdir_r (dirp, &u.d, &entp); |
|
|
582 | |
|
|
583 | if (!entp) |
|
|
584 | break; |
|
|
585 | |
|
|
586 | name = entp->d_name; |
|
|
587 | |
|
|
588 | if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) |
|
|
589 | { |
|
|
590 | int len = strlen (name) + 1; |
|
|
591 | |
|
|
592 | res++; |
|
|
593 | |
|
|
594 | while (memofs + len > memlen) |
|
|
595 | { |
|
|
596 | memlen *= 2; |
|
|
597 | names = realloc (names, memlen); |
|
|
598 | if (!names) |
|
|
599 | break; |
|
|
600 | } |
|
|
601 | |
|
|
602 | memcpy (names + memofs, name, len); |
|
|
603 | memofs += len; |
|
|
604 | } |
|
|
605 | } |
|
|
606 | |
|
|
607 | errorno = errno; |
|
|
608 | closedir (dirp); |
|
|
609 | |
|
|
610 | if (errorno) |
|
|
611 | { |
|
|
612 | free (names); |
|
|
613 | errno = errorno; |
|
|
614 | res = -1; |
|
|
615 | } |
|
|
616 | |
|
|
617 | *namesp = (void *)names; |
|
|
618 | return res; |
275 | } |
619 | } |
|
|
620 | |
|
|
621 | /*****************************************************************************/ |
276 | |
622 | |
277 | static void * |
623 | static void * |
278 | aio_proc (void *thr_arg) |
624 | aio_proc (void *thr_arg) |
279 | { |
625 | { |
280 | aio_req req; |
626 | aio_req req; |
… | |
… | |
306 | |
652 | |
307 | type = req->type; |
653 | type = req->type; |
308 | |
654 | |
309 | switch (type) |
655 | switch (type) |
310 | { |
656 | { |
311 | case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, req->offset); break; |
657 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
312 | case REQ_WRITE: req->result = pwrite64 (req->fd, req->dataptr, req->length, req->offset); break; |
658 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
313 | #if SYS_readahead |
659 | |
314 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
660 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
315 | #else |
661 | case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length); break; |
316 | case REQ_READAHEAD: req->result = -1; errno = ENOSYS; break; |
|
|
317 | #endif |
|
|
318 | |
662 | |
319 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
663 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
320 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
664 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
321 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
665 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
322 | |
666 | |
323 | case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
667 | case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
324 | case REQ_CLOSE: req->result = close (req->fd); break; |
668 | case REQ_CLOSE: req->result = close (req->fd); break; |
325 | case REQ_UNLINK: req->result = unlink (req->dataptr); break; |
669 | case REQ_UNLINK: req->result = unlink (req->dataptr); break; |
|
|
670 | case REQ_RMDIR: req->result = rmdir (req->dataptr); break; |
|
|
671 | case REQ_RENAME: req->result = rename (req->data2ptr, req->dataptr); break; |
|
|
672 | case REQ_LINK: req->result = link (req->data2ptr, req->dataptr); break; |
|
|
673 | case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; |
326 | |
674 | |
|
|
675 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
327 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
676 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
328 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
677 | case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break; |
329 | |
678 | |
330 | case REQ_QUIT: |
679 | case REQ_QUIT: |
331 | break; |
680 | break; |
332 | |
681 | |
333 | default: |
682 | default: |
… | |
… | |
359 | while (type != REQ_QUIT); |
708 | while (type != REQ_QUIT); |
360 | |
709 | |
361 | return 0; |
710 | return 0; |
362 | } |
711 | } |
363 | |
712 | |
|
|
713 | /*****************************************************************************/ |
|
|
714 | |
|
|
715 | static void atfork_prepare (void) |
|
|
716 | { |
|
|
717 | pthread_mutex_lock (&reqlock); |
|
|
718 | pthread_mutex_lock (&reslock); |
|
|
719 | #if !HAVE_PREADWRITE |
|
|
720 | pthread_mutex_lock (&preadwritelock); |
|
|
721 | #endif |
|
|
722 | #if !HAVE_READDIR_R |
|
|
723 | pthread_mutex_lock (&readdirlock); |
|
|
724 | #endif |
|
|
725 | } |
|
|
726 | |
|
|
727 | static void atfork_parent (void) |
|
|
728 | { |
|
|
729 | #if !HAVE_READDIR_R |
|
|
730 | pthread_mutex_unlock (&readdirlock); |
|
|
731 | #endif |
|
|
732 | #if !HAVE_PREADWRITE |
|
|
733 | pthread_mutex_unlock (&preadwritelock); |
|
|
734 | #endif |
|
|
735 | pthread_mutex_unlock (&reslock); |
|
|
736 | pthread_mutex_unlock (&reqlock); |
|
|
737 | } |
|
|
738 | |
|
|
739 | static void atfork_child (void) |
|
|
740 | { |
|
|
741 | aio_req prv; |
|
|
742 | |
|
|
743 | started = 0; |
|
|
744 | |
|
|
745 | while (reqs) |
|
|
746 | { |
|
|
747 | prv = reqs; |
|
|
748 | reqs = prv->next; |
|
|
749 | free_req (prv); |
|
|
750 | } |
|
|
751 | |
|
|
752 | reqs = reqe = 0; |
|
|
753 | |
|
|
754 | while (ress) |
|
|
755 | { |
|
|
756 | prv = ress; |
|
|
757 | ress = prv->next; |
|
|
758 | free_req (prv); |
|
|
759 | } |
|
|
760 | |
|
|
761 | ress = rese = 0; |
|
|
762 | |
|
|
763 | close (respipe [0]); |
|
|
764 | close (respipe [1]); |
|
|
765 | create_pipe (); |
|
|
766 | |
|
|
767 | atfork_parent (); |
|
|
768 | } |
|
|
769 | |
|
|
770 | #define dREQ \ |
|
|
771 | aio_req req; \ |
|
|
772 | \ |
|
|
773 | if (SvOK (callback) && !SvROK (callback)) \ |
|
|
774 | croak ("clalback must be undef or of reference type"); \ |
|
|
775 | \ |
|
|
776 | Newz (0, req, 1, aio_cb); \ |
|
|
777 | if (!req) \ |
|
|
778 | croak ("out of memory during aio_req allocation"); \ |
|
|
779 | \ |
|
|
780 | req->callback = newSVsv (callback); |
|
|
781 | |
364 | MODULE = IO::AIO PACKAGE = IO::AIO |
782 | MODULE = IO::AIO PACKAGE = IO::AIO |
365 | |
783 | |
|
|
784 | PROTOTYPES: ENABLE |
|
|
785 | |
366 | BOOT: |
786 | BOOT: |
367 | { |
787 | { |
368 | if (pipe (respipe)) |
788 | HV *stash = gv_stashpv ("IO::AIO", 1); |
369 | croak ("unable to initialize result pipe"); |
789 | newCONSTSUB (stash, "EXDEV", newSViv (EXDEV)); |
|
|
790 | newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY)); |
|
|
791 | newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY)); |
370 | |
792 | |
371 | if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)) |
793 | create_pipe (); |
372 | croak ("cannot set result pipe to nonblocking mode"); |
794 | pthread_atfork (atfork_prepare, atfork_parent, atfork_child); |
373 | |
|
|
374 | if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) |
|
|
375 | croak ("cannot set result pipe to nonblocking mode"); |
|
|
376 | } |
795 | } |
377 | |
796 | |
378 | void |
797 | void |
379 | min_parallel(nthreads) |
798 | min_parallel (nthreads) |
380 | int nthreads |
799 | int nthreads |
381 | PROTOTYPE: $ |
800 | PROTOTYPE: $ |
382 | CODE: |
|
|
383 | while (nthreads > started) |
|
|
384 | start_thread (); |
|
|
385 | |
801 | |
386 | void |
802 | void |
387 | max_parallel(nthreads) |
803 | max_parallel (nthreads) |
388 | int nthreads |
804 | int nthreads |
389 | PROTOTYPE: $ |
805 | PROTOTYPE: $ |
390 | CODE: |
|
|
391 | { |
|
|
392 | int cur = started; |
|
|
393 | while (cur > nthreads) |
|
|
394 | { |
|
|
395 | end_thread (); |
|
|
396 | cur--; |
|
|
397 | } |
|
|
398 | |
|
|
399 | while (started > nthreads) |
|
|
400 | { |
|
|
401 | poll_wait (); |
|
|
402 | poll_cb (); |
|
|
403 | } |
|
|
404 | } |
|
|
405 | |
806 | |
406 | int |
807 | int |
407 | max_outstanding(nreqs) |
808 | max_outstanding (nreqs) |
408 | int nreqs |
809 | int nreqs |
409 | PROTOTYPE: $ |
810 | PROTOTYPE: $ |
410 | CODE: |
811 | CODE: |
411 | RETVAL = max_outstanding; |
812 | RETVAL = max_outstanding; |
412 | max_outstanding = nreqs; |
813 | max_outstanding = nreqs; |
413 | |
814 | |
414 | void |
815 | void |
415 | aio_open(pathname,flags,mode,callback) |
816 | aio_open (pathname,flags,mode,callback=&PL_sv_undef) |
416 | SV * pathname |
817 | SV * pathname |
417 | int flags |
818 | int flags |
418 | int mode |
819 | int mode |
419 | SV * callback |
820 | SV * callback |
420 | PROTOTYPE: $$$$ |
821 | PROTOTYPE: $$$;$ |
421 | CODE: |
822 | CODE: |
422 | { |
823 | { |
423 | aio_req req; |
824 | dREQ; |
424 | |
|
|
425 | Newz (0, req, 1, aio_cb); |
|
|
426 | |
|
|
427 | if (!req) |
|
|
428 | croak ("out of memory during aio_req allocation"); |
|
|
429 | |
825 | |
430 | req->type = REQ_OPEN; |
826 | req->type = REQ_OPEN; |
431 | req->data = newSVsv (pathname); |
827 | req->data = newSVsv (pathname); |
432 | req->dataptr = SvPV_nolen (req->data); |
828 | req->dataptr = SvPVbyte_nolen (req->data); |
433 | req->fd = flags; |
829 | req->fd = flags; |
434 | req->mode = mode; |
830 | req->mode = mode; |
435 | req->callback = SvREFCNT_inc (callback); |
|
|
436 | |
831 | |
437 | send_req (req); |
832 | send_req (req); |
438 | } |
833 | } |
439 | |
834 | |
440 | void |
835 | void |
441 | aio_close(fh,callback) |
836 | aio_close (fh,callback=&PL_sv_undef) |
442 | InputStream fh |
837 | SV * fh |
443 | SV * callback |
838 | SV * callback |
444 | PROTOTYPE: $$ |
839 | PROTOTYPE: $;$ |
445 | ALIAS: |
840 | ALIAS: |
446 | aio_close = REQ_CLOSE |
841 | aio_close = REQ_CLOSE |
447 | aio_fsync = REQ_FSYNC |
842 | aio_fsync = REQ_FSYNC |
448 | aio_fdatasync = REQ_FDATASYNC |
843 | aio_fdatasync = REQ_FDATASYNC |
449 | CODE: |
844 | CODE: |
450 | { |
845 | { |
|
|
846 | dREQ; |
|
|
847 | |
|
|
848 | req->type = ix; |
|
|
849 | req->fh = newSVsv (fh); |
|
|
850 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
|
|
851 | |
|
|
852 | send_req (req); |
|
|
853 | } |
|
|
854 | |
|
|
855 | void |
|
|
856 | aio_read (fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
|
|
857 | SV * fh |
|
|
858 | UV offset |
|
|
859 | UV length |
|
|
860 | SV * data |
|
|
861 | UV dataoffset |
|
|
862 | SV * callback |
|
|
863 | ALIAS: |
|
|
864 | aio_read = REQ_READ |
|
|
865 | aio_write = REQ_WRITE |
|
|
866 | PROTOTYPE: $$$$$;$ |
|
|
867 | CODE: |
|
|
868 | { |
451 | aio_req req; |
869 | aio_req req; |
|
|
870 | STRLEN svlen; |
|
|
871 | char *svptr = SvPVbyte (data, svlen); |
452 | |
872 | |
453 | Newz (0, req, 1, aio_cb); |
873 | SvUPGRADE (data, SVt_PV); |
|
|
874 | SvPOK_on (data); |
454 | |
875 | |
455 | if (!req) |
876 | if (dataoffset < 0) |
456 | croak ("out of memory during aio_req allocation"); |
877 | dataoffset += svlen; |
457 | |
878 | |
458 | req->type = ix; |
879 | if (dataoffset < 0 || dataoffset > svlen) |
459 | req->fd = PerlIO_fileno (fh); |
880 | croak ("data offset outside of string"); |
460 | req->callback = SvREFCNT_inc (callback); |
|
|
461 | |
881 | |
462 | send_req (req); |
882 | if (ix == REQ_WRITE) |
463 | } |
883 | { |
464 | |
884 | /* write: check length and adjust. */ |
465 | void |
885 | if (length < 0 || length + dataoffset > svlen) |
466 | aio_read(fh,offset,length,data,dataoffset,callback) |
886 | length = svlen - dataoffset; |
467 | InputStream fh |
887 | } |
468 | UV offset |
888 | else |
469 | IV length |
889 | { |
470 | SV * data |
890 | /* read: grow scalar as necessary */ |
471 | IV dataoffset |
891 | svptr = SvGROW (data, length + dataoffset); |
472 | SV * callback |
892 | } |
473 | PROTOTYPE: $$$$$$ |
|
|
474 | CODE: |
|
|
475 | read_write (0, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
|
|
476 | |
|
|
477 | void |
|
|
478 | aio_write(fh,offset,length,data,dataoffset,callback) |
|
|
479 | OutputStream fh |
|
|
480 | UV offset |
|
|
481 | IV length |
|
|
482 | SV * data |
|
|
483 | IV dataoffset |
|
|
484 | SV * callback |
|
|
485 | PROTOTYPE: $$$$$$ |
|
|
486 | CODE: |
|
|
487 | read_write (1, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
|
|
488 | |
|
|
489 | void |
|
|
490 | aio_readahead(fh,offset,length,callback) |
|
|
491 | InputStream fh |
|
|
492 | UV offset |
|
|
493 | IV length |
|
|
494 | SV * callback |
|
|
495 | PROTOTYPE: $$$$ |
|
|
496 | CODE: |
|
|
497 | { |
|
|
498 | aio_req req; |
|
|
499 | |
893 | |
500 | if (length < 0) |
894 | if (length < 0) |
501 | croak ("length must not be negative"); |
895 | croak ("length must not be negative"); |
502 | |
896 | |
503 | Newz (0, req, 1, aio_cb); |
897 | { |
|
|
898 | dREQ; |
504 | |
899 | |
505 | if (!req) |
900 | req->type = ix; |
506 | croak ("out of memory during aio_req allocation"); |
901 | req->fh = newSVsv (fh); |
|
|
902 | req->fd = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh)) |
|
|
903 | : IoOFP (sv_2io (fh))); |
|
|
904 | req->offset = offset; |
|
|
905 | req->length = length; |
|
|
906 | req->data = SvREFCNT_inc (data); |
|
|
907 | req->dataptr = (char *)svptr + dataoffset; |
|
|
908 | |
|
|
909 | if (!SvREADONLY (data)) |
|
|
910 | { |
|
|
911 | SvREADONLY_on (data); |
|
|
912 | req->data2ptr = (void *)data; |
|
|
913 | } |
|
|
914 | |
|
|
915 | send_req (req); |
|
|
916 | } |
|
|
917 | } |
|
|
918 | |
|
|
919 | void |
|
|
920 | aio_sendfile (out_fh,in_fh,in_offset,length,callback=&PL_sv_undef) |
|
|
921 | SV * out_fh |
|
|
922 | SV * in_fh |
|
|
923 | UV in_offset |
|
|
924 | UV length |
|
|
925 | SV * callback |
|
|
926 | PROTOTYPE: $$$$;$ |
|
|
927 | CODE: |
|
|
928 | { |
|
|
929 | dREQ; |
|
|
930 | |
|
|
931 | req->type = REQ_SENDFILE; |
|
|
932 | req->fh = newSVsv (out_fh); |
|
|
933 | req->fd = PerlIO_fileno (IoIFP (sv_2io (out_fh))); |
|
|
934 | req->fh2 = newSVsv (in_fh); |
|
|
935 | req->fd2 = PerlIO_fileno (IoIFP (sv_2io (in_fh))); |
|
|
936 | req->offset = in_offset; |
|
|
937 | req->length = length; |
|
|
938 | |
|
|
939 | send_req (req); |
|
|
940 | } |
|
|
941 | |
|
|
942 | void |
|
|
943 | aio_readahead (fh,offset,length,callback=&PL_sv_undef) |
|
|
944 | SV * fh |
|
|
945 | UV offset |
|
|
946 | IV length |
|
|
947 | SV * callback |
|
|
948 | PROTOTYPE: $$$;$ |
|
|
949 | CODE: |
|
|
950 | { |
|
|
951 | dREQ; |
507 | |
952 | |
508 | req->type = REQ_READAHEAD; |
953 | req->type = REQ_READAHEAD; |
|
|
954 | req->fh = newSVsv (fh); |
509 | req->fd = PerlIO_fileno (fh); |
955 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
510 | req->offset = offset; |
956 | req->offset = offset; |
511 | req->length = length; |
957 | req->length = length; |
512 | req->callback = SvREFCNT_inc (callback); |
|
|
513 | |
958 | |
514 | send_req (req); |
959 | send_req (req); |
515 | } |
960 | } |
516 | |
961 | |
517 | void |
962 | void |
518 | aio_stat(fh_or_path,callback) |
963 | aio_stat (fh_or_path,callback=&PL_sv_undef) |
519 | SV * fh_or_path |
964 | SV * fh_or_path |
520 | SV * callback |
965 | SV * callback |
521 | PROTOTYPE: $$ |
|
|
522 | ALIAS: |
966 | ALIAS: |
|
|
967 | aio_stat = REQ_STAT |
523 | aio_lstat = 1 |
968 | aio_lstat = REQ_LSTAT |
524 | CODE: |
969 | CODE: |
525 | { |
970 | { |
526 | aio_req req; |
971 | dREQ; |
527 | |
|
|
528 | Newz (0, req, 1, aio_cb); |
|
|
529 | |
|
|
530 | if (!req) |
|
|
531 | croak ("out of memory during aio_req allocation"); |
|
|
532 | |
972 | |
533 | New (0, req->statdata, 1, Stat_t); |
973 | New (0, req->statdata, 1, Stat_t); |
534 | |
|
|
535 | if (!req->statdata) |
974 | if (!req->statdata) |
|
|
975 | { |
|
|
976 | free_req (req); |
536 | croak ("out of memory during aio_req->statdata allocation"); |
977 | croak ("out of memory during aio_req->statdata allocation"); |
|
|
978 | } |
537 | |
979 | |
538 | if (SvPOK (fh_or_path)) |
980 | if (SvPOK (fh_or_path)) |
539 | { |
981 | { |
540 | req->type = ix ? REQ_LSTAT : REQ_STAT; |
982 | req->type = ix; |
541 | req->data = newSVsv (fh_or_path); |
983 | req->data = newSVsv (fh_or_path); |
542 | req->dataptr = SvPV_nolen (req->data); |
984 | req->dataptr = SvPVbyte_nolen (req->data); |
543 | } |
985 | } |
544 | else |
986 | else |
545 | { |
987 | { |
546 | req->type = REQ_FSTAT; |
988 | req->type = REQ_FSTAT; |
|
|
989 | req->fh = newSVsv (fh_or_path); |
547 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
990 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
548 | } |
991 | } |
549 | |
992 | |
550 | req->callback = SvREFCNT_inc (callback); |
|
|
551 | |
|
|
552 | send_req (req); |
993 | send_req (req); |
553 | } |
994 | } |
554 | |
995 | |
555 | void |
996 | void |
556 | aio_unlink(pathname,callback) |
997 | aio_unlink (pathname,callback=&PL_sv_undef) |
557 | SV * pathname |
998 | SV * pathname |
558 | SV * callback |
999 | SV * callback |
559 | PROTOTYPE: $$ |
1000 | ALIAS: |
|
|
1001 | aio_unlink = REQ_UNLINK |
|
|
1002 | aio_rmdir = REQ_RMDIR |
|
|
1003 | aio_readdir = REQ_READDIR |
560 | CODE: |
1004 | CODE: |
561 | { |
1005 | { |
562 | aio_req req; |
1006 | dREQ; |
563 | |
1007 | |
564 | Newz (0, req, 1, aio_cb); |
1008 | req->type = ix; |
565 | |
|
|
566 | if (!req) |
|
|
567 | croak ("out of memory during aio_req allocation"); |
|
|
568 | |
|
|
569 | req->type = REQ_UNLINK; |
|
|
570 | req->data = newSVsv (pathname); |
1009 | req->data = newSVsv (pathname); |
571 | req->dataptr = SvPV_nolen (req->data); |
1010 | req->dataptr = SvPVbyte_nolen (req->data); |
572 | req->callback = SvREFCNT_inc (callback); |
|
|
573 | |
1011 | |
574 | send_req (req); |
1012 | send_req (req); |
575 | } |
1013 | } |
576 | |
1014 | |
577 | void |
1015 | void |
|
|
1016 | aio_link (oldpath,newpath,callback=&PL_sv_undef) |
|
|
1017 | SV * oldpath |
|
|
1018 | SV * newpath |
|
|
1019 | SV * callback |
|
|
1020 | ALIAS: |
|
|
1021 | aio_link = REQ_LINK |
|
|
1022 | aio_symlink = REQ_SYMLINK |
|
|
1023 | aio_rename = REQ_RENAME |
|
|
1024 | CODE: |
|
|
1025 | { |
|
|
1026 | dREQ; |
|
|
1027 | |
|
|
1028 | req->type = ix; |
|
|
1029 | req->fh = newSVsv (oldpath); |
|
|
1030 | req->data2ptr = SvPVbyte_nolen (req->fh); |
|
|
1031 | req->data = newSVsv (newpath); |
|
|
1032 | req->dataptr = SvPVbyte_nolen (req->data); |
|
|
1033 | |
|
|
1034 | send_req (req); |
|
|
1035 | } |
|
|
1036 | |
|
|
1037 | #if 0 |
|
|
1038 | |
|
|
1039 | # undocumented, because it does not cancel active requests |
|
|
1040 | void |
|
|
1041 | cancel_most_requests () |
|
|
1042 | PROTOTYPE: |
|
|
1043 | CODE: |
|
|
1044 | { |
|
|
1045 | aio_req *req; |
|
|
1046 | |
|
|
1047 | pthread_mutex_lock (&reqlock); |
|
|
1048 | for (req = reqs; req; req = req->next) |
|
|
1049 | req->flags |= 1; |
|
|
1050 | pthread_mutex_unlock (&reqlock); |
|
|
1051 | |
|
|
1052 | pthread_mutex_lock (&reslock); |
|
|
1053 | for (req = ress; req; req = req->next) |
|
|
1054 | req->flags |= 1; |
|
|
1055 | pthread_mutex_unlock (&reslock); |
|
|
1056 | } |
|
|
1057 | |
|
|
1058 | #endif |
|
|
1059 | |
|
|
1060 | void |
578 | flush() |
1061 | flush () |
579 | PROTOTYPE: |
1062 | PROTOTYPE: |
580 | CODE: |
1063 | CODE: |
581 | while (nreqs) |
1064 | while (nreqs) |
582 | { |
1065 | { |
583 | poll_wait (); |
1066 | poll_wait (); |