12
12
#include " Configuration.h"
13
13
#include " Debug.h"
14
14
#include " Interface.h"
15
+ #include " Mapping.h"
15
16
#include " Synchronization.h"
16
17
#include " Types.h"
17
18
#include " Utils.h"
@@ -221,10 +222,7 @@ void state::TeamStateTy::assertEqual(TeamStateTy &Other) const {
221
222
}
222
223
223
224
state::TeamStateTy SHARED (_OMP::state::TeamState);
224
-
225
- __attribute__ ((loader_uninitialized))
226
- state::ThreadStateTy *_OMP::state::ThreadStates[mapping::MaxThreadsPerTeam];
227
- #pragma omp allocate(_OMP::state::ThreadStates) allocator(omp_pteam_mem_alloc)
225
+ state::ThreadStateTy **SHARED (_OMP::state::ThreadStates);
228
226
229
227
namespace {
230
228
@@ -248,18 +246,32 @@ void state::init(bool IsSPMD) {
248
246
if (mapping::isInitialThreadInLevel0 (IsSPMD)) {
249
247
TeamState.init (IsSPMD);
250
248
DebugEntryRAII::init ();
249
+ ThreadStates = nullptr ;
251
250
}
252
-
253
- ThreadStates[mapping::getThreadIdInBlock ()] = nullptr ;
254
251
}
255
252
256
253
void state::enterDataEnvironment (IdentTy *Ident) {
257
254
ASSERT (config::mayUseThreadStates () &&
258
255
" Thread state modified while explicitly disabled!" );
256
+ if (!config::mayUseThreadStates ())
257
+ return ;
259
258
260
259
unsigned TId = mapping::getThreadIdInBlock ();
261
260
ThreadStateTy *NewThreadState =
262
261
static_cast <ThreadStateTy *>(__kmpc_alloc_shared (sizeof (ThreadStateTy)));
262
+ uintptr_t *ThreadStatesBitsPtr = reinterpret_cast <uintptr_t *>(&ThreadStates);
263
+ if (!atomic::load (ThreadStatesBitsPtr, atomic::seq_cst)) {
264
+ uint32_t Bytes = sizeof (ThreadStates[0 ]) * mapping::getBlockSize ();
265
+ void *ThreadStatesPtr =
266
+ memory::allocShared (Bytes, " Thread state array allocation" );
267
+ if (!atomic::cas (ThreadStatesBitsPtr, uintptr_t (0 ),
268
+ reinterpret_cast <uintptr_t >(ThreadStatesPtr),
269
+ atomic::seq_cst, atomic::seq_cst))
270
+ memory::freeShared (ThreadStatesPtr, Bytes,
271
+ " Thread state array allocated multiple times" );
272
+ ASSERT (atomic::load (ThreadStatesBitsPtr, atomic::seq_cst) &&
273
+ " Expected valid thread states bit!" );
274
+ }
263
275
NewThreadState->init (ThreadStates[TId]);
264
276
TeamState.HasThreadState = true ;
265
277
ThreadStates[TId] = NewThreadState;
@@ -274,6 +286,8 @@ void state::exitDataEnvironment() {
274
286
}
275
287
276
288
void state::resetStateForThread (uint32_t TId) {
289
+ if (!config::mayUseThreadStates ())
290
+ return ;
277
291
if (OMP_LIKELY (!TeamState.HasThreadState || !ThreadStates[TId]))
278
292
return ;
279
293
@@ -295,7 +309,6 @@ void state::assumeInitialState(bool IsSPMD) {
295
309
TeamStateTy InitialTeamState;
296
310
InitialTeamState.init (IsSPMD);
297
311
InitialTeamState.assertEqual (TeamState);
298
- ASSERT (!ThreadStates[mapping::getThreadIdInBlock ()]);
299
312
ASSERT (mapping::isSPMDMode () == IsSPMD);
300
313
}
301
314
0 commit comments