blob: f484610a6edb436427995958e4d143125f24ce04 [file] [log] [blame]
Muxi Yan0e00c432018-01-26 15:39:32 -08001/*
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 upon
20/// expiry or cancellation.
21#ifndef GRPCPP_ALARM_H
22#define GRPCPP_ALARM_H
23
24#include <grpc/grpc.h>
25#include <grpcpp/impl/codegen/completion_queue.h>
26#include <grpcpp/impl/codegen/completion_queue_tag.h>
27#include <grpcpp/impl/codegen/grpc_library.h>
28#include <grpcpp/impl/codegen/time.h>
29#include <grpcpp/impl/grpc_library.h>
30
31namespace grpc {
32
33/// A thin wrapper around \a grpc_alarm (see / \a / src/core/surface/alarm.h).
34class Alarm : private GrpcLibraryCodegen {
35 public:
36 /// Create an unset completion queue alarm
37 Alarm();
38
39 /// Destroy the given completion queue alarm, cancelling it in the process.
40 ~Alarm();
41
42 /// DEPRECATED: Create and set a completion queue alarm instance associated to
43 /// \a cq.
44 /// This form is deprecated because it is inherently racy.
45 /// \internal We rely on the presence of \a cq for grpc initialization. If \a
46 /// cq were ever to be removed, a reference to a static
47 /// internal::GrpcLibraryInitializer instance would need to be introduced
48 /// here. \endinternal.
49 template <typename T>
50 Alarm(CompletionQueue* cq, const T& deadline, void* tag) : Alarm() {
51 SetInternal(cq, TimePoint<T>(deadline).raw_time(), tag);
52 }
53
54 /// Trigger an alarm instance on completion queue \a cq at the specified time.
55 /// Once the alarm expires (at \a deadline) or it's cancelled (see \a Cancel),
56 /// an event with tag \a tag will be added to \a cq. If the alarm expired, the
57 /// event's success bit will be true, false otherwise (ie, upon cancellation).
58 template <typename T>
59 void Set(CompletionQueue* cq, const T& deadline, void* tag) {
60 SetInternal(cq, TimePoint<T>(deadline).raw_time(), tag);
61 }
62
63 /// Alarms aren't copyable.
64 Alarm(const Alarm&) = delete;
65 Alarm& operator=(const Alarm&) = delete;
66
67 /// Alarms are movable.
68 Alarm(Alarm&& rhs) : alarm_(rhs.alarm_) { rhs.alarm_ = nullptr; }
69 Alarm& operator=(Alarm&& rhs) {
70 alarm_ = rhs.alarm_;
71 rhs.alarm_ = nullptr;
72 return *this;
73 }
74
75 /// Cancel a completion queue alarm. Calling this function over an alarm that
76 /// has already fired has no effect.
77 void Cancel();
78
79 private:
80 void SetInternal(CompletionQueue* cq, gpr_timespec deadline, void* tag);
81
82 internal::CompletionQueueTag* alarm_;
83};
84
85} // namespace grpc
86
87#endif // GRPCPP_ALARM_H