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