-
Notifications
You must be signed in to change notification settings - Fork 107
/
thread_local_storage_internal.cc
316 lines (252 loc) · 8.77 KB
/
thread_local_storage_internal.cc
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// Copyright 2015 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "starboard/shared/starboard/thread_local_storage_internal.h"
#include <functional>
#include <limits>
#include <map>
#include <memory>
#include <utility>
#include <vector>
#include "starboard/common/flat_map.h"
#include "starboard/common/log.h"
#include "starboard/memory.h"
#include "starboard/once.h"
struct SbThreadLocalKeyPrivate {
int index;
};
namespace starboard {
namespace shared {
namespace {
TLSKeyManager* s_instance = NULL;
SbOnceControl s_instance_control = SB_ONCE_INITIALIZER;
void InitializeTLS() {
s_instance = new TLSKeyManager();
}
template <typename T>
class RawAllocator : public std::allocator<T> {
public:
typedef typename std::allocator<T>::pointer pointer;
typedef typename std::allocator<T>::const_pointer const_pointer;
typedef typename std::allocator<T>::reference reference;
typedef typename std::allocator<T>::const_reference const_reference;
typedef typename std::allocator<T>::size_type size_type;
typedef typename std::allocator<T>::value_type value_type;
typedef typename std::allocator<T>::difference_type difference_type;
RawAllocator() {}
// Constructor used for rebinding
template <typename U>
RawAllocator(const RawAllocator<U>& x) {}
pointer allocate(size_type n,
std::allocator<void>::const_pointer hint = NULL) {
void* ptr = malloc(n * sizeof(value_type));
return static_cast<pointer>(ptr);
}
void deallocate(pointer p, size_type n) { free(p); }
template <typename U>
struct rebind {
typedef RawAllocator<U> other;
};
};
// A map of ThreadId -> int. This map is highly concurrent and allows
// access of elements without much contention.
class ConcurrentThreadIdMap {
public:
ConcurrentThreadIdMap() {
// Prime number reduces collisions.
static const size_t kNumBuckets = 101;
map_vector_.resize(kNumBuckets);
for (size_t i = 0; i < map_vector_.size(); ++i) {
void* memory_block = malloc(sizeof(LockedMap));
map_vector_[i] = new (memory_block) LockedMap;
}
}
~ConcurrentThreadIdMap() {
for (size_t i = 0; i < map_vector_.size(); ++i) {
LockedMap* obj = map_vector_[i];
obj->~LockedMap();
free(obj);
}
}
bool GetIfExists(SbThreadId key, int* value) const {
const LockedMap& map = GetBucket(key);
ScopedLock lock(map.mutex_);
ThreadIdMap::const_iterator it = map.map_.find(key);
if (it != map.map_.end()) {
*value = it->second;
return true;
} else {
return false;
}
}
void Insert(SbThreadId key, int value) {
LockedMap& map = GetBucket(key);
ScopedLock lock(map.mutex_);
map.map_[key] = value;
}
void Erase(SbThreadId key) {
LockedMap& map = GetBucket(key);
ScopedLock lock(map.mutex_);
map.map_.erase(key);
}
private:
typedef std::map<SbThreadId,
int,
std::less<SbThreadId>,
RawAllocator<std::pair<const SbThreadId, int> > >
ThreadIdMap;
struct LockedMap {
Mutex mutex_;
ThreadIdMap map_;
};
// Simple hashing function for 32 bit numbers.
// Based off of Jenkins hash found at this url:
// https://gist.github.com/badboy/6267743#file-inthash-md
static size_t Hash(SbThreadId id) {
static const uint32_t kMagicNum1 = 0x7ed55d16;
static const uint32_t kMagicNum2 = 0xc761c23c;
static const uint32_t kMagicNum3 = 0x165667b1;
static const uint32_t kMagicNum4 = 0xd3a2646c;
static const uint32_t kMagicNum5 = 0xfd7046c5;
static const uint32_t kMagicNum6 = 0xfd7046c5;
static const uint32_t kMagicShift1 = 12;
static const uint32_t kMagicShift2 = 19;
static const uint32_t kMagicShift3 = 5;
static const uint32_t kMagicShift4 = 9;
static const uint32_t kMagicShift5 = 3;
static const uint32_t kMagicShift6 = 16;
uint32_t key = static_cast<uint32_t>(id);
key = (key + kMagicNum1) + (key << kMagicShift1);
key = (key ^ kMagicNum2) ^ (key >> kMagicShift2);
key = (key + kMagicNum3) + (key << kMagicShift3);
key = (key + kMagicNum4) ^ (key << kMagicShift4);
key = (key + kMagicNum5) + (key << kMagicShift5);
key = (key ^ kMagicNum6) ^ (key >> kMagicShift6);
return static_cast<size_t>(key);
}
const LockedMap& GetBucket(SbThreadId key) const {
size_t bucket_index = Hash(key) % map_vector_.size();
return *map_vector_[bucket_index];
}
LockedMap& GetBucket(SbThreadId key) {
size_t bucket_index = Hash(key) % map_vector_.size();
return *map_vector_[bucket_index];
}
typedef std::vector<LockedMap*, RawAllocator<LockedMap*> > MapVector;
MapVector map_vector_;
};
} // namespace
struct TLSKeyManager::InternalData {
// These data structures bypass memory reporting. If this wasn't here then
// any platform using this TLSKeyManager will crash during memory reporting.
typedef std::vector<KeyRecord, RawAllocator<KeyRecord> > VectorKeyRecord;
typedef std::vector<int, RawAllocator<int> > VectorInt;
typedef ConcurrentThreadIdMap ThreadIdMap;
// Overrides new/delete for InternalData to bypass memory reporting.
static void* operator new(size_t n) { return malloc(n); }
static void operator delete(void* p) { free(p); }
// The key record tracks all key values among all threads, along with their
// destructors, if specified.
VectorKeyRecord key_table_;
// Tracks all thread IDs that are still available.
VectorInt available_thread_ids_;
// This maps Starboard thread IDs to TLS thread ids.
ThreadIdMap thread_id_map_;
};
TLSKeyManager* TLSKeyManager::Get() {
SbOnce(&s_instance_control, &InitializeTLS);
return s_instance;
}
TLSKeyManager::TLSKeyManager() {
data_.reset(new InternalData);
data_->available_thread_ids_.reserve(kMaxThreads);
for (int i = 0; i < kMaxThreads; ++i) {
data_->available_thread_ids_.push_back(i);
}
}
SbThreadLocalKey TLSKeyManager::CreateKey(SbThreadLocalDestructor destructor) {
// Allocate key and bypass the the normal allocator. Otherwise there
// could be a re-entrant loop that kills the process.
void* memory_block = malloc(sizeof(SbThreadLocalKeyPrivate));
SbThreadLocalKey key = new (memory_block) SbThreadLocalKeyPrivate();
ScopedLock lock(mutex_);
key->index = GetUnusedKeyIndex();
KeyRecord* record = &data_->key_table_[key->index];
record->destructor = destructor;
memset(record->values.data(), 0,
record->values.size() * sizeof(record->values[0]));
record->valid = true;
return key;
}
void TLSKeyManager::DestroyKey(SbThreadLocalKey key) {
if (!SbThreadIsValidLocalKey(key)) {
return;
}
ScopedLock lock(mutex_);
SB_DCHECK(IsKeyActive(key));
data_->key_table_[key->index].valid = false;
key.~SbThreadLocalKey();
free(key);
}
bool TLSKeyManager::SetLocalValue(SbThreadLocalKey key, void* value) {
if (!SbThreadIsValidLocalKey(key)) {
return false;
}
int current_thread_id = GetCurrentThreadId();
ScopedLock lock(mutex_);
if (!IsKeyActive(key)) {
return false;
}
data_->key_table_[key->index].values[current_thread_id] = value;
return true;
}
void* TLSKeyManager::GetLocalValue(SbThreadLocalKey key) {
if (!SbThreadIsValidLocalKey(key)) {
return NULL;
}
int current_thread_id = GetCurrentThreadId();
ScopedLock lock(mutex_);
if (!IsKeyActive(key)) {
return NULL;
}
return data_->key_table_[key->index].values[current_thread_id];
}
bool TLSKeyManager::IsKeyActive(SbThreadLocalKey key) {
return data_->key_table_[key->index].valid;
}
int TLSKeyManager::GetUnusedKeyIndex() {
const size_t key_table_size = data_->key_table_.size();
for (int i = 0; i < key_table_size; ++i) {
if (!data_->key_table_[i].valid) {
return i;
}
}
data_->key_table_.push_back(KeyRecord());
data_->key_table_.back().valid = false;
return data_->key_table_.size() - 1;
}
int TLSKeyManager::GetCurrentThreadId() {
const SbThreadId thread_id = SbThreadGetId();
int value = -1;
if (data_->thread_id_map_.GetIfExists(thread_id, &value)) {
return value;
}
ScopedLock lock(mutex_);
SB_DCHECK(!data_->available_thread_ids_.empty());
int thread_tls_id = data_->available_thread_ids_.back();
data_->available_thread_ids_.pop_back();
data_->thread_id_map_.Insert(thread_id, thread_tls_id);
return thread_tls_id;
}
} // namespace shared
} // namespace starboard