… | |
… | |
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 */ |
72 | |
71 | |
… | |
… | |
76 | SvREFCNT_dec (req->data); |
75 | SvREFCNT_dec (req->data); |
77 | |
76 | |
78 | if (req->fh) |
77 | if (req->fh) |
79 | SvREFCNT_dec (req->fh); |
78 | SvREFCNT_dec (req->fh); |
80 | |
79 | |
81 | if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) |
80 | if (req->statdata) |
82 | Safefree (req->statdata); |
81 | Safefree (req->statdata); |
83 | |
82 | |
84 | if (req->callback) |
83 | if (req->callback) |
85 | SvREFCNT_dec (req->callback); |
84 | SvREFCNT_dec (req->callback); |
86 | |
85 | |
… | |
… | |
104 | poll_cb () |
103 | poll_cb () |
105 | { |
104 | { |
106 | dSP; |
105 | dSP; |
107 | int count = 0; |
106 | int count = 0; |
108 | int do_croak = 0; |
107 | int do_croak = 0; |
109 | aio_req req, prv; |
108 | aio_req req; |
110 | |
109 | |
111 | pthread_mutex_lock (&reslock); |
110 | for (;;) |
112 | |
|
|
113 | { |
|
|
114 | /* read any signals sent by the worker threads */ |
|
|
115 | char buf [32]; |
|
|
116 | while (read (respipe [0], buf, 32) == 32) |
|
|
117 | ; |
|
|
118 | } |
|
|
119 | |
|
|
120 | req = ress; |
|
|
121 | ress = rese = 0; |
|
|
122 | |
|
|
123 | pthread_mutex_unlock (&reslock); |
|
|
124 | |
|
|
125 | while (req) |
|
|
126 | { |
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 | |
127 | nreqs--; |
135 | nreqs--; |
128 | |
136 | |
129 | if (req->type == REQ_QUIT) |
137 | if (req->type == REQ_QUIT) |
130 | started--; |
138 | started--; |
131 | else |
139 | else |
132 | { |
140 | { |
133 | int errorno = errno; |
141 | int errorno = errno; |
134 | errno = req->errorno; |
142 | errno = req->errorno; |
135 | |
143 | |
136 | if (req->type == REQ_READ) |
144 | if (req->type == REQ_READ) |
137 | SvCUR_set (req->data, req->dataoffset |
145 | SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); |
138 | + req->result > 0 ? req->result : 0); |
|
|
139 | |
146 | |
140 | if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) |
147 | if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE)) |
|
|
148 | SvREADONLY_off (req->data); |
|
|
149 | |
|
|
150 | if (req->statdata) |
141 | { |
151 | { |
142 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
152 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
143 | PL_laststatval = req->result; |
153 | PL_laststatval = req->result; |
144 | PL_statcache = *(req->statdata); |
154 | PL_statcache = *(req->statdata); |
145 | } |
155 | } |
… | |
… | |
166 | if (SvOK (req->callback)) |
176 | if (SvOK (req->callback)) |
167 | { |
177 | { |
168 | PUTBACK; |
178 | PUTBACK; |
169 | call_sv (req->callback, G_VOID | G_EVAL); |
179 | call_sv (req->callback, G_VOID | G_EVAL); |
170 | SPAGAIN; |
180 | SPAGAIN; |
|
|
181 | |
|
|
182 | if (SvTRUE (ERRSV)) |
|
|
183 | { |
|
|
184 | free_req (req); |
|
|
185 | croak (0); |
|
|
186 | } |
171 | } |
187 | } |
172 | |
188 | |
173 | LEAVE; |
189 | LEAVE; |
174 | |
190 | |
175 | do_croak = SvTRUE (ERRSV); |
|
|
176 | |
|
|
177 | errno = errorno; |
191 | errno = errorno; |
178 | count++; |
192 | count++; |
179 | } |
193 | } |
180 | |
194 | |
181 | prv = req; |
|
|
182 | req = req->next; |
|
|
183 | free_req (prv); |
195 | free_req (req); |
184 | |
|
|
185 | if (do_croak) |
|
|
186 | croak (0); |
|
|
187 | } |
196 | } |
188 | |
197 | |
189 | return count; |
198 | return count; |
190 | } |
199 | } |
191 | |
200 | |
… | |
… | |
229 | reqe = reqs = req; |
238 | reqe = reqs = req; |
230 | |
239 | |
231 | pthread_cond_signal (&reqwait); |
240 | pthread_cond_signal (&reqwait); |
232 | pthread_mutex_unlock (&reqlock); |
241 | pthread_mutex_unlock (&reqlock); |
233 | |
242 | |
234 | while (nreqs > max_outstanding) |
243 | if (nreqs > max_outstanding) |
|
|
244 | for (;;) |
235 | { |
245 | { |
|
|
246 | poll_cb (); |
|
|
247 | |
|
|
248 | if (nreqs <= max_outstanding) |
|
|
249 | break; |
|
|
250 | |
236 | poll_wait (); |
251 | poll_wait (); |
237 | poll_cb (); |
|
|
238 | } |
252 | } |
239 | } |
253 | } |
240 | |
254 | |
241 | static void |
255 | static void |
242 | end_thread (void) |
256 | end_thread (void) |
243 | { |
257 | { |
… | |
… | |
246 | req->type = REQ_QUIT; |
260 | req->type = REQ_QUIT; |
247 | |
261 | |
248 | send_req (req); |
262 | send_req (req); |
249 | } |
263 | } |
250 | |
264 | |
251 | |
|
|
252 | static void min_parallel (int nthreads) |
265 | static void min_parallel (int nthreads) |
253 | { |
266 | { |
254 | while (nthreads > started) |
267 | while (nthreads > started) |
255 | start_thread (); |
268 | start_thread (); |
256 | } |
269 | } |
257 | |
270 | |
258 | static void max_parallel (int nthreads) |
271 | static void max_parallel (int nthreads) |
259 | { |
272 | { |
260 | int cur = started; |
273 | int cur = started; |
|
|
274 | |
261 | while (cur > nthreads) |
275 | while (cur > nthreads) |
262 | { |
276 | { |
263 | end_thread (); |
277 | end_thread (); |
264 | cur--; |
278 | cur--; |
265 | } |
279 | } |
… | |
… | |
283 | croak ("cannot set result pipe to nonblocking mode"); |
297 | croak ("cannot set result pipe to nonblocking mode"); |
284 | } |
298 | } |
285 | |
299 | |
286 | static void atfork_prepare (void) |
300 | static void atfork_prepare (void) |
287 | { |
301 | { |
288 | pthread_mutex_lock (&frklock); |
|
|
289 | pthread_mutex_lock (&reqlock); |
302 | pthread_mutex_lock (&reqlock); |
290 | pthread_mutex_lock (&reslock); |
303 | pthread_mutex_lock (&reslock); |
291 | } |
304 | } |
292 | |
305 | |
293 | static void atfork_parent (void) |
306 | static void atfork_parent (void) |
294 | { |
307 | { |
295 | pthread_mutex_unlock (&reslock); |
308 | pthread_mutex_unlock (&reslock); |
296 | pthread_mutex_unlock (&reqlock); |
309 | pthread_mutex_unlock (&reqlock); |
297 | pthread_mutex_unlock (&frklock); |
|
|
298 | } |
310 | } |
299 | |
311 | |
300 | static void atfork_child (void) |
312 | static void atfork_child (void) |
301 | { |
313 | { |
|
|
314 | aio_req prv; |
|
|
315 | |
302 | int restart = started; |
316 | int restart = started; |
303 | started = 0; |
317 | started = 0; |
304 | |
318 | |
305 | while (reqs) |
319 | while (reqs) |
306 | { |
320 | { |
307 | free_req (reqs); |
321 | prv = reqs; |
308 | reqs = reqs->next; |
322 | reqs = prv->next; |
|
|
323 | free_req (prv); |
309 | } |
324 | } |
310 | |
325 | |
311 | reqs = reqe = 0; |
326 | reqs = reqe = 0; |
312 | |
327 | |
313 | while (ress) |
328 | while (ress) |
314 | { |
329 | { |
315 | free_req (ress); |
330 | prv = ress; |
316 | ress = ress->next; |
331 | ress = prv->next; |
|
|
332 | free_req (prv); |
317 | } |
333 | } |
318 | |
334 | |
319 | ress = rese = 0; |
335 | ress = rese = 0; |
320 | |
336 | |
321 | close (respipe [0]); |
337 | close (respipe [0]); |
322 | close (respipe [1]); |
338 | close (respipe [1]); |
323 | |
|
|
324 | create_pipe (); |
339 | create_pipe (); |
325 | |
340 | |
326 | atfork_parent (); |
341 | atfork_parent (); |
327 | |
342 | |
328 | min_parallel (restart); |
343 | min_parallel (restart); |
… | |
… | |
496 | \ |
511 | \ |
497 | Newz (0, req, 1, aio_cb); \ |
512 | Newz (0, req, 1, aio_cb); \ |
498 | if (!req) \ |
513 | if (!req) \ |
499 | croak ("out of memory during aio_req allocation"); \ |
514 | croak ("out of memory during aio_req allocation"); \ |
500 | \ |
515 | \ |
501 | req->callback = SvREFCNT_inc (callback); |
516 | req->callback = newSVsv (callback); |
502 | |
517 | |
503 | MODULE = IO::AIO PACKAGE = IO::AIO |
518 | MODULE = IO::AIO PACKAGE = IO::AIO |
504 | |
519 | |
505 | PROTOTYPES: ENABLE |
520 | PROTOTYPES: ENABLE |
506 | |
521 | |
… | |
… | |
619 | : IoOFP (sv_2io (fh))); |
634 | : IoOFP (sv_2io (fh))); |
620 | req->offset = offset; |
635 | req->offset = offset; |
621 | req->length = length; |
636 | req->length = length; |
622 | req->data = SvREFCNT_inc (data); |
637 | req->data = SvREFCNT_inc (data); |
623 | req->dataptr = (char *)svptr + dataoffset; |
638 | req->dataptr = (char *)svptr + dataoffset; |
624 | req->callback = SvREFCNT_inc (callback); |
639 | |
|
|
640 | if (!SvREADONLY (data)) |
|
|
641 | { |
|
|
642 | SvREADONLY_on (data); |
|
|
643 | req->data2ptr = (void *)data; |
|
|
644 | } |
625 | |
645 | |
626 | send_req (req); |
646 | send_req (req); |
627 | } |
647 | } |
628 | } |
648 | } |
629 | |
649 | |
… | |
… | |
658 | { |
678 | { |
659 | dREQ; |
679 | dREQ; |
660 | |
680 | |
661 | New (0, req->statdata, 1, Stat_t); |
681 | New (0, req->statdata, 1, Stat_t); |
662 | if (!req->statdata) |
682 | if (!req->statdata) |
|
|
683 | { |
|
|
684 | free_req (req); |
663 | 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 | } |
664 | |
687 | |
665 | if (SvPOK (fh_or_path)) |
688 | if (SvPOK (fh_or_path)) |
666 | { |
689 | { |
667 | req->type = ix; |
690 | req->type = ix; |
668 | req->data = newSVsv (fh_or_path); |
691 | req->data = newSVsv (fh_or_path); |