VTK-m  2.0
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 
13 #include <vtkm/cont/vtkm_cont_export.h>
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 
35 class VTKM_CONT_EXPORT Token final
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 
101  template <typename T>
102  VTKM_CONT void Attach(T&& object,
103  vtkm::cont::Token::ReferenceCount* referenceCountPointer,
104  std::unique_lock<std::mutex>& lock,
105  std::condition_variable* conditionVariablePointer)
106  {
107  this->Attach(std::unique_ptr<ObjectReference>(
108  new ObjectReferenceImpl<typename std::decay<T>::type>(std::forward<T>(object))),
109  referenceCountPointer,
110  lock,
111  conditionVariablePointer);
112  }
113 
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  }
124 
130  VTKM_CONT bool IsAttached(vtkm::cont::Token::ReferenceCount* referenceCountPointer) const;
131 
132  class Reference
133  {
134  friend Token;
135  const void* InternalsPointer;
136  VTKM_CONT Reference(const void* internalsPointer)
137  : InternalsPointer(internalsPointer)
138  {
139  }
140 
141  public:
143  {
144  return this->InternalsPointer == rhs.InternalsPointer;
145  }
147  {
148  return this->InternalsPointer != rhs.InternalsPointer;
149  }
150  };
151 
162  VTKM_CONT Reference GetReference() const;
163 
164 private:
165  VTKM_CONT void Attach(std::unique_ptr<vtkm::cont::Token::ObjectReference>&& objectReference,
166  vtkm::cont::Token::ReferenceCount* referenceCountPointer,
167  std::unique_lock<std::mutex>& lock,
168  std::condition_variable* conditionVariablePointer);
169 
170  VTKM_CONT bool IsAttached(std::unique_lock<std::mutex>& lock,
171  vtkm::cont::Token::ReferenceCount* referenceCountPointer) const;
172 };
173 
175 {
176  return token.GetReference() == ref;
177 }
179 {
180  return token.GetReference() != ref;
181 }
182 
184 {
185  return ref == token.GetReference();
186 }
188 {
189  return ref != token.GetReference();
190 }
191 }
192 } // namespace vtkm::cont
193 
194 #endif //vtk_m_cont_Token_h
vtkm::cont::Token::GetReference
VTKM_CONT Reference GetReference() const
Returns a reference object to this Token.
vtkm::cont::Token::Reference::operator==
VTKM_CONT bool operator==(Reference rhs) const
Definition: Token.h:142
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::Reference::Reference
VTKM_CONT Reference(const void *internalsPointer)
Definition: Token.h:136
vtkm::IdComponent
vtkm::Int32 IdComponent
Represents a component ID (index of component in a vector).
Definition: Types.h:168
vtkm::cont::Token::Reference::InternalsPointer
const void * InternalsPointer
Definition: Token.h:135
vtkm::cont::Token::Attach
VTKM_CONT 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::cont::operator==
VTKM_CONT bool operator==(const vtkm::cont::Token &token, vtkm::cont::Token::Reference ref)
Definition: Token.h:174
vtkm::cont::Token::Reference::operator!=
VTKM_CONT bool operator!=(Reference rhs) const
Definition: Token.h:146
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
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:134
vtkm::cont::Token::ObjectReference
Definition: Token.h:42
vtkm::cont::Token::Reference
Definition: Token.h:132
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkm::cont::Token::Attach
VTKM_CONT 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:102
vtkm::cont::operator!=
VTKM_CONT bool operator!=(const vtkm::cont::Token &token, vtkm::cont::Token::Reference ref)
Definition: Token.h:178
vtkm::cont::Token::ObjectReferenceImpl::ObjectReferenceImpl
ObjectReferenceImpl(const ObjectType &object)
Definition: Token.h:52