From f522974146e8892c34e3713abab3b50ae24ee67c Mon Sep 17 00:00:00 2001 From: taocp Date: Wed, 17 Aug 2016 16:39:06 +0800 Subject: [PATCH] issue=#936 threadpool +1ms --- src/common/mutex.h | 11 ++++++++--- src/common/thread_pool.h | 4 ++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/common/mutex.h b/src/common/mutex.h index 956cc3641..b29ba4942 100644 --- a/src/common/mutex.h +++ b/src/common/mutex.h @@ -131,13 +131,13 @@ class CondVar { PthreadCall("condvar wait", pthread_cond_wait(&cond_, &mu_->mu_)); mu_->AfterLock(msg, msg_threshold); } - // Time wait in ms + // Time wait in us // timeout < 0 would cause ETIMEOUT and return false immediately - bool TimeWait(int timeout, const char* msg = NULL) { + bool TimeWaitInUs(int timeout, const char* msg = NULL) { timespec ts; struct timeval tv; gettimeofday(&tv, NULL); - int64_t usec = tv.tv_usec + timeout * 1000LL; + int64_t usec = tv.tv_usec + timeout; ts.tv_sec = tv.tv_sec + usec / 1000000; ts.tv_nsec = (usec % 1000000) * 1000; int64_t msg_threshold = mu_->msg_threshold_; @@ -146,6 +146,11 @@ class CondVar { mu_->AfterLock(msg, msg_threshold); return (ret == 0); } + // Time wait in ms + // timeout < 0 would cause ETIMEOUT and return false immediately + bool TimeWait(int timeout, const char* msg = NULL) { + return TimeWaitInUs(timeout * 1000LL, msg); + } void Signal() { PthreadCall("signal", pthread_cond_signal(&cond_)); } diff --git a/src/common/thread_pool.h b/src/common/thread_pool.h index d7d410d15..8bd3fa90f 100644 --- a/src/common/thread_pool.h +++ b/src/common/thread_pool.h @@ -188,7 +188,7 @@ class ThreadPool { if (!time_queue_.empty()) { int64_t now_time = timer::get_micros(); BGItem bg_item = time_queue_.top(); - int64_t wait_time = (bg_item.exe_time - now_time) / 1000; // in ms + int64_t wait_time = bg_item.exe_time - now_time; // in us if (wait_time <= 0) { time_queue_.pop(); BGMap::iterator it = latest_.find(bg_item.id); @@ -207,7 +207,7 @@ class ThreadPool { } continue; } else if (queue_.empty() && !stop_) { - work_cv_.TimeWait(wait_time, "ThreadProcTimeWait"); + work_cv_.TimeWaitInUs(wait_time, "ThreadProcTimeWait"); continue; } }