Skip to content

Commit b8d5126

Browse files
committed
Use [ThreadStatic] in Gdip.ThreadData.
1 parent 0c00ab3 commit b8d5126

File tree

1 file changed

+4
-20
lines changed
  • src/libraries/System.Drawing.Common/src/System/Drawing

1 file changed

+4
-20
lines changed

src/libraries/System.Drawing.Common/src/System/Drawing/Gdiplus.cs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,9 @@
33

44
using System.Collections;
55
using System.Diagnostics;
6-
using System.Diagnostics.CodeAnalysis;
76
using System.IO;
87
using System.Reflection;
9-
using System.Runtime.CompilerServices;
10-
using System.Runtime.ConstrainedExecution;
118
using System.Runtime.InteropServices;
12-
using System.Threading;
139

1410
namespace System.Drawing
1511
{
@@ -19,7 +15,9 @@ internal static partial class SafeNativeMethods
1915
internal static partial class Gdip
2016
{
2117
private static readonly IntPtr s_initToken;
22-
private const string ThreadDataSlotName = "system.drawing.threaddata";
18+
19+
[ThreadStatic]
20+
private static IDictionary? t_threadData;
2321

2422
static Gdip()
2523
{
@@ -47,21 +45,7 @@ static Gdip()
4745
/// a per-thread basis. This way we can avoid 'object in use' crashes when different threads are
4846
/// referencing the same drawing object.
4947
/// </summary>
50-
internal static IDictionary ThreadData
51-
{
52-
get
53-
{
54-
LocalDataStoreSlot slot = Thread.GetNamedDataSlot(ThreadDataSlotName);
55-
IDictionary? threadData = (IDictionary?)Thread.GetData(slot);
56-
if (threadData == null)
57-
{
58-
threadData = new Hashtable();
59-
Thread.SetData(slot, threadData);
60-
}
61-
62-
return threadData;
63-
}
64-
}
48+
internal static IDictionary ThreadData => t_threadData ??= new Hashtable();
6549

6650
// Used to ensure static constructor has run.
6751
internal static void DummyFunction()

0 commit comments

Comments
 (0)