GRPC C++  1.8.0
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 GRPCXX_ALARM_H
22 #define GRPCXX_ALARM_H
23 
29 #include <grpc/grpc.h>
30 
31 struct grpc_alarm;
32 
33 namespace grpc {
34 
35 class CompletionQueue;
36 
38 class Alarm : private GrpcLibraryCodegen {
39  public:
41  Alarm() : tag_(nullptr), alarm_(grpc_alarm_create(nullptr)) {}
42 
50  template <typename T>
51  Alarm(CompletionQueue* cq, const T& deadline, void* tag)
52  : tag_(tag), alarm_(grpc_alarm_create(nullptr)) {
53  grpc_alarm_set(alarm_, cq->cq(), TimePoint<T>(deadline).raw_time(),
54  static_cast<void*>(&tag_), nullptr);
55  }
56 
61  template <typename T>
62  void Set(CompletionQueue* cq, const T& deadline, void* tag) {
63  tag_.Set(tag);
64  grpc_alarm_set(alarm_, cq->cq(), TimePoint<T>(deadline).raw_time(),
65  static_cast<void*>(&tag_), nullptr);
66  }
67 
69  Alarm(const Alarm&) = delete;
70  Alarm& operator=(const Alarm&) = delete;
71 
73  Alarm(Alarm&& rhs) : tag_(rhs.tag_), alarm_(rhs.alarm_) {
74  rhs.alarm_ = nullptr;
75  }
76  Alarm& operator=(Alarm&& rhs) {
77  tag_ = rhs.tag_;
78  alarm_ = rhs.alarm_;
79  rhs.alarm_ = nullptr;
80  return *this;
81  }
82 
84  ~Alarm() {
85  if (alarm_ != nullptr) grpc_alarm_destroy(alarm_, nullptr);
86  }
87 
90  void Cancel() {
91  if (alarm_ != nullptr) grpc_alarm_cancel(alarm_, nullptr);
92  }
93 
94  private:
95  class AlarmEntry : public internal::CompletionQueueTag {
96  public:
97  AlarmEntry(void* tag) : tag_(tag) {}
98  void Set(void* tag) { tag_ = tag; }
99  bool FinalizeResult(void** tag, bool* status) override {
100  *tag = tag_;
101  return true;
102  }
103 
104  private:
105  void* tag_;
106  };
107 
108  AlarmEntry tag_;
109  grpc_alarm* alarm_; // owned
110 };
111 
112 } // namespace grpc
113 
114 #endif // GRPCXX_ALARM_H
void Set(CompletionQueue *cq, const T &deadline, void *tag)
Trigger an alarm instance on completion queue cq at the specified time.
Definition: alarm.h:62
A thin wrapper around grpc_alarm (see / / src/core/surface/alarm.h).
Definition: alarm.h:38
Alarm(CompletionQueue *cq, const T &deadline, void *tag)
DEPRECATED: Create and set a completion queue alarm instance associated to cq.
Definition: alarm.h:51
An interface allowing implementors to process and filter event tags.
Definition: completion_queue_tag.h:26
Alarm & operator=(const Alarm &)=delete
Classes that require gRPC to be initialized should inherit from this class.
Definition: grpc_library.h:37
If you are trying to use CompletionQueue::AsyncNext with a time class that isn't either gpr_timespec ...
Definition: time.h:40
grpc_completion_queue * cq()
Returns a raw pointer to the underlying grpc_completion_queue instance.
Definition: completion_queue.h:189
GRPCAPI void grpc_alarm_set(grpc_alarm *alarm, grpc_completion_queue *cq, gpr_timespec deadline, void *tag, void *reserved)
Set a completion queue alarm instance associated to cq.
struct grpc_alarm grpc_alarm
An alarm associated with a completion queue.
Definition: grpc_types.h:60
A thin wrapper around grpc_completion_queue (see src/core/lib/surface/completion_queue.h).
Definition: completion_queue.h:94
Alarm(Alarm &&rhs)
Alarms are movable.
Definition: alarm.h:73
Alarm()
Create an unset completion queue alarm.
Definition: alarm.h:41
GRPCAPI void grpc_alarm_destroy(grpc_alarm *alarm, void *reserved)
Destroy the given completion queue alarm, cancelling it in the process.
void Cancel()
Cancel a completion queue alarm.
Definition: alarm.h:90
GRPCAPI grpc_alarm * grpc_alarm_create(void *reserved)
Create a completion queue alarm instance.
GRPCAPI void grpc_alarm_cancel(grpc_alarm *alarm, void *reserved)
Cancel a completion queue alarm.
Alarm & operator=(Alarm &&rhs)
Definition: alarm.h:76
~Alarm()
Destroy the given completion queue alarm, cancelling it in the process.
Definition: alarm.h:84