diff options
author | Vijay Pai <vpai@google.com> | 2015-03-22 14:56:01 -0700 |
---|---|---|
committer | Vijay Pai <vpai@google.com> | 2015-03-22 14:56:01 -0700 |
commit | 10bb15cdd444777cf2dd47f8d7e4fb559c4ace8b (patch) | |
tree | 653b8ad00da0fbdb63def2eb693e6f3e1e5009ec /src | |
parent | 93f1ed39b3306dff4fb7f0d2f2c5d4cc93543e1e (diff) | |
parent | cdb2a6e071dd57c1cf6f3658d3954bf959519be5 (diff) |
Merge pull request #1108 from yang-g/overflowfix
Time point conversion fix
Diffstat (limited to 'src')
-rw-r--r-- | src/cpp/common/completion_queue.cc | 17 | ||||
-rw-r--r-- | src/cpp/util/time.cc | 6 |
2 files changed, 15 insertions, 8 deletions
diff --git a/src/cpp/common/completion_queue.cc b/src/cpp/common/completion_queue.cc index fede2da016..cea2d24831 100644 --- a/src/cpp/common/completion_queue.cc +++ b/src/cpp/common/completion_queue.cc @@ -36,7 +36,6 @@ #include <grpc/grpc.h> #include <grpc/support/log.h> -#include <grpc/support/time.h> #include "src/cpp/util/time.h" namespace grpc { @@ -57,15 +56,12 @@ class EventDeleter { } }; -CompletionQueue::NextStatus -CompletionQueue::AsyncNext(void** tag, bool* ok, - std::chrono::system_clock::time_point deadline) { +CompletionQueue::NextStatus CompletionQueue::AsyncNextInternal( + void** tag, bool* ok, gpr_timespec deadline) { std::unique_ptr<grpc_event, EventDeleter> ev; - gpr_timespec gpr_deadline; - Timepoint2Timespec(deadline, &gpr_deadline); for (;;) { - ev.reset(grpc_completion_queue_next(cq_, gpr_deadline)); + ev.reset(grpc_completion_queue_next(cq_, deadline)); if (!ev) { /* got a NULL back because deadline passed */ return TIMEOUT; } @@ -81,6 +77,13 @@ CompletionQueue::AsyncNext(void** tag, bool* ok, } } +CompletionQueue::NextStatus CompletionQueue::AsyncNext( + void** tag, bool* ok, std::chrono::system_clock::time_point deadline) { + gpr_timespec gpr_deadline; + Timepoint2Timespec(deadline, &gpr_deadline); + return AsyncNextInternal(tag, ok, gpr_deadline); +} + bool CompletionQueue::Pluck(CompletionQueueTag* tag) { std::unique_ptr<grpc_event, EventDeleter> ev; diff --git a/src/cpp/util/time.cc b/src/cpp/util/time.cc index 44d2283e76..059ea72abf 100644 --- a/src/cpp/util/time.cc +++ b/src/cpp/util/time.cc @@ -42,11 +42,15 @@ using std::chrono::system_clock; namespace grpc { -// TODO(yangg) prevent potential overflow. void Timepoint2Timespec(const system_clock::time_point& from, gpr_timespec* to) { system_clock::duration deadline = from.time_since_epoch(); seconds secs = duration_cast<seconds>(deadline); + if (from == system_clock::time_point::max() || + secs.count() >= gpr_inf_future.tv_sec || secs.count() < 0) { + *to = gpr_inf_future; + return; + } nanoseconds nsecs = duration_cast<nanoseconds>(deadline - secs); to->tv_sec = secs.count(); to->tv_nsec = nsecs.count(); |