vtkSMPThreadLocal.h.in 6.21 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
 /*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkSMPThreadLocal.h

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
// .NAME vtkSMPThreadLocal - A simple thread local implementation for sequential operations.
// .SECTION Description
// A thread local object is one that maintains a copy of an object of the
// template type for each thread that processes data. vtkSMPThreadLocal
// creates storage for all threads but the actual objects are created
// the first time Local() is called. Note that some of the vtkSMPThreadLocal
// API is not thread safe. It can be safely used in a multi-threaded
// environment because Local() returns storage specific to a particular
// thread, which by default will be accessed sequentially. It is also
// thread-safe to iterate over vtkSMPThreadLocal as long as each thread
// creates its own iterator and does not change any of the thread local
// objects.
//
// A common design pattern in using a thread local storage object is to
// write/accumulate data to local object when executing in parallel and
// then having a sequential code block that iterates over the whole storage
// using the iterators to do the final accumulation.
//
// Note that this particular implementation is designed to work in sequential
// mode and supports only 1 thread.

36 37
#ifndef vtkSMPThreadLocal_h
#define vtkSMPThreadLocal_h
38 39

#include "vtkSystemIncludes.h"
Sujin Philip's avatar
Sujin Philip committed
40

41 42 43 44 45 46 47 48 49 50
#include <vector>

template <typename T>
class vtkSMPThreadLocal
{
  typedef std::vector<T> TLS;
  typedef typename TLS::iterator TLSIter;
public:
  // Description:
  // Default constructor. Creates a default exemplar.
Sujin Philip's avatar
Sujin Philip committed
51
  vtkSMPThreadLocal() : NumInitialized(0)
52 53 54 55 56 57 58 59
    {
      this->Initialize();
    }

  // Description:
  // Constructor that allows the specification of an exemplar object
  // which is used when constructing objects when Local() is first called.
  // Note that a copy of the exemplar is created using its copy constructor.
Sujin Philip's avatar
Sujin Philip committed
60
  vtkSMPThreadLocal(const T& exemplar) : NumInitialized(0), Exemplar(exemplar)
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    {
      this->Initialize();
    }

  // Description:
  // Returns an object of type T that is local to the current thread.
  // This needs to be called mainly within a threaded execution path.
  // It will create a new object (local to the tread so each thread
  // get their own when calling Local) which is a copy of exemplar as passed
  // to the constructor (or a default object if no exemplar was provided)
  // the first time it is called. After the first time, it will return
  // the same object.
  T& Local()
    {
      int tid = this->GetThreadID();
      if (!this->Initialized[tid])
        {
        this->Internal[tid] = this->Exemplar;
        this->Initialized[tid] = true;
Sujin Philip's avatar
Sujin Philip committed
80
        ++this->NumInitialized;
81 82 83 84
        }
      return this->Internal[tid];
    }

Sujin Philip's avatar
Sujin Philip committed
85 86 87 88 89 90 91
  // Description:
  // Return the number of thread local objects that have been initialized
  size_t size() const
    {
      return this->NumInitialized;
    }

92 93
  // Description:
  // Subset of the standard iterator API.
Sujin Philip's avatar
Sujin Philip committed
94
  // The most common design pattern is to use iterators in a sequential
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
  // code block and to use only the thread local objects in parallel
  // code blocks.
  // It is thread safe to iterate over the thread local containers
  // as long as each thread uses its own iterator and does not modify
  // objects in the container.
  class iterator
  {
  public:
    iterator& operator++()
      {
        this->InitIter++;
        this->Iter++;

        // Make sure to skip uninitialized
        // entries.
        while(this->InitIter != this->EndIter)
          {
          if (*this->InitIter)
            {
            break;
            }
          this->InitIter++;
          this->Iter++;
          }
        return *this;
      }

Sujin Philip's avatar
Sujin Philip committed
122 123 124 125 126 127 128 129 130 131 132 133
    iterator operator++(int)
      {
      iterator copy = *this;
      ++(*this);
      return copy;
      }

    bool operator==(const iterator& other)
      {
        return this->Iter == other.Iter;
      }

134 135 136 137 138 139 140 141 142 143
    bool operator!=(const iterator& other)
      {
        return this->Iter != other.Iter;
      }

    T& operator*()
      {
        return *this->Iter;
      }

Sujin Philip's avatar
Sujin Philip committed
144 145 146 147 148
    T* operator->()
      {
        return &*this->Iter;
      }

149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
  private:
    friend class vtkSMPThreadLocal<T>;
    std::vector<bool>::iterator InitIter;
    std::vector<bool>::iterator EndIter;
    TLSIter Iter;
  };

  // Description:
  // Returns a new iterator pointing to the beginning of
  // the local storage container. Thread safe.
  iterator begin()
    {
      TLSIter iter = this->Internal.begin();
      std::vector<bool>::iterator iter2 =
        this->Initialized.begin();
164
      std::vector<bool>::iterator enditer =
165 166 167
        this->Initialized.end();
      // fast forward to first initialized
      // value
168
      while(iter2 != enditer)
169 170 171 172 173 174 175 176 177 178
        {
        if (*iter2)
          {
          break;
          }
        iter2++;
        iter++;
        }
      iterator retVal;
      retVal.InitIter = iter2;
179
      retVal.EndIter = enditer;
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
      retVal.Iter = iter;
      return retVal;
    };

  // Description:
  // Returns a new iterator pointing to past the end of
  // the local storage container. Thread safe.
  iterator end()
    {
      iterator retVal;
      retVal.InitIter = this->Initialized.end();
      retVal.EndIter = this->Initialized.end();
      retVal.Iter = this->Internal.end();
      return retVal;
    }

private:
  TLS Internal;
  std::vector<bool> Initialized;
Sujin Philip's avatar
Sujin Philip committed
199
  size_t NumInitialized;
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
  T Exemplar;

  void Initialize()
    {
      this->Internal.resize(this->GetNumberOfThreads());
      this->Initialized.resize(this->GetNumberOfThreads());
      std::fill(this->Initialized.begin(),
                this->Initialized.end(),
                false);
    }

  inline int GetNumberOfThreads()
    {
      return 1;
    }

  inline int GetThreadID()
    {
      return 0;
    }
};
#endif
// VTK-HeaderTest-Exclude: vtkSMPThreadLocal.h