NVIDIA DeepStream SDK API Reference

9.2 Release
9.2/includes/opentelemetry/context/runtime_context.h
Go to the documentation of this file.
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3 
4 #pragma once
5 
6 #include <stddef.h>
7 #include <algorithm>
8 
9 #include "opentelemetry/common/macros.h"
10 #include "opentelemetry/context/context.h"
11 #include "opentelemetry/context/context_value.h"
12 #include "opentelemetry/nostd/shared_ptr.h"
13 #include "opentelemetry/nostd/string_view.h"
14 #include "opentelemetry/nostd/unique_ptr.h"
15 #include "opentelemetry/version.h"
16 
18 namespace context
19 {
20 // The Token object provides is returned when attaching objects to the
21 // RuntimeContext object and is associated with a context object, and
22 // can be provided to the RuntimeContext Detach method to remove the
23 // associated context from the RuntimeContext.
24 class Token
25 {
26 public:
27  bool operator==(const Context &other) const noexcept { return context_ == other; }
28 
29  ~Token() noexcept;
30 
31 private:
32  friend class RuntimeContextStorage;
33 
34  // A constructor that sets the token's Context object to the
35  // one that was passed in.
36  Token(const Context &context) : context_(context) {}
37 
38  const Context context_;
39 };
40 
48 class OPENTELEMETRY_EXPORT RuntimeContextStorage
49 {
50 public:
55  virtual Context GetCurrent() noexcept = 0;
56 
62  virtual nostd::unique_ptr<Token> Attach(const Context &context) noexcept = 0;
63 
69  virtual bool Detach(Token &token) noexcept = 0;
70 
71  virtual ~RuntimeContextStorage() {}
72 
73 protected:
75  {
77  }
78 };
79 
84 static RuntimeContextStorage *GetDefaultStorage() noexcept;
85 
86 // Provides a wrapper for propagating the context object globally.
87 //
88 // By default, a thread-local runtime context storage is used.
89 class OPENTELEMETRY_EXPORT RuntimeContext
90 {
91 public:
92  // Return the current context.
93  static Context GetCurrent() noexcept { return GetRuntimeContextStorage()->GetCurrent(); }
94 
95  // Sets the current 'Context' object. Returns a token
96  // that can be used to reset to the previous Context.
98  {
99  return GetRuntimeContextStorage()->Attach(context);
100  }
101 
102  // Resets the context to a previous value stored in the
103  // passed in token. Returns true if successful, false otherwise
104  static bool Detach(Token &token) noexcept { return GetRuntimeContextStorage()->Detach(token); }
105 
106  // Sets the Key and Value into the passed in context or if a context is not
107  // passed in, the RuntimeContext.
108  // Should be used to SetValues to the current RuntimeContext, is essentially
109  // equivalent to RuntimeContext::GetCurrent().SetValue(key,value). Keep in
110  // mind that the current RuntimeContext will not be changed, and the new
111  // context will be returned.
113  const ContextValue &value,
114  Context *context = nullptr) noexcept
115  {
116  Context temp_context;
117  if (context == nullptr)
118  {
119  temp_context = GetCurrent();
120  }
121  else
122  {
123  temp_context = *context;
124  }
125  return temp_context.SetValue(key, value);
126  }
127 
128  // Returns the value associated with the passed in key and either the
129  // passed in context* or the runtime context if a context is not passed in.
130  // Should be used to get values from the current RuntimeContext, is
131  // essentially equivalent to RuntimeContext::GetCurrent().GetValue(key).
132  static ContextValue GetValue(nostd::string_view key, Context *context = nullptr) noexcept
133  {
134  Context temp_context;
135  if (context == nullptr)
136  {
137  temp_context = GetCurrent();
138  }
139  else
140  {
141  temp_context = *context;
142  }
143  return temp_context.GetValue(key);
144  }
145 
156  const nostd::shared_ptr<RuntimeContextStorage> &storage) noexcept
157  {
158  GetStorage() = storage;
159  }
160 
169  {
170  return GetRuntimeContextStorage();
171  }
172 
173 private:
174  static nostd::shared_ptr<RuntimeContextStorage> GetRuntimeContextStorage() noexcept
175  {
176  return GetStorage();
177  }
178 
180  {
182  return context;
183  }
184 };
185 
186 inline Token::~Token() noexcept
187 {
189 }
190 
191 // The ThreadLocalContextStorage class is a derived class from
192 // RuntimeContextStorage and provides a wrapper for propagating context through
193 // cpp thread locally. This file must be included to use the RuntimeContext
194 // class if another implementation has not been registered.
195 class ThreadLocalContextStorage : public RuntimeContextStorage
196 {
197 public:
198  ThreadLocalContextStorage() noexcept = default;
199 
200  // Return the current context.
201  Context GetCurrent() noexcept override { return GetStack().Top(); }
202 
203  // Resets the context to the value previous to the passed in token. This will
204  // also detach all child contexts of the passed in token.
205  // Returns true if successful, false otherwise.
206  bool Detach(Token &token) noexcept override
207  {
208  // In most cases, the context to be detached is on the top of the stack.
209  if (token == GetStack().Top())
210  {
211  GetStack().Pop();
212  return true;
213  }
214 
215  if (!GetStack().Contains(token))
216  {
217  return false;
218  }
219 
220  while (!(token == GetStack().Top()))
221  {
222  GetStack().Pop();
223  }
224 
225  GetStack().Pop();
226 
227  return true;
228  }
229 
230  // Sets the current 'Context' object. Returns a token
231  // that can be used to reset to the previous Context.
232  nostd::unique_ptr<Token> Attach(const Context &context) noexcept override
233  {
234  GetStack().Push(context);
235  return CreateToken(context);
236  }
237 
238 private:
239  // A nested class to store the attached contexts in a stack.
240  class Stack
241  {
242  friend class ThreadLocalContextStorage;
243 
244  Stack() noexcept : size_(0), capacity_(0), base_(nullptr) {}
245 
246  // Pops the top Context off the stack.
247  void Pop() noexcept
248  {
249  if (size_ == 0)
250  {
251  return;
252  }
253  // Store empty Context before decrementing `size`, to ensure
254  // the shared_ptr object (if stored in prev context object ) are released.
255  // The stack is not resized, and the unused memory would be reutilised
256  // for subsequent context storage.
257  base_[size_ - 1] = Context();
258  size_ -= 1;
259  }
260 
261  bool Contains(const Token &token) const noexcept
262  {
263  for (size_t pos = size_; pos > 0; --pos)
264  {
265  if (token == base_[pos - 1])
266  {
267  return true;
268  }
269  }
270 
271  return false;
272  }
273 
274  // Returns the Context at the top of the stack.
275  Context Top() const noexcept
276  {
277  if (size_ == 0)
278  {
279  return Context();
280  }
281  return base_[size_ - 1];
282  }
283 
284  // Pushes the passed in context pointer to the top of the stack
285  // and resizes if necessary.
286  void Push(const Context &context) noexcept
287  {
288  size_++;
289  if (size_ > capacity_)
290  {
291  Resize(size_ * 2);
292  }
293  base_[size_ - 1] = context;
294  }
295 
296  // Reallocates the storage array to the pass in new capacity size.
297  void Resize(size_t new_capacity) noexcept
298  {
299  size_t old_size = size_ - 1;
300  if (new_capacity == 0)
301  {
302  new_capacity = 2;
303  }
304  Context *temp = new Context[new_capacity];
305  if (base_ != nullptr)
306  {
307  // vs2015 does not like this construct considering it unsafe:
308  // - std::copy(base_, base_ + old_size, temp);
309  // Ref.
310  // https://stackoverflow.com/questions/12270224/xutility2227-warning-c4996-std-copy-impl
311  for (size_t i = 0; i < (std::min)(old_size, new_capacity); i++)
312  {
313  temp[i] = base_[i];
314  }
315  delete[] base_;
316  }
317  base_ = temp;
318  capacity_ = new_capacity;
319  }
320 
321  ~Stack() noexcept { delete[] base_; }
322 
323  size_t size_;
324  size_t capacity_;
325  Context *base_;
326  };
327 
328  OPENTELEMETRY_API_SINGLETON Stack &GetStack()
329  {
330  static thread_local Stack stack_ = Stack();
331  return stack_;
332  }
333 };
334 
336 {
337  return new ThreadLocalContextStorage();
338 }
339 } // namespace context
context::RuntimeContext::GetCurrent
static Context GetCurrent() noexcept
Definition: 9.2/includes/opentelemetry/context/runtime_context.h:93
nostd::unique_ptr
std::unique_ptr< _Types... > unique_ptr
Definition: includes/opentelemetry/std/unique_ptr.h:19
OPENTELEMETRY_EXPORT
#define OPENTELEMETRY_EXPORT
Definition: includes/opentelemetry/common/macros.h:386
nostd
Definition: includes/opentelemetry/nostd/function_ref.h:14
context::RuntimeContext::GetConstRuntimeContextStorage
static nostd::shared_ptr< const RuntimeContextStorage > GetConstRuntimeContextStorage() noexcept
Provide a pointer to const runtime context storage.
Definition: 9.2/includes/opentelemetry/context/runtime_context.h:168
context::ThreadLocalContextStorage::ThreadLocalContextStorage
ThreadLocalContextStorage() noexcept=default
absl::OTABSL_OPTION_NAMESPACE_NAME::variant
Definition: includes/opentelemetry/nostd/variant.h:61
context::RuntimeContext::Detach
static bool Detach(Token &token) noexcept
Definition: 9.2/includes/opentelemetry/context/runtime_context.h:104
nostd::unique_ptr
Provide a simplified port of std::unique_ptr that has ABI stability.
Definition: includes/opentelemetry/nostd/unique_ptr.h:45
context::RuntimeContextStorage
RuntimeContextStorage is used by RuntimeContext to store Context frames.
Definition: includes/opentelemetry/context/runtime_context.h:48
nostd::shared_ptr
Provide a type-erased version of std::shared_ptr that has ABI stability.
Definition: includes/opentelemetry/nostd/shared_ptr.h:28
context::ThreadLocalContextStorage::Detach
bool Detach(Token &token) noexcept override
Definition: 9.2/includes/opentelemetry/context/runtime_context.h:206
OPENTELEMETRY_BEGIN_NAMESPACE
#define OPENTELEMETRY_BEGIN_NAMESPACE
Definition: includes/opentelemetry/version.h:21
context::Token::operator==
bool operator==(const Context &other) const noexcept
Definition: 9.2/includes/opentelemetry/context/runtime_context.h:27
context::ThreadLocalContextStorage
Definition: includes/opentelemetry/context/runtime_context.h:195
context::Token
Definition: includes/opentelemetry/context/runtime_context.h:24
context::ThreadLocalContextStorage::GetCurrent
Context GetCurrent() noexcept override
Definition: includes/opentelemetry/context/runtime_context.h:201
context::RuntimeContext::SetValue
static Context SetValue(nostd::string_view key, const ContextValue &value, Context *context=nullptr) noexcept
Definition: 9.2/includes/opentelemetry/context/runtime_context.h:112
context
Definition: includes/opentelemetry/context/context.h:16
context::RuntimeContext::Attach
static nostd::unique_ptr< Token > Attach(const Context &context) noexcept
Definition: 9.2/includes/opentelemetry/context/runtime_context.h:97
context::RuntimeContext::SetRuntimeContextStorage
static void SetRuntimeContextStorage(const nostd::shared_ptr< RuntimeContextStorage > &storage) noexcept
Provide a custom runtime context storage.
Definition: 9.2/includes/opentelemetry/context/runtime_context.h:155
context::RuntimeContextStorage::CreateToken
nostd::unique_ptr< Token > CreateToken(const Context &context) noexcept
Definition: 9.2/includes/opentelemetry/context/runtime_context.h:74
context::Context::SetValue
Context SetValue(nostd::string_view key, ContextValue value) noexcept
Definition: includes/opentelemetry/context/context.h:60
OPENTELEMETRY_END_NAMESPACE
#define OPENTELEMETRY_END_NAMESPACE
Definition: includes/opentelemetry/version.h:24
context::GetDefaultStorage
static RuntimeContextStorage * GetDefaultStorage() noexcept
Construct and return the default RuntimeContextStorage.
Definition: includes/opentelemetry/context/runtime_context.h:335
context::ThreadLocalContextStorage::Attach
nostd::unique_ptr< Token > Attach(const Context &context) noexcept override
Definition: 9.2/includes/opentelemetry/context/runtime_context.h:232
OPENTELEMETRY_API_SINGLETON
#define OPENTELEMETRY_API_SINGLETON
Definition: includes/opentelemetry/common/macros.h:335
min
int min(int a, int b)
Compute the component-wise minimum.
Definition: sources/libs/nvmultiobjecttracker/include/modules/cuDCFv2/helper_math.h:70
context::Context
Definition: includes/opentelemetry/context/context.h:23
context::RuntimeContext::GetValue
static ContextValue GetValue(nostd::string_view key, Context *context=nullptr) noexcept
Definition: 9.2/includes/opentelemetry/context/runtime_context.h:132
context::Context::GetValue
context::ContextValue GetValue(const nostd::string_view key) const noexcept
Definition: includes/opentelemetry/context/context.h:68
nostd::string_view
Back port of std::string_view to work with pre-cpp-17 compilers.
Definition: includes/opentelemetry/nostd/string_view.h:37
context::Token::~Token
~Token() noexcept
Definition: includes/opentelemetry/context/runtime_context.h:186