From f5328d433c588e26a7763266208fe3460ef7ee99 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Mon, 9 Dec 2013 10:50:45 +0000 Subject: thrpool: sleep instead of yield when poking thread This unfortunate loop burned too much CPU on FreeBSD and caused shutdown to take too long when using sched_yield. nanosleep for 10ms instead, hopefully allowing the system to accomplish some disk I/O and other tasks before we poke it again. Reported-by: Mikolaj Golub --- thrpool.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/thrpool.c b/thrpool.c index 8ed5963..80bf1ff 100644 --- a/thrpool.c +++ b/thrpool.c @@ -86,9 +86,18 @@ static void poke(pthread_t thr, int sig) * This is an uncommon code path and only triggered when * we lower thread counts or shut down */ - while ((err = pthread_kill(thr, sig)) == 0) - mog_yield(); - + while ((err = pthread_kill(thr, sig)) == 0) { + /* + * sleep for 10 ms, sched_yield still burns too much CPU + * on FreeBSD (and likely other OSes) if a thread is waiting + * on disk I/O. + */ + struct timespec ts = { .tv_sec = 0, .tv_nsec = 10e6 }; + int rc = nanosleep(&ts, NULL); + + if (rc != 0) + assert(errno != EINVAL && "bug in using nanosleep"); + } assert(err == ESRCH && "pthread_kill() usage bug"); } -- cgit v1.2.3-24-ge0c7