… | |
… | |
62 | static int max_outstanding = 1<<30; |
62 | static int max_outstanding = 1<<30; |
63 | static int respipe [2]; |
63 | static int respipe [2]; |
64 | |
64 | |
65 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
65 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
66 | static pthread_mutex_t reqlock = 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; |
67 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
69 | |
68 | |
70 | static volatile aio_req reqs, reqe; /* queue start, queue end */ |
69 | static volatile aio_req reqs, reqe; /* queue start, queue end */ |
71 | static volatile aio_req ress, rese; /* queue start, queue end */ |
70 | static volatile aio_req ress, rese; /* queue start, queue end */ |
|
|
71 | |
|
|
72 | static void free_req (aio_req req) |
|
|
73 | { |
|
|
74 | if (req->data) |
|
|
75 | SvREFCNT_dec (req->data); |
|
|
76 | |
|
|
77 | if (req->fh) |
|
|
78 | SvREFCNT_dec (req->fh); |
|
|
79 | |
|
|
80 | if (req->statdata) |
|
|
81 | Safefree (req->statdata); |
|
|
82 | |
|
|
83 | if (req->callback) |
|
|
84 | SvREFCNT_dec (req->callback); |
|
|
85 | |
|
|
86 | Safefree (req); |
|
|
87 | } |
72 | |
88 | |
73 | static void |
89 | static void |
74 | poll_wait () |
90 | poll_wait () |
75 | { |
91 | { |
76 | if (nreqs && !ress) |
92 | if (nreqs && !ress) |
… | |
… | |
86 | static int |
102 | static int |
87 | poll_cb () |
103 | poll_cb () |
88 | { |
104 | { |
89 | dSP; |
105 | dSP; |
90 | int count = 0; |
106 | int count = 0; |
|
|
107 | int do_croak = 0; |
91 | aio_req req, prv; |
108 | aio_req req; |
92 | |
109 | |
93 | pthread_mutex_lock (&reslock); |
110 | for (;;) |
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) |
|
|
108 | { |
111 | { |
|
|
112 | pthread_mutex_lock (&reslock); |
|
|
113 | req = ress; |
|
|
114 | |
|
|
115 | if (req) |
|
|
116 | { |
|
|
117 | ress = req->next; |
|
|
118 | |
|
|
119 | if (!ress) |
|
|
120 | { |
|
|
121 | rese = 0; |
|
|
122 | |
|
|
123 | /* read any signals sent by the worker threads */ |
|
|
124 | char buf [32]; |
|
|
125 | while (read (respipe [0], buf, 32) == 32) |
|
|
126 | ; |
|
|
127 | } |
|
|
128 | } |
|
|
129 | |
|
|
130 | pthread_mutex_unlock (&reslock); |
|
|
131 | |
|
|
132 | if (!req) |
|
|
133 | break; |
|
|
134 | |
109 | nreqs--; |
135 | nreqs--; |
110 | |
136 | |
111 | if (req->type == REQ_QUIT) |
137 | if (req->type == REQ_QUIT) |
112 | started--; |
138 | started--; |
113 | else |
139 | else |
114 | { |
140 | { |
115 | int errorno = errno; |
141 | int errorno = errno; |
116 | errno = req->errorno; |
142 | errno = req->errorno; |
117 | |
143 | |
118 | if (req->type == REQ_READ) |
144 | if (req->type == REQ_READ) |
119 | SvCUR_set (req->data, req->dataoffset |
145 | SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); |
120 | + req->result > 0 ? req->result : 0); |
|
|
121 | |
146 | |
122 | if (req->data) |
147 | if (req->statdata) |
123 | SvREFCNT_dec (req->data); |
|
|
124 | |
|
|
125 | if (req->fh) |
|
|
126 | SvREFCNT_dec (req->fh); |
|
|
127 | |
|
|
128 | if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) |
|
|
129 | { |
148 | { |
130 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
149 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
131 | PL_laststatval = req->result; |
150 | PL_laststatval = req->result; |
132 | PL_statcache = *(req->statdata); |
151 | PL_statcache = *(req->statdata); |
133 | |
|
|
134 | Safefree (req->statdata); |
|
|
135 | } |
152 | } |
136 | |
153 | |
137 | ENTER; |
154 | ENTER; |
138 | PUSHMARK (SP); |
155 | PUSHMARK (SP); |
139 | XPUSHs (sv_2mortal (newSViv (req->result))); |
156 | XPUSHs (sv_2mortal (newSViv (req->result))); |
… | |
… | |
156 | if (SvOK (req->callback)) |
173 | if (SvOK (req->callback)) |
157 | { |
174 | { |
158 | PUTBACK; |
175 | PUTBACK; |
159 | call_sv (req->callback, G_VOID | G_EVAL); |
176 | call_sv (req->callback, G_VOID | G_EVAL); |
160 | SPAGAIN; |
177 | SPAGAIN; |
|
|
178 | |
|
|
179 | if (SvTRUE (ERRSV)) |
|
|
180 | { |
|
|
181 | free_req (req); |
|
|
182 | croak (0); |
|
|
183 | } |
161 | } |
184 | } |
162 | |
185 | |
163 | LEAVE; |
186 | LEAVE; |
164 | |
|
|
165 | if (req->callback) |
|
|
166 | SvREFCNT_dec (req->callback); |
|
|
167 | |
187 | |
168 | errno = errorno; |
188 | errno = errorno; |
169 | count++; |
189 | count++; |
170 | } |
190 | } |
171 | |
191 | |
172 | prv = req; |
192 | free_req (req); |
173 | req = req->next; |
|
|
174 | Safefree (prv); |
|
|
175 | |
|
|
176 | /* TODO: croak on errors? */ |
|
|
177 | } |
193 | } |
178 | |
194 | |
179 | return count; |
195 | return count; |
180 | } |
196 | } |
181 | |
197 | |
… | |
… | |
219 | reqe = reqs = req; |
235 | reqe = reqs = req; |
220 | |
236 | |
221 | pthread_cond_signal (&reqwait); |
237 | pthread_cond_signal (&reqwait); |
222 | pthread_mutex_unlock (&reqlock); |
238 | pthread_mutex_unlock (&reqlock); |
223 | |
239 | |
224 | while (nreqs > max_outstanding) |
240 | if (nreqs > max_outstanding) |
|
|
241 | for (;;) |
225 | { |
242 | { |
|
|
243 | poll_cb (); |
|
|
244 | |
|
|
245 | if (nreqs <= max_outstanding) |
|
|
246 | break; |
|
|
247 | |
226 | poll_wait (); |
248 | poll_wait (); |
227 | poll_cb (); |
|
|
228 | } |
249 | } |
229 | } |
250 | } |
230 | |
251 | |
231 | static void |
252 | static void |
232 | end_thread (void) |
253 | end_thread (void) |
233 | { |
254 | { |
234 | aio_req req; |
255 | aio_req req; |
235 | New (0, req, 1, aio_cb); |
256 | Newz (0, req, 1, aio_cb); |
236 | req->type = REQ_QUIT; |
257 | req->type = REQ_QUIT; |
237 | |
258 | |
238 | send_req (req); |
259 | send_req (req); |
239 | } |
260 | } |
240 | |
|
|
241 | |
261 | |
242 | static void min_parallel (int nthreads) |
262 | static void min_parallel (int nthreads) |
243 | { |
263 | { |
244 | while (nthreads > started) |
264 | while (nthreads > started) |
245 | start_thread (); |
265 | start_thread (); |
246 | } |
266 | } |
247 | |
267 | |
248 | static void max_parallel (int nthreads) |
268 | static void max_parallel (int nthreads) |
249 | { |
269 | { |
250 | int cur = started; |
270 | int cur = started; |
|
|
271 | |
251 | while (cur > nthreads) |
272 | while (cur > nthreads) |
252 | { |
273 | { |
253 | end_thread (); |
274 | end_thread (); |
254 | cur--; |
275 | cur--; |
255 | } |
276 | } |
… | |
… | |
259 | poll_wait (); |
280 | poll_wait (); |
260 | poll_cb (); |
281 | poll_cb (); |
261 | } |
282 | } |
262 | } |
283 | } |
263 | |
284 | |
264 | static int fork_started; |
285 | static void create_pipe () |
|
|
286 | { |
|
|
287 | if (pipe (respipe)) |
|
|
288 | croak ("unable to initialize result pipe"); |
|
|
289 | |
|
|
290 | if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)) |
|
|
291 | croak ("cannot set result pipe to nonblocking mode"); |
|
|
292 | |
|
|
293 | if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) |
|
|
294 | croak ("cannot set result pipe to nonblocking mode"); |
|
|
295 | } |
265 | |
296 | |
266 | static void atfork_prepare (void) |
297 | static void atfork_prepare (void) |
267 | { |
298 | { |
268 | pthread_mutex_lock (&frklock); |
|
|
269 | |
|
|
270 | fork_started = started; |
|
|
271 | |
|
|
272 | for (;;) { |
299 | for (;;) { |
273 | while (nreqs) |
300 | |
|
|
301 | for (;;) |
274 | { |
302 | { |
|
|
303 | poll_cb (); |
|
|
304 | |
|
|
305 | if (!nreqs) |
|
|
306 | break; |
|
|
307 | |
275 | poll_wait (); |
308 | poll_wait (); |
276 | poll_cb (); |
|
|
277 | } |
309 | } |
278 | |
310 | |
279 | max_parallel (0); |
|
|
280 | |
|
|
281 | pthread_mutex_lock (&reqlock); |
311 | pthread_mutex_lock (&reqlock); |
282 | |
312 | |
283 | if (!nreqs && !started) |
313 | if (!nreqs) |
284 | break; |
314 | break; |
285 | |
315 | |
286 | pthread_mutex_unlock (&reqlock); |
316 | pthread_mutex_unlock (&reqlock); |
287 | |
|
|
288 | min_parallel (fork_started); |
|
|
289 | } |
317 | } |
290 | |
318 | |
291 | pthread_mutex_lock (&reslock); |
319 | pthread_mutex_lock (&reslock); |
292 | |
320 | |
293 | assert (!started); |
|
|
294 | assert (!nreqs); |
|
|
295 | assert (!reqs && !reqe); |
321 | assert (!nreqs && !reqs && !ress); |
296 | assert (!ress && !rese); |
|
|
297 | } |
322 | } |
298 | |
323 | |
299 | static void atfork_parent (void) |
324 | static void atfork_parent (void) |
300 | { |
325 | { |
301 | pthread_mutex_unlock (&reslock); |
326 | pthread_mutex_unlock (&reslock); |
302 | min_parallel (fork_started); |
|
|
303 | pthread_mutex_unlock (&reqlock); |
327 | pthread_mutex_unlock (&reqlock); |
304 | pthread_mutex_unlock (&frklock); |
|
|
305 | } |
328 | } |
306 | |
329 | |
307 | static void atfork_child (void) |
330 | static void atfork_child (void) |
308 | { |
331 | { |
309 | reqs = reqe = 0; |
332 | int restart = started; |
|
|
333 | started = 0; |
310 | |
334 | |
311 | atfork_parent (); |
335 | atfork_parent (); |
|
|
336 | |
|
|
337 | min_parallel (restart); |
312 | } |
338 | } |
313 | |
339 | |
314 | /*****************************************************************************/ |
340 | /*****************************************************************************/ |
315 | /* work around various missing functions */ |
341 | /* work around various missing functions */ |
316 | |
342 | |
… | |
… | |
479 | \ |
505 | \ |
480 | Newz (0, req, 1, aio_cb); \ |
506 | Newz (0, req, 1, aio_cb); \ |
481 | if (!req) \ |
507 | if (!req) \ |
482 | croak ("out of memory during aio_req allocation"); \ |
508 | croak ("out of memory during aio_req allocation"); \ |
483 | \ |
509 | \ |
484 | req->callback = SvREFCNT_inc (callback); |
510 | req->callback = newSVsv (callback); |
485 | |
511 | |
486 | MODULE = IO::AIO PACKAGE = IO::AIO |
512 | MODULE = IO::AIO PACKAGE = IO::AIO |
487 | |
513 | |
488 | PROTOTYPES: ENABLE |
514 | PROTOTYPES: ENABLE |
489 | |
515 | |
490 | BOOT: |
516 | BOOT: |
491 | { |
517 | { |
492 | if (pipe (respipe)) |
518 | create_pipe (); |
493 | croak ("unable to initialize result pipe"); |
|
|
494 | |
|
|
495 | if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)) |
|
|
496 | croak ("cannot set result pipe to nonblocking mode"); |
|
|
497 | |
|
|
498 | if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) |
|
|
499 | croak ("cannot set result pipe to nonblocking mode"); |
|
|
500 | |
|
|
501 | pthread_atfork (atfork_prepare, atfork_parent, atfork_child); |
519 | pthread_atfork (atfork_prepare, atfork_parent, atfork_child); |
502 | } |
520 | } |
503 | |
521 | |
504 | void |
522 | void |
505 | min_parallel(nthreads) |
523 | min_parallel(nthreads) |
… | |
… | |
610 | : IoOFP (sv_2io (fh))); |
628 | : IoOFP (sv_2io (fh))); |
611 | req->offset = offset; |
629 | req->offset = offset; |
612 | req->length = length; |
630 | req->length = length; |
613 | req->data = SvREFCNT_inc (data); |
631 | req->data = SvREFCNT_inc (data); |
614 | req->dataptr = (char *)svptr + dataoffset; |
632 | req->dataptr = (char *)svptr + dataoffset; |
615 | req->callback = SvREFCNT_inc (callback); |
|
|
616 | |
633 | |
617 | send_req (req); |
634 | send_req (req); |
618 | } |
635 | } |
619 | } |
636 | } |
620 | |
637 | |
… | |
… | |
649 | { |
666 | { |
650 | dREQ; |
667 | dREQ; |
651 | |
668 | |
652 | New (0, req->statdata, 1, Stat_t); |
669 | New (0, req->statdata, 1, Stat_t); |
653 | if (!req->statdata) |
670 | if (!req->statdata) |
|
|
671 | { |
|
|
672 | free_req (req); |
654 | croak ("out of memory during aio_req->statdata allocation (sorry, i just leaked memory, too)"); |
673 | croak ("out of memory during aio_req->statdata allocation"); |
|
|
674 | } |
655 | |
675 | |
656 | if (SvPOK (fh_or_path)) |
676 | if (SvPOK (fh_or_path)) |
657 | { |
677 | { |
658 | req->type = ix; |
678 | req->type = ix; |
659 | req->data = newSVsv (fh_or_path); |
679 | req->data = newSVsv (fh_or_path); |