xref: /aosp_15_r20/external/grpc-grpc/include/grpcpp/alarm.h (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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 
19 /// An Alarm posts the user-provided tag to its associated completion queue or
20 /// invokes the user-provided function on expiry or cancellation.
21 #ifndef GRPCPP_ALARM_H
22 #define GRPCPP_ALARM_H
23 
24 #include <functional>
25 
26 #include <grpc/event_engine/event_engine.h>
27 #include <grpc/grpc.h>
28 #include <grpcpp/completion_queue.h>
29 #include <grpcpp/impl/completion_queue_tag.h>
30 #include <grpcpp/impl/grpc_library.h>
31 #include <grpcpp/support/time.h>
32 
33 namespace grpc {
34 
35 /// Trigger a \a CompletionQueue event, or asynchronous callback execution,
36 /// after some deadline.
37 ///
38 /// The \a Alarm API has separate \a Set methods for CompletionQueues and
39 /// callbacks, but only one can be used at any given time. After an alarm has
40 /// been triggered or cancelled, the same Alarm object may reused.
41 ///
42 /// Alarm methods are not thread-safe. Applications must ensure a strict
43 /// ordering between calls to \a Set and \a Cancel. This also implies that any
44 /// cancellation that occurs before the alarm has been set will have no effect
45 /// on any future \a Set calls.
46 class Alarm : private grpc::internal::GrpcLibrary {
47  public:
48   /// Create an unset Alarm.
49   Alarm();
50 
51   /// Destroy the given completion queue alarm, cancelling it in the process.
52   ~Alarm() override;
53 
54   /// DEPRECATED: Create and set a completion queue alarm instance associated to
55   /// \a cq.
56   /// This form is deprecated because it is inherently racy.
57   /// \internal We rely on the presence of \a cq for grpc initialization. If \a
58   /// cq were ever to be removed, a reference to a static
59   /// internal::GrpcLibraryInitializer instance would need to be introduced
60   /// here. \endinternal.
61   template <typename T>
Alarm(grpc::CompletionQueue * cq,const T & deadline,void * tag)62   Alarm(grpc::CompletionQueue* cq, const T& deadline, void* tag) : Alarm() {
63     SetInternal(cq, grpc::TimePoint<T>(deadline).raw_time(), tag);
64   }
65 
66   /// Trigger an alarm instance on completion queue \a cq at the specified time.
67   /// Once the alarm expires (at \a deadline) or it's cancelled (see \a Cancel),
68   /// an event with tag \a tag will be added to \a cq. If the alarm expired, the
69   /// event's success bit will be true, false otherwise (ie, upon cancellation).
70   //
71   // USAGE NOTE: This is frequently used to inject arbitrary tags into \a cq by
72   // setting an immediate deadline. Such usage allows synchronizing an external
73   // event with an application's \a grpc::CompletionQueue::Next loop.
74   template <typename T>
Set(grpc::CompletionQueue * cq,const T & deadline,void * tag)75   void Set(grpc::CompletionQueue* cq, const T& deadline, void* tag) {
76     SetInternal(cq, grpc::TimePoint<T>(deadline).raw_time(), tag);
77   }
78 
79   /// Alarms aren't copyable.
80   Alarm(const Alarm&) = delete;
81   Alarm& operator=(const Alarm&) = delete;
82 
83   /// Alarms are movable.
Alarm(Alarm && rhs)84   Alarm(Alarm&& rhs) noexcept : alarm_(rhs.alarm_) { rhs.alarm_ = nullptr; }
85   Alarm& operator=(Alarm&& rhs) noexcept {
86     std::swap(alarm_, rhs.alarm_);
87     return *this;
88   }
89 
90   /// Cancel a completion queue alarm. Calling this function over an alarm that
91   /// has already fired has no effect.
92   void Cancel();
93 
94   /// Set an alarm to invoke callback \a f. The argument to the callback
95   /// states whether the alarm expired at \a deadline (true) or was cancelled
96   /// (false)
97   template <typename T>
Set(const T & deadline,std::function<void (bool)> f)98   void Set(const T& deadline, std::function<void(bool)> f) {
99     SetInternal(grpc::TimePoint<T>(deadline).raw_time(), std::move(f));
100   }
101 
102  private:
103   void SetInternal(grpc::CompletionQueue* cq, gpr_timespec deadline, void* tag);
104   void SetInternal(gpr_timespec deadline, std::function<void(bool)> f);
105 
106   grpc::internal::CompletionQueueTag* alarm_;
107 };
108 
109 }  // namespace grpc
110 
111 #endif  // GRPCPP_ALARM_H
112