… | |
… | |
34 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_UNLINK, |
34 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_UNLINK, |
35 | REQ_FSYNC, REQ_FDATASYNC, |
35 | REQ_FSYNC, REQ_FDATASYNC, |
36 | }; |
36 | }; |
37 | |
37 | |
38 | typedef struct aio_cb { |
38 | typedef struct aio_cb { |
39 | struct aio_cb *next; |
39 | struct aio_cb *volatile next; |
40 | |
40 | |
41 | int type; |
41 | int type; |
42 | |
42 | |
43 | int fd; |
43 | int fd; |
44 | off_t offset; |
44 | off_t offset; |
… | |
… | |
55 | |
55 | |
56 | typedef aio_cb *aio_req; |
56 | typedef aio_cb *aio_req; |
57 | |
57 | |
58 | static int started; |
58 | static int started; |
59 | static int nreqs; |
59 | static int nreqs; |
60 | static int reqpipe[2], respipe[2]; |
60 | static int respipe [2]; |
61 | |
61 | |
|
|
62 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
|
|
63 | static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; |
|
|
64 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
|
|
65 | |
62 | static aio_req qs, qe; /* queue start, queue end */ |
66 | static volatile aio_req reqs, reqe; /* queue start, queue end */ |
|
|
67 | static volatile aio_req ress, rese; /* queue start, queue end */ |
63 | |
68 | |
64 | static void *aio_proc(void *arg); |
69 | static void *aio_proc(void *arg); |
65 | |
70 | |
66 | static void |
71 | static void |
67 | start_thread (void) |
72 | start_thread (void) |
… | |
… | |
82 | |
87 | |
83 | sigprocmask (SIG_SETMASK, &oldsigset, 0); |
88 | sigprocmask (SIG_SETMASK, &oldsigset, 0); |
84 | } |
89 | } |
85 | |
90 | |
86 | static void |
91 | static void |
87 | send_reqs (void) |
|
|
88 | { |
|
|
89 | /* this write is atomic */ |
|
|
90 | while (qs && write (reqpipe[1], &qs, sizeof qs) == sizeof qs) |
|
|
91 | { |
|
|
92 | qs = qs->next; |
|
|
93 | if (!qs) qe = 0; |
|
|
94 | } |
|
|
95 | } |
|
|
96 | |
|
|
97 | static void |
|
|
98 | send_req (aio_req req) |
92 | send_req (aio_req req) |
99 | { |
93 | { |
100 | nreqs++; |
94 | nreqs++; |
|
|
95 | |
|
|
96 | pthread_mutex_lock (&reqlock); |
|
|
97 | |
101 | req->next = 0; |
98 | req->next = 0; |
102 | |
99 | |
103 | if (qe) |
100 | if (reqe) |
104 | { |
101 | { |
105 | qe->next = req; |
102 | reqe->next = req; |
106 | qe = req; |
103 | reqe = req; |
107 | } |
104 | } |
108 | else |
105 | else |
109 | qe = qs = req; |
106 | reqe = reqs = req; |
110 | |
107 | |
111 | send_reqs (); |
108 | pthread_cond_signal (&reqwait); |
|
|
109 | pthread_mutex_unlock (&reqlock); |
112 | } |
110 | } |
113 | |
111 | |
114 | static void |
112 | static void |
115 | end_thread (void) |
113 | end_thread (void) |
116 | { |
114 | { |
… | |
… | |
171 | } |
169 | } |
172 | |
170 | |
173 | static void |
171 | static void |
174 | poll_wait () |
172 | poll_wait () |
175 | { |
173 | { |
|
|
174 | if (!nreqs) |
|
|
175 | return; |
|
|
176 | |
176 | fd_set rfd; |
177 | fd_set rfd; |
177 | FD_ZERO(&rfd); |
178 | FD_ZERO(&rfd); |
178 | FD_SET(respipe[0], &rfd); |
179 | FD_SET(respipe [0], &rfd); |
179 | |
180 | |
180 | select (respipe[0] + 1, &rfd, 0, 0, 0); |
181 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
181 | } |
182 | } |
182 | |
183 | |
183 | static int |
184 | static int |
184 | poll_cb (pTHX) |
185 | poll_cb (pTHX) |
185 | { |
186 | { |
186 | dSP; |
187 | dSP; |
187 | int count = 0; |
188 | int count = 0; |
188 | aio_req req; |
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 | } |
189 | |
197 | |
190 | while (read (respipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
198 | for (;;) |
191 | { |
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 | |
192 | nreqs--; |
215 | nreqs--; |
193 | |
216 | |
194 | if (req->type == REQ_QUIT) |
217 | if (req->type == REQ_QUIT) |
195 | started--; |
218 | started--; |
196 | else |
219 | else |
… | |
… | |
214 | Safefree (req->statdata); |
237 | Safefree (req->statdata); |
215 | } |
238 | } |
216 | |
239 | |
217 | PUSHMARK (SP); |
240 | PUSHMARK (SP); |
218 | XPUSHs (sv_2mortal (newSViv (req->result))); |
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 | |
219 | PUTBACK; |
258 | PUTBACK; |
220 | call_sv (req->callback, G_VOID); |
259 | call_sv (req->callback, G_VOID | G_EVAL); |
221 | SPAGAIN; |
260 | SPAGAIN; |
222 | |
261 | |
223 | if (req->callback) |
262 | if (req->callback) |
224 | SvREFCNT_dec (req->callback); |
263 | SvREFCNT_dec (req->callback); |
225 | |
264 | |
… | |
… | |
228 | } |
267 | } |
229 | |
268 | |
230 | Safefree (req); |
269 | Safefree (req); |
231 | } |
270 | } |
232 | |
271 | |
233 | if (qs) |
|
|
234 | send_reqs (); |
|
|
235 | |
|
|
236 | return count; |
272 | return count; |
237 | } |
273 | } |
238 | |
274 | |
239 | static void * |
275 | static void * |
240 | aio_proc (void *thr_arg) |
276 | aio_proc (void *thr_arg) |
241 | { |
277 | { |
242 | aio_req req; |
278 | aio_req req; |
|
|
279 | int type; |
243 | |
280 | |
244 | /* then loop */ |
281 | do |
245 | while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
|
|
246 | { |
282 | { |
|
|
283 | pthread_mutex_lock (&reqlock); |
|
|
284 | |
|
|
285 | for (;;) |
|
|
286 | { |
|
|
287 | req = reqs; |
|
|
288 | |
|
|
289 | if (reqs) |
|
|
290 | { |
|
|
291 | reqs = reqs->next; |
|
|
292 | if (!reqs) reqe = 0; |
|
|
293 | } |
|
|
294 | |
|
|
295 | if (req) |
|
|
296 | break; |
|
|
297 | |
|
|
298 | pthread_cond_wait (&reqwait, &reqlock); |
|
|
299 | } |
|
|
300 | |
|
|
301 | pthread_mutex_unlock (&reqlock); |
|
|
302 | |
247 | errno = 0; /* strictly unnecessary */ |
303 | errno = 0; /* strictly unnecessary */ |
248 | |
304 | |
|
|
305 | type = req->type; |
|
|
306 | |
249 | switch (req->type) |
307 | switch (type) |
250 | { |
308 | { |
251 | case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, req->offset); break; |
309 | case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, req->offset); break; |
252 | case REQ_WRITE: req->result = pwrite64 (req->fd, req->dataptr, req->length, req->offset); break; |
310 | case REQ_WRITE: req->result = pwrite64 (req->fd, req->dataptr, req->length, req->offset); break; |
253 | #if SYS_readahead |
311 | #if SYS_readahead |
254 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
312 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
… | |
… | |
266 | |
324 | |
267 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
325 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
268 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
326 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
269 | |
327 | |
270 | case REQ_QUIT: |
328 | case REQ_QUIT: |
271 | write (respipe[1], (void *)&req, sizeof (req)); |
329 | break; |
272 | return 0; |
|
|
273 | |
330 | |
274 | default: |
331 | default: |
275 | req->result = ENOSYS; |
332 | req->result = ENOSYS; |
276 | break; |
333 | break; |
277 | } |
334 | } |
278 | |
335 | |
279 | req->errorno = errno; |
336 | req->errorno = errno; |
280 | write (respipe[1], (void *)&req, sizeof (req)); |
337 | |
|
|
338 | pthread_mutex_lock (&reslock); |
|
|
339 | |
|
|
340 | req->next = 0; |
|
|
341 | |
|
|
342 | if (rese) |
|
|
343 | { |
|
|
344 | rese->next = req; |
|
|
345 | rese = req; |
|
|
346 | } |
|
|
347 | else |
|
|
348 | { |
|
|
349 | rese = ress = req; |
|
|
350 | |
|
|
351 | /* write a dummy byte to the pipe so fh becomes ready */ |
|
|
352 | write (respipe [1], &respipe, 1); |
|
|
353 | } |
|
|
354 | |
|
|
355 | pthread_mutex_unlock (&reslock); |
281 | } |
356 | } |
|
|
357 | while (type != REQ_QUIT); |
282 | |
358 | |
283 | return 0; |
359 | return 0; |
284 | } |
360 | } |
285 | |
361 | |
286 | MODULE = IO::AIO PACKAGE = IO::AIO |
362 | MODULE = IO::AIO PACKAGE = IO::AIO |
287 | |
363 | |
288 | BOOT: |
364 | BOOT: |
289 | { |
365 | { |
290 | if (pipe (reqpipe) || pipe (respipe)) |
366 | if (pipe (respipe)) |
291 | croak ("unable to initialize request or result pipe"); |
367 | croak ("unable to initialize result pipe"); |
292 | |
368 | |
293 | if (fcntl (reqpipe[1], F_SETFL, O_NONBLOCK)) |
369 | if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)) |
294 | croak ("cannot set result pipe to nonblocking mode"); |
370 | croak ("cannot set result pipe to nonblocking mode"); |
295 | |
371 | |
296 | if (fcntl (respipe[0], F_SETFL, O_NONBLOCK)) |
372 | if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) |
297 | croak ("cannot set result pipe to nonblocking mode"); |
373 | croak ("cannot set result pipe to nonblocking mode"); |
298 | } |
374 | } |
299 | |
375 | |
300 | void |
376 | void |
301 | min_parallel(nthreads) |
377 | min_parallel(nthreads) |
… | |
… | |
490 | |
566 | |
491 | int |
567 | int |
492 | poll_fileno() |
568 | poll_fileno() |
493 | PROTOTYPE: |
569 | PROTOTYPE: |
494 | CODE: |
570 | CODE: |
495 | RETVAL = respipe[0]; |
571 | RETVAL = respipe [0]; |
496 | OUTPUT: |
572 | OUTPUT: |
497 | RETVAL |
573 | RETVAL |
498 | |
574 | |
499 | int |
575 | int |
500 | poll_cb(...) |
576 | poll_cb(...) |
… | |
… | |
506 | |
582 | |
507 | void |
583 | void |
508 | poll_wait() |
584 | poll_wait() |
509 | PROTOTYPE: |
585 | PROTOTYPE: |
510 | CODE: |
586 | CODE: |
|
|
587 | if (nreqs) |
511 | poll_wait (); |
588 | poll_wait (); |
512 | |
589 | |
513 | int |
590 | int |
514 | nreqs() |
591 | nreqs() |
515 | PROTOTYPE: |
592 | PROTOTYPE: |
516 | CODE: |
593 | CODE: |