alarm.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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/completion_queue.h>
  25. #include <grpcpp/impl/completion_queue_tag.h>
  26. #include <grpcpp/impl/grpc_library.h>
  27. #include <grpcpp/support/time.h>
  28. namespace grpc {
  29. class Alarm : private grpc::internal::GrpcLibrary {
  30. public:
  31. /// Create an unset completion queue alarm
  32. Alarm();
  33. /// Destroy the given completion queue alarm, cancelling it in the process.
  34. ~Alarm() override;
  35. /// DEPRECATED: Create and set a completion queue alarm instance associated to
  36. /// \a cq.
  37. /// This form is deprecated because it is inherently racy.
  38. /// \internal We rely on the presence of \a cq for grpc initialization. If \a
  39. /// cq were ever to be removed, a reference to a static
  40. /// internal::GrpcLibraryInitializer instance would need to be introduced
  41. /// here. \endinternal.
  42. template <typename T>
  43. Alarm(grpc::CompletionQueue* cq, const T& deadline, void* tag) : Alarm() {
  44. SetInternal(cq, grpc::TimePoint<T>(deadline).raw_time(), tag);
  45. }
  46. /// Trigger an alarm instance on completion queue \a cq at the specified time.
  47. /// Once the alarm expires (at \a deadline) or it's cancelled (see \a Cancel),
  48. /// an event with tag \a tag will be added to \a cq. If the alarm expired, the
  49. /// event's success bit will be true, false otherwise (ie, upon cancellation).
  50. //
  51. // USAGE NOTE: This is frequently used to inject arbitrary tags into \a cq by
  52. // setting an immediate deadline. Such usage allows synchronizing an external
  53. // event with an application's \a grpc::CompletionQueue::Next loop.
  54. template <typename T>
  55. void Set(grpc::CompletionQueue* cq, const T& deadline, void* tag) {
  56. SetInternal(cq, grpc::TimePoint<T>(deadline).raw_time(), tag);
  57. }
  58. /// Alarms aren't copyable.
  59. Alarm(const Alarm&) = delete;
  60. Alarm& operator=(const Alarm&) = delete;
  61. /// Alarms are movable.
  62. Alarm(Alarm&& rhs) noexcept : alarm_(rhs.alarm_) { rhs.alarm_ = nullptr; }
  63. Alarm& operator=(Alarm&& rhs) noexcept {
  64. std::swap(alarm_, rhs.alarm_);
  65. return *this;
  66. }
  67. /// Cancel a completion queue alarm. Calling this function over an alarm that
  68. /// has already fired has no effect.
  69. void Cancel();
  70. /// Set an alarm to invoke callback \a f. The argument to the callback
  71. /// states whether the alarm expired at \a deadline (true) or was cancelled
  72. /// (false)
  73. template <typename T>
  74. void Set(const T& deadline, std::function<void(bool)> f) {
  75. SetInternal(grpc::TimePoint<T>(deadline).raw_time(), std::move(f));
  76. }
  77. private:
  78. void SetInternal(grpc::CompletionQueue* cq, gpr_timespec deadline, void* tag);
  79. void SetInternal(gpr_timespec deadline, std::function<void(bool)> f);
  80. grpc::internal::CompletionQueueTag* alarm_;
  81. };
  82. } // namespace grpc
  83. #endif // GRPCPP_ALARM_H