Skip to content
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

Stop using ThreadLocal<T>, use [ThreadStatic] #1460

Merged
merged 1 commit into from
Jan 23, 2024
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
1 change: 0 additions & 1 deletion src/WinRT.Runtime/ComWrappersSupport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
using WinRT.Interop;

#if NET
Expand Down
1 change: 0 additions & 1 deletion src/WinRT.Runtime/ComWrappersSupport.net5.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
Expand Down
1 change: 0 additions & 1 deletion src/WinRT.Runtime/Marshalers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
Expand Down
31 changes: 19 additions & 12 deletions src/WinRT.Runtime/TypeNameSupport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;

namespace WinRT
{
[Flags]
internal enum TypeNameGenerationFlags
{
None = 0,

/// <summary>
/// Generate the name of the type as if it was boxed in an object.
/// </summary>
GenerateBoxedName = 0x1,

/// <summary>
/// Don't output a type name of a custom .NET type. Generate a compatible WinRT type name if needed.
/// </summary>
Expand Down Expand Up @@ -304,18 +304,22 @@ struct VisitedType
{
public Type Type { get; set; }
public bool Covariant { get; set; }
}

}

#nullable enable
/// <summary>
/// Tracker for visited types when determining a WinRT interface to use as the type name.
/// Only used when GetNameForType is called with <see cref="TypeNameGenerationFlags.ForGetRuntimeClassName"/>.
/// </summary>
private static readonly ThreadLocal<Stack<VisitedType>> VisitedTypes = new ThreadLocal<Stack<VisitedType>>(() => new Stack<VisitedType>());
/// <remarks>
/// Only used when <see cref="GetNameForType"/> is called with <see cref="TypeNameGenerationFlags.ForGetRuntimeClassName"/>.
/// </remarks>
[ThreadStatic]
private static Stack<VisitedType>? visitedTypesInstance;

[ThreadStatic]
private static StringBuilder? nameForTypeBuilderInstance;

public static string GetNameForType(Type type, TypeNameGenerationFlags flags)
public static string GetNameForType(Type? type, TypeNameGenerationFlags flags)
{
if (type is null)
{
Expand All @@ -331,8 +335,9 @@ public static string GetNameForType(Type type, TypeNameGenerationFlags flags)
}

return string.Empty;
}

}
#nullable restore

private static bool TryAppendSimpleTypeName(Type type, StringBuilder builder, TypeNameGenerationFlags flags)
{
if (type.IsPrimitive || type == typeof(string) || type == typeof(Guid) || type == typeof(TimeSpan))
Expand Down Expand Up @@ -385,7 +390,7 @@ private static bool TryAppendWinRTInterfaceNameForType(Type type, StringBuilder
Debug.Assert((flags & TypeNameGenerationFlags.ForGetRuntimeClassName) != 0);
Debug.Assert(!type.IsGenericTypeDefinition);

var visitedTypes = VisitedTypes.Value;
var visitedTypes = visitedTypesInstance ??= new Stack<VisitedType>();

if (visitedTypes.Any(visited => visited.Type == type))
{
Expand Down Expand Up @@ -490,6 +495,8 @@ private static bool TryAppendTypeName(Type type, StringBuilder builder, TypeName
Type[] genericTypeArguments = type.GetGenericArguments();
Type[] genericTypeParameters = definition.GetGenericArguments();

var visitedTypes = visitedTypesInstance ??= new Stack<VisitedType>();

for (int i = 0; i < genericTypeArguments.Length; i++)
{
Type argument = genericTypeArguments[i];
Expand All @@ -507,7 +514,7 @@ private static bool TryAppendTypeName(Type type, StringBuilder builder, TypeName

if ((flags & TypeNameGenerationFlags.ForGetRuntimeClassName) != 0)
{
VisitedTypes.Value.Push(new VisitedType
visitedTypes.Push(new VisitedType
{
Type = type,
Covariant = (genericTypeParameters[i].GenericParameterAttributes & GenericParameterAttributes.VarianceMask) == GenericParameterAttributes.Covariant
Expand All @@ -518,7 +525,7 @@ private static bool TryAppendTypeName(Type type, StringBuilder builder, TypeName

if ((flags & TypeNameGenerationFlags.ForGetRuntimeClassName) != 0)
{
VisitedTypes.Value.Pop();
visitedTypes.Pop();
}

if (!success)
Expand Down
Loading