… | |
… | |
40 | #ifndef _WIN32 |
40 | #ifndef _WIN32 |
41 | # include "config.h" |
41 | # include "config.h" |
42 | #endif |
42 | #endif |
43 | |
43 | |
44 | #include "eio.h" |
44 | #include "eio.h" |
|
|
45 | #include "ecb.h" |
45 | |
46 | |
46 | #ifdef EIO_STACKSIZE |
47 | #ifdef EIO_STACKSIZE |
47 | # define XTHREAD_STACKSIZE EIO_STACKSIZE |
48 | # define XTHREAD_STACKSIZE EIO_STACKSIZE |
48 | #endif |
49 | #endif |
49 | #include "xthread.h" |
50 | #include "xthread.h" |
… | |
… | |
59 | #include <limits.h> |
60 | #include <limits.h> |
60 | #include <fcntl.h> |
61 | #include <fcntl.h> |
61 | #include <assert.h> |
62 | #include <assert.h> |
62 | |
63 | |
63 | /* intptr_t comes from unistd.h, says POSIX/UNIX/tradition */ |
64 | /* intptr_t comes from unistd.h, says POSIX/UNIX/tradition */ |
64 | /* intptr_t only comes form stdint.h, says idiot openbsd coder */ |
65 | /* intptr_t only comes from stdint.h, says idiot openbsd coder */ |
65 | #if HAVE_STDINT_H |
66 | #if HAVE_STDINT_H |
66 | # include <stdint.h> |
67 | # include <stdint.h> |
67 | #endif |
68 | #endif |
|
|
69 | |
|
|
70 | #ifndef ECANCELED |
|
|
71 | # define ECANCELED EDOM |
|
|
72 | #endif |
|
|
73 | |
|
|
74 | static void eio_destroy (eio_req *req); |
68 | |
75 | |
69 | #ifndef EIO_FINISH |
76 | #ifndef EIO_FINISH |
70 | # define EIO_FINISH(req) ((req)->finish) && !EIO_CANCELLED (req) ? (req)->finish (req) : 0 |
77 | # define EIO_FINISH(req) ((req)->finish) && !EIO_CANCELLED (req) ? (req)->finish (req) : 0 |
71 | #endif |
78 | #endif |
72 | |
79 | |
… | |
… | |
165 | if (!eio_buf) \ |
172 | if (!eio_buf) \ |
166 | return -1; |
173 | return -1; |
167 | |
174 | |
168 | #define EIO_TICKS ((1000000 + 1023) >> 10) |
175 | #define EIO_TICKS ((1000000 + 1023) >> 10) |
169 | |
176 | |
170 | /*****************************************************************************/ |
|
|
171 | |
|
|
172 | #if __GNUC__ >= 3 |
|
|
173 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
174 | #else |
|
|
175 | # define expect(expr,value) (expr) |
|
|
176 | #endif |
|
|
177 | |
|
|
178 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
179 | #define expect_true(expr) expect ((expr) != 0, 1) |
|
|
180 | |
|
|
181 | /*****************************************************************************/ |
|
|
182 | |
|
|
183 | #define ETP_PRI_MIN EIO_PRI_MIN |
177 | #define ETP_PRI_MIN EIO_PRI_MIN |
184 | #define ETP_PRI_MAX EIO_PRI_MAX |
178 | #define ETP_PRI_MAX EIO_PRI_MAX |
185 | |
179 | |
186 | struct etp_worker; |
180 | struct etp_worker; |
187 | |
181 | |
… | |
… | |
212 | /*****************************************************************************/ |
206 | /*****************************************************************************/ |
213 | |
207 | |
214 | #define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1) |
208 | #define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1) |
215 | |
209 | |
216 | /* calculate time difference in ~1/EIO_TICKS of a second */ |
210 | /* calculate time difference in ~1/EIO_TICKS of a second */ |
|
|
211 | ecb_inline int |
217 | static int tvdiff (struct timeval *tv1, struct timeval *tv2) |
212 | tvdiff (struct timeval *tv1, struct timeval *tv2) |
218 | { |
213 | { |
219 | return (tv2->tv_sec - tv1->tv_sec ) * EIO_TICKS |
214 | return (tv2->tv_sec - tv1->tv_sec ) * EIO_TICKS |
220 | + ((tv2->tv_usec - tv1->tv_usec) >> 10); |
215 | + ((tv2->tv_usec - tv1->tv_usec) >> 10); |
221 | } |
216 | } |
222 | |
217 | |
… | |
… | |
266 | #define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock) |
261 | #define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock) |
267 | #define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock) |
262 | #define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock) |
268 | |
263 | |
269 | /* worker threads management */ |
264 | /* worker threads management */ |
270 | |
265 | |
|
|
266 | static void ecb_cold |
271 | static void etp_worker_clear (etp_worker *wrk) |
267 | etp_worker_clear (etp_worker *wrk) |
272 | { |
268 | { |
273 | ETP_WORKER_CLEAR (wrk); |
269 | ETP_WORKER_CLEAR (wrk); |
274 | } |
270 | } |
275 | |
271 | |
|
|
272 | static void ecb_cold |
276 | static void etp_worker_free (etp_worker *wrk) |
273 | etp_worker_free (etp_worker *wrk) |
277 | { |
274 | { |
278 | wrk->next->prev = wrk->prev; |
275 | wrk->next->prev = wrk->prev; |
279 | wrk->prev->next = wrk->next; |
276 | wrk->prev->next = wrk->next; |
280 | |
277 | |
281 | free (wrk); |
278 | free (wrk); |
282 | } |
279 | } |
283 | |
280 | |
284 | static unsigned int etp_nreqs (void) |
281 | static unsigned int |
|
|
282 | etp_nreqs (void) |
285 | { |
283 | { |
286 | int retval; |
284 | int retval; |
287 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
285 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
288 | retval = nreqs; |
286 | retval = nreqs; |
289 | if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); |
287 | if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); |
290 | return retval; |
288 | return retval; |
291 | } |
289 | } |
292 | |
290 | |
293 | static unsigned int etp_nready (void) |
291 | static unsigned int |
|
|
292 | etp_nready (void) |
294 | { |
293 | { |
295 | unsigned int retval; |
294 | unsigned int retval; |
296 | |
295 | |
297 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
296 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
298 | retval = nready; |
297 | retval = nready; |
299 | if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); |
298 | if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); |
300 | |
299 | |
301 | return retval; |
300 | return retval; |
302 | } |
301 | } |
303 | |
302 | |
304 | static unsigned int etp_npending (void) |
303 | static unsigned int |
|
|
304 | etp_npending (void) |
305 | { |
305 | { |
306 | unsigned int retval; |
306 | unsigned int retval; |
307 | |
307 | |
308 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
308 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
309 | retval = npending; |
309 | retval = npending; |
310 | if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); |
310 | if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); |
311 | |
311 | |
312 | return retval; |
312 | return retval; |
313 | } |
313 | } |
314 | |
314 | |
315 | static unsigned int etp_nthreads (void) |
315 | static unsigned int |
|
|
316 | etp_nthreads (void) |
316 | { |
317 | { |
317 | unsigned int retval; |
318 | unsigned int retval; |
318 | |
319 | |
319 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
320 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
320 | retval = started; |
321 | retval = started; |
… | |
… | |
334 | } etp_reqq; |
335 | } etp_reqq; |
335 | |
336 | |
336 | static etp_reqq req_queue; |
337 | static etp_reqq req_queue; |
337 | static etp_reqq res_queue; |
338 | static etp_reqq res_queue; |
338 | |
339 | |
|
|
340 | static int ecb_noinline |
339 | static int reqq_push (etp_reqq *q, ETP_REQ *req) |
341 | reqq_push (etp_reqq *q, ETP_REQ *req) |
340 | { |
342 | { |
341 | int pri = req->pri; |
343 | int pri = req->pri; |
342 | req->next = 0; |
344 | req->next = 0; |
343 | |
345 | |
344 | if (q->qe[pri]) |
346 | if (q->qe[pri]) |
… | |
… | |
350 | q->qe[pri] = q->qs[pri] = req; |
352 | q->qe[pri] = q->qs[pri] = req; |
351 | |
353 | |
352 | return q->size++; |
354 | return q->size++; |
353 | } |
355 | } |
354 | |
356 | |
|
|
357 | static ETP_REQ * ecb_noinline |
355 | static ETP_REQ *reqq_shift (etp_reqq *q) |
358 | reqq_shift (etp_reqq *q) |
356 | { |
359 | { |
357 | int pri; |
360 | int pri; |
358 | |
361 | |
359 | if (!q->size) |
362 | if (!q->size) |
360 | return 0; |
363 | return 0; |
… | |
… | |
375 | } |
378 | } |
376 | |
379 | |
377 | abort (); |
380 | abort (); |
378 | } |
381 | } |
379 | |
382 | |
|
|
383 | static void ecb_cold |
380 | static void etp_thread_init (void) |
384 | etp_thread_init (void) |
381 | { |
385 | { |
382 | X_MUTEX_CREATE (wrklock); |
386 | X_MUTEX_CREATE (wrklock); |
383 | X_MUTEX_CREATE (reslock); |
387 | X_MUTEX_CREATE (reslock); |
384 | X_MUTEX_CREATE (reqlock); |
388 | X_MUTEX_CREATE (reqlock); |
385 | X_COND_CREATE (reqwait); |
389 | X_COND_CREATE (reqwait); |
386 | } |
390 | } |
387 | |
391 | |
|
|
392 | static void ecb_cold |
388 | static void etp_atfork_prepare (void) |
393 | etp_atfork_prepare (void) |
389 | { |
394 | { |
390 | X_LOCK (wrklock); |
395 | X_LOCK (wrklock); |
391 | X_LOCK (reqlock); |
396 | X_LOCK (reqlock); |
392 | X_LOCK (reslock); |
397 | X_LOCK (reslock); |
393 | #if !HAVE_PREADWRITE |
398 | #if !HAVE_PREADWRITE |
394 | X_LOCK (preadwritelock); |
399 | X_LOCK (preadwritelock); |
395 | #endif |
400 | #endif |
396 | } |
401 | } |
397 | |
402 | |
|
|
403 | static void ecb_cold |
398 | static void etp_atfork_parent (void) |
404 | etp_atfork_parent (void) |
399 | { |
405 | { |
400 | #if !HAVE_PREADWRITE |
406 | #if !HAVE_PREADWRITE |
401 | X_UNLOCK (preadwritelock); |
407 | X_UNLOCK (preadwritelock); |
402 | #endif |
408 | #endif |
403 | X_UNLOCK (reslock); |
409 | X_UNLOCK (reslock); |
404 | X_UNLOCK (reqlock); |
410 | X_UNLOCK (reqlock); |
405 | X_UNLOCK (wrklock); |
411 | X_UNLOCK (wrklock); |
406 | } |
412 | } |
407 | |
413 | |
|
|
414 | static void ecb_cold |
408 | static void etp_atfork_child (void) |
415 | etp_atfork_child (void) |
409 | { |
416 | { |
410 | ETP_REQ *prv; |
417 | ETP_REQ *prv; |
411 | |
418 | |
412 | while ((prv = reqq_shift (&req_queue))) |
419 | while ((prv = reqq_shift (&req_queue))) |
413 | ETP_DESTROY (prv); |
420 | ETP_DESTROY (prv); |
… | |
… | |
433 | npending = 0; |
440 | npending = 0; |
434 | |
441 | |
435 | etp_thread_init (); |
442 | etp_thread_init (); |
436 | } |
443 | } |
437 | |
444 | |
438 | static void |
445 | static void ecb_cold |
439 | etp_once_init (void) |
446 | etp_once_init (void) |
440 | { |
447 | { |
441 | etp_thread_init (); |
448 | etp_thread_init (); |
442 | X_THREAD_ATFORK (etp_atfork_prepare, etp_atfork_parent, etp_atfork_child); |
449 | X_THREAD_ATFORK (etp_atfork_prepare, etp_atfork_parent, etp_atfork_child); |
443 | } |
450 | } |
444 | |
451 | |
445 | static int |
452 | static int ecb_cold |
446 | etp_init (void (*want_poll)(void), void (*done_poll)(void)) |
453 | etp_init (void (*want_poll)(void), void (*done_poll)(void)) |
447 | { |
454 | { |
448 | static pthread_once_t doinit = PTHREAD_ONCE_INIT; |
455 | static pthread_once_t doinit = PTHREAD_ONCE_INIT; |
449 | |
456 | |
450 | pthread_once (&doinit, etp_once_init); |
457 | pthread_once (&doinit, etp_once_init); |
… | |
… | |
455 | return 0; |
462 | return 0; |
456 | } |
463 | } |
457 | |
464 | |
458 | X_THREAD_PROC (etp_proc); |
465 | X_THREAD_PROC (etp_proc); |
459 | |
466 | |
|
|
467 | static void ecb_cold |
460 | static void etp_start_thread (void) |
468 | etp_start_thread (void) |
461 | { |
469 | { |
462 | etp_worker *wrk = calloc (1, sizeof (etp_worker)); |
470 | etp_worker *wrk = calloc (1, sizeof (etp_worker)); |
463 | |
471 | |
464 | /*TODO*/ |
472 | /*TODO*/ |
465 | assert (("unable to allocate worker thread data", wrk)); |
473 | assert (("unable to allocate worker thread data", wrk)); |
… | |
… | |
478 | free (wrk); |
486 | free (wrk); |
479 | |
487 | |
480 | X_UNLOCK (wrklock); |
488 | X_UNLOCK (wrklock); |
481 | } |
489 | } |
482 | |
490 | |
|
|
491 | static void |
483 | static void etp_maybe_start_thread (void) |
492 | etp_maybe_start_thread (void) |
484 | { |
493 | { |
485 | if (expect_true (etp_nthreads () >= wanted)) |
494 | if (ecb_expect_true (etp_nthreads () >= wanted)) |
486 | return; |
495 | return; |
487 | |
496 | |
488 | /* todo: maybe use idle here, but might be less exact */ |
497 | /* todo: maybe use idle here, but might be less exact */ |
489 | if (expect_true (0 <= (int)etp_nthreads () + (int)etp_npending () - (int)etp_nreqs ())) |
498 | if (ecb_expect_true (0 <= (int)etp_nthreads () + (int)etp_npending () - (int)etp_nreqs ())) |
490 | return; |
499 | return; |
491 | |
500 | |
492 | etp_start_thread (); |
501 | etp_start_thread (); |
493 | } |
502 | } |
494 | |
503 | |
|
|
504 | static void ecb_cold |
495 | static void etp_end_thread (void) |
505 | etp_end_thread (void) |
496 | { |
506 | { |
497 | eio_req *req = calloc (1, sizeof (eio_req)); |
507 | eio_req *req = calloc (1, sizeof (eio_req)); |
498 | |
508 | |
499 | req->type = -1; |
509 | req->type = -1; |
500 | req->pri = ETP_PRI_MAX - ETP_PRI_MIN; |
510 | req->pri = ETP_PRI_MAX - ETP_PRI_MIN; |
… | |
… | |
507 | X_LOCK (wrklock); |
517 | X_LOCK (wrklock); |
508 | --started; |
518 | --started; |
509 | X_UNLOCK (wrklock); |
519 | X_UNLOCK (wrklock); |
510 | } |
520 | } |
511 | |
521 | |
512 | static int etp_poll (void) |
522 | static int |
|
|
523 | etp_poll (void) |
513 | { |
524 | { |
514 | unsigned int maxreqs; |
525 | unsigned int maxreqs; |
515 | unsigned int maxtime; |
526 | unsigned int maxtime; |
516 | struct timeval tv_start, tv_now; |
527 | struct timeval tv_start, tv_now; |
517 | |
528 | |
… | |
… | |
547 | |
558 | |
548 | X_LOCK (reqlock); |
559 | X_LOCK (reqlock); |
549 | --nreqs; |
560 | --nreqs; |
550 | X_UNLOCK (reqlock); |
561 | X_UNLOCK (reqlock); |
551 | |
562 | |
552 | if (expect_false (req->type == EIO_GROUP && req->size)) |
563 | if (ecb_expect_false (req->type == EIO_GROUP && req->size)) |
553 | { |
564 | { |
554 | req->int1 = 1; /* mark request as delayed */ |
565 | req->int1 = 1; /* mark request as delayed */ |
555 | continue; |
566 | continue; |
556 | } |
567 | } |
557 | else |
568 | else |
558 | { |
569 | { |
559 | int res = ETP_FINISH (req); |
570 | int res = ETP_FINISH (req); |
560 | if (expect_false (res)) |
571 | if (ecb_expect_false (res)) |
561 | return res; |
572 | return res; |
562 | } |
573 | } |
563 | |
574 | |
564 | if (expect_false (maxreqs && !--maxreqs)) |
575 | if (ecb_expect_false (maxreqs && !--maxreqs)) |
565 | break; |
576 | break; |
566 | |
577 | |
567 | if (maxtime) |
578 | if (maxtime) |
568 | { |
579 | { |
569 | gettimeofday (&tv_now, 0); |
580 | gettimeofday (&tv_now, 0); |
… | |
… | |
575 | |
586 | |
576 | errno = EAGAIN; |
587 | errno = EAGAIN; |
577 | return -1; |
588 | return -1; |
578 | } |
589 | } |
579 | |
590 | |
|
|
591 | static void |
580 | static void etp_cancel (ETP_REQ *req) |
592 | etp_cancel (ETP_REQ *req) |
581 | { |
593 | { |
582 | X_LOCK (wrklock); |
594 | req->cancelled = 1; |
583 | req->flags |= EIO_FLAG_CANCELLED; |
|
|
584 | X_UNLOCK (wrklock); |
|
|
585 | |
595 | |
586 | eio_grp_cancel (req); |
596 | eio_grp_cancel (req); |
587 | } |
597 | } |
588 | |
598 | |
|
|
599 | static void |
589 | static void etp_submit (ETP_REQ *req) |
600 | etp_submit (ETP_REQ *req) |
590 | { |
601 | { |
591 | req->pri -= ETP_PRI_MIN; |
602 | req->pri -= ETP_PRI_MIN; |
592 | |
603 | |
593 | if (expect_false (req->pri < ETP_PRI_MIN - ETP_PRI_MIN)) req->pri = ETP_PRI_MIN - ETP_PRI_MIN; |
604 | if (ecb_expect_false (req->pri < ETP_PRI_MIN - ETP_PRI_MIN)) req->pri = ETP_PRI_MIN - ETP_PRI_MIN; |
594 | if (expect_false (req->pri > ETP_PRI_MAX - ETP_PRI_MIN)) req->pri = ETP_PRI_MAX - ETP_PRI_MIN; |
605 | if (ecb_expect_false (req->pri > ETP_PRI_MAX - ETP_PRI_MIN)) req->pri = ETP_PRI_MAX - ETP_PRI_MIN; |
595 | |
606 | |
596 | if (expect_false (req->type == EIO_GROUP)) |
607 | if (ecb_expect_false (req->type == EIO_GROUP)) |
597 | { |
608 | { |
598 | /* I hope this is worth it :/ */ |
609 | /* I hope this is worth it :/ */ |
599 | X_LOCK (reqlock); |
610 | X_LOCK (reqlock); |
600 | ++nreqs; |
611 | ++nreqs; |
601 | X_UNLOCK (reqlock); |
612 | X_UNLOCK (reqlock); |
… | |
… | |
620 | |
631 | |
621 | etp_maybe_start_thread (); |
632 | etp_maybe_start_thread (); |
622 | } |
633 | } |
623 | } |
634 | } |
624 | |
635 | |
|
|
636 | static void ecb_cold |
625 | static void etp_set_max_poll_time (double nseconds) |
637 | etp_set_max_poll_time (double nseconds) |
626 | { |
638 | { |
627 | if (WORDACCESS_UNSAFE) X_LOCK (reslock); |
639 | if (WORDACCESS_UNSAFE) X_LOCK (reslock); |
628 | max_poll_time = nseconds * EIO_TICKS; |
640 | max_poll_time = nseconds * EIO_TICKS; |
629 | if (WORDACCESS_UNSAFE) X_UNLOCK (reslock); |
641 | if (WORDACCESS_UNSAFE) X_UNLOCK (reslock); |
630 | } |
642 | } |
631 | |
643 | |
|
|
644 | static void ecb_cold |
632 | static void etp_set_max_poll_reqs (unsigned int maxreqs) |
645 | etp_set_max_poll_reqs (unsigned int maxreqs) |
633 | { |
646 | { |
634 | if (WORDACCESS_UNSAFE) X_LOCK (reslock); |
647 | if (WORDACCESS_UNSAFE) X_LOCK (reslock); |
635 | max_poll_reqs = maxreqs; |
648 | max_poll_reqs = maxreqs; |
636 | if (WORDACCESS_UNSAFE) X_UNLOCK (reslock); |
649 | if (WORDACCESS_UNSAFE) X_UNLOCK (reslock); |
637 | } |
650 | } |
638 | |
651 | |
|
|
652 | static void ecb_cold |
639 | static void etp_set_max_idle (unsigned int nthreads) |
653 | etp_set_max_idle (unsigned int nthreads) |
640 | { |
654 | { |
641 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
655 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
642 | max_idle = nthreads; |
656 | max_idle = nthreads; |
643 | if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); |
657 | if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); |
644 | } |
658 | } |
645 | |
659 | |
|
|
660 | static void ecb_cold |
646 | static void etp_set_idle_timeout (unsigned int seconds) |
661 | etp_set_idle_timeout (unsigned int seconds) |
647 | { |
662 | { |
648 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
663 | if (WORDACCESS_UNSAFE) X_LOCK (reqlock); |
649 | idle_timeout = seconds; |
664 | idle_timeout = seconds; |
650 | if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); |
665 | if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); |
651 | } |
666 | } |
652 | |
667 | |
|
|
668 | static void ecb_cold |
653 | static void etp_set_min_parallel (unsigned int nthreads) |
669 | etp_set_min_parallel (unsigned int nthreads) |
654 | { |
670 | { |
655 | if (wanted < nthreads) |
671 | if (wanted < nthreads) |
656 | wanted = nthreads; |
672 | wanted = nthreads; |
657 | } |
673 | } |
658 | |
674 | |
|
|
675 | static void ecb_cold |
659 | static void etp_set_max_parallel (unsigned int nthreads) |
676 | etp_set_max_parallel (unsigned int nthreads) |
660 | { |
677 | { |
661 | if (wanted > nthreads) |
678 | if (wanted > nthreads) |
662 | wanted = nthreads; |
679 | wanted = nthreads; |
663 | |
680 | |
664 | while (started > wanted) |
681 | while (started > wanted) |
665 | etp_end_thread (); |
682 | etp_end_thread (); |
666 | } |
683 | } |
667 | |
684 | |
668 | /*****************************************************************************/ |
685 | /*****************************************************************************/ |
669 | |
686 | |
|
|
687 | static void |
670 | static void grp_try_feed (eio_req *grp) |
688 | grp_try_feed (eio_req *grp) |
671 | { |
689 | { |
672 | while (grp->size < grp->int2 && !EIO_CANCELLED (grp)) |
690 | while (grp->size < grp->int2 && !EIO_CANCELLED (grp)) |
673 | { |
691 | { |
674 | grp->flags &= ~EIO_FLAG_GROUPADD; |
692 | grp->flags &= ~EIO_FLAG_GROUPADD; |
675 | |
693 | |
… | |
… | |
682 | break; |
700 | break; |
683 | } |
701 | } |
684 | } |
702 | } |
685 | } |
703 | } |
686 | |
704 | |
|
|
705 | static int |
687 | static int grp_dec (eio_req *grp) |
706 | grp_dec (eio_req *grp) |
688 | { |
707 | { |
689 | --grp->size; |
708 | --grp->size; |
690 | |
709 | |
691 | /* call feeder, if applicable */ |
710 | /* call feeder, if applicable */ |
692 | grp_try_feed (grp); |
711 | grp_try_feed (grp); |
… | |
… | |
696 | return eio_finish (grp); |
715 | return eio_finish (grp); |
697 | else |
716 | else |
698 | return 0; |
717 | return 0; |
699 | } |
718 | } |
700 | |
719 | |
|
|
720 | static void |
701 | void eio_destroy (eio_req *req) |
721 | eio_destroy (eio_req *req) |
702 | { |
722 | { |
703 | if ((req)->flags & EIO_FLAG_PTR1_FREE) free (req->ptr1); |
723 | if ((req)->flags & EIO_FLAG_PTR1_FREE) free (req->ptr1); |
704 | if ((req)->flags & EIO_FLAG_PTR2_FREE) free (req->ptr2); |
724 | if ((req)->flags & EIO_FLAG_PTR2_FREE) free (req->ptr2); |
705 | |
725 | |
706 | EIO_DESTROY (req); |
726 | EIO_DESTROY (req); |
707 | } |
727 | } |
708 | |
728 | |
|
|
729 | static int |
709 | static int eio_finish (eio_req *req) |
730 | eio_finish (eio_req *req) |
710 | { |
731 | { |
711 | int res = EIO_FINISH (req); |
732 | int res = EIO_FINISH (req); |
712 | |
733 | |
713 | if (req->grp) |
734 | if (req->grp) |
714 | { |
735 | { |
… | |
… | |
722 | if (grp->grp_first == req) |
743 | if (grp->grp_first == req) |
723 | grp->grp_first = req->grp_next; |
744 | grp->grp_first = req->grp_next; |
724 | |
745 | |
725 | res2 = grp_dec (grp); |
746 | res2 = grp_dec (grp); |
726 | |
747 | |
727 | if (!res && res2) |
748 | if (!res) |
728 | res = res2; |
749 | res = res2; |
729 | } |
750 | } |
730 | |
751 | |
731 | eio_destroy (req); |
752 | eio_destroy (req); |
732 | |
753 | |
733 | return res; |
754 | return res; |
734 | } |
755 | } |
735 | |
756 | |
|
|
757 | void |
736 | void eio_grp_cancel (eio_req *grp) |
758 | eio_grp_cancel (eio_req *grp) |
737 | { |
759 | { |
738 | for (grp = grp->grp_first; grp; grp = grp->grp_next) |
760 | for (grp = grp->grp_first; grp; grp = grp->grp_next) |
739 | eio_cancel (grp); |
761 | eio_cancel (grp); |
740 | } |
762 | } |
741 | |
763 | |
|
|
764 | void |
742 | void eio_cancel (eio_req *req) |
765 | eio_cancel (eio_req *req) |
743 | { |
766 | { |
744 | etp_cancel (req); |
767 | etp_cancel (req); |
745 | } |
768 | } |
746 | |
769 | |
|
|
770 | void |
747 | void eio_submit (eio_req *req) |
771 | eio_submit (eio_req *req) |
748 | { |
772 | { |
749 | etp_submit (req); |
773 | etp_submit (req); |
750 | } |
774 | } |
751 | |
775 | |
752 | unsigned int eio_nreqs (void) |
776 | unsigned int |
|
|
777 | eio_nreqs (void) |
753 | { |
778 | { |
754 | return etp_nreqs (); |
779 | return etp_nreqs (); |
755 | } |
780 | } |
756 | |
781 | |
757 | unsigned int eio_nready (void) |
782 | unsigned int |
|
|
783 | eio_nready (void) |
758 | { |
784 | { |
759 | return etp_nready (); |
785 | return etp_nready (); |
760 | } |
786 | } |
761 | |
787 | |
762 | unsigned int eio_npending (void) |
788 | unsigned int |
|
|
789 | eio_npending (void) |
763 | { |
790 | { |
764 | return etp_npending (); |
791 | return etp_npending (); |
765 | } |
792 | } |
766 | |
793 | |
767 | unsigned int eio_nthreads (void) |
794 | unsigned int ecb_cold |
|
|
795 | eio_nthreads (void) |
768 | { |
796 | { |
769 | return etp_nthreads (); |
797 | return etp_nthreads (); |
770 | } |
798 | } |
771 | |
799 | |
|
|
800 | void ecb_cold |
772 | void eio_set_max_poll_time (double nseconds) |
801 | eio_set_max_poll_time (double nseconds) |
773 | { |
802 | { |
774 | etp_set_max_poll_time (nseconds); |
803 | etp_set_max_poll_time (nseconds); |
775 | } |
804 | } |
776 | |
805 | |
|
|
806 | void ecb_cold |
777 | void eio_set_max_poll_reqs (unsigned int maxreqs) |
807 | eio_set_max_poll_reqs (unsigned int maxreqs) |
778 | { |
808 | { |
779 | etp_set_max_poll_reqs (maxreqs); |
809 | etp_set_max_poll_reqs (maxreqs); |
780 | } |
810 | } |
781 | |
811 | |
|
|
812 | void ecb_cold |
782 | void eio_set_max_idle (unsigned int nthreads) |
813 | eio_set_max_idle (unsigned int nthreads) |
783 | { |
814 | { |
784 | etp_set_max_idle (nthreads); |
815 | etp_set_max_idle (nthreads); |
785 | } |
816 | } |
786 | |
817 | |
|
|
818 | void ecb_cold |
787 | void eio_set_idle_timeout (unsigned int seconds) |
819 | eio_set_idle_timeout (unsigned int seconds) |
788 | { |
820 | { |
789 | etp_set_idle_timeout (seconds); |
821 | etp_set_idle_timeout (seconds); |
790 | } |
822 | } |
791 | |
823 | |
|
|
824 | void ecb_cold |
792 | void eio_set_min_parallel (unsigned int nthreads) |
825 | eio_set_min_parallel (unsigned int nthreads) |
793 | { |
826 | { |
794 | etp_set_min_parallel (nthreads); |
827 | etp_set_min_parallel (nthreads); |
795 | } |
828 | } |
796 | |
829 | |
|
|
830 | void ecb_cold |
797 | void eio_set_max_parallel (unsigned int nthreads) |
831 | eio_set_max_parallel (unsigned int nthreads) |
798 | { |
832 | { |
799 | etp_set_max_parallel (nthreads); |
833 | etp_set_max_parallel (nthreads); |
800 | } |
834 | } |
801 | |
835 | |
802 | int eio_poll (void) |
836 | int eio_poll (void) |
… | |
… | |
872 | #ifndef HAVE_FUTIMES |
906 | #ifndef HAVE_FUTIMES |
873 | |
907 | |
874 | # undef futimes |
908 | # undef futimes |
875 | # define futimes(fd,times) eio__futimes (fd, times) |
909 | # define futimes(fd,times) eio__futimes (fd, times) |
876 | |
910 | |
|
|
911 | static int |
877 | static int eio__futimes (int fd, const struct timeval tv[2]) |
912 | eio__futimes (int fd, const struct timeval tv[2]) |
878 | { |
913 | { |
879 | errno = ENOSYS; |
914 | errno = ENOSYS; |
880 | return -1; |
915 | return -1; |
881 | } |
916 | } |
882 | |
917 | |
… | |
… | |
886 | # undef fdatasync |
921 | # undef fdatasync |
887 | # define fdatasync(fd) fsync (fd) |
922 | # define fdatasync(fd) fsync (fd) |
888 | #endif |
923 | #endif |
889 | |
924 | |
890 | /* sync_file_range always needs emulation */ |
925 | /* sync_file_range always needs emulation */ |
891 | int |
926 | static int |
892 | eio__sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags) |
927 | eio__sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags) |
893 | { |
928 | { |
894 | #if HAVE_SYNC_FILE_RANGE |
929 | #if HAVE_SYNC_FILE_RANGE |
895 | int res; |
930 | int res; |
896 | |
931 | |
… | |
… | |
950 | if (!count) |
985 | if (!count) |
951 | return 0; |
986 | return 0; |
952 | |
987 | |
953 | for (;;) |
988 | for (;;) |
954 | { |
989 | { |
|
|
990 | #ifdef __APPLE__ |
|
|
991 | # undef HAVE_SENDFILE /* broken, as everything on os x */ |
|
|
992 | #endif |
955 | #if HAVE_SENDFILE |
993 | #if HAVE_SENDFILE |
956 | # if __linux |
994 | # if __linux |
957 | off_t soffset = offset; |
995 | off_t soffset = offset; |
958 | res = sendfile (ofd, ifd, &soffset, count); |
996 | res = sendfile (ofd, ifd, &soffset, count); |
959 | |
997 | |
… | |
… | |
1084 | count -= cnt; |
1122 | count -= cnt; |
1085 | } |
1123 | } |
1086 | } |
1124 | } |
1087 | |
1125 | |
1088 | return res; |
1126 | return res; |
|
|
1127 | } |
|
|
1128 | |
|
|
1129 | #ifdef PAGESIZE |
|
|
1130 | # define eio_pagesize() PAGESIZE |
|
|
1131 | #else |
|
|
1132 | static intptr_t |
|
|
1133 | eio_pagesize (void) |
|
|
1134 | { |
|
|
1135 | static intptr_t page; |
|
|
1136 | |
|
|
1137 | if (!page) |
|
|
1138 | page = sysconf (_SC_PAGESIZE); |
|
|
1139 | |
|
|
1140 | return page; |
|
|
1141 | } |
|
|
1142 | #endif |
|
|
1143 | |
|
|
1144 | static void |
|
|
1145 | eio_page_align (void **addr, size_t *length) |
|
|
1146 | { |
|
|
1147 | intptr_t mask = eio_pagesize () - 1; |
|
|
1148 | |
|
|
1149 | /* round down addr */ |
|
|
1150 | intptr_t adj = mask & (intptr_t)*addr; |
|
|
1151 | |
|
|
1152 | *addr = (void *)((intptr_t)*addr - adj); |
|
|
1153 | *length += adj; |
|
|
1154 | |
|
|
1155 | /* round up length */ |
|
|
1156 | *length = (*length + mask) & ~mask; |
|
|
1157 | } |
|
|
1158 | |
|
|
1159 | #if !_POSIX_MEMLOCK |
|
|
1160 | # define eio__mlockall(a) ((errno = ENOSYS), -1) |
|
|
1161 | #else |
|
|
1162 | |
|
|
1163 | static int |
|
|
1164 | eio__mlockall (int flags) |
|
|
1165 | { |
|
|
1166 | #if __GLIBC__ == 2 && __GLIBC_MINOR__ <= 7 |
|
|
1167 | extern int mallopt (int, int); |
|
|
1168 | mallopt (-6, 238); /* http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=473812 */ |
|
|
1169 | #endif |
|
|
1170 | |
|
|
1171 | if (EIO_MCL_CURRENT != MCL_CURRENT |
|
|
1172 | || EIO_MCL_FUTURE != MCL_FUTURE) |
|
|
1173 | { |
|
|
1174 | flags = 0 |
|
|
1175 | | (flags & EIO_MCL_CURRENT ? MCL_CURRENT : 0) |
|
|
1176 | | (flags & EIO_MCL_FUTURE ? MCL_FUTURE : 0); |
|
|
1177 | } |
|
|
1178 | |
|
|
1179 | return mlockall (flags); |
|
|
1180 | } |
|
|
1181 | #endif |
|
|
1182 | |
|
|
1183 | #if !_POSIX_MEMLOCK_RANGE |
|
|
1184 | # define eio__mlock(a,b) ((errno = ENOSYS), -1) |
|
|
1185 | #else |
|
|
1186 | |
|
|
1187 | static int |
|
|
1188 | eio__mlock (void *addr, size_t length) |
|
|
1189 | { |
|
|
1190 | eio_page_align (&addr, &length); |
|
|
1191 | |
|
|
1192 | return mlock (addr, length); |
|
|
1193 | } |
|
|
1194 | |
|
|
1195 | #endif |
|
|
1196 | |
|
|
1197 | #if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO) |
|
|
1198 | # define eio__msync(a,b,c) ((errno = ENOSYS), -1) |
|
|
1199 | #else |
|
|
1200 | |
|
|
1201 | static int |
|
|
1202 | eio__msync (void *mem, size_t len, int flags) |
|
|
1203 | { |
|
|
1204 | eio_page_align (&mem, &len); |
|
|
1205 | |
|
|
1206 | if (EIO_MS_ASYNC != MS_SYNC |
|
|
1207 | || EIO_MS_INVALIDATE != MS_INVALIDATE |
|
|
1208 | || EIO_MS_SYNC != MS_SYNC) |
|
|
1209 | { |
|
|
1210 | flags = 0 |
|
|
1211 | | (flags & EIO_MS_ASYNC ? MS_ASYNC : 0) |
|
|
1212 | | (flags & EIO_MS_INVALIDATE ? MS_INVALIDATE : 0) |
|
|
1213 | | (flags & EIO_MS_SYNC ? MS_SYNC : 0); |
|
|
1214 | } |
|
|
1215 | |
|
|
1216 | return msync (mem, len, flags); |
|
|
1217 | } |
|
|
1218 | |
|
|
1219 | #endif |
|
|
1220 | |
|
|
1221 | static int |
|
|
1222 | eio__mtouch (eio_req *req) |
|
|
1223 | { |
|
|
1224 | void *mem = req->ptr2; |
|
|
1225 | size_t len = req->size; |
|
|
1226 | int flags = req->int1; |
|
|
1227 | |
|
|
1228 | eio_page_align (&mem, &len); |
|
|
1229 | |
|
|
1230 | { |
|
|
1231 | intptr_t addr = (intptr_t)mem; |
|
|
1232 | intptr_t end = addr + len; |
|
|
1233 | intptr_t page = eio_pagesize (); |
|
|
1234 | |
|
|
1235 | if (addr < end) |
|
|
1236 | if (flags & EIO_MT_MODIFY) /* modify */ |
|
|
1237 | do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < len && !EIO_CANCELLED (req)); |
|
|
1238 | else |
|
|
1239 | do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < len && !EIO_CANCELLED (req)); |
|
|
1240 | } |
|
|
1241 | |
|
|
1242 | return 0; |
|
|
1243 | } |
|
|
1244 | |
|
|
1245 | /*****************************************************************************/ |
|
|
1246 | /* requests implemented outside eio_execute, because they are so large */ |
|
|
1247 | |
|
|
1248 | static void |
|
|
1249 | eio__realpath (eio_req *req, etp_worker *self) |
|
|
1250 | { |
|
|
1251 | char *rel = req->ptr1; |
|
|
1252 | char *res; |
|
|
1253 | char *tmp1, *tmp2; |
|
|
1254 | #if SYMLOOP_MAX > 32 |
|
|
1255 | int symlinks = SYMLOOP_MAX; |
|
|
1256 | #else |
|
|
1257 | int symlinks = 32; |
|
|
1258 | #endif |
|
|
1259 | |
|
|
1260 | req->result = -1; |
|
|
1261 | |
|
|
1262 | errno = EINVAL; |
|
|
1263 | if (!rel) |
|
|
1264 | return; |
|
|
1265 | |
|
|
1266 | errno = ENOENT; |
|
|
1267 | if (!*rel) |
|
|
1268 | return; |
|
|
1269 | |
|
|
1270 | if (!req->ptr2) |
|
|
1271 | { |
|
|
1272 | X_LOCK (wrklock); |
|
|
1273 | req->flags |= EIO_FLAG_PTR2_FREE; |
|
|
1274 | X_UNLOCK (wrklock); |
|
|
1275 | req->ptr2 = malloc (PATH_MAX * 3); |
|
|
1276 | |
|
|
1277 | errno = ENOMEM; |
|
|
1278 | if (!req->ptr2) |
|
|
1279 | return; |
|
|
1280 | } |
|
|
1281 | |
|
|
1282 | res = req->ptr2; |
|
|
1283 | tmp1 = res + PATH_MAX; |
|
|
1284 | tmp2 = tmp1 + PATH_MAX; |
|
|
1285 | |
|
|
1286 | #if 0 /* disabled, the musl way to do things is just too racy */ |
|
|
1287 | #if __linux && defined(O_NONBLOCK) && defined(O_NOATIME) |
|
|
1288 | /* on linux we may be able to ask the kernel */ |
|
|
1289 | { |
|
|
1290 | int fd = open (rel, O_RDONLY | O_NONBLOCK | O_NOCTTY | O_NOATIME); |
|
|
1291 | |
|
|
1292 | if (fd >= 0) |
|
|
1293 | { |
|
|
1294 | sprintf (tmp1, "/proc/self/fd/%d", fd); |
|
|
1295 | req->result = readlink (tmp1, res, PATH_MAX); |
|
|
1296 | close (fd); |
|
|
1297 | |
|
|
1298 | /* here we should probably stat the open file and the disk file, to make sure they still match */ |
|
|
1299 | |
|
|
1300 | if (req->result > 0) |
|
|
1301 | goto done; |
|
|
1302 | } |
|
|
1303 | else if (errno == ELOOP || errno == ENAMETOOLONG || errno == ENOENT || errno == ENOTDIR || errno == EIO) |
|
|
1304 | return; |
|
|
1305 | } |
|
|
1306 | #endif |
|
|
1307 | #endif |
|
|
1308 | |
|
|
1309 | if (*rel != '/') |
|
|
1310 | { |
|
|
1311 | if (!getcwd (res, PATH_MAX)) |
|
|
1312 | return; |
|
|
1313 | |
|
|
1314 | if (res [1]) /* only use if not / */ |
|
|
1315 | res += strlen (res); |
|
|
1316 | } |
|
|
1317 | |
|
|
1318 | while (*rel) |
|
|
1319 | { |
|
|
1320 | ssize_t len, linklen; |
|
|
1321 | char *beg = rel; |
|
|
1322 | |
|
|
1323 | while (*rel && *rel != '/') |
|
|
1324 | ++rel; |
|
|
1325 | |
|
|
1326 | len = rel - beg; |
|
|
1327 | |
|
|
1328 | if (!len) /* skip slashes */ |
|
|
1329 | { |
|
|
1330 | ++rel; |
|
|
1331 | continue; |
|
|
1332 | } |
|
|
1333 | |
|
|
1334 | if (beg [0] == '.') |
|
|
1335 | { |
|
|
1336 | if (len == 1) |
|
|
1337 | continue; /* . - nop */ |
|
|
1338 | |
|
|
1339 | if (beg [1] == '.' && len == 2) |
|
|
1340 | { |
|
|
1341 | /* .. - back up one component, if possible */ |
|
|
1342 | |
|
|
1343 | while (res != req->ptr2) |
|
|
1344 | if (*--res == '/') |
|
|
1345 | break; |
|
|
1346 | |
|
|
1347 | continue; |
|
|
1348 | } |
|
|
1349 | } |
|
|
1350 | |
|
|
1351 | errno = ENAMETOOLONG; |
|
|
1352 | if (res + 1 + len + 1 >= tmp1) |
|
|
1353 | return; |
|
|
1354 | |
|
|
1355 | /* copy one component */ |
|
|
1356 | *res = '/'; |
|
|
1357 | memcpy (res + 1, beg, len); |
|
|
1358 | |
|
|
1359 | /* zero-terminate, for readlink */ |
|
|
1360 | res [len + 1] = 0; |
|
|
1361 | |
|
|
1362 | /* now check if it's a symlink */ |
|
|
1363 | linklen = readlink (req->ptr2, tmp1, PATH_MAX); |
|
|
1364 | |
|
|
1365 | if (linklen < 0) |
|
|
1366 | { |
|
|
1367 | if (errno != EINVAL) |
|
|
1368 | return; |
|
|
1369 | |
|
|
1370 | /* it's a normal directory. hopefully */ |
|
|
1371 | res += len + 1; |
|
|
1372 | } |
|
|
1373 | else |
|
|
1374 | { |
|
|
1375 | /* yay, it was a symlink - build new path in tmp2 */ |
|
|
1376 | int rellen = strlen (rel); |
|
|
1377 | |
|
|
1378 | errno = ENAMETOOLONG; |
|
|
1379 | if (linklen + 1 + rellen >= PATH_MAX) |
|
|
1380 | return; |
|
|
1381 | |
|
|
1382 | errno = ELOOP; |
|
|
1383 | if (!--symlinks) |
|
|
1384 | return; |
|
|
1385 | |
|
|
1386 | if (*tmp1 == '/') |
|
|
1387 | res = req->ptr2; /* symlink resolves to an absolute path */ |
|
|
1388 | |
|
|
1389 | /* we need to be careful, as rel might point into tmp2 already */ |
|
|
1390 | memmove (tmp2 + linklen + 1, rel, rellen + 1); |
|
|
1391 | tmp2 [linklen] = '/'; |
|
|
1392 | memcpy (tmp2, tmp1, linklen); |
|
|
1393 | |
|
|
1394 | rel = tmp2; |
|
|
1395 | } |
|
|
1396 | } |
|
|
1397 | |
|
|
1398 | /* special case for the lone root path */ |
|
|
1399 | if (res == req->ptr2) |
|
|
1400 | *res++ = '/'; |
|
|
1401 | |
|
|
1402 | req->result = res - (char *)req->ptr2; |
|
|
1403 | |
|
|
1404 | done: |
|
|
1405 | req->ptr2 = realloc (req->ptr2, req->result); /* trade time for space savings */ |
1089 | } |
1406 | } |
1090 | |
1407 | |
1091 | static signed char |
1408 | static signed char |
1092 | eio_dent_cmp (const eio_dirent *a, const eio_dirent *b) |
1409 | eio_dent_cmp (const eio_dirent *a, const eio_dirent *b) |
1093 | { |
1410 | { |
… | |
… | |
1275 | |
1592 | |
1276 | X_LOCK (wrklock); |
1593 | X_LOCK (wrklock); |
1277 | /* the corresponding closedir is in ETP_WORKER_CLEAR */ |
1594 | /* the corresponding closedir is in ETP_WORKER_CLEAR */ |
1278 | self->dirp = dirp = opendir (req->ptr1); |
1595 | self->dirp = dirp = opendir (req->ptr1); |
1279 | |
1596 | |
|
|
1597 | if (req->flags & EIO_FLAG_PTR1_FREE) |
|
|
1598 | free (req->ptr1); |
|
|
1599 | |
1280 | req->flags |= EIO_FLAG_PTR1_FREE | EIO_FLAG_PTR2_FREE; |
1600 | req->flags |= EIO_FLAG_PTR1_FREE | EIO_FLAG_PTR2_FREE; |
1281 | req->ptr1 = dents = flags ? malloc (dentalloc * sizeof (eio_dirent)) : 0; |
1601 | req->ptr1 = dents = flags ? malloc (dentalloc * sizeof (eio_dirent)) : 0; |
1282 | req->ptr2 = names = malloc (namesalloc); |
1602 | req->ptr2 = names = malloc (namesalloc); |
1283 | X_UNLOCK (wrklock); |
1603 | X_UNLOCK (wrklock); |
1284 | |
1604 | |
… | |
… | |
1335 | /* skip . and .. entries */ |
1655 | /* skip . and .. entries */ |
1336 | if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) |
1656 | if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) |
1337 | { |
1657 | { |
1338 | int len = D_NAMLEN (entp) + 1; |
1658 | int len = D_NAMLEN (entp) + 1; |
1339 | |
1659 | |
1340 | while (expect_false (namesoffs + len > namesalloc)) |
1660 | while (ecb_expect_false (namesoffs + len > namesalloc)) |
1341 | { |
1661 | { |
1342 | namesalloc *= 2; |
1662 | namesalloc *= 2; |
1343 | X_LOCK (wrklock); |
1663 | X_LOCK (wrklock); |
1344 | req->ptr2 = names = realloc (names, namesalloc); |
1664 | req->ptr2 = names = realloc (names, namesalloc); |
1345 | X_UNLOCK (wrklock); |
1665 | X_UNLOCK (wrklock); |
… | |
… | |
1352 | |
1672 | |
1353 | if (dents) |
1673 | if (dents) |
1354 | { |
1674 | { |
1355 | struct eio_dirent *ent; |
1675 | struct eio_dirent *ent; |
1356 | |
1676 | |
1357 | if (expect_false (dentoffs == dentalloc)) |
1677 | if (ecb_expect_false (dentoffs == dentalloc)) |
1358 | { |
1678 | { |
1359 | dentalloc *= 2; |
1679 | dentalloc *= 2; |
1360 | X_LOCK (wrklock); |
1680 | X_LOCK (wrklock); |
1361 | req->ptr1 = dents = realloc (dents, dentalloc * sizeof (eio_dirent)); |
1681 | req->ptr1 = dents = realloc (dents, dentalloc * sizeof (eio_dirent)); |
1362 | X_UNLOCK (wrklock); |
1682 | X_UNLOCK (wrklock); |
… | |
… | |
1450 | break; |
1770 | break; |
1451 | } |
1771 | } |
1452 | } |
1772 | } |
1453 | } |
1773 | } |
1454 | |
1774 | |
1455 | #ifdef PAGESIZE |
|
|
1456 | # define eio_pagesize() PAGESIZE |
|
|
1457 | #else |
|
|
1458 | static intptr_t |
|
|
1459 | eio_pagesize (void) |
|
|
1460 | { |
|
|
1461 | static intptr_t page; |
|
|
1462 | |
|
|
1463 | if (!page) |
|
|
1464 | page = sysconf (_SC_PAGESIZE); |
|
|
1465 | |
|
|
1466 | return page; |
|
|
1467 | } |
|
|
1468 | #endif |
|
|
1469 | |
|
|
1470 | static void |
|
|
1471 | eio_page_align (void **addr, size_t *length) |
|
|
1472 | { |
|
|
1473 | intptr_t mask = eio_pagesize () - 1; |
|
|
1474 | |
|
|
1475 | /* round down addr */ |
|
|
1476 | intptr_t adj = mask & (intptr_t)*addr; |
|
|
1477 | |
|
|
1478 | *addr = (void *)((intptr_t)*addr - adj); |
|
|
1479 | *length += adj; |
|
|
1480 | |
|
|
1481 | /* round up length */ |
|
|
1482 | *length = (*length + mask) & ~mask; |
|
|
1483 | } |
|
|
1484 | |
|
|
1485 | #if !_POSIX_MEMLOCK |
|
|
1486 | # define eio__mlockall(a) ((errno = ENOSYS), -1) |
|
|
1487 | #else |
|
|
1488 | |
|
|
1489 | static int |
|
|
1490 | eio__mlockall (int flags) |
|
|
1491 | { |
|
|
1492 | #if __GLIBC__ == 2 && __GLIBC_MINOR__ <= 7 |
|
|
1493 | extern int mallopt (int, int); |
|
|
1494 | mallopt (-6, 238); /* http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=473812 */ |
|
|
1495 | #endif |
|
|
1496 | |
|
|
1497 | if (EIO_MCL_CURRENT != MCL_CURRENT |
|
|
1498 | || EIO_MCL_FUTURE != MCL_FUTURE) |
|
|
1499 | { |
|
|
1500 | flags = 0 |
|
|
1501 | | (flags & EIO_MCL_CURRENT ? MCL_CURRENT : 0) |
|
|
1502 | | (flags & EIO_MCL_FUTURE ? MCL_FUTURE : 0); |
|
|
1503 | } |
|
|
1504 | |
|
|
1505 | return mlockall (flags); |
|
|
1506 | } |
|
|
1507 | #endif |
|
|
1508 | |
|
|
1509 | #if !_POSIX_MEMLOCK_RANGE |
|
|
1510 | # define eio__mlock(a,b) ((errno = ENOSYS), -1) |
|
|
1511 | #else |
|
|
1512 | |
|
|
1513 | static int |
|
|
1514 | eio__mlock (void *addr, size_t length) |
|
|
1515 | { |
|
|
1516 | eio_page_align (&addr, &length); |
|
|
1517 | |
|
|
1518 | return mlock (addr, length); |
|
|
1519 | } |
|
|
1520 | |
|
|
1521 | #endif |
|
|
1522 | |
|
|
1523 | #if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO) |
|
|
1524 | # define eio__msync(a,b,c) ((errno = ENOSYS), -1) |
|
|
1525 | #else |
|
|
1526 | |
|
|
1527 | int |
|
|
1528 | eio__msync (void *mem, size_t len, int flags) |
|
|
1529 | { |
|
|
1530 | eio_page_align (&mem, &len); |
|
|
1531 | |
|
|
1532 | if (EIO_MS_ASYNC != MS_SYNC |
|
|
1533 | || EIO_MS_INVALIDATE != MS_INVALIDATE |
|
|
1534 | || EIO_MS_SYNC != MS_SYNC) |
|
|
1535 | { |
|
|
1536 | flags = 0 |
|
|
1537 | | (flags & EIO_MS_ASYNC ? MS_ASYNC : 0) |
|
|
1538 | | (flags & EIO_MS_INVALIDATE ? MS_INVALIDATE : 0) |
|
|
1539 | | (flags & EIO_MS_SYNC ? MS_SYNC : 0); |
|
|
1540 | } |
|
|
1541 | |
|
|
1542 | return msync (mem, len, flags); |
|
|
1543 | } |
|
|
1544 | |
|
|
1545 | #endif |
|
|
1546 | |
|
|
1547 | int |
|
|
1548 | eio__mtouch (eio_req *req) |
|
|
1549 | { |
|
|
1550 | void *mem = req->ptr2; |
|
|
1551 | size_t len = req->size; |
|
|
1552 | int flags = req->int1; |
|
|
1553 | |
|
|
1554 | eio_page_align (&mem, &len); |
|
|
1555 | |
|
|
1556 | { |
|
|
1557 | intptr_t addr = (intptr_t)mem; |
|
|
1558 | intptr_t end = addr + len; |
|
|
1559 | intptr_t page = eio_pagesize (); |
|
|
1560 | |
|
|
1561 | if (addr < end) |
|
|
1562 | if (flags & EIO_MT_MODIFY) /* modify */ |
|
|
1563 | do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < len && !EIO_CANCELLED (req)); |
|
|
1564 | else |
|
|
1565 | do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < len && !EIO_CANCELLED (req)); |
|
|
1566 | } |
|
|
1567 | |
|
|
1568 | return 0; |
|
|
1569 | } |
|
|
1570 | |
|
|
1571 | /*****************************************************************************/ |
1775 | /*****************************************************************************/ |
1572 | |
1776 | |
1573 | #define ALLOC(len) \ |
1777 | #define ALLOC(len) \ |
1574 | if (!req->ptr2) \ |
1778 | if (!req->ptr2) \ |
1575 | { \ |
1779 | { \ |
… | |
… | |
1632 | X_UNLOCK (reqlock); |
1836 | X_UNLOCK (reqlock); |
1633 | |
1837 | |
1634 | if (req->type < 0) |
1838 | if (req->type < 0) |
1635 | goto quit; |
1839 | goto quit; |
1636 | |
1840 | |
1637 | if (!EIO_CANCELLED (req)) |
|
|
1638 | ETP_EXECUTE (self, req); |
1841 | ETP_EXECUTE (self, req); |
1639 | |
1842 | |
1640 | X_LOCK (reslock); |
1843 | X_LOCK (reslock); |
1641 | |
1844 | |
1642 | ++npending; |
1845 | ++npending; |
1643 | |
1846 | |
… | |
… | |
1658 | return 0; |
1861 | return 0; |
1659 | } |
1862 | } |
1660 | |
1863 | |
1661 | /*****************************************************************************/ |
1864 | /*****************************************************************************/ |
1662 | |
1865 | |
|
|
1866 | int ecb_cold |
1663 | int eio_init (void (*want_poll)(void), void (*done_poll)(void)) |
1867 | eio_init (void (*want_poll)(void), void (*done_poll)(void)) |
1664 | { |
1868 | { |
1665 | return etp_init (want_poll, done_poll); |
1869 | return etp_init (want_poll, done_poll); |
1666 | } |
1870 | } |
1667 | |
1871 | |
|
|
1872 | ecb_inline void |
1668 | static void eio_api_destroy (eio_req *req) |
1873 | eio_api_destroy (eio_req *req) |
1669 | { |
1874 | { |
1670 | free (req); |
1875 | free (req); |
1671 | } |
1876 | } |
1672 | |
1877 | |
1673 | #define REQ(rtype) \ |
1878 | #define REQ(rtype) \ |
… | |
… | |
1692 | { \ |
1897 | { \ |
1693 | eio_api_destroy (req); \ |
1898 | eio_api_destroy (req); \ |
1694 | return 0; \ |
1899 | return 0; \ |
1695 | } |
1900 | } |
1696 | |
1901 | |
|
|
1902 | static void |
1697 | static void eio_execute (etp_worker *self, eio_req *req) |
1903 | eio_execute (etp_worker *self, eio_req *req) |
1698 | { |
1904 | { |
|
|
1905 | if (ecb_expect_false (EIO_CANCELLED (req))) |
|
|
1906 | { |
|
|
1907 | req->result = -1; |
|
|
1908 | req->errorno = ECANCELED; |
|
|
1909 | return; |
|
|
1910 | } |
|
|
1911 | |
1699 | switch (req->type) |
1912 | switch (req->type) |
1700 | { |
1913 | { |
1701 | case EIO_READ: ALLOC (req->size); |
1914 | case EIO_READ: ALLOC (req->size); |
1702 | req->result = req->offs >= 0 |
1915 | req->result = req->offs >= 0 |
1703 | ? pread (req->int1, req->ptr2, req->size, req->offs) |
1916 | ? pread (req->int1, req->ptr2, req->size, req->offs) |
… | |
… | |
1737 | case EIO_RENAME: req->result = rename (req->ptr1, req->ptr2); break; |
1950 | case EIO_RENAME: req->result = rename (req->ptr1, req->ptr2); break; |
1738 | case EIO_LINK: req->result = link (req->ptr1, req->ptr2); break; |
1951 | case EIO_LINK: req->result = link (req->ptr1, req->ptr2); break; |
1739 | case EIO_SYMLINK: req->result = symlink (req->ptr1, req->ptr2); break; |
1952 | case EIO_SYMLINK: req->result = symlink (req->ptr1, req->ptr2); break; |
1740 | case EIO_MKNOD: req->result = mknod (req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break; |
1953 | case EIO_MKNOD: req->result = mknod (req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break; |
1741 | |
1954 | |
|
|
1955 | case EIO_REALPATH: eio__realpath (req, self); break; |
|
|
1956 | |
1742 | case EIO_READLINK: ALLOC (PATH_MAX); |
1957 | case EIO_READLINK: ALLOC (PATH_MAX); |
1743 | req->result = readlink (req->ptr1, req->ptr2, PATH_MAX); break; |
1958 | req->result = readlink (req->ptr1, req->ptr2, PATH_MAX); break; |
1744 | |
1959 | |
1745 | case EIO_SYNC: req->result = 0; sync (); break; |
1960 | case EIO_SYNC: req->result = 0; sync (); break; |
1746 | case EIO_FSYNC: req->result = fsync (req->int1); break; |
1961 | case EIO_FSYNC: req->result = fsync (req->int1); break; |
… | |
… | |
1963 | eio_req *eio_readlink (const char *path, int pri, eio_cb cb, void *data) |
2178 | eio_req *eio_readlink (const char *path, int pri, eio_cb cb, void *data) |
1964 | { |
2179 | { |
1965 | return eio__1path (EIO_READLINK, path, pri, cb, data); |
2180 | return eio__1path (EIO_READLINK, path, pri, cb, data); |
1966 | } |
2181 | } |
1967 | |
2182 | |
|
|
2183 | eio_req *eio_realpath (const char *path, int pri, eio_cb cb, void *data) |
|
|
2184 | { |
|
|
2185 | return eio__1path (EIO_REALPATH, path, pri, cb, data); |
|
|
2186 | } |
|
|
2187 | |
1968 | eio_req *eio_stat (const char *path, int pri, eio_cb cb, void *data) |
2188 | eio_req *eio_stat (const char *path, int pri, eio_cb cb, void *data) |
1969 | { |
2189 | { |
1970 | return eio__1path (EIO_STAT, path, pri, cb, data); |
2190 | return eio__1path (EIO_STAT, path, pri, cb, data); |
1971 | } |
2191 | } |
1972 | |
2192 | |
… | |
… | |
2029 | eio_req *eio_rename (const char *path, const char *new_path, int pri, eio_cb cb, void *data) |
2249 | eio_req *eio_rename (const char *path, const char *new_path, int pri, eio_cb cb, void *data) |
2030 | { |
2250 | { |
2031 | return eio__2path (EIO_RENAME, path, new_path, pri, cb, data); |
2251 | return eio__2path (EIO_RENAME, path, new_path, pri, cb, data); |
2032 | } |
2252 | } |
2033 | |
2253 | |
2034 | eio_req *eio_custom (void (*)(eio_req *) execute, int pri, eio_cb cb, void *data); |
2254 | eio_req *eio_custom (void (*execute)(eio_req *), int pri, eio_cb cb, void *data) |
2035 | { |
2255 | { |
2036 | REQ (EIO_CUSTOM); req->feed = execute; SEND; |
2256 | REQ (EIO_CUSTOM); req->feed = execute; SEND; |
2037 | } |
2257 | } |
2038 | |
2258 | |
2039 | #endif |
2259 | #endif |
… | |
… | |
2050 | #undef SEND |
2270 | #undef SEND |
2051 | |
2271 | |
2052 | /*****************************************************************************/ |
2272 | /*****************************************************************************/ |
2053 | /* grp functions */ |
2273 | /* grp functions */ |
2054 | |
2274 | |
|
|
2275 | void |
2055 | void eio_grp_feed (eio_req *grp, void (*feed)(eio_req *req), int limit) |
2276 | eio_grp_feed (eio_req *grp, void (*feed)(eio_req *req), int limit) |
2056 | { |
2277 | { |
2057 | grp->int2 = limit; |
2278 | grp->int2 = limit; |
2058 | grp->feed = feed; |
2279 | grp->feed = feed; |
2059 | |
2280 | |
2060 | grp_try_feed (grp); |
2281 | grp_try_feed (grp); |
2061 | } |
2282 | } |
2062 | |
2283 | |
|
|
2284 | void |
2063 | void eio_grp_limit (eio_req *grp, int limit) |
2285 | eio_grp_limit (eio_req *grp, int limit) |
2064 | { |
2286 | { |
2065 | grp->int2 = limit; |
2287 | grp->int2 = limit; |
2066 | |
2288 | |
2067 | grp_try_feed (grp); |
2289 | grp_try_feed (grp); |
2068 | } |
2290 | } |
2069 | |
2291 | |
|
|
2292 | void |
2070 | void eio_grp_add (eio_req *grp, eio_req *req) |
2293 | eio_grp_add (eio_req *grp, eio_req *req) |
2071 | { |
2294 | { |
2072 | assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2)); |
2295 | assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2)); |
2073 | |
2296 | |
2074 | grp->flags |= EIO_FLAG_GROUPADD; |
2297 | grp->flags |= EIO_FLAG_GROUPADD; |
2075 | |
2298 | |
… | |
… | |
2086 | } |
2309 | } |
2087 | |
2310 | |
2088 | /*****************************************************************************/ |
2311 | /*****************************************************************************/ |
2089 | /* misc garbage */ |
2312 | /* misc garbage */ |
2090 | |
2313 | |
|
|
2314 | ssize_t |
2091 | ssize_t eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count) |
2315 | eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count) |
2092 | { |
2316 | { |
2093 | etp_worker wrk; |
2317 | etp_worker wrk; |
2094 | ssize_t ret; |
2318 | ssize_t ret; |
2095 | |
2319 | |
2096 | wrk.dbuf = 0; |
2320 | wrk.dbuf = 0; |