@@ -277,6 +277,25 @@ ContextRoot *FunctionData::getOrAllocateContextRoot() {
277
277
return Root;
278
278
}
279
279
280
+ ContextNode *tryStartContextGivenRoot (ContextRoot *Root, GUID Guid,
281
+ uint32_t Counters, uint32_t Callsites)
282
+ SANITIZER_NO_THREAD_SAFETY_ANALYSIS {
283
+ IsUnderContext = true ;
284
+ __sanitizer::atomic_fetch_add (&Root->TotalEntries , 1 ,
285
+ __sanitizer::memory_order_relaxed);
286
+ if (!Root->FirstMemBlock ) {
287
+ setupContext (Root, Guid, Counters, Callsites);
288
+ }
289
+ if (Root->Taken .TryLock ()) {
290
+ __llvm_ctx_profile_current_context_root = Root;
291
+ onContextEnter (*Root->FirstNode );
292
+ return Root->FirstNode ;
293
+ }
294
+ // If this thread couldn't take the lock, return scratch context.
295
+ __llvm_ctx_profile_current_context_root = nullptr ;
296
+ return TheScratchContext;
297
+ }
298
+
280
299
ContextNode *getUnhandledContext (FunctionData &Data, GUID Guid,
281
300
uint32_t NumCounters) {
282
301
@@ -369,24 +388,8 @@ ContextNode *__llvm_ctx_profile_get_context(FunctionData *Data, void *Callee,
369
388
ContextNode *__llvm_ctx_profile_start_context (
370
389
FunctionData *FData, GUID Guid, uint32_t Counters,
371
390
uint32_t Callsites) SANITIZER_NO_THREAD_SAFETY_ANALYSIS {
372
- IsUnderContext = true ;
373
-
374
- auto *Root = FData->getOrAllocateContextRoot ();
375
-
376
- __sanitizer::atomic_fetch_add (&Root->TotalEntries , 1 ,
377
- __sanitizer::memory_order_relaxed);
378
-
379
- if (!Root->FirstMemBlock ) {
380
- setupContext (Root, Guid, Counters, Callsites);
381
- }
382
- if (Root->Taken .TryLock ()) {
383
- __llvm_ctx_profile_current_context_root = Root;
384
- onContextEnter (*Root->FirstNode );
385
- return Root->FirstNode ;
386
- }
387
- // If this thread couldn't take the lock, return scratch context.
388
- __llvm_ctx_profile_current_context_root = nullptr ;
389
- return TheScratchContext;
391
+ return tryStartContextGivenRoot (FData->getOrAllocateContextRoot (), Guid,
392
+ Counters, Callsites);
390
393
}
391
394
392
395
void __llvm_ctx_profile_release_context (FunctionData *FData)
0 commit comments