diff options
author | Stephen Toub <stoub@microsoft.com> | 2018-02-26 17:51:44 +0300 |
---|---|---|
committer | Stephen Toub <stoub@microsoft.com> | 2018-02-28 16:13:58 +0300 |
commit | 3980e74d867f260702310d59773fa1d5a28b81f9 (patch) | |
tree | 70233d8e3838d82dc5c9d791dd4e3430875f8e73 /src/System.Threading.ThreadPool | |
parent | ea4d2e8ba0ef3ef84388e25ae77cde393b39bbdf (diff) |
Change new ThreadPool.QueueUserWorkItem overload to be generic
Diffstat (limited to 'src/System.Threading.ThreadPool')
-rw-r--r-- | src/System.Threading.ThreadPool/ref/System.Threading.ThreadPool.cs | 2 | ||||
-rw-r--r-- | src/System.Threading.ThreadPool/tests/ThreadPoolTests.netcoreapp.cs | 50 |
2 files changed, 41 insertions, 11 deletions
diff --git a/src/System.Threading.ThreadPool/ref/System.Threading.ThreadPool.cs b/src/System.Threading.ThreadPool/ref/System.Threading.ThreadPool.cs index bb4b87c06c..7d1911d50a 100644 --- a/src/System.Threading.ThreadPool/ref/System.Threading.ThreadPool.cs +++ b/src/System.Threading.ThreadPool/ref/System.Threading.ThreadPool.cs @@ -23,7 +23,7 @@ namespace System.Threading public static void GetMinThreads(out int workerThreads, out int completionPortThreads) { throw null; } public static bool QueueUserWorkItem(System.Threading.WaitCallback callBack) { throw null; } public static bool QueueUserWorkItem(System.Threading.WaitCallback callBack, object state) { throw null; } - public static bool QueueUserWorkItem(System.Threading.WaitCallback callBack, object state, bool preferLocal) { throw null; } + public static bool QueueUserWorkItem<TState>(System.Action<TState> callBack, TState state, bool preferLocal) { throw null; } public static System.Threading.RegisteredWaitHandle RegisterWaitForSingleObject(System.Threading.WaitHandle waitObject, System.Threading.WaitOrTimerCallback callBack, object state, int millisecondsTimeOutInterval, bool executeOnlyOnce) { throw null; } public static System.Threading.RegisteredWaitHandle RegisterWaitForSingleObject(System.Threading.WaitHandle waitObject, System.Threading.WaitOrTimerCallback callBack, object state, long millisecondsTimeOutInterval, bool executeOnlyOnce) { throw null; } public static System.Threading.RegisteredWaitHandle RegisterWaitForSingleObject(System.Threading.WaitHandle waitObject, System.Threading.WaitOrTimerCallback callBack, object state, System.TimeSpan timeout, bool executeOnlyOnce) { throw null; } diff --git a/src/System.Threading.ThreadPool/tests/ThreadPoolTests.netcoreapp.cs b/src/System.Threading.ThreadPool/tests/ThreadPoolTests.netcoreapp.cs index c3b9a92c3a..4aac66daed 100644 --- a/src/System.Threading.ThreadPool/tests/ThreadPoolTests.netcoreapp.cs +++ b/src/System.Threading.ThreadPool/tests/ThreadPoolTests.netcoreapp.cs @@ -14,7 +14,7 @@ namespace System.Threading.ThreadPools.Tests [InlineData(true)] public void QueueUserWorkItem_PreferLocal_InvalidArguments_Throws(bool preferLocal) { - Assert.Throws<ArgumentNullException>(() => ThreadPool.QueueUserWorkItem(null, new object(), preferLocal)); + AssertExtensions.Throws<ArgumentNullException>("callBack", () => ThreadPool.QueueUserWorkItem(null, new object(), preferLocal)); } [Theory] @@ -23,24 +23,54 @@ namespace System.Threading.ThreadPools.Tests public async Task QueueUserWorkItem_PreferLocal_NullValidForState(bool preferLocal) { var tcs = new TaskCompletionSource<int>(); - ThreadPool.QueueUserWorkItem(s => - { - tcs.SetResult(84); - }, null, preferLocal); + ThreadPool.QueueUserWorkItem(s => tcs.SetResult(84), (object)null, preferLocal); Assert.Equal(84, await tcs.Task); } [Theory] [InlineData(false)] [InlineData(true)] - public async Task QueueUserWorkItem_PreferLocal_StateObjectPassedThrough(bool preferLocal) + public async Task QueueUserWorkItem_PreferLocal_ReferenceTypeStateObjectPassedThrough(bool preferLocal) { var tcs = new TaskCompletionSource<int>(); - ThreadPool.QueueUserWorkItem(s => - { - ((TaskCompletionSource<int>)s).SetResult(84); - }, tcs, preferLocal); + ThreadPool.QueueUserWorkItem(s => s.SetResult(84), tcs, preferLocal); Assert.Equal(84, await tcs.Task); } + + [Theory] + [InlineData(false)] + [InlineData(true)] + public async Task QueueUserWorkItem_PreferLocal_ValueTypeStateObjectPassedThrough(bool preferLocal) + { + var tcs = new TaskCompletionSource<int>(); + ThreadPool.QueueUserWorkItem(s => s.tcs.SetResult(s.value), (tcs, value: 42), preferLocal); + Assert.Equal(42, await tcs.Task); + } + + [Theory] + [InlineData(false)] + [InlineData(true)] + public async Task QueueUserWorkItem_PreferLocal_RunsAsynchronously(bool preferLocal) + { + await Task.Factory.StartNew(() => + { + int origThread = Environment.CurrentManagedThreadId; + var tcs = new TaskCompletionSource<int>(); + ThreadPool.QueueUserWorkItem(s => s.SetResult(Environment.CurrentManagedThreadId), tcs, preferLocal); + Assert.NotEqual(origThread, tcs.Task.GetAwaiter().GetResult()); + }, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default); + } + + [Theory] + [InlineData(false)] + [InlineData(true)] + public async Task QueueUserWorkItem_PreferLocal_ExecutionContextFlowed(bool preferLocal) + { + var tcs = new TaskCompletionSource<int>(); + var asyncLocal = new AsyncLocal<int>() { Value = 42 }; + ThreadPool.QueueUserWorkItem(s => s.SetResult(asyncLocal.Value), tcs, preferLocal); + asyncLocal.Value = 0; + Assert.Equal(42, await tcs.Task); + } } } |