… | |
… | |
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 | |
123 | if (req->data) |
147 | 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 | { |
148 | { |
131 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
149 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
132 | PL_laststatval = req->result; |
150 | PL_laststatval = req->result; |
133 | PL_statcache = *(req->statdata); |
151 | PL_statcache = *(req->statdata); |
134 | |
|
|
135 | Safefree (req->statdata); |
|
|
136 | } |
152 | } |
137 | |
153 | |
138 | ENTER; |
154 | ENTER; |
139 | PUSHMARK (SP); |
155 | PUSHMARK (SP); |
140 | XPUSHs (sv_2mortal (newSViv (req->result))); |
156 | XPUSHs (sv_2mortal (newSViv (req->result))); |
… | |
… | |
157 | if (SvOK (req->callback)) |
173 | if (SvOK (req->callback)) |
158 | { |
174 | { |
159 | PUTBACK; |
175 | PUTBACK; |
160 | call_sv (req->callback, G_VOID | G_EVAL); |
176 | call_sv (req->callback, G_VOID | G_EVAL); |
161 | SPAGAIN; |
177 | SPAGAIN; |
|
|
178 | |
|
|
179 | if (SvTRUE (ERRSV)) |
|
|
180 | { |
|
|
181 | free_req (req); |
|
|
182 | croak (0); |
|
|
183 | } |
162 | } |
184 | } |
163 | |
185 | |
164 | do_croak = SvTRUE (ERRSV); |
|
|
165 | |
|
|
166 | LEAVE; |
186 | LEAVE; |
167 | |
|
|
168 | if (req->callback) |
|
|
169 | SvREFCNT_dec (req->callback); |
|
|
170 | |
187 | |
171 | errno = errorno; |
188 | errno = errorno; |
172 | count++; |
189 | count++; |
173 | } |
190 | } |
174 | |
191 | |
175 | prv = req; |
192 | free_req (req); |
176 | req = req->next; |
|
|
177 | Safefree (prv); |
|
|
178 | |
|
|
179 | if (do_croak) |
|
|
180 | croak (0); |
|
|
181 | } |
193 | } |
182 | |
194 | |
183 | return count; |
195 | return count; |
184 | } |
196 | } |
185 | |
197 | |
… | |
… | |
223 | reqe = reqs = req; |
235 | reqe = reqs = req; |
224 | |
236 | |
225 | pthread_cond_signal (&reqwait); |
237 | pthread_cond_signal (&reqwait); |
226 | pthread_mutex_unlock (&reqlock); |
238 | pthread_mutex_unlock (&reqlock); |
227 | |
239 | |
228 | while (nreqs > max_outstanding) |
240 | if (nreqs > max_outstanding) |
|
|
241 | for (;;) |
229 | { |
242 | { |
|
|
243 | poll_cb (); |
|
|
244 | |
|
|
245 | if (nreqs <= max_outstanding) |
|
|
246 | break; |
|
|
247 | |
230 | poll_wait (); |
248 | poll_wait (); |
231 | poll_cb (); |
|
|
232 | } |
249 | } |
233 | } |
250 | } |
234 | |
251 | |
235 | static void |
252 | static void |
236 | end_thread (void) |
253 | end_thread (void) |
237 | { |
254 | { |
238 | aio_req req; |
255 | aio_req req; |
239 | New (0, req, 1, aio_cb); |
256 | Newz (0, req, 1, aio_cb); |
240 | req->type = REQ_QUIT; |
257 | req->type = REQ_QUIT; |
241 | |
258 | |
242 | send_req (req); |
259 | send_req (req); |
243 | } |
260 | } |
244 | |
|
|
245 | |
261 | |
246 | static void min_parallel (int nthreads) |
262 | static void min_parallel (int nthreads) |
247 | { |
263 | { |
248 | while (nthreads > started) |
264 | while (nthreads > started) |
249 | start_thread (); |
265 | start_thread (); |
250 | } |
266 | } |
251 | |
267 | |
252 | static void max_parallel (int nthreads) |
268 | static void max_parallel (int nthreads) |
253 | { |
269 | { |
254 | int cur = started; |
270 | int cur = started; |
|
|
271 | |
255 | while (cur > nthreads) |
272 | while (cur > nthreads) |
256 | { |
273 | { |
257 | end_thread (); |
274 | end_thread (); |
258 | cur--; |
275 | cur--; |
259 | } |
276 | } |
… | |
… | |
263 | poll_wait (); |
280 | poll_wait (); |
264 | poll_cb (); |
281 | poll_cb (); |
265 | } |
282 | } |
266 | } |
283 | } |
267 | |
284 | |
268 | 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 | } |
269 | |
296 | |
270 | static void atfork_prepare (void) |
297 | static void atfork_prepare (void) |
271 | { |
298 | { |
272 | int nstarted; |
|
|
273 | |
|
|
274 | for (;;) { |
299 | for (;;) { |
275 | while (nreqs) |
300 | |
|
|
301 | for (;;) |
276 | { |
302 | { |
|
|
303 | poll_cb (); |
|
|
304 | |
|
|
305 | if (!nreqs) |
|
|
306 | break; |
|
|
307 | |
277 | poll_wait (); |
308 | poll_wait (); |
278 | poll_cb (); |
|
|
279 | } |
309 | } |
280 | |
310 | |
281 | nstarted = started; |
|
|
282 | max_parallel (0); |
|
|
283 | |
|
|
284 | pthread_mutex_lock (&reqlock); |
311 | pthread_mutex_lock (&reqlock); |
285 | |
312 | |
286 | if (!nreqs && !started) |
313 | if (!nreqs) |
287 | break; |
314 | break; |
288 | |
315 | |
289 | pthread_mutex_unlock (&reqlock); |
316 | pthread_mutex_unlock (&reqlock); |
290 | |
|
|
291 | min_parallel (fork_started); |
|
|
292 | } |
317 | } |
293 | |
318 | |
294 | pthread_mutex_lock (&frklock); |
|
|
295 | fork_started = nstarted; |
|
|
296 | pthread_mutex_lock (&reslock); |
319 | pthread_mutex_lock (&reslock); |
297 | |
320 | |
298 | assert (!started); |
|
|
299 | assert (!nreqs); |
|
|
300 | assert (!reqs && !reqe); |
321 | assert (!nreqs && !reqs && !ress); |
301 | assert (!ress && !rese); |
|
|
302 | } |
322 | } |
303 | |
323 | |
304 | static void atfork_parent (void) |
324 | static void atfork_parent (void) |
305 | { |
325 | { |
306 | pthread_mutex_unlock (&reslock); |
326 | pthread_mutex_unlock (&reslock); |
307 | pthread_mutex_unlock (&frklock); |
|
|
308 | pthread_mutex_unlock (&reqlock); |
327 | pthread_mutex_unlock (&reqlock); |
|
|
328 | } |
309 | |
329 | |
|
|
330 | static void atfork_child (void) |
|
|
331 | { |
|
|
332 | int restart = started; |
|
|
333 | started = 0; |
|
|
334 | |
|
|
335 | atfork_parent (); |
|
|
336 | |
310 | min_parallel (fork_started); |
337 | min_parallel (restart); |
311 | } |
338 | } |
312 | |
|
|
313 | #define atfork_child atfork_parent |
|
|
314 | |
339 | |
315 | /*****************************************************************************/ |
340 | /*****************************************************************************/ |
316 | /* work around various missing functions */ |
341 | /* work around various missing functions */ |
317 | |
342 | |
318 | #if !HAVE_PREADWRITE |
343 | #if !HAVE_PREADWRITE |
… | |
… | |
480 | \ |
505 | \ |
481 | Newz (0, req, 1, aio_cb); \ |
506 | Newz (0, req, 1, aio_cb); \ |
482 | if (!req) \ |
507 | if (!req) \ |
483 | croak ("out of memory during aio_req allocation"); \ |
508 | croak ("out of memory during aio_req allocation"); \ |
484 | \ |
509 | \ |
485 | req->callback = SvREFCNT_inc (callback); |
510 | req->callback = newSVsv (callback); |
486 | |
511 | |
487 | MODULE = IO::AIO PACKAGE = IO::AIO |
512 | MODULE = IO::AIO PACKAGE = IO::AIO |
488 | |
513 | |
489 | PROTOTYPES: ENABLE |
514 | PROTOTYPES: ENABLE |
490 | |
515 | |
491 | BOOT: |
516 | BOOT: |
492 | { |
517 | { |
493 | if (pipe (respipe)) |
518 | create_pipe (); |
494 | croak ("unable to initialize result pipe"); |
|
|
495 | |
|
|
496 | if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)) |
|
|
497 | croak ("cannot set result pipe to nonblocking mode"); |
|
|
498 | |
|
|
499 | if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) |
|
|
500 | croak ("cannot set result pipe to nonblocking mode"); |
|
|
501 | |
|
|
502 | pthread_atfork (atfork_prepare, atfork_parent, atfork_child); |
519 | pthread_atfork (atfork_prepare, atfork_parent, atfork_child); |
503 | } |
520 | } |
504 | |
521 | |
505 | void |
522 | void |
506 | min_parallel(nthreads) |
523 | min_parallel(nthreads) |
… | |
… | |
611 | : IoOFP (sv_2io (fh))); |
628 | : IoOFP (sv_2io (fh))); |
612 | req->offset = offset; |
629 | req->offset = offset; |
613 | req->length = length; |
630 | req->length = length; |
614 | req->data = SvREFCNT_inc (data); |
631 | req->data = SvREFCNT_inc (data); |
615 | req->dataptr = (char *)svptr + dataoffset; |
632 | req->dataptr = (char *)svptr + dataoffset; |
616 | req->callback = SvREFCNT_inc (callback); |
|
|
617 | |
633 | |
618 | send_req (req); |
634 | send_req (req); |
619 | } |
635 | } |
620 | } |
636 | } |
621 | |
637 | |
… | |
… | |
650 | { |
666 | { |
651 | dREQ; |
667 | dREQ; |
652 | |
668 | |
653 | New (0, req->statdata, 1, Stat_t); |
669 | New (0, req->statdata, 1, Stat_t); |
654 | if (!req->statdata) |
670 | if (!req->statdata) |
|
|
671 | { |
|
|
672 | free_req (req); |
655 | 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 | } |
656 | |
675 | |
657 | if (SvPOK (fh_or_path)) |
676 | if (SvPOK (fh_or_path)) |
658 | { |
677 | { |
659 | req->type = ix; |
678 | req->type = ix; |
660 | req->data = newSVsv (fh_or_path); |
679 | req->data = newSVsv (fh_or_path); |