GRPC C++  1.62.0
alarm.h
Go to the documentation of this file.
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 
21 #ifndef GRPCPP_ALARM_H
22 #define GRPCPP_ALARM_H
23 
24 #include <functional>
25 
27 #include <grpc/grpc.h>
31 #include <grpcpp/support/time.h>
32 
33 namespace grpc {
34 
47  public:
49  Alarm();
50 
52  ~Alarm() override;
53 
61  template <typename T>
62  Alarm(grpc::CompletionQueue* cq, const T& deadline, void* tag) : Alarm() {
63  SetInternal(cq, grpc::TimePoint<T>(deadline).raw_time(), tag);
64  }
65 
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>
75  void Set(grpc::CompletionQueue* cq, const T& deadline, void* tag) {
76  SetInternal(cq, grpc::TimePoint<T>(deadline).raw_time(), tag);
77  }
78 
80  Alarm(const Alarm&) = delete;
81  Alarm& operator=(const Alarm&) = delete;
82 
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 
92  void Cancel();
93 
97  template <typename T>
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 
107 };
108 
109 } // namespace grpc
110 
111 #endif // GRPCPP_ALARM_H
time.h
grpc::Alarm::operator=
Alarm & operator=(const Alarm &)=delete
grpc
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm.h:33
event_engine.h
completion_queue_tag.h
grpc::Alarm::Alarm
Alarm(Alarm &&rhs) noexcept
Alarms are movable.
Definition: alarm.h:84
grpc::Alarm::operator=
Alarm & operator=(Alarm &&rhs) noexcept
Definition: alarm.h:85
grpc::Alarm::Cancel
void Cancel()
Cancel a completion queue alarm.
completion_queue.h
grpc.h
grpc::Alarm::Set
void Set(grpc::CompletionQueue *cq, const T &deadline, void *tag)
Trigger an alarm instance on completion queue cq at the specified time.
Definition: alarm.h:75
grpc::Alarm::Alarm
Alarm(grpc::CompletionQueue *cq, const T &deadline, void *tag)
DEPRECATED: Create and set a completion queue alarm instance associated to cq.
Definition: alarm.h:62
grpc::internal::GrpcLibrary
Classes that require gRPC to be initialized should inherit from this class.
Definition: grpc_library.h:32
grpc::Alarm::Alarm
Alarm()
Create an unset Alarm.
grpc::Alarm
Trigger a CompletionQueue event, or asynchronous callback execution, after some deadline.
Definition: alarm.h:46
grpc::internal::CompletionQueueTag
An interface allowing implementors to process and filter event tags.
Definition: completion_queue_tag.h:26
grpc::Alarm::~Alarm
~Alarm() override
Destroy the given completion queue alarm, cancelling it in the process.
grpc_library.h
grpc::CompletionQueue
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue....
Definition: completion_queue.h:103
gpr_timespec
Analogous to struct timespec.
Definition: time.h:48
grpc::Alarm::Set
void Set(const T &deadline, std::function< void(bool)> f)
Set an alarm to invoke callback f.
Definition: alarm.h:98
grpc::TimePoint
If you are trying to use CompletionQueue::AsyncNext with a time class that isn't either gpr_timespec ...
Definition: time.h:40