alarm.h 3.6 KB

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