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