Skip to content

Commit bd305f6

Browse files
authored
Use field to replace a few more explicit ThreadStatic fields (#120243)
1 parent 2c9fdf0 commit bd305f6

File tree

7 files changed

+22
-58
lines changed

7 files changed

+22
-58
lines changed

src/libraries/Common/src/System/Net/LazyAsyncResult.cs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,8 @@ internal class LazyAsyncResult : IAsyncResult
1515
private const int ForceAsyncCount = 50;
1616

1717
// This is to avoid user mistakes when they queue another async op from a callback the completes sync.
18-
[ThreadStatic]
19-
private static ThreadContext? t_threadContext;
20-
21-
private static ThreadContext CurrentThreadContext
22-
{
23-
get
24-
{
25-
ThreadContext? threadContext = t_threadContext;
26-
if (threadContext == null)
27-
{
28-
threadContext = new ThreadContext();
29-
t_threadContext = threadContext;
30-
}
31-
32-
return threadContext;
33-
}
34-
}
18+
[field: ThreadStatic]
19+
private static ThreadContext CurrentThreadContext => field ??= new();
3520

3621
private sealed class ThreadContext
3722
{

src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/ContractNameServices.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,8 @@ internal static class ContractNameServices
2525
private const char GenericFormatOpeningBracket = '{';
2626
private const char GenericFormatClosingBracket = '}';
2727

28-
[ThreadStatic]
29-
private static Dictionary<Type, string>? typeIdentityCache;
30-
31-
private static Dictionary<Type, string> TypeIdentityCache
32-
{
33-
get
34-
{
35-
return typeIdentityCache ??= new Dictionary<Type, string>();
36-
}
37-
}
28+
[field: ThreadStatic]
29+
private static Dictionary<Type, string> TypeIdentityCache => field ??= new();
3830

3931
internal static string GetTypeIdentity(Type type)
4032
{

src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/ReflectionModel/ImportType.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,8 @@ internal sealed class ImportType
2121
private Func<Export, object>? _castSingleValue;
2222
private readonly bool _isOpenGeneric;
2323

24-
[ThreadStatic]
25-
internal static Dictionary<Type, Func<Export, object>?>? _castSingleValueCache;
26-
27-
private static Dictionary<Type, Func<Export, object>?> CastSingleValueCache
28-
{
29-
get
30-
{
31-
return _castSingleValueCache ??= new Dictionary<Type, Func<Export, object>?>();
32-
}
33-
}
24+
[field: ThreadStatic]
25+
private static Dictionary<Type, Func<Export, object>?> CastSingleValueCache => field ??= new();
3426

3527
public ImportType(Type type, ImportCardinality cardinality)
3628
{

src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/RandomNumberGenerator.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@ namespace System.Diagnostics
88
/// </summary>
99
internal sealed class RandomNumberGenerator
1010
{
11-
[ThreadStatic] private static RandomNumberGenerator? t_random;
12-
1311
private ulong _s0, _s1, _s2, _s3;
1412

15-
public static RandomNumberGenerator Current => t_random ??= new RandomNumberGenerator();
13+
[field: ThreadStatic]
14+
public static RandomNumberGenerator Current => field ??= new();
1615

1716
public unsafe RandomNumberGenerator()
1817
{

src/libraries/System.Private.CoreLib/src/System/Diagnostics/Debug.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,14 @@ public static bool AutoFlush
3232
set { }
3333
}
3434

35-
[ThreadStatic]
36-
private static int t_indentLevel;
35+
[field: ThreadStatic]
3736
public static int IndentLevel
3837
{
39-
get => t_indentLevel;
38+
get => field;
4039
set
4140
{
42-
t_indentLevel = value < 0 ? 0 : value;
43-
s_provider.OnIndentLevelChanged(t_indentLevel);
41+
field = value < 0 ? 0 : value;
42+
s_provider.OnIndentLevelChanged(field);
4443
}
4544
}
4645

src/libraries/System.Security.Cryptography.Cose/tests/CoseTestHelpers.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -464,9 +464,8 @@ private static void AssertHeaders(CborReader reader, List<(CoseHeaderLabel, Read
464464
}
465465
}
466466

467-
[ThreadStatic]
468-
private static TestKeyRing? t_threadKeys;
469-
private static TestKeyRing ThreadKeys => (t_threadKeys ??= new TestKeyRing());
467+
[field: ThreadStatic]
468+
private static TestKeyRing ThreadKeys => field ??= new();
470469

471470
internal static ECDsa ES256 => ThreadKeys.ES256;
472471
internal static ECDsa ES384 => ThreadKeys.ES384;

src/libraries/System.Transactions.Local/src/System/Transactions/Transaction.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,31 +1126,29 @@ internal sealed class ContextData
11261126

11271127
internal bool _asyncFlow;
11281128

1129-
[ThreadStatic]
1130-
private static ContextData? t_staticData;
1131-
11321129
internal ContextData(bool asyncFlow)
11331130
{
11341131
_asyncFlow = asyncFlow;
11351132
}
11361133

11371134
[AllowNull]
1135+
[field: ThreadStatic]
11381136
internal static ContextData TLSCurrentData
11391137
{
1140-
get => t_staticData ??= new ContextData(false);
1138+
get => field ??= new(false);
11411139
set
11421140
{
1143-
if (value == null && t_staticData != null)
1141+
if (value == null && field != null)
11441142
{
11451143
// set each property to null to retain one TLS ContextData copy.
1146-
t_staticData.CurrentScope = null;
1147-
t_staticData.CurrentTransaction = null;
1148-
t_staticData.DefaultComContextState = DefaultComContextState.Unknown;
1149-
t_staticData.WeakDefaultComContext = null;
1144+
field.CurrentScope = null;
1145+
field.CurrentTransaction = null;
1146+
field.DefaultComContextState = DefaultComContextState.Unknown;
1147+
field.WeakDefaultComContext = null;
11501148
}
11511149
else
11521150
{
1153-
t_staticData = value;
1151+
field = value;
11541152
}
11551153
}
11561154
}

0 commit comments

Comments
 (0)