diff options
author | cyy <cyyever@outlook.com> | 2019-01-12 14:38:28 +0800 |
---|---|---|
committer | cyy <cyyever@outlook.com> | 2019-01-22 10:36:18 +0800 |
commit | bfdbd0344bd3ddf348985b51e02212e8092859d4 (patch) | |
tree | 78ba8220a1f8a72f94eb79c053b89df3a3d77f2a /lib/cpp/src/thrift/concurrency/TimerManager.cpp | |
parent | d12dbed670acb9fbe65662fb72d2069e5ee6d0a0 (diff) | |
download | thrift-bfdbd0344bd3ddf348985b51e02212e8092859d4.tar.gz |
use chrono
Diffstat (limited to 'lib/cpp/src/thrift/concurrency/TimerManager.cpp')
-rw-r--r-- | lib/cpp/src/thrift/concurrency/TimerManager.cpp | 91 |
1 files changed, 33 insertions, 58 deletions
diff --git a/lib/cpp/src/thrift/concurrency/TimerManager.cpp b/lib/cpp/src/thrift/concurrency/TimerManager.cpp index 61a34ff69..edd336be0 100644 --- a/lib/cpp/src/thrift/concurrency/TimerManager.cpp +++ b/lib/cpp/src/thrift/concurrency/TimerManager.cpp @@ -19,7 +19,6 @@ #include <thrift/concurrency/TimerManager.h> #include <thrift/concurrency/Exception.h> -#include <thrift/concurrency/Util.h> #include <assert.h> #include <iostream> @@ -90,21 +89,22 @@ public: { Synchronized s(manager_->monitor_); task_iterator expiredTaskEnd; - int64_t now = Util::currentTime(); + auto now = std::chrono::steady_clock::now(); while (manager_->state_ == TimerManager::STARTED && (expiredTaskEnd = manager_->taskMap_.upper_bound(now)) == manager_->taskMap_.begin()) { - int64_t timeout = 0LL; + std::chrono::milliseconds timeout(0); if (!manager_->taskMap_.empty()) { - timeout = manager_->taskMap_.begin()->first - now; - } - assert((timeout != 0 && manager_->taskCount_ > 0) - || (timeout == 0 && manager_->taskCount_ == 0)); - try { - manager_->monitor_.wait(timeout); - } catch (TimedOutException&) { + timeout = std::chrono::duration_cast<std::chrono::milliseconds>(manager_->taskMap_.begin()->first - now); + //because the unit of steady_clock is smaller than millisecond,timeout may be 0. + if (timeout.count() == 0) { + timeout = std::chrono::milliseconds(1); + } + manager_->monitor_.waitForTimeRelative(timeout); + } else { + manager_->monitor_.waitForTimeRelative(0); } - now = Util::currentTime(); + now = std::chrono::steady_clock::now(); } if (manager_->state_ == TimerManager::STARTED) { @@ -239,64 +239,39 @@ size_t TimerManager::taskCount() const { return taskCount_; } -TimerManager::Timer TimerManager::add(shared_ptr<Runnable> task, int64_t timeout) { - int64_t now = Util::currentTime(); - timeout += now; - - { - Synchronized s(monitor_); - if (state_ != TimerManager::STARTED) { - throw IllegalStateException(); - } - - // If the task map is empty, we will kick the dispatcher for sure. Otherwise, we kick him - // if the expiration time is shorter than the current value. Need to test before we insert, - // because the new task might insert at the front. - bool notifyRequired = (taskCount_ == 0) ? true : timeout < taskMap_.begin()->first; - - shared_ptr<Task> timer(new Task(task)); - taskCount_++; - timer->it_ = taskMap_.insert(std::pair<int64_t, shared_ptr<Task> >(timeout, timer)); - - // If the task map was empty, or if we have an expiration that is earlier - // than any previously seen, kick the dispatcher so it can update its - // timeout - if (notifyRequired) { - monitor_.notify(); - } - - return timer; - } +TimerManager::Timer TimerManager::add(shared_ptr<Runnable> task, const std::chrono::milliseconds &timeout) { + return add(task, std::chrono::steady_clock::now() + timeout); } TimerManager::Timer TimerManager::add(shared_ptr<Runnable> task, - const struct THRIFT_TIMESPEC& value) { + const std::chrono::time_point<std::chrono::steady_clock>& abstime) { + auto now = std::chrono::steady_clock::now(); - int64_t expiration; - Util::toMilliseconds(expiration, value); - - int64_t now = Util::currentTime(); - - if (expiration < now) { + if (abstime < now) { throw InvalidArgumentException(); } + Synchronized s(monitor_); + if (state_ != TimerManager::STARTED) { + throw IllegalStateException(); + } - return add(task, expiration - now); -} - -TimerManager::Timer TimerManager::add(shared_ptr<Runnable> task, - const struct timeval& value) { - - int64_t expiration; - Util::toMilliseconds(expiration, value); + // If the task map is empty, we will kick the dispatcher for sure. Otherwise, we kick him + // if the expiration time is shorter than the current value. Need to test before we insert, + // because the new task might insert at the front. + bool notifyRequired = (taskCount_ == 0) ? true : abstime < taskMap_.begin()->first; - int64_t now = Util::currentTime(); + shared_ptr<Task> timer(new Task(task)); + taskCount_++; + timer->it_ = taskMap_.emplace(abstime, timer); - if (expiration < now) { - throw InvalidArgumentException(); + // If the task map was empty, or if we have an expiration that is earlier + // than any previously seen, kick the dispatcher so it can update its + // timeout + if (notifyRequired) { + monitor_.notify(); } - return add(task, expiration - now); + return timer; } void TimerManager::remove(shared_ptr<Runnable> task) { |