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

Replace the Dictionary with HashSet in AppDomainShutdownMonitor #3932

Merged
Merged
Show file tree
Hide file tree
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
Expand Up @@ -85,7 +85,7 @@ public D3DImage(double dpiX, double dpiY)
_dpiX = dpiX;
_dpiY = dpiY;

_listener = new WeakReference(this);
_listener = new WeakReference<IAppDomainShutdownListener>(this);
AppDomainShutdownMonitor.Add(_listener);
}

Expand Down Expand Up @@ -944,7 +944,7 @@ void IAppDomainShutdownListener.NotifyShutdown()
private EventHandler _sendPresentDelegate;

// WeakReference to this used to listen to AddDomain shutdown
private WeakReference _listener;
private WeakReference<IAppDomainShutdownListener> _listener;

// Keeps track of how many times the user has nested Lock
private uint _lockCount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,59 +28,57 @@ internal static class AppDomainShutdownMonitor
{
static AppDomainShutdownMonitor()
{
_listeners =
new HashSet<WeakReference<IAppDomainShutdownListener>>();

AppDomain.CurrentDomain.DomainUnload += OnShutdown;
AppDomain.CurrentDomain.ProcessExit += OnShutdown;

_dictionary = new Dictionary<WeakReference, WeakReference>();
}
public static void Add(WeakReference listener)

public static void Add(WeakReference<IAppDomainShutdownListener> listener)
{
Debug.Assert(listener.Target != null);
Debug.Assert(listener.Target is IAppDomainShutdownListener);
Debug.Assert(listener.TryGetTarget(out _));

lock (_dictionary)
lock (_listeners)
{
if (!_shuttingDown)
{
_dictionary.Add(listener, listener);
_listeners.Add(listener);
}
}
}

public static void Remove(WeakReference listener)
public static void Remove(WeakReference<IAppDomainShutdownListener> listener)
{
Debug.Assert(listener.Target == null || listener.Target is IAppDomainShutdownListener);

lock (_dictionary)
lock (_listeners)
{
if (!_shuttingDown)
{
_dictionary.Remove(listener);
_listeners.Remove(listener);
}
}
}

private static void OnShutdown(object sender, EventArgs e)
{
lock (_dictionary)
{
lock (_listeners)
{
// Setting this to true prevents Add and Remove from modifying the list. This
// way we call out without holding a lock (which would be bad)
_shuttingDown = true;
}
foreach (WeakReference value in _dictionary.Values)

foreach (WeakReference<IAppDomainShutdownListener> weakReference in _listeners)
{
IAppDomainShutdownListener listener = value.Target as IAppDomainShutdownListener;
if (listener != null)
if (weakReference.TryGetTarget(out var listener))
{
listener.NotifyShutdown();
}
}
}

private static Dictionary<WeakReference, WeakReference> _dictionary;

private static readonly HashSet<WeakReference<IAppDomainShutdownListener>> _listeners;

private static bool _shuttingDown;
}
}