Debian Codegraph @

src/core/lib/gprpp/single_set_ptr.h

raw
1 // Copyright 2022 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #ifndef GRPC_CORE_LIB_GPRPP_SINGLE_SET_PTR_H
16 #define GRPC_CORE_LIB_GPRPP_SINGLE_SET_PTR_H
17
18 #include <grpc/support/port_platform.h>
19
20 #include <atomic>
21 #include <memory>
22
23 #include <grpc/support/log.h>
24
25 namespace grpc_core {
26
27 template <class T, class Deleter = std::default_delete<T>>
28 class SingleSetPtr {
29 public:
30 SingleSetPtr() = default;
31 explicit SingleSetPtr(T* p) : p_{p} {}
32 explicit SingleSetPtr(std::unique_ptr<T, Deleter> p) : p_{p.release()} {}
33 ~SingleSetPtr() { Delete(p_.load(std::memory_order_relaxed)); }
34
35 SingleSetPtr(const SingleSetPtr&) = delete;
36 SingleSetPtr& operator=(const SingleSetPtr&) = delete;
37 SingleSetPtr(SingleSetPtr&& other) noexcept
38 : p_(other.p_.exchange(nullptr)) {}
39 SingleSetPtr& operator=(SingleSetPtr&& other) noexcept {
40 Set(other.p_.exchange(nullptr, std::memory_order_acq_rel));
41 return *this;
42 }
43
44 // Set the pointer;
45 // if already set, return the pre-set value and delete ptr;
46 // if deleted, return nullptr and delete ptr.
47 T* Set(T* ptr) {
48 T* expected = nullptr;
49 if (!p_.compare_exchange_strong(expected, ptr, std::memory_order_acq_rel,
50 std::memory_order_acquire)) {
51 Delete(ptr);
52 return expected;
53 }
54 return ptr;
55 }
56
57 // Set the pointer from a compatible unique_ptr - with the same caveats as
58 // above.
59 T* Set(std::unique_ptr<T, Deleter> ptr) { return Set(ptr.release()); }
60
61 // Clear the pointer.
62 void Reset() { Delete(p_.exchange(nullptr, std::memory_order_acq_rel)); }
63
64 bool is_set() const {
65 T* p = p_.load(std::memory_order_acquire);
66 return p != nullptr;
67 }
68
69 T* operator->() const {
70 T* p = p_.load(std::memory_order_acquire);
71 GPR_DEBUG_ASSERT(p != nullptr);
72 return p;
73 }
74
75 T& operator*() const { return *operator->(); }
76
77 private:
78 static void Delete(T* p) {
79 if (p == nullptr) return;
80 Deleter()(p);
81 }
82 std::atomic<T*> p_{nullptr};
83 };
84
85 } // namespace grpc_core
86
87 #endif // GRPC_CORE_LIB_GPRPP_SINGLE_SET_PTR_H