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 | |
7 | #include <sys/types.h> |
10 | #include <sys/types.h> |
8 | #include <sys/stat.h> |
11 | #include <sys/stat.h> |
|
|
12 | |
9 | #include <unistd.h> |
13 | #include <unistd.h> |
10 | #include <fcntl.h> |
14 | #include <fcntl.h> |
11 | #include <signal.h> |
15 | #include <signal.h> |
12 | #include <sched.h> |
16 | #include <sched.h> |
13 | #include <endian.h> |
|
|
14 | |
17 | |
15 | #include <pthread.h> |
18 | #include <pthread.h> |
16 | #include <sys/syscall.h> |
|
|
17 | |
19 | |
18 | typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
20 | 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 ;) */ |
21 | 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 ;) */ |
22 | typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
21 | |
23 | |
22 | #if __i386 || __amd64 |
|
|
23 | # define STACKSIZE ( 256 * sizeof (long)) |
|
|
24 | #elif __ia64 |
24 | #if __ia64 |
25 | # define STACKSIZE (8192 * sizeof (long)) |
25 | # define STACKSIZE 65536 |
26 | #else |
26 | #else |
27 | # define STACKSIZE ( 512 * sizeof (long)) |
27 | # define STACKSIZE 4096 |
28 | #endif |
28 | #endif |
29 | |
29 | |
30 | enum { |
30 | enum { |
31 | REQ_QUIT, |
31 | REQ_QUIT, |
32 | REQ_OPEN, REQ_CLOSE, |
32 | REQ_OPEN, REQ_CLOSE, |
33 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
33 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
34 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_UNLINK, |
34 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
35 | REQ_FSYNC, REQ_FDATASYNC, |
35 | REQ_FSYNC, REQ_FDATASYNC, |
|
|
36 | REQ_UNLINK, REQ_RMDIR, |
|
|
37 | REQ_SYMLINK, |
36 | }; |
38 | }; |
37 | |
39 | |
38 | typedef struct aio_cb { |
40 | typedef struct aio_cb { |
39 | struct aio_cb *next; |
41 | struct aio_cb *volatile next; |
40 | |
42 | |
41 | int type; |
43 | int type; |
42 | |
44 | |
43 | int fd; |
45 | int fd; |
44 | off_t offset; |
46 | off_t offset; |
45 | size_t length; |
47 | size_t length; |
46 | ssize_t result; |
48 | ssize_t result; |
47 | mode_t mode; /* open */ |
49 | mode_t mode; /* open */ |
48 | int errorno; |
50 | int errorno; |
49 | SV *data, *callback; |
51 | SV *data, *callback, *fh; |
50 | void *dataptr; |
52 | void *dataptr, *data2ptr; |
51 | STRLEN dataoffset; |
53 | STRLEN dataoffset; |
52 | |
54 | |
53 | Stat_t *statdata; |
55 | Stat_t *statdata; |
54 | } aio_cb; |
56 | } aio_cb; |
55 | |
57 | |
56 | typedef aio_cb *aio_req; |
58 | typedef aio_cb *aio_req; |
57 | |
59 | |
58 | static int started; |
60 | static int started; |
59 | static int nreqs; |
61 | static volatile int nreqs; |
|
|
62 | static int max_outstanding = 1<<30; |
60 | static int reqpipe[2], respipe[2]; |
63 | static int respipe [2]; |
61 | |
64 | |
|
|
65 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
|
|
66 | static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; |
|
|
67 | static pthread_mutex_t frklock = PTHREAD_MUTEX_INITIALIZER; |
|
|
68 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
|
|
69 | |
62 | static aio_req qs, qe; /* queue start, queue end */ |
70 | static volatile aio_req reqs, reqe; /* queue start, queue end */ |
63 | |
71 | static volatile aio_req ress, rese; /* queue start, queue end */ |
64 | static void *aio_proc(void *arg); |
|
|
65 | |
|
|
66 | static void |
|
|
67 | start_thread (void) |
|
|
68 | { |
|
|
69 | sigset_t fullsigset, oldsigset; |
|
|
70 | pthread_t tid; |
|
|
71 | pthread_attr_t attr; |
|
|
72 | |
|
|
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) |
|
|
160 | croak ("out of memory during aio_req allocation"); |
|
|
161 | |
|
|
162 | req->type = dowrite ? REQ_WRITE : REQ_READ; |
|
|
163 | req->fd = fd; |
|
|
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 | |
|
|
170 | send_req (req); |
|
|
171 | } |
|
|
172 | |
72 | |
173 | static void |
73 | static void |
174 | poll_wait () |
74 | poll_wait () |
175 | { |
75 | { |
|
|
76 | if (nreqs && !ress) |
|
|
77 | { |
176 | fd_set rfd; |
78 | fd_set rfd; |
177 | FD_ZERO(&rfd); |
79 | FD_ZERO(&rfd); |
178 | FD_SET(respipe[0], &rfd); |
80 | FD_SET(respipe [0], &rfd); |
179 | |
81 | |
180 | select (respipe[0] + 1, &rfd, 0, 0, 0); |
82 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
|
|
83 | } |
181 | } |
84 | } |
182 | |
85 | |
183 | static int |
86 | static int |
184 | poll_cb (pTHX) |
87 | poll_cb () |
185 | { |
88 | { |
186 | dSP; |
89 | dSP; |
187 | int count = 0; |
90 | int count = 0; |
|
|
91 | int do_croak = 0; |
188 | aio_req req; |
92 | aio_req req, prv; |
189 | |
93 | |
190 | while (read (respipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
94 | pthread_mutex_lock (&reslock); |
|
|
95 | |
|
|
96 | { |
|
|
97 | /* read any signals sent by the worker threads */ |
|
|
98 | char buf [32]; |
|
|
99 | while (read (respipe [0], buf, 32) == 32) |
|
|
100 | ; |
|
|
101 | } |
|
|
102 | |
|
|
103 | req = ress; |
|
|
104 | ress = rese = 0; |
|
|
105 | |
|
|
106 | pthread_mutex_unlock (&reslock); |
|
|
107 | |
|
|
108 | while (req) |
191 | { |
109 | { |
192 | nreqs--; |
110 | nreqs--; |
193 | |
111 | |
194 | if (req->type == REQ_QUIT) |
112 | if (req->type == REQ_QUIT) |
195 | started--; |
113 | started--; |
… | |
… | |
203 | + req->result > 0 ? req->result : 0); |
121 | + req->result > 0 ? req->result : 0); |
204 | |
122 | |
205 | if (req->data) |
123 | if (req->data) |
206 | SvREFCNT_dec (req->data); |
124 | SvREFCNT_dec (req->data); |
207 | |
125 | |
|
|
126 | if (req->fh) |
|
|
127 | SvREFCNT_dec (req->fh); |
|
|
128 | |
208 | if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) |
129 | if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) |
209 | { |
130 | { |
210 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
131 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
211 | PL_laststatval = req->result; |
132 | PL_laststatval = req->result; |
212 | PL_statcache = *(req->statdata); |
133 | PL_statcache = *(req->statdata); |
213 | |
134 | |
214 | Safefree (req->statdata); |
135 | Safefree (req->statdata); |
215 | } |
136 | } |
216 | |
137 | |
|
|
138 | ENTER; |
217 | PUSHMARK (SP); |
139 | PUSHMARK (SP); |
218 | XPUSHs (sv_2mortal (newSViv (req->result))); |
140 | XPUSHs (sv_2mortal (newSViv (req->result))); |
|
|
141 | |
|
|
142 | if (req->type == REQ_OPEN) |
|
|
143 | { |
|
|
144 | /* convert fd to fh */ |
|
|
145 | SV *fh; |
|
|
146 | |
219 | PUTBACK; |
147 | PUTBACK; |
220 | call_sv (req->callback, G_VOID); |
148 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
221 | SPAGAIN; |
149 | SPAGAIN; |
|
|
150 | |
|
|
151 | fh = SvREFCNT_inc (POPs); |
|
|
152 | |
|
|
153 | PUSHMARK (SP); |
|
|
154 | XPUSHs (sv_2mortal (fh)); |
|
|
155 | } |
|
|
156 | |
|
|
157 | if (SvOK (req->callback)) |
|
|
158 | { |
|
|
159 | PUTBACK; |
|
|
160 | call_sv (req->callback, G_VOID | G_EVAL); |
|
|
161 | SPAGAIN; |
|
|
162 | } |
|
|
163 | |
|
|
164 | do_croak = SvTRUE (ERRSV); |
|
|
165 | |
|
|
166 | LEAVE; |
222 | |
167 | |
223 | if (req->callback) |
168 | if (req->callback) |
224 | SvREFCNT_dec (req->callback); |
169 | SvREFCNT_dec (req->callback); |
225 | |
170 | |
226 | errno = errorno; |
171 | errno = errorno; |
227 | count++; |
172 | count++; |
228 | } |
173 | } |
229 | |
174 | |
|
|
175 | prv = req; |
|
|
176 | req = req->next; |
230 | Safefree (req); |
177 | Safefree (prv); |
|
|
178 | |
|
|
179 | if (do_croak) |
|
|
180 | croak (0); |
231 | } |
181 | } |
232 | |
182 | |
233 | if (qs) |
|
|
234 | send_reqs (); |
|
|
235 | |
|
|
236 | return count; |
183 | return count; |
237 | } |
184 | } |
|
|
185 | |
|
|
186 | static void *aio_proc(void *arg); |
|
|
187 | |
|
|
188 | static void |
|
|
189 | start_thread (void) |
|
|
190 | { |
|
|
191 | sigset_t fullsigset, oldsigset; |
|
|
192 | pthread_t tid; |
|
|
193 | pthread_attr_t attr; |
|
|
194 | |
|
|
195 | pthread_attr_init (&attr); |
|
|
196 | pthread_attr_setstacksize (&attr, STACKSIZE); |
|
|
197 | pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); |
|
|
198 | |
|
|
199 | sigfillset (&fullsigset); |
|
|
200 | sigprocmask (SIG_SETMASK, &fullsigset, &oldsigset); |
|
|
201 | |
|
|
202 | if (pthread_create (&tid, &attr, aio_proc, 0) == 0) |
|
|
203 | started++; |
|
|
204 | |
|
|
205 | sigprocmask (SIG_SETMASK, &oldsigset, 0); |
|
|
206 | } |
|
|
207 | |
|
|
208 | static void |
|
|
209 | send_req (aio_req req) |
|
|
210 | { |
|
|
211 | nreqs++; |
|
|
212 | |
|
|
213 | pthread_mutex_lock (&reqlock); |
|
|
214 | |
|
|
215 | req->next = 0; |
|
|
216 | |
|
|
217 | if (reqe) |
|
|
218 | { |
|
|
219 | reqe->next = req; |
|
|
220 | reqe = req; |
|
|
221 | } |
|
|
222 | else |
|
|
223 | reqe = reqs = req; |
|
|
224 | |
|
|
225 | pthread_cond_signal (&reqwait); |
|
|
226 | pthread_mutex_unlock (&reqlock); |
|
|
227 | |
|
|
228 | while (nreqs > max_outstanding) |
|
|
229 | { |
|
|
230 | poll_wait (); |
|
|
231 | poll_cb (); |
|
|
232 | } |
|
|
233 | } |
|
|
234 | |
|
|
235 | static void |
|
|
236 | end_thread (void) |
|
|
237 | { |
|
|
238 | aio_req req; |
|
|
239 | New (0, req, 1, aio_cb); |
|
|
240 | req->type = REQ_QUIT; |
|
|
241 | |
|
|
242 | send_req (req); |
|
|
243 | } |
|
|
244 | |
|
|
245 | |
|
|
246 | static void min_parallel (int nthreads) |
|
|
247 | { |
|
|
248 | while (nthreads > started) |
|
|
249 | start_thread (); |
|
|
250 | } |
|
|
251 | |
|
|
252 | static void max_parallel (int nthreads) |
|
|
253 | { |
|
|
254 | int cur = started; |
|
|
255 | while (cur > nthreads) |
|
|
256 | { |
|
|
257 | end_thread (); |
|
|
258 | cur--; |
|
|
259 | } |
|
|
260 | |
|
|
261 | while (started > nthreads) |
|
|
262 | { |
|
|
263 | poll_wait (); |
|
|
264 | poll_cb (); |
|
|
265 | } |
|
|
266 | } |
|
|
267 | |
|
|
268 | static int fork_started; |
|
|
269 | |
|
|
270 | static void atfork_prepare (void) |
|
|
271 | { |
|
|
272 | int nstarted; |
|
|
273 | |
|
|
274 | for (;;) { |
|
|
275 | while (nreqs) |
|
|
276 | { |
|
|
277 | poll_wait (); |
|
|
278 | poll_cb (); |
|
|
279 | } |
|
|
280 | |
|
|
281 | nstarted = started; |
|
|
282 | max_parallel (0); |
|
|
283 | |
|
|
284 | pthread_mutex_lock (&reqlock); |
|
|
285 | |
|
|
286 | if (!nreqs && !started) |
|
|
287 | break; |
|
|
288 | |
|
|
289 | pthread_mutex_unlock (&reqlock); |
|
|
290 | |
|
|
291 | min_parallel (fork_started); |
|
|
292 | } |
|
|
293 | |
|
|
294 | pthread_mutex_lock (&frklock); |
|
|
295 | fork_started = nstarted; |
|
|
296 | pthread_mutex_lock (&reslock); |
|
|
297 | |
|
|
298 | assert (!started); |
|
|
299 | assert (!nreqs); |
|
|
300 | assert (!reqs && !reqe); |
|
|
301 | assert (!ress && !rese); |
|
|
302 | } |
|
|
303 | |
|
|
304 | static void atfork_parent (void) |
|
|
305 | { |
|
|
306 | pthread_mutex_unlock (&reslock); |
|
|
307 | pthread_mutex_unlock (&frklock); |
|
|
308 | pthread_mutex_unlock (&reqlock); |
|
|
309 | |
|
|
310 | min_parallel (fork_started); |
|
|
311 | } |
|
|
312 | |
|
|
313 | #define atfork_child atfork_parent |
|
|
314 | |
|
|
315 | /*****************************************************************************/ |
|
|
316 | /* work around various missing functions */ |
|
|
317 | |
|
|
318 | #if !HAVE_PREADWRITE |
|
|
319 | # define pread aio_pread |
|
|
320 | # define pwrite aio_pwrite |
|
|
321 | |
|
|
322 | /* |
|
|
323 | * make our pread/pwrite safe against themselves, but not against |
|
|
324 | * normal read/write by using a mutex. slows down execution a lot, |
|
|
325 | * but that's your problem, not mine. |
|
|
326 | */ |
|
|
327 | static pthread_mutex_t iolock = PTHREAD_MUTEX_INITIALIZER; |
|
|
328 | |
|
|
329 | static ssize_t |
|
|
330 | pread (int fd, void *buf, size_t count, off_t offset) |
|
|
331 | { |
|
|
332 | ssize_t res; |
|
|
333 | off_t ooffset; |
|
|
334 | |
|
|
335 | pthread_mutex_lock (&iolock); |
|
|
336 | ooffset = lseek (fd, 0, SEEK_CUR); |
|
|
337 | lseek (fd, offset, SEEK_SET); |
|
|
338 | res = read (fd, buf, count); |
|
|
339 | lseek (fd, ooffset, SEEK_SET); |
|
|
340 | pthread_mutex_unlock (&iolock); |
|
|
341 | |
|
|
342 | return res; |
|
|
343 | } |
|
|
344 | |
|
|
345 | static ssize_t |
|
|
346 | pwrite (int fd, void *buf, size_t count, off_t offset) |
|
|
347 | { |
|
|
348 | ssize_t res; |
|
|
349 | off_t ooffset; |
|
|
350 | |
|
|
351 | pthread_mutex_lock (&iolock); |
|
|
352 | ooffset = lseek (fd, 0, SEEK_CUR); |
|
|
353 | lseek (fd, offset, SEEK_SET); |
|
|
354 | res = write (fd, buf, count); |
|
|
355 | lseek (fd, offset, SEEK_SET); |
|
|
356 | pthread_mutex_unlock (&iolock); |
|
|
357 | |
|
|
358 | return res; |
|
|
359 | } |
|
|
360 | #endif |
|
|
361 | |
|
|
362 | #if !HAVE_FDATASYNC |
|
|
363 | # define fdatasync fsync |
|
|
364 | #endif |
|
|
365 | |
|
|
366 | #if !HAVE_READAHEAD |
|
|
367 | # define readahead aio_readahead |
|
|
368 | |
|
|
369 | static char readahead_buf[4096]; |
|
|
370 | |
|
|
371 | static ssize_t |
|
|
372 | readahead (int fd, off_t offset, size_t count) |
|
|
373 | { |
|
|
374 | while (count > 0) |
|
|
375 | { |
|
|
376 | size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); |
|
|
377 | |
|
|
378 | pread (fd, readahead_buf, len, offset); |
|
|
379 | offset += len; |
|
|
380 | count -= len; |
|
|
381 | } |
|
|
382 | |
|
|
383 | errno = 0; |
|
|
384 | } |
|
|
385 | #endif |
|
|
386 | |
|
|
387 | /*****************************************************************************/ |
238 | |
388 | |
239 | static void * |
389 | static void * |
240 | aio_proc (void *thr_arg) |
390 | aio_proc (void *thr_arg) |
241 | { |
391 | { |
242 | aio_req req; |
392 | aio_req req; |
|
|
393 | int type; |
243 | |
394 | |
244 | /* then loop */ |
395 | do |
245 | while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
|
|
246 | { |
396 | { |
|
|
397 | pthread_mutex_lock (&reqlock); |
|
|
398 | |
|
|
399 | for (;;) |
|
|
400 | { |
|
|
401 | req = reqs; |
|
|
402 | |
|
|
403 | if (reqs) |
|
|
404 | { |
|
|
405 | reqs = reqs->next; |
|
|
406 | if (!reqs) reqe = 0; |
|
|
407 | } |
|
|
408 | |
|
|
409 | if (req) |
|
|
410 | break; |
|
|
411 | |
|
|
412 | pthread_cond_wait (&reqwait, &reqlock); |
|
|
413 | } |
|
|
414 | |
|
|
415 | pthread_mutex_unlock (&reqlock); |
|
|
416 | |
247 | errno = 0; /* strictly unnecessary */ |
417 | errno = 0; /* strictly unnecessary */ |
248 | |
418 | |
|
|
419 | type = req->type; |
|
|
420 | |
249 | switch (req->type) |
421 | switch (type) |
250 | { |
422 | { |
251 | case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, req->offset); break; |
423 | 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; |
424 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
253 | #if SYS_readahead |
425 | |
254 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
426 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
255 | #else |
|
|
256 | case REQ_READAHEAD: req->result = -1; errno = ENOSYS; break; |
|
|
257 | #endif |
|
|
258 | |
427 | |
259 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
428 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
260 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
429 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
261 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
430 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
262 | |
431 | |
263 | case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
432 | case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
264 | case REQ_CLOSE: req->result = close (req->fd); break; |
433 | case REQ_CLOSE: req->result = close (req->fd); break; |
265 | case REQ_UNLINK: req->result = unlink (req->dataptr); break; |
434 | case REQ_UNLINK: req->result = unlink (req->dataptr); break; |
|
|
435 | case REQ_RMDIR: req->result = rmdir (req->dataptr); break; |
|
|
436 | case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; |
266 | |
437 | |
|
|
438 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
267 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
439 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
268 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
|
|
269 | |
440 | |
270 | case REQ_QUIT: |
441 | case REQ_QUIT: |
271 | write (respipe[1], (void *)&req, sizeof (req)); |
442 | break; |
272 | return 0; |
|
|
273 | |
443 | |
274 | default: |
444 | default: |
275 | req->result = ENOSYS; |
445 | req->result = ENOSYS; |
276 | break; |
446 | break; |
277 | } |
447 | } |
278 | |
448 | |
279 | req->errorno = errno; |
449 | req->errorno = errno; |
280 | write (respipe[1], (void *)&req, sizeof (req)); |
450 | |
|
|
451 | pthread_mutex_lock (&reslock); |
|
|
452 | |
|
|
453 | req->next = 0; |
|
|
454 | |
|
|
455 | if (rese) |
|
|
456 | { |
|
|
457 | rese->next = req; |
|
|
458 | rese = req; |
|
|
459 | } |
|
|
460 | else |
|
|
461 | { |
|
|
462 | rese = ress = req; |
|
|
463 | |
|
|
464 | /* write a dummy byte to the pipe so fh becomes ready */ |
|
|
465 | write (respipe [1], &respipe, 1); |
|
|
466 | } |
|
|
467 | |
|
|
468 | pthread_mutex_unlock (&reslock); |
281 | } |
469 | } |
|
|
470 | while (type != REQ_QUIT); |
282 | |
471 | |
283 | return 0; |
472 | return 0; |
284 | } |
473 | } |
285 | |
474 | |
|
|
475 | #define dREQ \ |
|
|
476 | aio_req req; \ |
|
|
477 | \ |
|
|
478 | if (SvOK (callback) && !SvROK (callback)) \ |
|
|
479 | croak ("clalback must be undef or of reference type"); \ |
|
|
480 | \ |
|
|
481 | Newz (0, req, 1, aio_cb); \ |
|
|
482 | if (!req) \ |
|
|
483 | croak ("out of memory during aio_req allocation"); \ |
|
|
484 | \ |
|
|
485 | req->callback = SvREFCNT_inc (callback); |
|
|
486 | |
286 | MODULE = IO::AIO PACKAGE = IO::AIO |
487 | MODULE = IO::AIO PACKAGE = IO::AIO |
287 | |
488 | |
|
|
489 | PROTOTYPES: ENABLE |
|
|
490 | |
288 | BOOT: |
491 | BOOT: |
289 | { |
492 | { |
290 | if (pipe (reqpipe) || pipe (respipe)) |
493 | if (pipe (respipe)) |
291 | croak ("unable to initialize request or result pipe"); |
494 | croak ("unable to initialize result pipe"); |
292 | |
495 | |
293 | if (fcntl (reqpipe[1], F_SETFL, O_NONBLOCK)) |
496 | if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)) |
294 | croak ("cannot set result pipe to nonblocking mode"); |
497 | croak ("cannot set result pipe to nonblocking mode"); |
295 | |
498 | |
296 | if (fcntl (respipe[0], F_SETFL, O_NONBLOCK)) |
499 | if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) |
297 | croak ("cannot set result pipe to nonblocking mode"); |
500 | croak ("cannot set result pipe to nonblocking mode"); |
|
|
501 | |
|
|
502 | pthread_atfork (atfork_prepare, atfork_parent, atfork_child); |
298 | } |
503 | } |
299 | |
504 | |
300 | void |
505 | void |
301 | min_parallel(nthreads) |
506 | min_parallel(nthreads) |
302 | int nthreads |
507 | int nthreads |
303 | PROTOTYPE: $ |
508 | PROTOTYPE: $ |
304 | CODE: |
|
|
305 | while (nthreads > started) |
|
|
306 | start_thread (); |
|
|
307 | |
509 | |
308 | void |
510 | void |
309 | max_parallel(nthreads) |
511 | max_parallel(nthreads) |
310 | int nthreads |
512 | int nthreads |
311 | PROTOTYPE: $ |
513 | PROTOTYPE: $ |
|
|
514 | |
|
|
515 | int |
|
|
516 | max_outstanding(nreqs) |
|
|
517 | int nreqs |
|
|
518 | PROTOTYPE: $ |
312 | CODE: |
519 | CODE: |
313 | { |
520 | RETVAL = max_outstanding; |
314 | int cur = started; |
521 | max_outstanding = nreqs; |
315 | while (cur > nthreads) |
|
|
316 | { |
|
|
317 | end_thread (); |
|
|
318 | cur--; |
|
|
319 | } |
|
|
320 | |
522 | |
321 | while (started > nthreads) |
|
|
322 | { |
|
|
323 | poll_wait (); |
|
|
324 | poll_cb (aTHX); |
|
|
325 | } |
|
|
326 | } |
|
|
327 | |
|
|
328 | void |
523 | void |
329 | aio_open(pathname,flags,mode,callback) |
524 | aio_open(pathname,flags,mode,callback=&PL_sv_undef) |
330 | SV * pathname |
525 | SV * pathname |
331 | int flags |
526 | int flags |
332 | int mode |
527 | int mode |
333 | SV * callback |
528 | SV * callback |
334 | PROTOTYPE: $$$$ |
529 | PROTOTYPE: $$$;$ |
335 | CODE: |
530 | CODE: |
336 | { |
531 | { |
337 | aio_req req; |
532 | dREQ; |
338 | |
|
|
339 | Newz (0, req, 1, aio_cb); |
|
|
340 | |
|
|
341 | if (!req) |
|
|
342 | croak ("out of memory during aio_req allocation"); |
|
|
343 | |
533 | |
344 | req->type = REQ_OPEN; |
534 | req->type = REQ_OPEN; |
345 | req->data = newSVsv (pathname); |
535 | req->data = newSVsv (pathname); |
346 | req->dataptr = SvPV_nolen (req->data); |
536 | req->dataptr = SvPVbyte_nolen (req->data); |
347 | req->fd = flags; |
537 | req->fd = flags; |
348 | req->mode = mode; |
538 | req->mode = mode; |
349 | req->callback = SvREFCNT_inc (callback); |
|
|
350 | |
539 | |
351 | send_req (req); |
540 | send_req (req); |
352 | } |
541 | } |
353 | |
542 | |
354 | void |
543 | void |
355 | aio_close(fh,callback) |
544 | aio_close(fh,callback=&PL_sv_undef) |
356 | InputStream fh |
545 | SV * fh |
357 | SV * callback |
546 | SV * callback |
358 | PROTOTYPE: $$ |
547 | PROTOTYPE: $;$ |
359 | ALIAS: |
548 | ALIAS: |
360 | aio_close = REQ_CLOSE |
549 | aio_close = REQ_CLOSE |
361 | aio_fsync = REQ_FSYNC |
550 | aio_fsync = REQ_FSYNC |
362 | aio_fdatasync = REQ_FDATASYNC |
551 | aio_fdatasync = REQ_FDATASYNC |
363 | CODE: |
552 | CODE: |
364 | { |
553 | { |
|
|
554 | dREQ; |
|
|
555 | |
|
|
556 | req->type = ix; |
|
|
557 | req->fh = newSVsv (fh); |
|
|
558 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
|
|
559 | |
|
|
560 | send_req (req); |
|
|
561 | } |
|
|
562 | |
|
|
563 | void |
|
|
564 | aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
|
|
565 | SV * fh |
|
|
566 | UV offset |
|
|
567 | IV length |
|
|
568 | SV * data |
|
|
569 | IV dataoffset |
|
|
570 | SV * callback |
|
|
571 | ALIAS: |
|
|
572 | aio_read = REQ_READ |
|
|
573 | aio_write = REQ_WRITE |
|
|
574 | PROTOTYPE: $$$$$;$ |
|
|
575 | CODE: |
|
|
576 | { |
365 | aio_req req; |
577 | aio_req req; |
|
|
578 | STRLEN svlen; |
|
|
579 | char *svptr = SvPVbyte (data, svlen); |
366 | |
580 | |
367 | Newz (0, req, 1, aio_cb); |
581 | SvUPGRADE (data, SVt_PV); |
|
|
582 | SvPOK_on (data); |
368 | |
583 | |
369 | if (!req) |
584 | if (dataoffset < 0) |
370 | croak ("out of memory during aio_req allocation"); |
585 | dataoffset += svlen; |
371 | |
586 | |
372 | req->type = ix; |
587 | if (dataoffset < 0 || dataoffset > svlen) |
373 | req->fd = PerlIO_fileno (fh); |
588 | croak ("data offset outside of string"); |
374 | req->callback = SvREFCNT_inc (callback); |
|
|
375 | |
589 | |
376 | send_req (req); |
590 | if (ix == REQ_WRITE) |
377 | } |
591 | { |
378 | |
592 | /* write: check length and adjust. */ |
379 | void |
593 | if (length < 0 || length + dataoffset > svlen) |
380 | aio_read(fh,offset,length,data,dataoffset,callback) |
594 | length = svlen - dataoffset; |
381 | InputStream fh |
595 | } |
382 | UV offset |
596 | else |
383 | IV length |
597 | { |
384 | SV * data |
598 | /* read: grow scalar as necessary */ |
385 | IV dataoffset |
599 | svptr = SvGROW (data, length + dataoffset); |
386 | SV * callback |
600 | } |
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 | |
601 | |
414 | if (length < 0) |
602 | if (length < 0) |
415 | croak ("length must not be negative"); |
603 | croak ("length must not be negative"); |
416 | |
604 | |
417 | Newz (0, req, 1, aio_cb); |
605 | { |
|
|
606 | dREQ; |
418 | |
607 | |
419 | if (!req) |
608 | req->type = ix; |
420 | croak ("out of memory during aio_req allocation"); |
609 | req->fh = newSVsv (fh); |
|
|
610 | req->fd = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh)) |
|
|
611 | : IoOFP (sv_2io (fh))); |
|
|
612 | req->offset = offset; |
|
|
613 | req->length = length; |
|
|
614 | req->data = SvREFCNT_inc (data); |
|
|
615 | req->dataptr = (char *)svptr + dataoffset; |
|
|
616 | req->callback = SvREFCNT_inc (callback); |
|
|
617 | |
|
|
618 | send_req (req); |
|
|
619 | } |
|
|
620 | } |
|
|
621 | |
|
|
622 | void |
|
|
623 | aio_readahead(fh,offset,length,callback=&PL_sv_undef) |
|
|
624 | SV * fh |
|
|
625 | UV offset |
|
|
626 | IV length |
|
|
627 | SV * callback |
|
|
628 | PROTOTYPE: $$$;$ |
|
|
629 | CODE: |
|
|
630 | { |
|
|
631 | dREQ; |
421 | |
632 | |
422 | req->type = REQ_READAHEAD; |
633 | req->type = REQ_READAHEAD; |
|
|
634 | req->fh = newSVsv (fh); |
423 | req->fd = PerlIO_fileno (fh); |
635 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
424 | req->offset = offset; |
636 | req->offset = offset; |
425 | req->length = length; |
637 | req->length = length; |
426 | req->callback = SvREFCNT_inc (callback); |
|
|
427 | |
638 | |
428 | send_req (req); |
639 | send_req (req); |
429 | } |
640 | } |
430 | |
641 | |
431 | void |
642 | void |
432 | aio_stat(fh_or_path,callback) |
643 | aio_stat(fh_or_path,callback=&PL_sv_undef) |
433 | SV * fh_or_path |
644 | SV * fh_or_path |
434 | SV * callback |
645 | SV * callback |
435 | PROTOTYPE: $$ |
|
|
436 | ALIAS: |
646 | ALIAS: |
|
|
647 | aio_stat = REQ_STAT |
437 | aio_lstat = 1 |
648 | aio_lstat = REQ_LSTAT |
438 | CODE: |
649 | CODE: |
439 | { |
650 | { |
440 | aio_req req; |
651 | dREQ; |
441 | |
|
|
442 | Newz (0, req, 1, aio_cb); |
|
|
443 | |
|
|
444 | if (!req) |
|
|
445 | croak ("out of memory during aio_req allocation"); |
|
|
446 | |
652 | |
447 | New (0, req->statdata, 1, Stat_t); |
653 | New (0, req->statdata, 1, Stat_t); |
448 | |
|
|
449 | if (!req->statdata) |
654 | if (!req->statdata) |
450 | croak ("out of memory during aio_req->statdata allocation"); |
655 | croak ("out of memory during aio_req->statdata allocation (sorry, i just leaked memory, too)"); |
451 | |
656 | |
452 | if (SvPOK (fh_or_path)) |
657 | if (SvPOK (fh_or_path)) |
453 | { |
658 | { |
454 | req->type = ix ? REQ_LSTAT : REQ_STAT; |
659 | req->type = ix; |
455 | req->data = newSVsv (fh_or_path); |
660 | req->data = newSVsv (fh_or_path); |
456 | req->dataptr = SvPV_nolen (req->data); |
661 | req->dataptr = SvPVbyte_nolen (req->data); |
457 | } |
662 | } |
458 | else |
663 | else |
459 | { |
664 | { |
460 | req->type = REQ_FSTAT; |
665 | req->type = REQ_FSTAT; |
|
|
666 | req->fh = newSVsv (fh_or_path); |
461 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
667 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
462 | } |
668 | } |
463 | |
669 | |
464 | req->callback = SvREFCNT_inc (callback); |
|
|
465 | |
|
|
466 | send_req (req); |
670 | send_req (req); |
467 | } |
671 | } |
468 | |
672 | |
469 | void |
673 | void |
470 | aio_unlink(pathname,callback) |
674 | aio_unlink(pathname,callback=&PL_sv_undef) |
471 | SV * pathname |
675 | SV * pathname |
472 | SV * callback |
676 | SV * callback |
473 | PROTOTYPE: $$ |
677 | ALIAS: |
|
|
678 | aio_unlink = REQ_UNLINK |
|
|
679 | aio_rmdir = REQ_RMDIR |
474 | CODE: |
680 | CODE: |
475 | { |
681 | { |
476 | aio_req req; |
682 | dREQ; |
477 | |
683 | |
478 | Newz (0, req, 1, aio_cb); |
684 | 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); |
685 | req->data = newSVsv (pathname); |
485 | req->dataptr = SvPV_nolen (req->data); |
686 | req->dataptr = SvPVbyte_nolen (req->data); |
486 | req->callback = SvREFCNT_inc (callback); |
|
|
487 | |
687 | |
488 | send_req (req); |
688 | send_req (req); |
489 | } |
689 | } |
|
|
690 | |
|
|
691 | void |
|
|
692 | aio_symlink(oldpath,newpath,callback=&PL_sv_undef) |
|
|
693 | SV * oldpath |
|
|
694 | SV * newpath |
|
|
695 | SV * callback |
|
|
696 | CODE: |
|
|
697 | { |
|
|
698 | dREQ; |
|
|
699 | |
|
|
700 | req->type = REQ_SYMLINK; |
|
|
701 | req->fh = newSVsv (oldpath); |
|
|
702 | req->data2ptr = SvPVbyte_nolen (req->fh); |
|
|
703 | req->data = newSVsv (newpath); |
|
|
704 | req->dataptr = SvPVbyte_nolen (req->data); |
|
|
705 | |
|
|
706 | send_req (req); |
|
|
707 | } |
|
|
708 | |
|
|
709 | void |
|
|
710 | flush() |
|
|
711 | PROTOTYPE: |
|
|
712 | CODE: |
|
|
713 | while (nreqs) |
|
|
714 | { |
|
|
715 | poll_wait (); |
|
|
716 | poll_cb (); |
|
|
717 | } |
|
|
718 | |
|
|
719 | void |
|
|
720 | poll() |
|
|
721 | PROTOTYPE: |
|
|
722 | CODE: |
|
|
723 | if (nreqs) |
|
|
724 | { |
|
|
725 | poll_wait (); |
|
|
726 | poll_cb (); |
|
|
727 | } |
490 | |
728 | |
491 | int |
729 | int |
492 | poll_fileno() |
730 | poll_fileno() |
493 | PROTOTYPE: |
731 | PROTOTYPE: |
494 | CODE: |
732 | CODE: |
495 | RETVAL = respipe[0]; |
733 | RETVAL = respipe [0]; |
496 | OUTPUT: |
734 | OUTPUT: |
497 | RETVAL |
735 | RETVAL |
498 | |
736 | |
499 | int |
737 | int |
500 | poll_cb(...) |
738 | poll_cb(...) |
501 | PROTOTYPE: |
739 | PROTOTYPE: |
502 | CODE: |
740 | CODE: |
503 | RETVAL = poll_cb (aTHX); |
741 | RETVAL = poll_cb (); |
504 | OUTPUT: |
742 | OUTPUT: |
505 | RETVAL |
743 | RETVAL |
506 | |
744 | |
507 | void |
745 | void |
508 | poll_wait() |
746 | poll_wait() |
509 | PROTOTYPE: |
747 | PROTOTYPE: |
510 | CODE: |
748 | CODE: |
|
|
749 | if (nreqs) |
511 | poll_wait (); |
750 | poll_wait (); |
512 | |
751 | |
513 | int |
752 | int |
514 | nreqs() |
753 | nreqs() |
515 | PROTOTYPE: |
754 | PROTOTYPE: |
516 | CODE: |
755 | CODE: |