Skip to content

Handle ThreadContext cache more cleanly #13530

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

Merged
merged 1 commit into from
May 27, 2025
Merged
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Concurrent;
using System.ComponentModel;
using System.Runtime.ExceptionServices;
using Microsoft.Office;
Expand All @@ -22,9 +23,7 @@ internal abstract unsafe partial class ThreadContext : MarshalByRefObject, IHand
private bool _inThreadException;
private bool _filterSnapshotValid;

private static readonly Dictionary<uint, ThreadContext> s_contextHash = [];

private static readonly Lock s_lock = new();
private static readonly ConcurrentDictionary<uint, ThreadContext> s_contextHash = [];
private readonly Lock _marshallingControlLock = new();

private static int s_totalMessageLoopCount;
Expand Down Expand Up @@ -87,11 +86,7 @@ protected ThreadContext()
_id = PInvokeCore.GetCurrentThreadId();
_messageLoopCount = 0;
t_currentThreadContext = this;

lock (s_lock)
{
s_contextHash[_id] = this;
}
s_contextHash[_id] = this;
}

public ApplicationContext? ApplicationContext { get; private set; }
Expand Down Expand Up @@ -316,10 +311,7 @@ private void DisposeInternal(bool disposing)
}
finally
{
lock (s_lock)
{
s_contextHash.Remove(_id);
}
s_contextHash.Remove(_id, out _);

if (t_currentThreadContext == this)
{
Expand Down Expand Up @@ -439,27 +431,17 @@ protected virtual void EndModalMessageLoop() { }
/// <summary>
/// Exits the program by disposing of all thread contexts and message loops.
/// </summary>
internal static void ExitApplication() => ExitCommon(disposing: true);

private static void ExitCommon(bool disposing)
internal static void ExitApplication()
{
lock (s_lock)
foreach ((_, var threadContext) in s_contextHash)
{
if (s_contextHash is not null)
if (threadContext.ApplicationContext is ApplicationContext applicationContext)
{
ThreadContext[] contexts = new ThreadContext[s_contextHash.Values.Count];
s_contextHash.Values.CopyTo(contexts, 0);
for (int i = 0; i < contexts.Length; ++i)
{
if (contexts[i].ApplicationContext is ApplicationContext context)
{
context.ExitThread();
}
else
{
contexts[i].Dispose(disposing);
}
}
applicationContext.ExitThread();
}
else
{
threadContext.Dispose(disposing: true);
}
}
}
Expand Down Expand Up @@ -517,14 +499,8 @@ private static ThreadContext Create()

if (id == PInvokeCore.GetCurrentThreadId())
{
lock (s_lock)
{
// Check again inside the lock as another thread may have added it
if (!s_contextHash.TryGetValue(id, out context))
{
context = Create();
}
}
context = Create();
Debug.Assert(context._id == id);
}

return context;
Expand Down