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 2 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
6 changes: 4 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)
% localDel = _state.del;
manodasanW marked this conversation as resolved.
Show resolved Hide resolved
if (localDel == null)
{
return %;
}
%_state.del.Invoke(%);
%localDel.Invoke(%);
};
}
return handler;
Expand All @@ -6506,6 +6507,7 @@ bind<write_type_name>(type, typedef_name_type::CCW, true)
eventTypeCode,
bind<write_event_source_type_name>(eventTypeSemantics),
bind<write_event_invoke_params>(invokeMethodSig),
eventTypeCode,
bind<write_event_invoke_return_default>(invokeMethodSig),
bind<write_event_invoke_return>(invokeMethodSig),
bind<write_event_invoke_args>(invokeMethodSig));
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);
System.EventHandler<T> delLocal = _state.del;
if (delLocal != null)
delLocal.Invoke(obj, e);
manodasanW marked this conversation as resolved.
Show resolved Hide resolved
};
}
return handler;
Expand Down