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 =========================================================================*/
41 #ifndef __vtkSMPThreadLocal_h
42 #define __vtkSMPThreadLocal_h
43 
44 #include "vtkSystemIncludes.h"
45 #include <vector>
46 
47 template <typename T>
49 {
50  typedef std::vector<T> TLS;
51  typedef typename TLS::iterator TLSIter;
52 public:
54 
56  {
57  this->Initialize();
58  }
60 
62 
65  vtkSMPThreadLocal(const T& exemplar) : Exemplar(exemplar)
66  {
67  this->Initialize();
68  }
70 
72 
79  T& Local()
80  {
81  int tid = this->GetThreadID();
82  if (!this->Initialized[tid])
83  {
84  this->Internal[tid] = this->Exemplar;
85  this->Initialized[tid] = true;
86  }
87  return this->Internal[tid];
88  }
90 
92 
97  class iterator
98  {
99  public:
101  {
102  this->InitIter++;
103  this->Iter++;
105 
106  // Make sure to skip uninitialized
107  // entries.
108  while(this->InitIter != this->EndIter)
109  {
110  if (*this->InitIter)
111  {
112  break;
113  }
114  this->InitIter++;
115  this->Iter++;
116  }
117  return *this;
118  }
119 
120  bool operator!=(const iterator& other)
121  {
122  return this->Iter != other.Iter;
123  }
124 
126  {
127  return *this->Iter;
128  }
129 
130  private:
131  friend class vtkSMPThreadLocal<T>;
132  std::vector<bool>::iterator InitIter;
133  std::vector<bool>::iterator EndIter;
134  TLSIter Iter;
135  };
136 
138 
140  iterator begin()
141  {
142  TLSIter iter = this->Internal.begin();
143  std::vector<bool>::iterator iter2 =
144  this->Initialized.begin();
145  std::vector<bool>::iterator enditer =
146  this->Initialized.end();
147  // fast forward to first initialized
148  // value
149  while(iter2 != enditer)
150  {
151  if (*iter2)
152  {
153  break;
154  }
155  iter2++;
156  iter++;
157  }
158  iterator retVal;
159  retVal.InitIter = iter2;
160  retVal.EndIter = enditer;
161  retVal.Iter = iter;
162  return retVal;
163  };
165 
167 
169  iterator end()
170  {
171  iterator retVal;
172  retVal.InitIter = this->Initialized.end();
173  retVal.EndIter = this->Initialized.end();
174  retVal.Iter = this->Internal.end();
175  return retVal;
176  }
178 
179 private:
180  TLS Internal;
181  std::vector<bool> Initialized;
182  T Exemplar;
183 
184  void Initialize()
185  {
186  this->Internal.resize(this->GetNumberOfThreads());
187  this->Initialized.resize(this->GetNumberOfThreads());
188  std::fill(this->Initialized.begin(),
189  this->Initialized.end(),
190  false);
191  }
192 
193  inline int GetNumberOfThreads()
194  {
195  return 1;
196  }
197 
198  inline int GetThreadID()
199  {
200  return 0;
201  }
202 };
203 #endif
204 // VTK-HeaderTest-Exclude: vtkSMPThreadLocal.h
vtkSMPThreadLocal(const T &exemplar)
bool operator!=(const iterator &other)
A simple thread local implementation for sequential operations.