Commit bbab86d5 authored by Marc Gravell's avatar Marc Gravell

DenyExecSync can be init-only

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