-
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 2 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
Some comments aren't visible on the classic Files Changed page.
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
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 |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ set(SOURCES | |
| random.c | ||
| debugger.c | ||
| strings.c | ||
| thread.c | ||
| time.c | ||
| unicodedata.c | ||
| utf8.c | ||
|
|
||
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,130 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| #define _GNU_SOURCE | ||
|
|
||
| #include <stdbool.h> | ||
| #include "utils.h" | ||
| #include "thread.h" | ||
|
|
||
| #ifdef TARGET_UNIX | ||
janvorli marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // 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 | ||
| { | ||
| struct ThreadEntry entries[MAX_THREADS_IN_SEGMENT]; | ||
| struct ThreadSegment* pNext; | ||
| }; | ||
|
|
||
| static struct ThreadSegment *s_pAsyncSafeThreadMapHead = NULL; | ||
janvorli marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| bool minipal_insert_thread_into_async_safe_map(size_t osThread, void* pThread) | ||
| { | ||
| size_t startIndex = osThread % MAX_THREADS_IN_SEGMENT; | ||
|
|
||
| struct ThreadSegment* pSegment = s_pAsyncSafeThreadMapHead; | ||
| struct ThreadSegment** ppSegment = &s_pAsyncSafeThreadMapHead; | ||
| while (true) | ||
| { | ||
| if (pSegment == NULL) | ||
| { | ||
| // Need to add a new segment | ||
| struct ThreadSegment* pNewSegment = (struct ThreadSegment*)malloc(sizeof(struct ThreadSegment)); | ||
| if (pNewSegment == NULL) | ||
| { | ||
| // Memory allocation failed | ||
| return false; | ||
| } | ||
|
|
||
| memset(pNewSegment, 0, sizeof(struct ThreadSegment)); | ||
| struct 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 | ||
| free(pNewSegment); | ||
| pNewSegment = *ppSegment; | ||
| } | ||
|
|
||
| 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 | ||
| pSegment->entries[index].pThread = pThread; | ||
janvorli marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return true; | ||
| } | ||
| } | ||
|
|
||
| ppSegment = &pSegment->pNext; | ||
| pSegment = pSegment->pNext; | ||
| } | ||
| } | ||
|
|
||
| void minipal_remove_thread_from_async_safe_map(size_t osThread, void* pThread) | ||
| { | ||
| size_t startIndex = osThread % MAX_THREADS_IN_SEGMENT; | ||
|
|
||
| struct 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 = pSegment->pNext; | ||
| } | ||
| } | ||
|
|
||
| void *minipal_find_thread_in_async_safe_map(size_t osThread) | ||
| { | ||
| size_t startIndex = osThread % MAX_THREADS_IN_SEGMENT; | ||
| struct 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 insert_thread_to_async_safe_map | ||
| if (__atomic_load_n(&pSegment->entries[index].osThread, __ATOMIC_ACQUIRE) == osThread) | ||
| { | ||
| return pSegment->entries[index].pThread; | ||
| } | ||
| } | ||
| pSegment = pSegment->pNext; | ||
janvorli marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| return NULL; | ||
| } | ||
|
|
||
| #endif // TARGET_UNIX | ||
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.