VTK
vtkSMPThreadLocal.h
Go to the documentation of this file.
1  /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkSMPThreadLocal.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
44 #ifndef vtkSMPThreadLocal_h
45 #define vtkSMPThreadLocal_h
46 
47 #include "vtkSystemIncludes.h"
48 
49 #include <vector>
50 
51 template <typename T>
53 {
54  typedef std::vector<T> TLS;
55  typedef typename TLS::iterator TLSIter;
56 public:
58 
59  vtkSMPThreadLocal() : NumInitialized(0)
60  {
61  this->Initialize();
62  }
64 
66 
69  vtkSMPThreadLocal(const T& exemplar) : NumInitialized(0), Exemplar(exemplar)
70  {
71  this->Initialize();
72  }
74 
76 
83  T& Local()
84  {
85  int tid = this->GetThreadID();
86  if (!this->Initialized[tid])
87  {
88  this->Internal[tid] = this->Exemplar;
89  this->Initialized[tid] = true;
90  ++this->NumInitialized;
91  }
92  return this->Internal[tid];
93  }
95 
97 
98  size_t size() const
99  {
100  return this->NumInitialized;
101  }
103 
105 
110  class iterator
111  {
112  public:
114  {
115  this->InitIter++;
116  this->Iter++;
118 
119  // Make sure to skip uninitialized
120  // entries.
121  while(this->InitIter != this->EndIter)
122  {
123  if (*this->InitIter)
124  {
125  break;
126  }
127  this->InitIter++;
128  this->Iter++;
129  }
130  return *this;
131  }
132 
134  {
135  iterator copy = *this;
136  ++(*this);
137  return copy;
138  }
139 
140  bool operator==(const iterator& other)
141  {
142  return this->Iter == other.Iter;
143  }
144 
145  bool operator!=(const iterator& other)
146  {
147  return this->Iter != other.Iter;
148  }
149 
151  {
152  return *this->Iter;
153  }
154 
156  {
157  return &*this->Iter;
158  }
159 
160  private:
161  friend class vtkSMPThreadLocal<T>;
162  std::vector<bool>::iterator InitIter;
163  std::vector<bool>::iterator EndIter;
164  TLSIter Iter;
165  };
166 
168 
170  iterator begin()
171  {
172  TLSIter iter = this->Internal.begin();
173  std::vector<bool>::iterator iter2 =
174  this->Initialized.begin();
175  std::vector<bool>::iterator enditer =
176  this->Initialized.end();
177  // fast forward to first initialized
178  // value
179  while(iter2 != enditer)
180  {
181  if (*iter2)
182  {
183  break;
184  }
185  iter2++;
186  iter++;
187  }
188  iterator retVal;
189  retVal.InitIter = iter2;
190  retVal.EndIter = enditer;
191  retVal.Iter = iter;
192  return retVal;
193  };
195 
197 
199  iterator end()
200  {
201  iterator retVal;
202  retVal.InitIter = this->Initialized.end();
203  retVal.EndIter = this->Initialized.end();
204  retVal.Iter = this->Internal.end();
205  return retVal;
206  }
208 
209 private:
210  TLS Internal;
211  std::vector<bool> Initialized;
212  size_t NumInitialized;
213  T Exemplar;
214 
215  void Initialize()
216  {
217  this->Internal.resize(this->GetNumberOfThreads());
218  this->Initialized.resize(this->GetNumberOfThreads());
219  std::fill(this->Initialized.begin(),
220  this->Initialized.end(),
221  false);
222  }
223 
224  inline int GetNumberOfThreads()
225  {
226  return 1;
227  }
228 
229  inline int GetThreadID()
230  {
231  return 0;
232  }
233 };
234 #endif
235 // VTK-HeaderTest-Exclude: vtkSMPThreadLocal.h
vtkSMPThreadLocal(const T &exemplar)
bool operator!=(const iterator &other)
bool operator==(const iterator &other)
A simple thread local implementation for sequential operations.
size_t size() const