VTK: Common/Core/SMP/STDThread/vtkSMPThreadLocalImpl.h Source File

| VTK 9.3.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
9 #include "SMP/Common/vtkSMPThreadLocalImplAbstract.h"
10 #include "SMP/STDThread/vtkSMPThreadLocalBackend.h"
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>
25 class vtkSMPThreadLocalImpl<BackendType::STDThread, T> : public vtkSMPThreadLocalImplAbstract<T>
26 {
27 typedef typename vtkSMPThreadLocalImplAbstract<T>::ItImpl ItImplAbstract;
28
29 public:
30 vtkSMPThreadLocalImpl()
31 : Backend(GetNumberOfThreadsSTDThread())
32 {
33 }
34
35 explicit vtkSMPThreadLocalImpl(const T& exemplar)
36 : Backend(GetNumberOfThreadsSTDThread())
37 , Exemplar(exemplar)
38 {
39 }
40
41 ~vtkSMPThreadLocalImpl() override
42 {
43 vtk::detail::smp::STDThread::ThreadSpecificStorageIterator it;
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 ptr = local = new T(this->Exemplar);
58 }
59 return *local;
60 }
61
62 size_t size() const override { return this->Backend.GetSize(); }
63
64 class ItImpl : public vtkSMPThreadLocalImplAbstract<T>::ItImpl
65 {
66 public:
67 void Increment() override { this->Impl.Forward(); }
68
69 bool Compare(ItImplAbstract* other) override
70 {
71 return this->Impl == static_cast<ItImpl*>(other)->Impl;
72 }
73
74 T& GetContent() override { return *reinterpret_cast<T*>(this->Impl.GetStorage()); }
75
76 T* GetContentPtr() override { return reinterpret_cast<T*>(this->Impl.GetStorage()); }
77
78 protected:
79 ItImpl* CloneImpl() const override { return new ItImpl(*this); };
80
81 private:
82 vtk::detail::smp::STDThread::ThreadSpecificStorageIterator Impl;
83
84 friend class vtkSMPThreadLocalImpl<BackendType::STDThread, T>;
85 };
86
87 std::unique_ptr<ItImplAbstract> begin() override
88 {
89 // XXX(c++14): use std::make_unique
90 auto it = std::unique_ptr<ItImpl>(new ItImpl());
91 it->Impl.SetThreadSpecificStorage(this->Backend);
92 it->Impl.SetToBegin();
93 // XXX(c++14): remove std::move and cast variable
94 std::unique_ptr<ItImplAbstract> abstractIt(std::move(it));
95 return abstractIt;
96 }
97
98 std::unique_ptr<ItImplAbstract> end() override
99 {
100 // XXX(c++14): use std::make_unique
101 auto it = std::unique_ptr<ItImpl>(new ItImpl());
102 it->Impl.SetThreadSpecificStorage(this->Backend);
103 it->Impl.SetToEnd();
104 // XXX(c++14): remove std::move and cast variable
105 std::unique_ptr<ItImplAbstract> abstractIt(std::move(it));
106 return abstractIt;
107 }
108
109 private:
110 vtk::detail::smp::STDThread::ThreadSpecific Backend;
111 T Exemplar;
112
113 // disable copying
114 vtkSMPThreadLocalImpl(const vtkSMPThreadLocalImpl&) = delete;
115 void operator=(const vtkSMPThreadLocalImpl&) = delete;
116 };
117
118 VTK_ABI_NAMESPACE_END
119 } // namespace smp
120 } // namespace detail
121 } // namespace vtk
122
123 #endif
124 /* VTK-HeaderTest-Exclude: vtkSMPThreadLocalImpl.h */
vtkSMPThreadLocalBackend.h
vtk::detail::smp::STDThread::ThreadSpecificStorageIterator
Definition: vtkSMPThreadLocalBackend.h:97
vtk::detail::smp::STDThread::ThreadSpecificStorageIterator::Forward
void Forward()
Definition: vtkSMPThreadLocalBackend.h:131
vtk::detail::smp::STDThread::ThreadSpecificStorageIterator::SetToBegin
void SetToBegin()
Definition: vtkSMPThreadLocalBackend.h:111
vtk::detail::smp::STDThread::ThreadSpecificStorageIterator::GetStorage
StoragePointerType & GetStorage() const
Definition: vtkSMPThreadLocalBackend.h:152
vtk::detail::smp::STDThread::ThreadSpecificStorageIterator::SetThreadSpecificStorage
void SetThreadSpecificStorage(ThreadSpecific &threadSpecifc)
Definition: vtkSMPThreadLocalBackend.h:106
vtk::detail::smp::STDThread::ThreadSpecificStorageIterator::GetAtEnd
bool GetAtEnd() const
Definition: vtkSMPThreadLocalBackend.h:129
vtk::detail::smp::STDThread::ThreadSpecific
Definition: vtkSMPThreadLocalBackend.h:80
vtk::detail::smp::vtkSMPThreadLocalImplAbstract::ItImpl
Definition: vtkSMPThreadLocalImplAbstract.h:30
vtk::detail::smp::vtkSMPThreadLocalImplAbstract::ItImpl::ItImpl
ItImpl()=default
vtk::detail::smp::vtkSMPThreadLocalImplAbstract::ItImpl::operator=
ItImpl & operator=(const ItImpl &)=default
vtk::detail::smp::vtkSMPThreadLocalImplAbstract
Definition: vtkSMPThreadLocalImplAbstract.h:21
vtk::detail::smp::vtkSMPThreadLocalImplAbstract::begin
virtual std::unique_ptr< ItImpl > begin()=0
vtk::detail::smp::vtkSMPThreadLocalImpl< BackendType::STDThread, T >::ItImpl::Increment
void Increment() override
Definition: vtkSMPThreadLocalImpl.h:67
vtk::detail::smp::vtkSMPThreadLocalImpl< BackendType::STDThread, T >::ItImpl::GetContentPtr
T * GetContentPtr() override
Definition: vtkSMPThreadLocalImpl.h:76
vtk::detail::smp::vtkSMPThreadLocalImpl< BackendType::STDThread, T >::ItImpl::Compare
bool Compare(ItImplAbstract *other) override
Definition: vtkSMPThreadLocalImpl.h:69
vtk::detail::smp::vtkSMPThreadLocalImpl< BackendType::STDThread, T >::ItImpl::CloneImpl
ItImpl * CloneImpl() const override
Definition: vtkSMPThreadLocalImpl.h:79
vtk::detail::smp::vtkSMPThreadLocalImpl< BackendType::STDThread, T >::ItImpl::GetContent
T & GetContent() override
Definition: vtkSMPThreadLocalImpl.h:74
vtk::detail::smp::vtkSMPThreadLocalImpl< BackendType::STDThread, T >::size
size_t size() const override
Definition: vtkSMPThreadLocalImpl.h:62
vtk::detail::smp::vtkSMPThreadLocalImpl< BackendType::STDThread, T >::end
std::unique_ptr< ItImplAbstract > end() override
Definition: vtkSMPThreadLocalImpl.h:98
vtk::detail::smp::vtkSMPThreadLocalImpl< BackendType::STDThread, T >::Local
T & Local() override
Definition: vtkSMPThreadLocalImpl.h:51
vtk::detail::smp::vtkSMPThreadLocalImpl< BackendType::STDThread, T >::vtkSMPThreadLocalImpl
vtkSMPThreadLocalImpl()
Definition: vtkSMPThreadLocalImpl.h:30
vtk::detail::smp::vtkSMPThreadLocalImpl< BackendType::STDThread, T >::vtkSMPThreadLocalImpl
vtkSMPThreadLocalImpl(const T &exemplar)
Definition: vtkSMPThreadLocalImpl.h:35
vtk::detail::smp::vtkSMPThreadLocalImpl< BackendType::STDThread, T >::~vtkSMPThreadLocalImpl
~vtkSMPThreadLocalImpl() override
Definition: vtkSMPThreadLocalImpl.h:41
vtk::detail::smp::vtkSMPThreadLocalImpl
Definition: vtkSMPThreadLocalImplAbstract.h:60
detail
Definition: vtkCellTreeLocator.h:59
vtk::detail::smp::STDThread::StoragePointerType
void * StoragePointerType
Definition: vtkSMPThreadLocalBackend.h:46
vtk::detail::smp::BackendType
BackendType
Definition: vtkSMPToolsImpl.h:28
vtk::detail::smp::BackendType::STDThread
@ STDThread
vtk
Specialization of tuple ranges and iterators for vtkAOSDataArrayTemplate.
Definition: vtkSMPThreadLocalAPI.h:30
vtkSMPThreadLocalImplAbstract.h