… | |
… | |
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) |
… | |
… | |
87 | poll_cb () |
103 | poll_cb () |
88 | { |
104 | { |
89 | dSP; |
105 | dSP; |
90 | int count = 0; |
106 | int count = 0; |
91 | int do_croak = 0; |
107 | int do_croak = 0; |
92 | aio_req req, prv; |
108 | aio_req req; |
93 | |
109 | |
94 | pthread_mutex_lock (&reslock); |
110 | for (;;) |
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) |
|
|
109 | { |
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 | |
110 | nreqs--; |
135 | nreqs--; |
111 | |
136 | |
112 | if (req->type == REQ_QUIT) |
137 | if (req->type == REQ_QUIT) |
113 | started--; |
138 | started--; |
114 | else |
139 | else |
115 | { |
140 | { |
116 | int errorno = errno; |
141 | int errorno = errno; |
117 | errno = req->errorno; |
142 | errno = req->errorno; |
118 | |
143 | |
119 | if (req->type == REQ_READ) |
144 | if (req->type == REQ_READ) |
120 | SvCUR_set (req->data, req->dataoffset |
145 | SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); |
121 | + req->result > 0 ? req->result : 0); |
|
|
122 | |
146 | |
|
|
147 | if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE)) |
|
|
148 | SvREADONLY_off (req->data); |
|
|
149 | |
123 | if (req->data) |
150 | if (req->statdata) |
124 | SvREFCNT_dec (req->data); |
|
|
125 | |
|
|
126 | if (req->fh) |
|
|
127 | SvREFCNT_dec (req->fh); |
|
|
128 | |
|
|
129 | if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) |
|
|
130 | { |
151 | { |
131 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
152 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
132 | PL_laststatval = req->result; |
153 | PL_laststatval = req->result; |
133 | PL_statcache = *(req->statdata); |
154 | PL_statcache = *(req->statdata); |
134 | |
|
|
135 | Safefree (req->statdata); |
|
|
136 | } |
155 | } |
137 | |
156 | |
138 | ENTER; |
157 | ENTER; |
139 | PUSHMARK (SP); |
158 | PUSHMARK (SP); |
140 | XPUSHs (sv_2mortal (newSViv (req->result))); |
159 | XPUSHs (sv_2mortal (newSViv (req->result))); |
… | |
… | |
157 | if (SvOK (req->callback)) |
176 | if (SvOK (req->callback)) |
158 | { |
177 | { |
159 | PUTBACK; |
178 | PUTBACK; |
160 | call_sv (req->callback, G_VOID | G_EVAL); |
179 | call_sv (req->callback, G_VOID | G_EVAL); |
161 | SPAGAIN; |
180 | SPAGAIN; |
|
|
181 | |
|
|
182 | if (SvTRUE (ERRSV)) |
|
|
183 | { |
|
|
184 | free_req (req); |
|
|
185 | croak (0); |
|
|
186 | } |
162 | } |
187 | } |
163 | |
188 | |
164 | do_croak = SvTRUE (ERRSV); |
|
|
165 | |
|
|
166 | LEAVE; |
189 | LEAVE; |
167 | |
|
|
168 | if (req->callback) |
|
|
169 | SvREFCNT_dec (req->callback); |
|
|
170 | |
190 | |
171 | errno = errorno; |
191 | errno = errorno; |
172 | count++; |
192 | count++; |
173 | } |
193 | } |
174 | |
194 | |
175 | prv = req; |
195 | free_req (req); |
176 | req = req->next; |
|
|
177 | Safefree (prv); |
|
|
178 | |
|
|
179 | if (do_croak) |
|
|
180 | croak (0); |
|
|
181 | } |
196 | } |
182 | |
197 | |
183 | return count; |
198 | return count; |
184 | } |
199 | } |
185 | |
200 | |
… | |
… | |
223 | reqe = reqs = req; |
238 | reqe = reqs = req; |
224 | |
239 | |
225 | pthread_cond_signal (&reqwait); |
240 | pthread_cond_signal (&reqwait); |
226 | pthread_mutex_unlock (&reqlock); |
241 | pthread_mutex_unlock (&reqlock); |
227 | |
242 | |
228 | while (nreqs > max_outstanding) |
243 | if (nreqs > max_outstanding) |
|
|
244 | for (;;) |
229 | { |
245 | { |
|
|
246 | poll_cb (); |
|
|
247 | |
|
|
248 | if (nreqs <= max_outstanding) |
|
|
249 | break; |
|
|
250 | |
230 | poll_wait (); |
251 | poll_wait (); |
231 | poll_cb (); |
|
|
232 | } |
252 | } |
233 | } |
253 | } |
234 | |
254 | |
235 | static void |
255 | static void |
236 | end_thread (void) |
256 | end_thread (void) |
237 | { |
257 | { |
238 | aio_req req; |
258 | aio_req req; |
239 | New (0, req, 1, aio_cb); |
259 | Newz (0, req, 1, aio_cb); |
240 | req->type = REQ_QUIT; |
260 | req->type = REQ_QUIT; |
241 | |
261 | |
242 | send_req (req); |
262 | send_req (req); |
243 | } |
263 | } |
244 | |
|
|
245 | |
264 | |
246 | static void min_parallel (int nthreads) |
265 | static void min_parallel (int nthreads) |
247 | { |
266 | { |
248 | while (nthreads > started) |
267 | while (nthreads > started) |
249 | start_thread (); |
268 | start_thread (); |
250 | } |
269 | } |
251 | |
270 | |
252 | static void max_parallel (int nthreads) |
271 | static void max_parallel (int nthreads) |
253 | { |
272 | { |
254 | int cur = started; |
273 | int cur = started; |
|
|
274 | |
255 | while (cur > nthreads) |
275 | while (cur > nthreads) |
256 | { |
276 | { |
257 | end_thread (); |
277 | end_thread (); |
258 | cur--; |
278 | cur--; |
259 | } |
279 | } |
… | |
… | |
263 | poll_wait (); |
283 | poll_wait (); |
264 | poll_cb (); |
284 | poll_cb (); |
265 | } |
285 | } |
266 | } |
286 | } |
267 | |
287 | |
268 | static int fork_started; |
288 | static void create_pipe () |
|
|
289 | { |
|
|
290 | if (pipe (respipe)) |
|
|
291 | croak ("unable to initialize result pipe"); |
|
|
292 | |
|
|
293 | if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)) |
|
|
294 | croak ("cannot set result pipe to nonblocking mode"); |
|
|
295 | |
|
|
296 | if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) |
|
|
297 | croak ("cannot set result pipe to nonblocking mode"); |
|
|
298 | } |
269 | |
299 | |
270 | static void atfork_prepare (void) |
300 | static void atfork_prepare (void) |
271 | { |
301 | { |
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); |
302 | 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); |
303 | pthread_mutex_lock (&reslock); |
296 | |
|
|
297 | assert (!started); |
|
|
298 | assert (!nreqs); |
|
|
299 | assert (!reqs && !reqe); |
|
|
300 | assert (!ress && !rese); |
|
|
301 | } |
304 | } |
302 | |
305 | |
303 | static void atfork_parent (void) |
306 | static void atfork_parent (void) |
304 | { |
307 | { |
305 | pthread_mutex_unlock (&reslock); |
308 | pthread_mutex_unlock (&reslock); |
306 | min_parallel (fork_started); |
|
|
307 | pthread_mutex_unlock (&reqlock); |
309 | pthread_mutex_unlock (&reqlock); |
308 | pthread_mutex_unlock (&frklock); |
|
|
309 | } |
310 | } |
310 | |
311 | |
311 | static void atfork_child (void) |
312 | static void atfork_child (void) |
312 | { |
313 | { |
|
|
314 | aio_req prv; |
|
|
315 | |
|
|
316 | int restart = started; |
|
|
317 | started = 0; |
|
|
318 | |
|
|
319 | while (reqs) |
|
|
320 | { |
|
|
321 | prv = reqs; |
|
|
322 | reqs = prv->next; |
|
|
323 | free_req (prv); |
|
|
324 | } |
|
|
325 | |
313 | reqs = reqe = 0; |
326 | reqs = reqe = 0; |
|
|
327 | |
|
|
328 | while (ress) |
|
|
329 | { |
|
|
330 | prv = ress; |
|
|
331 | ress = prv->next; |
|
|
332 | free_req (prv); |
|
|
333 | } |
|
|
334 | |
|
|
335 | ress = rese = 0; |
|
|
336 | |
|
|
337 | close (respipe [0]); |
|
|
338 | close (respipe [1]); |
|
|
339 | create_pipe (); |
314 | |
340 | |
315 | atfork_parent (); |
341 | atfork_parent (); |
|
|
342 | |
|
|
343 | min_parallel (restart); |
316 | } |
344 | } |
317 | |
345 | |
318 | /*****************************************************************************/ |
346 | /*****************************************************************************/ |
319 | /* work around various missing functions */ |
347 | /* work around various missing functions */ |
320 | |
348 | |
… | |
… | |
483 | \ |
511 | \ |
484 | Newz (0, req, 1, aio_cb); \ |
512 | Newz (0, req, 1, aio_cb); \ |
485 | if (!req) \ |
513 | if (!req) \ |
486 | croak ("out of memory during aio_req allocation"); \ |
514 | croak ("out of memory during aio_req allocation"); \ |
487 | \ |
515 | \ |
488 | req->callback = SvREFCNT_inc (callback); |
516 | req->callback = newSVsv (callback); |
489 | |
517 | |
490 | MODULE = IO::AIO PACKAGE = IO::AIO |
518 | MODULE = IO::AIO PACKAGE = IO::AIO |
491 | |
519 | |
492 | PROTOTYPES: ENABLE |
520 | PROTOTYPES: ENABLE |
493 | |
521 | |
494 | BOOT: |
522 | BOOT: |
495 | { |
523 | { |
496 | if (pipe (respipe)) |
524 | create_pipe (); |
497 | croak ("unable to initialize result pipe"); |
|
|
498 | |
|
|
499 | if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)) |
|
|
500 | croak ("cannot set result pipe to nonblocking mode"); |
|
|
501 | |
|
|
502 | if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) |
|
|
503 | croak ("cannot set result pipe to nonblocking mode"); |
|
|
504 | |
|
|
505 | pthread_atfork (atfork_prepare, atfork_parent, atfork_child); |
525 | pthread_atfork (atfork_prepare, atfork_parent, atfork_child); |
506 | } |
526 | } |
507 | |
527 | |
508 | void |
528 | void |
509 | min_parallel(nthreads) |
529 | min_parallel(nthreads) |
… | |
… | |
614 | : IoOFP (sv_2io (fh))); |
634 | : IoOFP (sv_2io (fh))); |
615 | req->offset = offset; |
635 | req->offset = offset; |
616 | req->length = length; |
636 | req->length = length; |
617 | req->data = SvREFCNT_inc (data); |
637 | req->data = SvREFCNT_inc (data); |
618 | req->dataptr = (char *)svptr + dataoffset; |
638 | req->dataptr = (char *)svptr + dataoffset; |
619 | req->callback = SvREFCNT_inc (callback); |
639 | |
|
|
640 | if (!SvREADONLY (data)) |
|
|
641 | { |
|
|
642 | SvREADONLY_on (data); |
|
|
643 | req->data2ptr = (void *)data; |
|
|
644 | } |
620 | |
645 | |
621 | send_req (req); |
646 | send_req (req); |
622 | } |
647 | } |
623 | } |
648 | } |
624 | |
649 | |
… | |
… | |
653 | { |
678 | { |
654 | dREQ; |
679 | dREQ; |
655 | |
680 | |
656 | New (0, req->statdata, 1, Stat_t); |
681 | New (0, req->statdata, 1, Stat_t); |
657 | if (!req->statdata) |
682 | if (!req->statdata) |
|
|
683 | { |
|
|
684 | free_req (req); |
658 | croak ("out of memory during aio_req->statdata allocation (sorry, i just leaked memory, too)"); |
685 | croak ("out of memory during aio_req->statdata allocation"); |
|
|
686 | } |
659 | |
687 | |
660 | if (SvPOK (fh_or_path)) |
688 | if (SvPOK (fh_or_path)) |
661 | { |
689 | { |
662 | req->type = ix; |
690 | req->type = ix; |
663 | req->data = newSVsv (fh_or_path); |
691 | req->data = newSVsv (fh_or_path); |