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

Address potential races in recent event changes #882

Merged
merged 4 commits into from
Jun 17, 2021
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
5 changes: 3 additions & 2 deletions src/cswinrt/code_writers.h
Original file line number Diff line number Diff line change
Expand Up @@ -6488,11 +6488,12 @@ bind<write_type_name>(type, typedef_name_type::CCW, true)
{
handler = (%) =>
{
if (_state.del == null)
var localDel = _state.del;
if (localDel == null)
{
return %;
}
%_state.del.Invoke(%);
%localDel.Invoke(%);
};
}
return handler;
Expand Down
17 changes: 10 additions & 7 deletions src/cswinrt/strings/WinRT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,10 @@ public void Subscribe(TDelegate del)
{
lock (this)
{
if (_state.del is null)
bool registerHandler = _state.del is null;

_state.del = (TDelegate)global::System.Delegate.Combine(_state.del, del);
if (registerHandler)
{
var marshaler = CreateMarshaler((TDelegate)EventInvoke);
try
Expand All @@ -385,7 +388,6 @@ public void Subscribe(TDelegate del)
DisposeMarshaler(marshaler);
}
}
_state.del = (TDelegate)global::System.Delegate.Combine(_state.del, del);
}
}

Expand Down Expand Up @@ -445,10 +447,10 @@ private class Cache
}

private IWeakReference target;
private ConcurrentDictionary<int, EventSource<TDelegate>.State> states = new ConcurrentDictionary<int, EventSource<TDelegate>.State>();
private readonly ConcurrentDictionary<int, EventSource<TDelegate>.State> states = new ConcurrentDictionary<int, EventSource<TDelegate>.State>();

private static ReaderWriterLockSlim cachesLock = new ReaderWriterLockSlim();
private static ConcurrentDictionary<IntPtr, Cache> caches = new ConcurrentDictionary<IntPtr, Cache>();
private static readonly ReaderWriterLockSlim cachesLock = new ReaderWriterLockSlim();
private static readonly ConcurrentDictionary<IntPtr, Cache> caches = new ConcurrentDictionary<IntPtr, Cache>();

private Cache Update(IWeakReference target, EventSource<TDelegate> source, int index)
{
Expand Down Expand Up @@ -579,8 +581,9 @@ override protected System.Delegate EventInvoke
{
handler = (System.Object obj, T e) =>
{
if (_state.del != null)
_state.del.Invoke(obj, e);
var localDel = _state.del;
if (localDel != null)
localDel.Invoke(obj, e);
};
}
return handler;
Expand Down