|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +#include "common.h" |
| 5 | + |
| 6 | +#include "asyncsafethreadmap.h" |
| 7 | + |
| 8 | +// Async safe lock free thread map for use in signal handlers |
| 9 | + |
| 10 | +struct ThreadEntry |
| 11 | +{ |
| 12 | + size_t osThread; |
| 13 | + void* pThread; |
| 14 | +}; |
| 15 | + |
| 16 | +#define MAX_THREADS_IN_SEGMENT 256 |
| 17 | + |
| 18 | +struct ThreadSegment |
| 19 | +{ |
| 20 | + ThreadEntry entries[MAX_THREADS_IN_SEGMENT]; |
| 21 | + ThreadSegment* pNext; |
| 22 | +}; |
| 23 | + |
| 24 | +static ThreadSegment *s_pAsyncSafeThreadMapHead = NULL; |
| 25 | + |
| 26 | +bool InsertThreadIntoAsyncSafeMap(size_t osThread, void* pThread) |
| 27 | +{ |
| 28 | + size_t startIndex = osThread % MAX_THREADS_IN_SEGMENT; |
| 29 | + |
| 30 | + ThreadSegment* pSegment = s_pAsyncSafeThreadMapHead; |
| 31 | + ThreadSegment** ppSegment = &s_pAsyncSafeThreadMapHead; |
| 32 | + while (true) |
| 33 | + { |
| 34 | + if (pSegment == NULL) |
| 35 | + { |
| 36 | + // Need to add a new segment |
| 37 | + ThreadSegment* pNewSegment = new (nothrow) ThreadSegment(); |
| 38 | + if (pNewSegment == NULL) |
| 39 | + { |
| 40 | + // Memory allocation failed |
| 41 | + return false; |
| 42 | + } |
| 43 | + |
| 44 | + memset(pNewSegment, 0, sizeof(ThreadSegment)); |
| 45 | + ThreadSegment* pExpected = NULL; |
| 46 | + if (!__atomic_compare_exchange_n( |
| 47 | + ppSegment, |
| 48 | + &pExpected, |
| 49 | + pNewSegment, |
| 50 | + false /* weak */, |
| 51 | + __ATOMIC_RELEASE /* success_memorder */, |
| 52 | + __ATOMIC_RELAXED /* failure_memorder */)) |
| 53 | + { |
| 54 | + // Another thread added the segment first |
| 55 | + delete pNewSegment; |
| 56 | + pNewSegment = pExpected; |
| 57 | + } |
| 58 | + |
| 59 | + pSegment = pNewSegment; |
| 60 | + } |
| 61 | + for (size_t i = 0; i < MAX_THREADS_IN_SEGMENT; i++) |
| 62 | + { |
| 63 | + size_t index = (startIndex + i) % MAX_THREADS_IN_SEGMENT; |
| 64 | + |
| 65 | + size_t expected = 0; |
| 66 | + if (__atomic_compare_exchange_n( |
| 67 | + &pSegment->entries[index].osThread, |
| 68 | + &expected, |
| 69 | + osThread, |
| 70 | + false /* weak */, |
| 71 | + __ATOMIC_RELEASE /* success_memorder */, |
| 72 | + __ATOMIC_RELAXED /* failure_memorder */)) |
| 73 | + { |
| 74 | + // Successfully inserted |
| 75 | + // Use atomic store with release to ensure proper ordering |
| 76 | + __atomic_store_n(&pSegment->entries[index].pThread, pThread, __ATOMIC_RELEASE); |
| 77 | + return true; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + ppSegment = &pSegment->pNext; |
| 82 | + pSegment = __atomic_load_n(&pSegment->pNext, __ATOMIC_ACQUIRE); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +void RemoveThreadFromAsyncSafeMap(size_t osThread, void* pThread) |
| 87 | +{ |
| 88 | + size_t startIndex = osThread % MAX_THREADS_IN_SEGMENT; |
| 89 | + |
| 90 | + ThreadSegment* pSegment = s_pAsyncSafeThreadMapHead; |
| 91 | + while (pSegment) |
| 92 | + { |
| 93 | + for (size_t i = 0; i < MAX_THREADS_IN_SEGMENT; i++) |
| 94 | + { |
| 95 | + size_t index = (startIndex + i) % MAX_THREADS_IN_SEGMENT; |
| 96 | + if (pSegment->entries[index].pThread == pThread) |
| 97 | + { |
| 98 | + // Found the entry, remove it |
| 99 | + pSegment->entries[index].pThread = NULL; |
| 100 | + __atomic_exchange_n(&pSegment->entries[index].osThread, (size_t)0, __ATOMIC_RELEASE); |
| 101 | + return; |
| 102 | + } |
| 103 | + } |
| 104 | + pSegment = __atomic_load_n(&pSegment->pNext, __ATOMIC_ACQUIRE); |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +void *FindThreadInAsyncSafeMap(size_t osThread) |
| 109 | +{ |
| 110 | + size_t startIndex = osThread % MAX_THREADS_IN_SEGMENT; |
| 111 | + ThreadSegment* pSegment = s_pAsyncSafeThreadMapHead; |
| 112 | + while (pSegment) |
| 113 | + { |
| 114 | + for (size_t i = 0; i < MAX_THREADS_IN_SEGMENT; i++) |
| 115 | + { |
| 116 | + size_t index = (startIndex + i) % MAX_THREADS_IN_SEGMENT; |
| 117 | + // Use acquire to synchronize with release in InsertThreadIntoAsyncSafeMap |
| 118 | + if (__atomic_load_n(&pSegment->entries[index].osThread, __ATOMIC_ACQUIRE) == osThread) |
| 119 | + { |
| 120 | + return pSegment->entries[index].pThread; |
| 121 | + } |
| 122 | + } |
| 123 | + pSegment = __atomic_load_n(&pSegment->pNext, __ATOMIC_ACQUIRE); |
| 124 | + } |
| 125 | + return NULL; |
| 126 | +} |
0 commit comments