From cd91d4aa72a251c3216120f4a01835c1ac6bb8c7 Mon Sep 17 00:00:00 2001 From: Alex Shinn Date: Tue, 22 Nov 2011 21:15:05 +0900 Subject: [PATCH] fix to avoid maxing cpu when we want to wait on an fd --- lib/srfi/18/threads.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/srfi/18/threads.c b/lib/srfi/18/threads.c index 6d9ffe80..460c49f5 100644 --- a/lib/srfi/18/threads.c +++ b/lib/srfi/18/threads.c @@ -517,7 +517,18 @@ sexp sexp_scheduler (sexp ctx, sexp self, sexp_sint_t n, sexp root_thread) { sexp_cdr(sexp_global(ctx, SEXP_G_THREADS_BACK)) = SEXP_NULL; } } else { + /* no threads to dequeue */ res = ctx; + /* prefer a thread we can wait on instead of spinning */ + if (sexp_context_refuel(ctx) <= 0) { + for (ls1=paused; sexp_pairp(ls1); ls1=sexp_cdr(ls1)) { + evt = sexp_context_event(sexp_car(ls1)); + if (sexp_fixnump(evt) || sexp_portp(evt)) { + res = sexp_car(ls1); + break; + } + } + } } if (sexp_context_waitp(res)) {