diff --git a/BUILD b/BUILD index 51eb446630be67f75c83423b0ab1af47343ab98a..0e9f37a0e3fdadeb3a8eebcddfca9f7e70a6aaf6 100644 --- a/BUILD +++ b/BUILD @@ -818,7 +818,6 @@ cc_library( "src/cpp/client/credentials.cc", "src/cpp/client/generic_stub.cc", "src/cpp/client/insecure_credentials.cc", - "src/cpp/common/alarm.cc", "src/cpp/common/call.cc", "src/cpp/common/channel_arguments.cc", "src/cpp/common/completion_queue.cc", @@ -942,7 +941,6 @@ cc_library( "src/cpp/client/credentials.cc", "src/cpp/client/generic_stub.cc", "src/cpp/client/insecure_credentials.cc", - "src/cpp/common/alarm.cc", "src/cpp/common/call.cc", "src/cpp/common/channel_arguments.cc", "src/cpp/common/completion_queue.cc", diff --git a/Makefile b/Makefile index 941827bf9c5ebf9d91dce48a02330e29e8ecdde7..5be0d146afc88f7e5c7dfc129fadd3a3a1da5ffb 100644 --- a/Makefile +++ b/Makefile @@ -2990,7 +2990,6 @@ LIBGRPC++_SRC = \ src/cpp/client/credentials.cc \ src/cpp/client/generic_stub.cc \ src/cpp/client/insecure_credentials.cc \ - src/cpp/common/alarm.cc \ src/cpp/common/call.cc \ src/cpp/common/channel_arguments.cc \ src/cpp/common/completion_queue.cc \ @@ -3271,7 +3270,6 @@ LIBGRPC++_UNSECURE_SRC = \ src/cpp/client/credentials.cc \ src/cpp/client/generic_stub.cc \ src/cpp/client/insecure_credentials.cc \ - src/cpp/common/alarm.cc \ src/cpp/common/call.cc \ src/cpp/common/channel_arguments.cc \ src/cpp/common/completion_queue.cc \ diff --git a/build.yaml b/build.yaml index c4bbca6e2ce055de77b581da1fe0f7c9a24299c1..a277539a054b9ef85fc52924c6d70e132787f665 100644 --- a/build.yaml +++ b/build.yaml @@ -184,7 +184,6 @@ filegroups: - src/cpp/client/credentials.cc - src/cpp/client/generic_stub.cc - src/cpp/client/insecure_credentials.cc - - src/cpp/common/alarm.cc - src/cpp/common/call.cc - src/cpp/common/channel_arguments.cc - src/cpp/common/completion_queue.cc diff --git a/include/grpc++/alarm.h b/include/grpc++/alarm.h index 9979c34e4f13190ea70333af35a80ad5aec2f6e3..3b8104d135700fc45bd8418eaa002f38ddf75fc8 100644 --- a/include/grpc++/alarm.h +++ b/include/grpc++/alarm.h @@ -36,9 +36,12 @@ #ifndef GRPCXX_ALARM_H #define GRPCXX_ALARM_H +#include <grpc++/impl/codegen/completion_queue.h> #include <grpc++/impl/codegen/completion_queue_tag.h> #include <grpc++/impl/codegen/grpc_library.h> #include <grpc++/impl/codegen/time.h> +#include <grpc++/impl/grpc_library.h> +#include <grpc/grpc.h> struct grpc_alarm; @@ -54,14 +57,22 @@ class Alarm : private GrpcLibrary { /// Once the alarm expires (at \a deadline) or it's cancelled (see \a Cancel), /// an event with tag \a tag will be added to \a cq. If the alarm expired, the /// event's success bit will be true, false otherwise (ie, upon cancellation). - Alarm(CompletionQueue* cq, gpr_timespec deadline, void* tag); + /// \internal We rely on the presence of \a cq for grpc initialization. If \a + /// cq were ever to be removed, a reference to a static + /// internal::GrpcLibraryInitializer instance would need to be introduced + /// here. \endinternal. + template <typename T> + Alarm(CompletionQueue* cq, const T& deadline, void* tag) + : tag_(tag), + alarm_(grpc_alarm_create(cq->cq(), TimePoint<T>(deadline).raw_time(), + static_cast<void*>(&tag_))) {} /// Destroy the given completion queue alarm, cancelling it in the process. - ~Alarm(); + ~Alarm() { grpc_alarm_destroy(alarm_); } /// Cancel a completion queue alarm. Calling this function over an alarm that /// has already fired has no effect. - void Cancel(); + void Cancel() { grpc_alarm_cancel(alarm_); } private: class AlarmEntry : public CompletionQueueTag { diff --git a/src/core/surface/alarm.c b/src/core/surface/alarm.c index fb496f6c474ace029a792b43dada119c2d7b2706..8169ede065511111a326a992623e4eefccef947d 100644 --- a/src/core/surface/alarm.c +++ b/src/core/surface/alarm.c @@ -64,8 +64,9 @@ grpc_alarm *grpc_alarm_create(grpc_completion_queue *cq, gpr_timespec deadline, alarm->tag = tag; grpc_cq_begin_op(cq, tag); - grpc_timer_init(&exec_ctx, &alarm->alarm, deadline, alarm_cb, alarm, - gpr_now(GPR_CLOCK_MONOTONIC)); + grpc_timer_init(&exec_ctx, &alarm->alarm, + gpr_convert_clock_type(deadline, GPR_CLOCK_MONOTONIC), + alarm_cb, alarm, gpr_now(GPR_CLOCK_MONOTONIC)); grpc_exec_ctx_finish(&exec_ctx); return alarm; } diff --git a/src/cpp/common/alarm.cc b/src/cpp/common/alarm.cc deleted file mode 100644 index a2896887682aeea4e258ffe5a4dbcfca01b64052..0000000000000000000000000000000000000000 --- a/src/cpp/common/alarm.cc +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright 2015-2016, Google Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * * Neither the name of Google Inc. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ - -#include <grpc++/alarm.h> -#include <grpc++/completion_queue.h> -#include <grpc++/impl/grpc_library.h> -#include <grpc/grpc.h> - -namespace grpc { - -static internal::GrpcLibraryInitializer g_gli_initializer; -Alarm::Alarm(CompletionQueue* cq, gpr_timespec deadline, void* tag) - : tag_(tag), - alarm_(grpc_alarm_create(cq->cq(), deadline, static_cast<void*>(&tag_))) { - g_gli_initializer.summon(); -} - -Alarm::~Alarm() { grpc_alarm_destroy(alarm_); } - -void Alarm::Cancel() { grpc_alarm_cancel(alarm_); } - -} // namespace grpc diff --git a/test/cpp/common/alarm_cpp_test.cc b/test/cpp/common/alarm_cpp_test.cc index 5d7344046ce564df72fd0c323ac181fb21796ca3..d4381c051589cd1477d9e05bd7922daadbc76412 100644 --- a/test/cpp/common/alarm_cpp_test.cc +++ b/test/cpp/common/alarm_cpp_test.cc @@ -55,6 +55,23 @@ TEST(AlarmTest, RegularExpiry) { EXPECT_EQ(junk, output_tag); } +TEST(AlarmTest, RegularExpiryChrono) { + CompletionQueue cq; + void* junk = reinterpret_cast<void*>(1618033); + std::chrono::system_clock::time_point one_sec_deadline = + std::chrono::system_clock::now() + std::chrono::seconds(1); + Alarm alarm(&cq, one_sec_deadline, junk); + + void* output_tag; + bool ok; + const CompletionQueue::NextStatus status = cq.AsyncNext( + (void**)&output_tag, &ok, GRPC_TIMEOUT_SECONDS_TO_DEADLINE(2)); + + EXPECT_EQ(status, CompletionQueue::GOT_EVENT); + EXPECT_TRUE(ok); + EXPECT_EQ(junk, output_tag); +} + TEST(AlarmTest, ZeroExpiry) { CompletionQueue cq; void* junk = reinterpret_cast<void*>(1618033); diff --git a/tools/doxygen/Doxyfile.c++.internal b/tools/doxygen/Doxyfile.c++.internal index d5e5df86f64e920dd271c11ebe2058381b7f8edb..a2853108476f1c1b0cbf18fc62f679e9205f56a9 100644 --- a/tools/doxygen/Doxyfile.c++.internal +++ b/tools/doxygen/Doxyfile.c++.internal @@ -854,7 +854,6 @@ src/cpp/client/create_channel_internal.cc \ src/cpp/client/credentials.cc \ src/cpp/client/generic_stub.cc \ src/cpp/client/insecure_credentials.cc \ -src/cpp/common/alarm.cc \ src/cpp/common/call.cc \ src/cpp/common/channel_arguments.cc \ src/cpp/common/completion_queue.cc \ diff --git a/tools/run_tests/sources_and_headers.json b/tools/run_tests/sources_and_headers.json index 6b70f091ebec69594ed2cdf5a180e690b7f94308..0c7a6c7b5f34f277870ef17db4f1d4843033f620 100644 --- a/tools/run_tests/sources_and_headers.json +++ b/tools/run_tests/sources_and_headers.json @@ -5041,7 +5041,6 @@ "src/cpp/client/secure_credentials.cc", "src/cpp/client/secure_credentials.h", "src/cpp/codegen/grpc_library.cc", - "src/cpp/common/alarm.cc", "src/cpp/common/auth_property_iterator.cc", "src/cpp/common/call.cc", "src/cpp/common/channel_arguments.cc", @@ -5300,7 +5299,6 @@ "src/cpp/client/generic_stub.cc", "src/cpp/client/insecure_credentials.cc", "src/cpp/codegen/grpc_library.cc", - "src/cpp/common/alarm.cc", "src/cpp/common/call.cc", "src/cpp/common/channel_arguments.cc", "src/cpp/common/completion_queue.cc", diff --git a/vsprojects/vcxproj/grpc++/grpc++.vcxproj b/vsprojects/vcxproj/grpc++/grpc++.vcxproj index c62faf33e6fc8e7098d19c42e6ed342f77edf91d..0b8c345196913fbfef1cb03f287699ddf37b7f7e 100644 --- a/vsprojects/vcxproj/grpc++/grpc++.vcxproj +++ b/vsprojects/vcxproj/grpc++/grpc++.vcxproj @@ -369,8 +369,6 @@ </ClCompile> <ClCompile Include="$(SolutionDir)\..\src\cpp\client\insecure_credentials.cc"> </ClCompile> - <ClCompile Include="$(SolutionDir)\..\src\cpp\common\alarm.cc"> - </ClCompile> <ClCompile Include="$(SolutionDir)\..\src\cpp\common\call.cc"> </ClCompile> <ClCompile Include="$(SolutionDir)\..\src\cpp\common\channel_arguments.cc"> diff --git a/vsprojects/vcxproj/grpc++/grpc++.vcxproj.filters b/vsprojects/vcxproj/grpc++/grpc++.vcxproj.filters index 5f9350e76a00a708caa25bb92aab62e1d5b9a06f..0f3dccf17c5dce96580269ebaaf2dbc7656254ba 100644 --- a/vsprojects/vcxproj/grpc++/grpc++.vcxproj.filters +++ b/vsprojects/vcxproj/grpc++/grpc++.vcxproj.filters @@ -40,9 +40,6 @@ <ClCompile Include="$(SolutionDir)\..\src\cpp\client\insecure_credentials.cc"> <Filter>src\cpp\client</Filter> </ClCompile> - <ClCompile Include="$(SolutionDir)\..\src\cpp\common\alarm.cc"> - <Filter>src\cpp\common</Filter> - </ClCompile> <ClCompile Include="$(SolutionDir)\..\src\cpp\common\call.cc"> <Filter>src\cpp\common</Filter> </ClCompile> diff --git a/vsprojects/vcxproj/grpc++_unsecure/grpc++_unsecure.vcxproj b/vsprojects/vcxproj/grpc++_unsecure/grpc++_unsecure.vcxproj index fb4246580fd57a136610c45e3eca81dac7f563e4..2dcadbaec0c56d3e99aae6635740277300b3a673 100644 --- a/vsprojects/vcxproj/grpc++_unsecure/grpc++_unsecure.vcxproj +++ b/vsprojects/vcxproj/grpc++_unsecure/grpc++_unsecure.vcxproj @@ -356,8 +356,6 @@ </ClCompile> <ClCompile Include="$(SolutionDir)\..\src\cpp\client\insecure_credentials.cc"> </ClCompile> - <ClCompile Include="$(SolutionDir)\..\src\cpp\common\alarm.cc"> - </ClCompile> <ClCompile Include="$(SolutionDir)\..\src\cpp\common\call.cc"> </ClCompile> <ClCompile Include="$(SolutionDir)\..\src\cpp\common\channel_arguments.cc"> diff --git a/vsprojects/vcxproj/grpc++_unsecure/grpc++_unsecure.vcxproj.filters b/vsprojects/vcxproj/grpc++_unsecure/grpc++_unsecure.vcxproj.filters index eeff7d3697c49bc168147e3ea53cde47f2ff0fc0..3572c651b6d117446fb032c194787c398d6d0b50 100644 --- a/vsprojects/vcxproj/grpc++_unsecure/grpc++_unsecure.vcxproj.filters +++ b/vsprojects/vcxproj/grpc++_unsecure/grpc++_unsecure.vcxproj.filters @@ -25,9 +25,6 @@ <ClCompile Include="$(SolutionDir)\..\src\cpp\client\insecure_credentials.cc"> <Filter>src\cpp\client</Filter> </ClCompile> - <ClCompile Include="$(SolutionDir)\..\src\cpp\common\alarm.cc"> - <Filter>src\cpp\common</Filter> - </ClCompile> <ClCompile Include="$(SolutionDir)\..\src\cpp\common\call.cc"> <Filter>src\cpp\common</Filter> </ClCompile>