diff --git a/src/coreclr/vm/threadstatics.cpp b/src/coreclr/vm/threadstatics.cpp index ab7a585215da6e..b5032edffd17c0 100644 --- a/src/coreclr/vm/threadstatics.cpp +++ b/src/coreclr/vm/threadstatics.cpp @@ -750,8 +750,8 @@ void GetTLSIndexForThreadStatic(MethodTable* pMT, bool gcStatic, TLSIndex* pInde { g_directThreadLocalTLSBytesAvailable = newBytesAvailable - alignmentAdjust; newTLSIndex = TLSIndex(TLSIndexType::DirectOnThreadLocalData, actualIndexOffset); + usedDirectOnThreadLocalDataPath = true; } - usedDirectOnThreadLocalDataPath = true; } if (usedDirectOnThreadLocalDataPath) VolatileStore(&g_pMethodTablesForDirectThreadLocalData[IndexOffsetToDirectThreadLocalIndex(newTLSIndex.GetIndexOffset())], pMT); diff --git a/src/tests/baseservices/threading/threadstatic/threadstatic09.cs b/src/tests/baseservices/threading/threadstatic/threadstatic09.cs new file mode 100644 index 00000000000000..fb6084e81f48cf --- /dev/null +++ b/src/tests/baseservices/threading/threadstatic/threadstatic09.cs @@ -0,0 +1,72 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Runtime.CompilerServices; +using System.Threading; + +public static class ThreadStaticAlignmentFallback +{ + private const int Pass = 100; + private const int Fail = -1; + private const int PaddingCount = 12; + + public static int Main() + { + int paddingCount = new Random().Next(PaddingCount); + Console.WriteLine($"Using {paddingCount} padding fields."); + return InitializePadding(paddingCount) && LongStorage.Check() ? Pass : Fail; + } + + [MethodImpl(MethodImplOptions.NoInlining)] + private static bool InitializePadding(int count) + { + if (count == 0) + { + return true; + } + + if (!Padding.Check()) + { + return false; + } + + return InitializePadding>(count - 1); + } + + private sealed class Padding + { + [ThreadStatic] + private static int s_value; + + [MethodImpl(MethodImplOptions.NoInlining)] + public static bool Check() + { + if (s_value != 0) + { + return false; + } + + s_value = 42; + return s_value == 42; + } + } + + private static class LongStorage + { + [ThreadStatic] + private static long s_value; + + [MethodImpl(MethodImplOptions.NoInlining)] + public static bool Check() + { + if (Interlocked.Increment(ref s_value) != 1) + { + return false; + } + + s_value = 0x1122334455667788; + return s_value == 0x1122334455667788; + } + } +} diff --git a/src/tests/baseservices/threading/threadstatic/threadstatic09.csproj b/src/tests/baseservices/threading/threadstatic/threadstatic09.csproj new file mode 100644 index 00000000000000..f21acafb595d74 --- /dev/null +++ b/src/tests/baseservices/threading/threadstatic/threadstatic09.csproj @@ -0,0 +1,13 @@ + + + Exe + 1 + true + false + true + true + + + + +