• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 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_SRC_CORE_LIB_PROMISE_CONTEXT_H
16 #define GRPC_SRC_CORE_LIB_PROMISE_CONTEXT_H
17 
18 #include <grpc/support/port_platform.h>
19 
20 #include <utility>
21 
22 #include "absl/meta/type_traits.h"
23 
24 #include <grpc/support/log.h>
25 
26 #include "src/core/lib/gprpp/down_cast.h"
27 
28 namespace grpc_core {
29 
30 // To avoid accidentally creating context types, we require an explicit
31 // specialization of this template per context type. The specialization need
32 // not contain any members, only exist.
33 // The reason for avoiding this is that context types each use a thread local.
34 template <typename T>
35 struct ContextType;
36 
37 // Some contexts can be subclassed. If the subclass is set as that context
38 // then GetContext<Base>() will return the base, and GetContext<Derived>() will
39 // DownCast to the derived type.
40 // Specializations of this type should be created for each derived type, and
41 // should have a single using statement Base pointing to the derived base class.
42 // Example:
43 //  class SomeContext {};
44 //  class SomeDerivedContext : public SomeContext {};
45 //  template <> struct ContextType<SomeContext> {};
46 //  template <> struct ContextSubclass<SomeDerivedContext> {
47 //    using Base = SomeContext;
48 //  };
49 template <typename Derived>
50 struct ContextSubclass;
51 
52 namespace promise_detail {
53 
54 template <typename T, typename = void>
55 class Context;
56 
57 template <typename T>
58 class ThreadLocalContext : public ContextType<T> {
59  public:
ThreadLocalContext(T * p)60   explicit ThreadLocalContext(T* p) : old_(current_) { current_ = p; }
~ThreadLocalContext()61   ~ThreadLocalContext() { current_ = old_; }
62   ThreadLocalContext(const ThreadLocalContext&) = delete;
63   ThreadLocalContext& operator=(const ThreadLocalContext&) = delete;
64 
get()65   static T* get() { return current_; }
66 
67  private:
68   T* const old_;
69   static thread_local T* current_;
70 };
71 
72 template <typename T>
73 thread_local T* ThreadLocalContext<T>::current_;
74 
75 template <typename T>
76 class Context<T, absl::void_t<decltype(ContextType<T>())>>
77     : public ThreadLocalContext<T> {
78   using ThreadLocalContext<T>::ThreadLocalContext;
79 };
80 
81 template <typename T>
82 class Context<T, absl::void_t<typename ContextSubclass<T>::Base>>
83     : public Context<typename ContextSubclass<T>::Base> {
84  public:
85   using Context<typename ContextSubclass<T>::Base>::Context;
get()86   static T* get() {
87     return DownCast<T*>(Context<typename ContextSubclass<T>::Base>::get());
88   }
89 };
90 
91 template <typename T, typename F>
92 class WithContext {
93  public:
WithContext(F f,T * context)94   WithContext(F f, T* context) : context_(context), f_(std::move(f)) {}
95 
operator()96   decltype(std::declval<F>()()) operator()() {
97     Context<T> ctx(context_);
98     return f_();
99   }
100 
101  private:
102   T* context_;
103   F f_;
104 };
105 
106 }  // namespace promise_detail
107 
108 // Return true if a context of type T is currently active.
109 template <typename T>
HasContext()110 bool HasContext() {
111   return promise_detail::Context<T>::get() != nullptr;
112 }
113 
114 // Retrieve the current value of a context, or abort if the value is unset.
115 template <typename T>
GetContext()116 T* GetContext() {
117   auto* p = promise_detail::Context<T>::get();
118   GPR_ASSERT(p != nullptr);
119   return p;
120 }
121 
122 // Given a promise and a context, return a promise that has that context set.
123 template <typename T, typename F>
WithContext(F f,T * context)124 promise_detail::WithContext<T, F> WithContext(F f, T* context) {
125   return promise_detail::WithContext<T, F>(std::move(f), context);
126 }
127 
128 }  // namespace grpc_core
129 
130 #endif  // GRPC_SRC_CORE_LIB_PROMISE_CONTEXT_H
131