VTK  9.7.1
vtkSMPThreadLocalImpl.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
3 // .NAME vtkSMPThreadLocalImpl - A thread local storage implementation using
4 // platform specific facilities.
5 
6 #ifndef STDThreadvtkSMPThreadLocalImpl_h
7 #define STDThreadvtkSMPThreadLocalImpl_h
8 
11 #include "SMP/STDThread/vtkSMPToolsImpl.txx"
12 
13 #include <iterator>
14 #include <utility> // For std::move
15 
16 namespace vtk
17 {
18 namespace detail
19 {
20 namespace smp
21 {
22 VTK_ABI_NAMESPACE_BEGIN
23 
24 template <typename T>
26 {
28 
29 public:
31  : Backend(GetNumberOfThreadsSTDThread())
32  {
33  }
34 
35  explicit vtkSMPThreadLocalImpl(const T& exemplar)
36  : Backend(GetNumberOfThreadsSTDThread())
37  , Exemplar(exemplar)
38  {
39  }
40 
42  {
44  it.SetThreadSpecificStorage(this->Backend);
45  for (it.SetToBegin(); !it.GetAtEnd(); it.Forward())
46  {
47  delete reinterpret_cast<T*>(it.GetStorage());
48  }
49  }
50 
51  T& Local() override
52  {
53  vtk::detail::smp::STDThread::StoragePointerType& ptr = this->Backend.GetStorage();
54  T* local = reinterpret_cast<T*>(ptr);
55  if (!ptr)
56  {
57  local = new T(this->Exemplar);
58  ptr = static_cast<vtk::detail::smp::STDThread::StoragePointerType>(local);
59  }
60  return *local;
61  }
62 
63  size_t size() const override { return this->Backend.GetSize(); }
64 
66  {
67  public:
68  void Increment() override { this->Impl.Forward(); }
69 
70  bool Compare(ItImplAbstract* other) override
71  {
72  return this->Impl == static_cast<ItImpl*>(other)->Impl;
73  }
74 
75  T& GetContent() override { return *reinterpret_cast<T*>(this->Impl.GetStorage()); }
76 
77  T* GetContentPtr() override { return reinterpret_cast<T*>(this->Impl.GetStorage()); }
78 
79  protected:
80  ItImpl* CloneImpl() const override { return new ItImpl(*this); }
81 
82  private:
84 
86  };
87 
88  std::unique_ptr<ItImplAbstract> begin() override
89  {
90  auto it = std::make_unique<ItImpl>();
91  it->Impl.SetThreadSpecificStorage(this->Backend);
92  it->Impl.SetToBegin();
93  return it;
94  }
95 
96  std::unique_ptr<ItImplAbstract> end() override
97  {
98  auto it = std::make_unique<ItImpl>();
99  it->Impl.SetThreadSpecificStorage(this->Backend);
100  it->Impl.SetToEnd();
101  return it;
102  }
103 
104 private:
106  T Exemplar;
107 
108  // disable copying
110  void operator=(const vtkSMPThreadLocalImpl&) = delete;
111 };
112 
113 VTK_ABI_NAMESPACE_END
114 } // namespace smp
115 } // namespace detail
116 } // namespace vtk
117 
118 #endif
119 /* VTK-HeaderTest-Exclude: vtkSMPThreadLocalImpl.h */
virtual std::unique_ptr< ItImpl > begin()=0
Specialization of tuple ranges and iterators for vtkAOSDataArrayTemplate.