Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/coreclr/vm/threadstatics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -751,8 +751,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);
Expand Down
72 changes: 72 additions & 0 deletions src/tests/baseservices/threading/threadstatic/threadstatic09.cs
Original file line number Diff line number Diff line change
@@ -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<object>(paddingCount) && LongStorage.Check() ? Pass : Fail;
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static bool InitializePadding<T>(int count)
{
if (count == 0)
{
return true;
}

if (!Padding<T>.Check())
{
return false;
}

return InitializePadding<Padding<T>>(count - 1);
}

private sealed class Padding<T>
{
[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;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<CLRTestPriority>1</CLRTestPriority>
<BuildAsStandalone>true</BuildAsStandalone>
<ReferenceXUnitWrapperGenerator>false</ReferenceXUnitWrapperGenerator>
<NativeAotIncompatible>true</NativeAotIncompatible>
<CLRTestTargetUnsupported Condition="'$(RuntimeFlavor)' != 'coreclr'">true</CLRTestTargetUnsupported>
</PropertyGroup>
<ItemGroup>
<Compile Include="threadstatic09.cs" />
</ItemGroup>
</Project>
Loading