VTK-m  2.2
Token.h
Go to the documentation of this file.
1 //============================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //============================================================================
10 #ifndef vtk_m_cont_Token_h
11 #define vtk_m_cont_Token_h
12 
14 
15 #include <vtkm/Types.h>
16 
17 #include <condition_variable>
18 #include <memory>
19 #include <mutex>
20 #include <type_traits>
21 
22 namespace vtkm
23 {
24 namespace cont
25 {
26 
36 {
37  class InternalStruct;
38  mutable std::unique_ptr<InternalStruct> Internals;
39 
40  struct HeldReference;
41 
43  {
44  virtual ~ObjectReference() = default;
45  };
46 
47  template <typename ObjectType>
49  {
50  ObjectType Object;
51 
52  ObjectReferenceImpl(const ObjectType& object)
53  : Object(object)
54  {
55  }
56 
57  ObjectReferenceImpl(ObjectType&& object)
58  : Object(std::move(object))
59  {
60  }
61 
62  ObjectReferenceImpl() = delete;
64 
65  ~ObjectReferenceImpl() override = default;
66  };
67 
68 public:
69  VTKM_CONT Token();
70  VTKM_CONT Token(Token&& rhs);
71  VTKM_CONT ~Token();
72 
75 
79  VTKM_CONT void DetachFromAll();
80 
100  template <typename T>
101  VTKM_CONT void Attach(T&& object,
102  vtkm::cont::Token::ReferenceCount* referenceCountPointer,
103  std::unique_lock<std::mutex>& lock,
104  std::condition_variable* conditionVariablePointer)
105  {
106  this->Attach(std::unique_ptr<ObjectReference>(
107  new ObjectReferenceImpl<typename std::decay<T>::type>(std::forward<T>(object))),
108  referenceCountPointer,
109  lock,
110  conditionVariablePointer);
111  }
112 
114  template <typename T>
115  VTKM_CONT void Attach(T&& object,
116  vtkm::cont::Token::ReferenceCount* referenceCountPoiner,
117  std::mutex* mutexPointer,
118  std::condition_variable* conditionVariablePointer)
119  {
120  std::unique_lock<std::mutex> lock(*mutexPointer, std::defer_lock);
121  this->Attach(std::forward<T>(object), referenceCountPoiner, lock, conditionVariablePointer);
122  }
123 
129  VTKM_CONT bool IsAttached(vtkm::cont::Token::ReferenceCount* referenceCountPointer) const;
130 
131  class Reference
132  {
133  friend Token;
134  const void* InternalsPointer;
135  VTKM_CONT Reference(const void* internalsPointer)
136  : InternalsPointer(internalsPointer)
137  {
138  }
139 
140  public:
142  {
143  return this->InternalsPointer == rhs.InternalsPointer;
144  }
146  {
147  return this->InternalsPointer != rhs.InternalsPointer;
148  }
149  };
150 
161  VTKM_CONT Reference GetReference() const;
162 
163 private:
164  VTKM_CONT void Attach(std::unique_ptr<vtkm::cont::Token::ObjectReference>&& objectReference,
165  vtkm::cont::Token::ReferenceCount* referenceCountPointer,
166  std::unique_lock<std::mutex>& lock,
167  std::condition_variable* conditionVariablePointer);
168 
169  VTKM_CONT bool IsAttached(std::unique_lock<std::mutex>& lock,
170  vtkm::cont::Token::ReferenceCount* referenceCountPointer) const;
171 };
172 
174 {
175  return token.GetReference() == ref;
176 }
178 {
179  return token.GetReference() != ref;
180 }
181 
183 {
184  return ref == token.GetReference();
185 }
187 {
188  return ref != token.GetReference();
189 }
190 }
191 } // namespace vtkm::cont
192 
193 #endif //vtk_m_cont_Token_h
vtkm::cont::Token::ObjectReferenceImpl
Definition: Token.h:48
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
Types.h
vtkm::cont::Token::Attach
void Attach(T &&object, vtkm::cont::Token::ReferenceCount *referenceCountPoiner, std::mutex *mutexPointer, std::condition_variable *conditionVariablePointer)
Add an object to attach to the Token.
Definition: Token.h:115
vtkm::IdComponent
vtkm::Int32 IdComponent
Base type to use to index small lists.
Definition: Types.h:194
vtkm::cont::Token::Reference::InternalsPointer
const void * InternalsPointer
Definition: Token.h:134
vtkm::cont::Token::Reference::Reference
Reference(const void *internalsPointer)
Definition: Token.h:135
vtkm::cont::operator!=
bool operator!=(const vtkm::cont::Token &token, vtkm::cont::Token::Reference ref)
Definition: Token.h:177
vtkm::cont::Token::Reference::operator==
bool operator==(Reference rhs) const
Definition: Token.h:141
vtkm::cont::Token::Attach
void Attach(T &&object, vtkm::cont::Token::ReferenceCount *referenceCountPointer, std::unique_lock< std::mutex > &lock, std::condition_variable *conditionVariablePointer)
Add an object to attach to the Token.
Definition: Token.h:101
vtkm::cont::Token::ObjectReferenceImpl::ObjectReferenceImpl
ObjectReferenceImpl(ObjectType &&object)
Definition: Token.h:57
vtkm::cont::Token::Internals
std::unique_ptr< InternalStruct > Internals
Definition: Token.h:37
vtkm::cont::Token::ReferenceCount
vtkm::IdComponent ReferenceCount
Use this type to represent counts of how many tokens are holding a resource.
Definition: Token.h:74
vtkm::cont::Token::Reference::operator!=
bool operator!=(Reference rhs) const
Definition: Token.h:145
vtkm::cont::Token
A token to hold the scope of an ArrayHandle or other object.
Definition: Token.h:35
vtkm::cont::Token::ObjectReferenceImpl::Object
ObjectType Object
Definition: Token.h:50
vtkm::cont::Token::Reference::Token
friend Token
Definition: Token.h:133
vtkm::cont::Token::ObjectReference
Definition: Token.h:42
VTKM_CONT_EXPORT
#define VTKM_CONT_EXPORT
Definition: vtkm_cont_export.h:44
vtkm::cont::Token::Reference
Definition: Token.h:131
vtkm_cont_export.h
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkm::cont::Token::ObjectReferenceImpl::ObjectReferenceImpl
ObjectReferenceImpl(const ObjectType &object)
Definition: Token.h:52
vtkm::cont::operator==
bool operator==(const vtkm::cont::Token &token, vtkm::cont::Token::Reference ref)
Definition: Token.h:173
vtkm::cont::Token::GetReference
Reference GetReference() const
Returns a reference object to this Token.