-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Fix lookup for current Thread in async signal handler #122048
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
janvorli
merged 11 commits into
dotnet:main
from
janvorli:fix-async-thread-by-threadid-lookup
Dec 8, 2025
+328
−55
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a504300
Fix lookup for current Thread in async signal handler
janvorli 712bdb6
Add support for NativeAOT
janvorli d6bdf80
Move the implementation to src/coreclr/runtime
janvorli 6cecdd0
Fix build break
janvorli a3408f2
Reflect PR feedback
janvorli c8e660c
More PR feedback
janvorli 4feff02
Fix another build break
janvorli 8ae5f94
Reflect PR feedback and fix windows build break
janvorli baf5756
One more build break
janvorli 058a2e0
Forward activation signal to previous handler
janvorli 3d02525
Apply suggestions from code review
janvorli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| #include "common.h" | ||
|
|
||
| #include "asyncsafethreadmap.h" | ||
|
|
||
| // Async safe lock free thread map for use in signal handlers | ||
|
|
||
| struct ThreadEntry | ||
| { | ||
| size_t osThread; | ||
| void* pThread; | ||
| }; | ||
|
|
||
| #define MAX_THREADS_IN_SEGMENT 256 | ||
|
|
||
| struct ThreadSegment | ||
| { | ||
| ThreadEntry entries[MAX_THREADS_IN_SEGMENT]; | ||
| ThreadSegment* pNext; | ||
| }; | ||
|
|
||
| static ThreadSegment *s_pAsyncSafeThreadMapHead = NULL; | ||
|
|
||
| bool InsertThreadIntoAsyncSafeMap(size_t osThread, void* pThread) | ||
| { | ||
| size_t startIndex = osThread % MAX_THREADS_IN_SEGMENT; | ||
|
|
||
| ThreadSegment* pSegment = s_pAsyncSafeThreadMapHead; | ||
| ThreadSegment** ppSegment = &s_pAsyncSafeThreadMapHead; | ||
| while (true) | ||
| { | ||
| if (pSegment == NULL) | ||
| { | ||
| // Need to add a new segment | ||
| ThreadSegment* pNewSegment = new (nothrow) ThreadSegment(); | ||
| if (pNewSegment == NULL) | ||
| { | ||
| // Memory allocation failed | ||
| return false; | ||
| } | ||
|
|
||
| memset(pNewSegment, 0, sizeof(ThreadSegment)); | ||
| ThreadSegment* pExpected = NULL; | ||
| if (!__atomic_compare_exchange_n( | ||
| ppSegment, | ||
| &pExpected, | ||
| pNewSegment, | ||
| false /* weak */, | ||
| __ATOMIC_RELEASE /* success_memorder */, | ||
| __ATOMIC_RELAXED /* failure_memorder */)) | ||
| { | ||
| // Another thread added the segment first | ||
| delete pNewSegment; | ||
| pNewSegment = pExpected; | ||
| } | ||
|
|
||
| pSegment = pNewSegment; | ||
| } | ||
| for (size_t i = 0; i < MAX_THREADS_IN_SEGMENT; i++) | ||
| { | ||
| size_t index = (startIndex + i) % MAX_THREADS_IN_SEGMENT; | ||
|
|
||
| size_t expected = 0; | ||
| if (__atomic_compare_exchange_n( | ||
| &pSegment->entries[index].osThread, | ||
| &expected, | ||
| osThread, | ||
| false /* weak */, | ||
| __ATOMIC_RELEASE /* success_memorder */, | ||
| __ATOMIC_RELAXED /* failure_memorder */)) | ||
| { | ||
| // Successfully inserted | ||
| // Use atomic store with release to ensure proper ordering | ||
| __atomic_store_n(&pSegment->entries[index].pThread, pThread, __ATOMIC_RELEASE); | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| ppSegment = &pSegment->pNext; | ||
| pSegment = __atomic_load_n(&pSegment->pNext, __ATOMIC_ACQUIRE); | ||
| } | ||
| } | ||
|
|
||
| void RemoveThreadFromAsyncSafeMap(size_t osThread, void* pThread) | ||
| { | ||
| size_t startIndex = osThread % MAX_THREADS_IN_SEGMENT; | ||
|
|
||
| ThreadSegment* pSegment = s_pAsyncSafeThreadMapHead; | ||
| while (pSegment) | ||
| { | ||
| for (size_t i = 0; i < MAX_THREADS_IN_SEGMENT; i++) | ||
| { | ||
| size_t index = (startIndex + i) % MAX_THREADS_IN_SEGMENT; | ||
| if (pSegment->entries[index].pThread == pThread) | ||
| { | ||
| // Found the entry, remove it | ||
| pSegment->entries[index].pThread = NULL; | ||
| __atomic_exchange_n(&pSegment->entries[index].osThread, (size_t)0, __ATOMIC_RELEASE); | ||
| return; | ||
| } | ||
| } | ||
| pSegment = __atomic_load_n(&pSegment->pNext, __ATOMIC_ACQUIRE); | ||
| } | ||
| } | ||
|
|
||
| void *FindThreadInAsyncSafeMap(size_t osThread) | ||
| { | ||
| size_t startIndex = osThread % MAX_THREADS_IN_SEGMENT; | ||
| ThreadSegment* pSegment = s_pAsyncSafeThreadMapHead; | ||
| while (pSegment) | ||
| { | ||
| for (size_t i = 0; i < MAX_THREADS_IN_SEGMENT; i++) | ||
| { | ||
| size_t index = (startIndex + i) % MAX_THREADS_IN_SEGMENT; | ||
| // Use acquire to synchronize with release in InsertThreadIntoAsyncSafeMap | ||
| if (__atomic_load_n(&pSegment->entries[index].osThread, __ATOMIC_ACQUIRE) == osThread) | ||
| { | ||
| return pSegment->entries[index].pThread; | ||
janvorli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| pSegment = __atomic_load_n(&pSegment->pNext, __ATOMIC_ACQUIRE); | ||
| } | ||
| return NULL; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| #ifndef __ASYNCSAFETHREADMAP_H__ | ||
| #define __ASYNCSAFETHREADMAP_H__ | ||
|
|
||
| #if defined(TARGET_UNIX) && !defined(TARGET_WASM) | ||
|
|
||
| // Insert a thread into the async-safe map. | ||
| // * osThread - The OS thread ID to insert. | ||
| // * pThread - A pointer to the thread object to associate with the OS thread ID. | ||
| // * return true if the insertion was successful, false otherwise (OOM). | ||
| bool InsertThreadIntoAsyncSafeMap(size_t osThread, void* pThread); | ||
|
|
||
| // Remove a thread from the async-safe map. | ||
| // * osThread - The OS thread ID to remove. | ||
| // * pThread - A pointer to the thread object associated with the OS thread ID. | ||
| void RemoveThreadFromAsyncSafeMap(size_t osThread, void* pThread); | ||
|
|
||
| // Find a thread in the async-safe map. | ||
| // * osThread = The OS thread ID to search for. | ||
| // * return - A pointer to the thread object associated with the OS thread ID, or NULL if not found. | ||
| void* FindThreadInAsyncSafeMap(size_t osThread); | ||
|
|
||
| #endif // TARGET_UNIX && !TARGET_WASM | ||
|
|
||
| #endif // __ASYNCSAFETHREADMAP_H__ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.