1 | #define PERL_NO_GET_CONTEXT |
|
|
2 | |
|
|
3 | #include "EXTERN.h" |
1 | #include "EXTERN.h" |
4 | #include "perl.h" |
2 | #include "perl.h" |
5 | #include "XSUB.h" |
3 | #include "XSUB.h" |
6 | |
4 | |
7 | #include <sys/types.h> |
5 | #include <sys/types.h> |
… | |
… | |
17 | |
15 | |
18 | typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
16 | 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 ;) */ |
17 | 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 ;) */ |
18 | typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
21 | |
19 | |
22 | #if __i386 || __amd64 |
|
|
23 | # define STACKSIZE ( 256 * sizeof (long)) |
|
|
24 | #elif __ia64 |
20 | #if __ia64 |
25 | # define STACKSIZE (8192 * sizeof (long)) |
21 | # define STACKSIZE 65536 |
26 | #else |
22 | #else |
27 | # define STACKSIZE ( 512 * sizeof (long)) |
23 | # define STACKSIZE 4096 |
28 | #endif |
24 | #endif |
29 | |
25 | |
30 | enum { |
26 | enum { |
31 | REQ_QUIT, |
27 | REQ_QUIT, |
32 | REQ_OPEN, REQ_CLOSE, |
28 | REQ_OPEN, REQ_CLOSE, |
… | |
… | |
55 | |
51 | |
56 | typedef aio_cb *aio_req; |
52 | typedef aio_cb *aio_req; |
57 | |
53 | |
58 | static int started; |
54 | static int started; |
59 | static int nreqs; |
55 | static int nreqs; |
|
|
56 | static int max_outstanding = 1<<30; |
60 | static int respipe [2]; |
57 | static int respipe [2]; |
61 | |
58 | |
62 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
59 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
63 | static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; |
60 | static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; |
64 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
61 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
65 | |
62 | |
66 | static volatile aio_req reqs, reqe; /* queue start, queue end */ |
63 | static volatile aio_req reqs, reqe; /* queue start, queue end */ |
67 | static volatile aio_req ress, rese; /* queue start, queue end */ |
64 | static volatile aio_req ress, rese; /* queue start, queue end */ |
|
|
65 | |
|
|
66 | static void |
|
|
67 | poll_wait () |
|
|
68 | { |
|
|
69 | if (!nreqs) |
|
|
70 | return; |
|
|
71 | |
|
|
72 | fd_set rfd; |
|
|
73 | FD_ZERO(&rfd); |
|
|
74 | FD_SET(respipe [0], &rfd); |
|
|
75 | |
|
|
76 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
|
|
77 | } |
|
|
78 | |
|
|
79 | static int |
|
|
80 | poll_cb () |
|
|
81 | { |
|
|
82 | dSP; |
|
|
83 | int count = 0; |
|
|
84 | aio_req req; |
|
|
85 | |
|
|
86 | { |
|
|
87 | /* read and signals sent by the worker threads */ |
|
|
88 | char buf [32]; |
|
|
89 | while (read (respipe [0], buf, 32) > 0) |
|
|
90 | ; |
|
|
91 | } |
|
|
92 | |
|
|
93 | for (;;) |
|
|
94 | { |
|
|
95 | pthread_mutex_lock (&reslock); |
|
|
96 | |
|
|
97 | req = ress; |
|
|
98 | |
|
|
99 | if (ress) |
|
|
100 | { |
|
|
101 | ress = ress->next; |
|
|
102 | if (!ress) rese = 0; |
|
|
103 | } |
|
|
104 | |
|
|
105 | pthread_mutex_unlock (&reslock); |
|
|
106 | |
|
|
107 | if (!req) |
|
|
108 | break; |
|
|
109 | |
|
|
110 | nreqs--; |
|
|
111 | |
|
|
112 | if (req->type == REQ_QUIT) |
|
|
113 | started--; |
|
|
114 | else |
|
|
115 | { |
|
|
116 | int errorno = errno; |
|
|
117 | errno = req->errorno; |
|
|
118 | |
|
|
119 | if (req->type == REQ_READ) |
|
|
120 | SvCUR_set (req->data, req->dataoffset |
|
|
121 | + req->result > 0 ? req->result : 0); |
|
|
122 | |
|
|
123 | if (req->data) |
|
|
124 | SvREFCNT_dec (req->data); |
|
|
125 | |
|
|
126 | if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) |
|
|
127 | { |
|
|
128 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
|
|
129 | PL_laststatval = req->result; |
|
|
130 | PL_statcache = *(req->statdata); |
|
|
131 | |
|
|
132 | Safefree (req->statdata); |
|
|
133 | } |
|
|
134 | |
|
|
135 | PUSHMARK (SP); |
|
|
136 | XPUSHs (sv_2mortal (newSViv (req->result))); |
|
|
137 | |
|
|
138 | if (req->type == REQ_OPEN) |
|
|
139 | { |
|
|
140 | /* convert fd to fh */ |
|
|
141 | SV *fh; |
|
|
142 | |
|
|
143 | PUTBACK; |
|
|
144 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
|
|
145 | SPAGAIN; |
|
|
146 | |
|
|
147 | fh = POPs; |
|
|
148 | |
|
|
149 | PUSHMARK (SP); |
|
|
150 | XPUSHs (fh); |
|
|
151 | } |
|
|
152 | |
|
|
153 | PUTBACK; |
|
|
154 | call_sv (req->callback, G_VOID | G_EVAL); |
|
|
155 | SPAGAIN; |
|
|
156 | |
|
|
157 | if (req->callback) |
|
|
158 | SvREFCNT_dec (req->callback); |
|
|
159 | |
|
|
160 | errno = errorno; |
|
|
161 | count++; |
|
|
162 | } |
|
|
163 | |
|
|
164 | Safefree (req); |
|
|
165 | } |
|
|
166 | |
|
|
167 | return count; |
|
|
168 | } |
68 | |
169 | |
69 | static void *aio_proc(void *arg); |
170 | static void *aio_proc(void *arg); |
70 | |
171 | |
71 | static void |
172 | static void |
72 | start_thread (void) |
173 | start_thread (void) |
… | |
… | |
105 | else |
206 | else |
106 | reqe = reqs = req; |
207 | reqe = reqs = req; |
107 | |
208 | |
108 | pthread_cond_signal (&reqwait); |
209 | pthread_cond_signal (&reqwait); |
109 | pthread_mutex_unlock (&reqlock); |
210 | pthread_mutex_unlock (&reqlock); |
|
|
211 | |
|
|
212 | while (nreqs > max_outstanding) |
|
|
213 | { |
|
|
214 | poll_wait (); |
|
|
215 | poll_cb (); |
|
|
216 | } |
110 | } |
217 | } |
111 | |
218 | |
112 | static void |
219 | static void |
113 | end_thread (void) |
220 | end_thread (void) |
114 | { |
221 | { |
… | |
… | |
118 | |
225 | |
119 | send_req (req); |
226 | send_req (req); |
120 | } |
227 | } |
121 | |
228 | |
122 | static void |
229 | static void |
123 | read_write (pTHX_ |
|
|
124 | int dowrite, int fd, off_t offset, size_t length, |
230 | read_write (int dowrite, int fd, off_t offset, size_t length, |
125 | SV *data, STRLEN dataoffset, SV *callback) |
231 | SV *data, STRLEN dataoffset, SV *callback) |
126 | { |
232 | { |
127 | aio_req req; |
233 | aio_req req; |
128 | STRLEN svlen; |
234 | STRLEN svlen; |
129 | char *svptr = SvPV (data, svlen); |
235 | char *svptr = SvPV (data, svlen); |
… | |
… | |
166 | req->callback = SvREFCNT_inc (callback); |
272 | req->callback = SvREFCNT_inc (callback); |
167 | |
273 | |
168 | send_req (req); |
274 | send_req (req); |
169 | } |
275 | } |
170 | |
276 | |
171 | static void |
|
|
172 | poll_wait () |
|
|
173 | { |
|
|
174 | if (!nreqs) |
|
|
175 | return; |
|
|
176 | |
|
|
177 | fd_set rfd; |
|
|
178 | FD_ZERO(&rfd); |
|
|
179 | FD_SET(respipe [0], &rfd); |
|
|
180 | |
|
|
181 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
|
|
182 | } |
|
|
183 | |
|
|
184 | static int |
|
|
185 | poll_cb (pTHX) |
|
|
186 | { |
|
|
187 | dSP; |
|
|
188 | int count = 0; |
|
|
189 | aio_req req; |
|
|
190 | |
|
|
191 | { |
|
|
192 | /* read and signals sent by the worker threads */ |
|
|
193 | char buf [32]; |
|
|
194 | while (read (respipe [0], buf, 32) > 0) |
|
|
195 | ; |
|
|
196 | } |
|
|
197 | |
|
|
198 | for (;;) |
|
|
199 | { |
|
|
200 | pthread_mutex_lock (&reslock); |
|
|
201 | |
|
|
202 | req = ress; |
|
|
203 | |
|
|
204 | if (ress) |
|
|
205 | { |
|
|
206 | ress = ress->next; |
|
|
207 | if (!ress) rese = 0; |
|
|
208 | } |
|
|
209 | |
|
|
210 | pthread_mutex_unlock (&reslock); |
|
|
211 | |
|
|
212 | if (!req) |
|
|
213 | break; |
|
|
214 | |
|
|
215 | nreqs--; |
|
|
216 | |
|
|
217 | if (req->type == REQ_QUIT) |
|
|
218 | started--; |
|
|
219 | else |
|
|
220 | { |
|
|
221 | int errorno = errno; |
|
|
222 | errno = req->errorno; |
|
|
223 | |
|
|
224 | if (req->type == REQ_READ) |
|
|
225 | SvCUR_set (req->data, req->dataoffset |
|
|
226 | + req->result > 0 ? req->result : 0); |
|
|
227 | |
|
|
228 | if (req->data) |
|
|
229 | SvREFCNT_dec (req->data); |
|
|
230 | |
|
|
231 | if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) |
|
|
232 | { |
|
|
233 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
|
|
234 | PL_laststatval = req->result; |
|
|
235 | PL_statcache = *(req->statdata); |
|
|
236 | |
|
|
237 | Safefree (req->statdata); |
|
|
238 | } |
|
|
239 | |
|
|
240 | PUSHMARK (SP); |
|
|
241 | XPUSHs (sv_2mortal (newSViv (req->result))); |
|
|
242 | |
|
|
243 | if (req->type == REQ_OPEN) |
|
|
244 | { |
|
|
245 | /* convert fd to fh */ |
|
|
246 | SV *fh; |
|
|
247 | |
|
|
248 | PUTBACK; |
|
|
249 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
|
|
250 | SPAGAIN; |
|
|
251 | |
|
|
252 | fh = POPs; |
|
|
253 | |
|
|
254 | PUSHMARK (SP); |
|
|
255 | XPUSHs (fh); |
|
|
256 | } |
|
|
257 | |
|
|
258 | PUTBACK; |
|
|
259 | call_sv (req->callback, G_VOID | G_EVAL); |
|
|
260 | SPAGAIN; |
|
|
261 | |
|
|
262 | if (req->callback) |
|
|
263 | SvREFCNT_dec (req->callback); |
|
|
264 | |
|
|
265 | errno = errorno; |
|
|
266 | count++; |
|
|
267 | } |
|
|
268 | |
|
|
269 | Safefree (req); |
|
|
270 | } |
|
|
271 | |
|
|
272 | return count; |
|
|
273 | } |
|
|
274 | |
|
|
275 | static void * |
277 | static void * |
276 | aio_proc (void *thr_arg) |
278 | aio_proc (void *thr_arg) |
277 | { |
279 | { |
278 | aio_req req; |
280 | aio_req req; |
279 | int type; |
281 | int type; |
… | |
… | |
395 | } |
397 | } |
396 | |
398 | |
397 | while (started > nthreads) |
399 | while (started > nthreads) |
398 | { |
400 | { |
399 | poll_wait (); |
401 | poll_wait (); |
400 | poll_cb (aTHX); |
402 | poll_cb (); |
401 | } |
403 | } |
402 | } |
404 | } |
|
|
405 | |
|
|
406 | int |
|
|
407 | max_outstanding(nreqs) |
|
|
408 | int nreqs |
|
|
409 | PROTOTYPE: $ |
|
|
410 | CODE: |
|
|
411 | RETVAL = max_outstanding; |
|
|
412 | max_outstanding = nreqs; |
403 | |
413 | |
404 | void |
414 | void |
405 | aio_open(pathname,flags,mode,callback) |
415 | aio_open(pathname,flags,mode,callback) |
406 | SV * pathname |
416 | SV * pathname |
407 | int flags |
417 | int flags |
… | |
… | |
460 | SV * data |
470 | SV * data |
461 | IV dataoffset |
471 | IV dataoffset |
462 | SV * callback |
472 | SV * callback |
463 | PROTOTYPE: $$$$$$ |
473 | PROTOTYPE: $$$$$$ |
464 | CODE: |
474 | CODE: |
465 | read_write (aTHX_ 0, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
475 | read_write (0, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
466 | |
476 | |
467 | void |
477 | void |
468 | aio_write(fh,offset,length,data,dataoffset,callback) |
478 | aio_write(fh,offset,length,data,dataoffset,callback) |
469 | OutputStream fh |
479 | OutputStream fh |
470 | UV offset |
480 | UV offset |
… | |
… | |
472 | SV * data |
482 | SV * data |
473 | IV dataoffset |
483 | IV dataoffset |
474 | SV * callback |
484 | SV * callback |
475 | PROTOTYPE: $$$$$$ |
485 | PROTOTYPE: $$$$$$ |
476 | CODE: |
486 | CODE: |
477 | read_write (aTHX_ 1, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
487 | read_write (1, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
478 | |
488 | |
479 | void |
489 | void |
480 | aio_readahead(fh,offset,length,callback) |
490 | aio_readahead(fh,offset,length,callback) |
481 | InputStream fh |
491 | InputStream fh |
482 | UV offset |
492 | UV offset |
… | |
… | |
574 | |
584 | |
575 | int |
585 | int |
576 | poll_cb(...) |
586 | poll_cb(...) |
577 | PROTOTYPE: |
587 | PROTOTYPE: |
578 | CODE: |
588 | CODE: |
579 | RETVAL = poll_cb (aTHX); |
589 | RETVAL = poll_cb (); |
580 | OUTPUT: |
590 | OUTPUT: |
581 | RETVAL |
591 | RETVAL |
582 | |
592 | |
583 | void |
593 | void |
584 | poll_wait() |
594 | poll_wait() |