limit waiting in thread scheduler to 10ms

This commit is contained in:
Alex Shinn 2018-06-13 22:33:39 +08:00
parent e8c10ce259
commit 5f428d1299

View file

@ -592,6 +592,8 @@ sexp sexp_scheduler (sexp ctx, sexp self, sexp_sint_t n, sexp root_thread) {
if (sexp_context_waitp(res)) { if (sexp_context_waitp(res)) {
/* the only thread available was waiting */ /* the only thread available was waiting */
/* TODO: if another thread is blocked on I/O, wait on that with
* the appropriate minimum timeout */
if (sexp_pairp(paused) if (sexp_pairp(paused)
&& sexp_context_before(sexp_car(paused), sexp_context_timeval(res))) { && sexp_context_before(sexp_car(paused), sexp_context_timeval(res))) {
tmp = res; tmp = res;
@ -609,13 +611,14 @@ sexp sexp_scheduler (sexp ctx, sexp self, sexp_sint_t n, sexp root_thread) {
/* no timeout, wait for default 10ms */ /* no timeout, wait for default 10ms */
usecs = 10*1000; usecs = 10*1000;
} else { } else {
/* wait until the next timeout */ /* wait until the next timeout, or at most 10ms */
gettimeofday(&tval, NULL); gettimeofday(&tval, NULL);
if (tval.tv_sec <= sexp_context_timeval(res).tv_sec) { if (tval.tv_sec <= sexp_context_timeval(res).tv_sec) {
usecs = (sexp_context_timeval(res).tv_sec - tval.tv_sec) * 1000000; usecs = (sexp_context_timeval(res).tv_sec - tval.tv_sec) * 1000000;
if (tval.tv_usec < sexp_context_timeval(res).tv_usec || usecs > 0) if (tval.tv_usec < sexp_context_timeval(res).tv_usec || usecs > 0)
usecs += sexp_context_timeval(res).tv_usec - tval.tv_usec; usecs += sexp_context_timeval(res).tv_usec - tval.tv_usec;
} }
if (usecs > 10*1000) usecs = 10*1000;
} }
/* take a nap to avoid busy looping */ /* take a nap to avoid busy looping */
usleep(usecs); usleep(usecs);