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