You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

alarm.h 4.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. *
  3. * Copyright 2015 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. /// An Alarm posts the user-provided tag to its associated completion queue or
  19. /// invokes the user-provided function on expiry or cancellation.
  20. #ifndef GRPCPP_ALARM_H
  21. #define GRPCPP_ALARM_H
  22. #include <functional>
  23. #include <grpc/grpc.h>
  24. #include <grpcpp/impl/codegen/completion_queue.h>
  25. #include <grpcpp/impl/codegen/completion_queue_tag.h>
  26. #include <grpcpp/impl/codegen/grpc_library.h>
  27. #include <grpcpp/impl/codegen/time.h>
  28. #include <grpcpp/impl/grpc_library.h>
  29. namespace grpc
  30. {
  31. class Alarm : private grpc::GrpcLibraryCodegen
  32. {
  33. public:
  34. /// Create an unset completion queue alarm
  35. Alarm();
  36. /// Destroy the given completion queue alarm, cancelling it in the process.
  37. ~Alarm() override;
  38. /// DEPRECATED: Create and set a completion queue alarm instance associated to
  39. /// \a cq.
  40. /// This form is deprecated because it is inherently racy.
  41. /// \internal We rely on the presence of \a cq for grpc initialization. If \a
  42. /// cq were ever to be removed, a reference to a static
  43. /// internal::GrpcLibraryInitializer instance would need to be introduced
  44. /// here. \endinternal.
  45. template<typename T>
  46. Alarm(grpc::CompletionQueue* cq, const T& deadline, void* tag) :
  47. Alarm()
  48. {
  49. SetInternal(cq, grpc::TimePoint<T>(deadline).raw_time(), tag);
  50. }
  51. /// Trigger an alarm instance on completion queue \a cq at the specified time.
  52. /// Once the alarm expires (at \a deadline) or it's cancelled (see \a Cancel),
  53. /// an event with tag \a tag will be added to \a cq. If the alarm expired, the
  54. /// event's success bit will be true, false otherwise (ie, upon cancellation).
  55. //
  56. // USAGE NOTE: This is frequently used to inject arbitrary tags into \a cq by
  57. // setting an immediate deadline. Such usage allows synchronizing an external
  58. // event with an application's \a grpc::CompletionQueue::Next loop.
  59. template<typename T>
  60. void Set(grpc::CompletionQueue* cq, const T& deadline, void* tag)
  61. {
  62. SetInternal(cq, grpc::TimePoint<T>(deadline).raw_time(), tag);
  63. }
  64. /// Alarms aren't copyable.
  65. Alarm(const Alarm&) = delete;
  66. Alarm& operator=(const Alarm&) = delete;
  67. /// Alarms are movable.
  68. Alarm(Alarm&& rhs) noexcept :
  69. alarm_(rhs.alarm_)
  70. {
  71. rhs.alarm_ = nullptr;
  72. }
  73. Alarm& operator=(Alarm&& rhs) noexcept
  74. {
  75. alarm_ = rhs.alarm_;
  76. rhs.alarm_ = nullptr;
  77. return *this;
  78. }
  79. /// Cancel a completion queue alarm. Calling this function over an alarm that
  80. /// has already fired has no effect.
  81. void Cancel();
  82. /// Set an alarm to invoke callback \a f. The argument to the callback
  83. /// states whether the alarm expired at \a deadline (true) or was cancelled
  84. /// (false)
  85. template<typename T>
  86. void Set(const T& deadline, std::function<void(bool)> f)
  87. {
  88. SetInternal(grpc::TimePoint<T>(deadline).raw_time(), std::move(f));
  89. }
  90. private:
  91. void SetInternal(grpc::CompletionQueue* cq, gpr_timespec deadline, void* tag);
  92. void SetInternal(gpr_timespec deadline, std::function<void(bool)> f);
  93. grpc::internal::CompletionQueueTag* alarm_;
  94. };
  95. } // namespace grpc
  96. #endif // GRPCPP_ALARM_H