File: vtkSMPThreadLocalImpl.h.in

package info (click to toggle)
vtk7 7.1.1%2Bdfsg2-8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 127,396 kB
  • sloc: cpp: 1,539,584; ansic: 124,382; python: 78,038; tcl: 47,013; xml: 8,142; yacc: 5,040; java: 4,439; perl: 3,132; lex: 1,926; sh: 1,500; makefile: 126; objc: 83
file content (188 lines) | stat: -rw-r--r-- 4,620 bytes parent folder | download | duplicates (3)
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkSMPThreadLocalImpl.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.

=========================================================================*/

// Thread Specific Storage is implemented as a Hash Table, with the Thread Id
// as the key and a Pointer to the data as the value. The Hash Table implements
// Open Addressing with Linear Probing. A fixed-size array (HashTableArray) is
// used as the hash table. The size of this array is allocated to be large
// enough to store thread specific data for all the threads with a Load Factor
// of 0.5. In case the number of threads changes dynamically and the current
// array is not able to accommodate more entries, a new array is allocated that
// is twice the size of the current array. To avoid rehashing and blocking the
// threads, a rehash is not performed immediately. Instead, a linked list of
// hash table arrays is maintained with the current array at the root and older
// arrays along the list. All lookups are sequentially performed along the
// linked list. If the root array does not have an entry, it is created for
// faster lookup next time. The ThreadSpecific::GetStorage() function is thread
// safe and only blocks when a new array needs to be allocated, which should be
// rare.

#ifndef vtkSMPThreadLocalImpl_h
#define vtkSMPThreadLocalImpl_h

#include "vtkCommonCoreModule.h" // For export macro
#include "vtkAtomic.h"
#include "vtkConfigure.h"
#include "vtkSystemIncludes.h"

#include <omp.h>


namespace detail
{

typedef void* ThreadIdType;
typedef vtkTypeUInt32 HashType;
typedef void* StoragePointerType;


struct Slot
{
  vtkAtomic<ThreadIdType> ThreadId;
  omp_lock_t ModifyLock;
  StoragePointerType Storage;

  Slot();
  ~Slot();

private:
  // not copyable
  Slot(const Slot&);
  void operator=(const Slot&);
};


struct HashTableArray
{
  size_t Size, SizeLg;
  vtkAtomic<size_t> NumberOfEntries;
  Slot *Slots;
  HashTableArray *Prev;

  explicit HashTableArray(size_t sizeLg);
  ~HashTableArray();

private:
  // disallow copying
  HashTableArray(const HashTableArray&);
  void operator=(const HashTableArray&);
};


class VTKCOMMONCORE_EXPORT ThreadSpecific
{
public:
  explicit ThreadSpecific(unsigned numThreads);
  ~ThreadSpecific();

  StoragePointerType& GetStorage();
  size_t Size() const;

private:
  vtkAtomic<HashTableArray*> Root;
  vtkAtomic<size_t> Count;

  friend class ThreadSpecificStorageIterator;
};

inline size_t ThreadSpecific::Size() const
{
  return this->Count;
}


class ThreadSpecificStorageIterator
{
public:
  ThreadSpecificStorageIterator()
    : ThreadSpecificStorage(NULL), CurrentArray(NULL), CurrentSlot(0)
  {
  }

  void SetThreadSpecificStorage(ThreadSpecific &threadSpecifc)
  {
    this->ThreadSpecificStorage = &threadSpecifc;
  }

  void SetToBegin()
  {
    this->CurrentArray = this->ThreadSpecificStorage->Root;
    this->CurrentSlot = 0;
    if (!this->CurrentArray->Slots->Storage)
    {
      this->Forward();
    }
  }

  void SetToEnd()
  {
    this->CurrentArray = NULL;
    this->CurrentSlot = 0;
  }

  bool GetInitialized() const
  {
    return this->ThreadSpecificStorage != NULL;
  }

  bool GetAtEnd() const
  {
    return this->CurrentArray == NULL;
  }

  void Forward()
  {
    for (;;)
    {
      if (++this->CurrentSlot >= this->CurrentArray->Size)
      {
        this->CurrentArray = this->CurrentArray->Prev;
        this->CurrentSlot = 0;
        if (!this->CurrentArray)
        {
          break;
        }
      }
      Slot *slot = this->CurrentArray->Slots + this->CurrentSlot;
      if (slot->Storage)
      {
        break;
      }
    }
  }

  StoragePointerType& GetStorage() const
  {
    Slot *slot = this->CurrentArray->Slots + this->CurrentSlot;
    return slot->Storage;
  }

  bool operator==(const ThreadSpecificStorageIterator &it) const
  {
    return (this->ThreadSpecificStorage == it.ThreadSpecificStorage) &&
           (this->CurrentArray == it.CurrentArray) &&
           (this->CurrentSlot == it.CurrentSlot);
  }

private:
  ThreadSpecific *ThreadSpecificStorage;
  HashTableArray *CurrentArray;
  size_t CurrentSlot;
};

} // detail;

#endif
// VTK-HeaderTest-Exclude: vtkSMPThreadLocalImpl.h