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