Commit bbab86d5 authored by Marc Gravell's avatar Marc Gravell

DenyExecSync can be init-only

parent 9a3d93c6
......@@ -20,7 +20,7 @@ static class TaskSource
/// Indicates whether the specified task will not hijack threads when results are set
/// </summary>
public static readonly Func<Task, bool> IsSyncSafe;
static Action<Task> denyExecSync;
private static readonly Action<Task> DenyExecSync;
static TaskSource()
{
try
......@@ -41,7 +41,7 @@ static TaskSource()
il.Emit(OpCodes.Or); // [task, combined]
il.Emit(OpCodes.Stfld, stateField); // []
il.Emit(OpCodes.Ret);
denyExecSync = (Action<Task>)method.CreateDelegate(typeof(Action<Task>));
DenyExecSync = (Action<Task>)method.CreateDelegate(typeof(Action<Task>));
method = new DynamicMethod("IsSyncSafe", typeof(bool), new[] { typeof(Task) }, typeof(Task), true);
il = method.GetILGenerator();
......@@ -56,13 +56,13 @@ static TaskSource()
// and test them (check for an exception etc)
var tcs = new TaskCompletionSource<int>();
denyExecSync(tcs.Task);
DenyExecSync(tcs.Task);
if(!IsSyncSafe(tcs.Task))
{
Debug.WriteLine("IsSyncSafe reported false!");
Trace.WriteLine("IsSyncSafe reported false!");
// revert to not trusting them
denyExecSync = null;
DenyExecSync = null;
IsSyncSafe = null;
}
}
......@@ -71,12 +71,12 @@ static TaskSource()
{
Debug.WriteLine(ex.Message);
Trace.WriteLine(ex.Message);
denyExecSync = null;
DenyExecSync = null;
IsSyncSafe = null;
}
if(denyExecSync == null)
denyExecSync = t => { }; // no-op if that fails
if(DenyExecSync == null)
DenyExecSync = t => { }; // no-op if that fails
if (IsSyncSafe == null)
IsSyncSafe = t => false; // assume: not
}
......@@ -87,7 +87,7 @@ static TaskSource()
public static TaskCompletionSource<T> CreateDenyExecSync<T>(object asyncState)
{
var source = new TaskCompletionSource<T>(asyncState);
denyExecSync(source.Task);
DenyExecSync(source.Task);
return source;
}
/// <summary>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment