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

Remove generic type limitation around collectible assemblies #4731

Closed
Tracked by #93172
stephentoub opened this issue Nov 30, 2015 · 3 comments
Closed
Tracked by #93172

Remove generic type limitation around collectible assemblies #4731

stephentoub opened this issue Nov 30, 2015 · 3 comments
Assignees
Milestone

Comments

@stephentoub
Copy link
Member

See the discussion at dotnet/corefx@b460c27#commitcomment-14671045.

Example:

using System;
using System.Reflection;
using System.Reflection.Emit;

class MyType<T>
{
    [ThreadStatic]
    private static int t_value;
}

class Repro
{
    static void Main()
    {
        var assembly = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName("dynamic_assembly"), AssemblyBuilderAccess.RunAndCollect);
        var module = assembly.DefineDynamicModule("dynamic");
        var typeBuilder = module.DefineType("Dummy");
        typeBuilder.DefineDefaultConstructor(MethodAttributes.Public);
        var dummType = typeBuilder.CreateType();

        var constructedType = typeof(MyType<>).MakeGenericType(dummType);
    }
}

fails with:

Unhandled Exception: System.TypeLoadException: Collectible type 'MyType`1[Dummy]' may not have Thread or Context static members.
   at System.RuntimeType.MakeGenericType(Type[] instantiation)
   at Repro.Main() in Program.cs:line 22

This means that a developer writing a general-purpose library with generic types can't use [ThreadStatic] because it would break a consumer of the library, and/or that a developer writing a collectible assembly needs to know the implementation details of generic types being used. Neither of those is very good.

Related to https://github.com/dotnet/corefx/issues/4726.

@gkhanna79 gkhanna79 assigned JohnChen0 and unassigned gkhanna79 Jan 8, 2016
stephentoub referenced this issue in stephentoub/corefx Jan 13, 2016
As a perf improvement awhile back, ImmutableList<T> took a dependency on [ThreadStatic].  This unfortunately breaks cases where ImmutableList<T> is used with a T from a collectible assembly, due to https://github.com/dotnet/coreclr/issues/2191.

Until that limitation is removed, this provides a workaround to restore the ability to use ImmutableList<T> with such types.  Some microbenchmarks around enumeration (which this was originally introduced to help with) show potential throughput regressions up to 10-15%.
@gkhanna79 gkhanna79 assigned fadimounir and unassigned JohnChen0 Mar 10, 2016
@fadimounir fadimounir assigned janvorli and unassigned fadimounir Apr 4, 2019
@fadimounir
Copy link
Contributor

@janvorli I'm assigning this to you since you're working on collectible assemblies currently

@janvorli
Copy link
Member

janvorli commented Apr 4, 2019

This issue no longer exists. I've removed this limitation in the past while working on unloadability.

@janvorli janvorli closed this as completed Apr 4, 2019
@msftgits msftgits transferred this issue from dotnet/coreclr Jan 30, 2020
@msftgits msftgits added this to the 3.0 milestone Jan 30, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Jan 4, 2021
@danmoseley
Copy link
Member

Note we still have this code

        // Workaround for https://github.com/dotnet/runtime/issues/4731.
        // When that's fixed, a [ThreadStatic] Stack should be added back to AllocFreeConcurrentStack<T>.

        [ThreadStatic]
        internal static Dictionary<Type, object>? t_stacks;

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants