Skip to content

Commit

Permalink
Use [ThreadStatic] in Gdip.ThreadData.
Browse files Browse the repository at this point in the history
  • Loading branch information
teo-tsirpanis committed Apr 19, 2022
1 parent 0c00ab3 commit b8d5126
Showing 1 changed file with 4 additions and 20 deletions.
24 changes: 4 additions & 20 deletions src/libraries/System.Drawing.Common/src/System/Drawing/Gdiplus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@

using System.Collections;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.ConstrainedExecution;
using System.Runtime.InteropServices;
using System.Threading;

namespace System.Drawing
{
Expand All @@ -19,7 +15,9 @@ internal static partial class SafeNativeMethods
internal static partial class Gdip
{
private static readonly IntPtr s_initToken;
private const string ThreadDataSlotName = "system.drawing.threaddata";

[ThreadStatic]
private static IDictionary? t_threadData;

static Gdip()
{
Expand Down Expand Up @@ -47,21 +45,7 @@ static Gdip()
/// a per-thread basis. This way we can avoid 'object in use' crashes when different threads are
/// referencing the same drawing object.
/// </summary>
internal static IDictionary ThreadData
{
get
{
LocalDataStoreSlot slot = Thread.GetNamedDataSlot(ThreadDataSlotName);
IDictionary? threadData = (IDictionary?)Thread.GetData(slot);
if (threadData == null)
{
threadData = new Hashtable();
Thread.SetData(slot, threadData);
}

return threadData;
}
}
internal static IDictionary ThreadData => t_threadData ??= new Hashtable();

// Used to ensure static constructor has run.
internal static void DummyFunction()
Expand Down

0 comments on commit b8d5126

Please sign in to comment.