diff options
author | Jérémie Laval <jeremie.laval@gmail.com> | 2011-01-14 20:20:48 +0300 |
---|---|---|
committer | Jérémie Laval <jeremie.laval@gmail.com> | 2011-01-14 20:21:23 +0300 |
commit | 674699abeff8de3f8e07040dd873e6eb0f0dd30c (patch) | |
tree | fa0c5f8e928c07c027737e147ef4b370db18319b /mcs/class/Mono.Parallel | |
parent | a4078aa9a919dfae2103fd873a18e5683eef325f (diff) |
Add MonoTaskExtensions to Mono.Parallel (with test and doc).
Diffstat (limited to 'mcs/class/Mono.Parallel')
7 files changed, 204 insertions, 2 deletions
diff --git a/mcs/class/Mono.Parallel/Documentation/en/Mono.Threading.Tasks/MonoTaskExtensions.xml b/mcs/class/Mono.Parallel/Documentation/en/Mono.Threading.Tasks/MonoTaskExtensions.xml new file mode 100644 index 00000000000..24650d0effa --- /dev/null +++ b/mcs/class/Mono.Parallel/Documentation/en/Mono.Threading.Tasks/MonoTaskExtensions.xml @@ -0,0 +1,55 @@ +<Type Name="MonoTaskExtensions" FullName="Mono.Threading.Tasks.MonoTaskExtensions"> + <TypeSignature Language="C#" Value="public static class MonoTaskExtensions" /> + <TypeSignature Language="ILAsm" Value=".class public auto ansi abstract sealed beforefieldinit MonoTaskExtensions extends System.Object" /> + <AssemblyInfo> + <AssemblyName>Mono.Parallel</AssemblyName> + <AssemblyVersion>4.0.0.0</AssemblyVersion> + </AssemblyInfo> + <Base> + <BaseTypeName>System.Object</BaseTypeName> + </Base> + <Interfaces /> + <Docs> + <summary>Provide public wrappers around internal methods used in Mono implementation of <see cref="T:System.Threading.Tasks.Task" /> type.</summary> + <remarks>These extensions are targeted at developers coding custom schedulers so that they can tap into the specificities of the Mono implementation.</remarks> + </Docs> + <Members> + <Member MemberName="Execute"> + <MemberSignature Language="C#" Value="public static void Execute (this System.Threading.Tasks.Task task, Action<System.Threading.Tasks.Task> childWorkAdder);" /> + <MemberSignature Language="ILAsm" Value=".method public static hidebysig void Execute(class System.Threading.Tasks.Task task, class System.Action`1<class System.Threading.Tasks.Task> childWorkAdder) cil managed" /> + <MemberType>Method</MemberType> + <AssemblyInfo> + <AssemblyVersion>4.0.0.0</AssemblyVersion> + </AssemblyInfo> + <ReturnValue> + <ReturnType>System.Void</ReturnType> + </ReturnValue> + <Parameters> + <Parameter Name="task" Type="System.Threading.Tasks.Task" RefType="this" /> + <Parameter Name="childWorkAdder" Type="System.Action<System.Threading.Tasks.Task>" /> + </Parameters> + <Docs> + <param name="task">Task to run.</param> + <param name="childWorkAdder">Action that will be called with the Task that is being scheduled while above Task is executed as a parameter.</param> + <summary>Execute a <see cref="T:System.Threading.Tasks.Task" /> body and track any other Task scheduling during it which is then passed to the supplied action for specific scheduling.</summary> + <remarks><block subset="none" type="note"> + <para> + If the inner Task that is scheduled has been created with TaskCreationOptions.PreferFairness, the supplied action won't be called and instead the Task will be scheduled normally on the current <see cref="T:System.Threading.Tasks.TaskScheduler" /> + </para> +</block> + +The following example retrieve a task from a local deque, execute it and add any eventual child Task created/scheduled to its local deque using PushBottom (safe since we are on the same thread the whole time). + +<example> + <code lang="C#"> + Task task; + IConcurrentDeque<Task> deque = new CyclicDeque<Task> (); + + if (deque.PopBottom (out task) == PopResult.Succeed)) + task.Execute (deque.PushBottom); + </code> +</example></remarks> + </Docs> + </Member> + </Members> +</Type> diff --git a/mcs/class/Mono.Parallel/Documentation/en/index.xml b/mcs/class/Mono.Parallel/Documentation/en/index.xml index 6739a805613..089df754a57 100644 --- a/mcs/class/Mono.Parallel/Documentation/en/index.xml +++ b/mcs/class/Mono.Parallel/Documentation/en/index.xml @@ -29,8 +29,34 @@ <Namespace Name="Mono.Threading.Tasks"> <Type Name="CyclicDeque`1" DisplayName="CyclicDeque<T>" Kind="Class" /> <Type Name="IConcurrentDeque`1" DisplayName="IConcurrentDeque<T>" Kind="Interface" /> + <Type Name="MonoTaskExtensions" Kind="Class" /> <Type Name="PopResult" Kind="Enumeration" /> </Namespace> </Types> <Title>Mono.Parallel</Title> + <ExtensionMethods> + <ExtensionMethod> + <Targets> + <Target Type="T:System.Threading.Tasks.Task" /> + </Targets> + <Member MemberName="Execute"> + <MemberSignature Language="C#" Value="public static void Execute (this System.Threading.Tasks.Task task, Action<System.Threading.Tasks.Task> childWorkAdder);" /> + <MemberSignature Language="ILAsm" Value=".method public static hidebysig void Execute(class System.Threading.Tasks.Task task, class System.Action`1<class System.Threading.Tasks.Task> childWorkAdder) cil managed" /> + <MemberType>ExtensionMethod</MemberType> + <ReturnValue> + <ReturnType>System.Void</ReturnType> + </ReturnValue> + <Parameters> + <Parameter Name="task" Type="System.Threading.Tasks.Task" RefType="this" /> + <Parameter Name="childWorkAdder" Type="System.Action<System.Threading.Tasks.Task>" /> + </Parameters> + <Docs> + <param name="task">To be added.</param> + <param name="childWorkAdder">To be added.</param> + <summary>To be added.</summary> + </Docs> + <Link Type="Mono.Threading.Tasks.MonoTaskExtensions" Member="M:Mono.Threading.Tasks.MonoTaskExtensions.Execute(System.Threading.Tasks.Task,System.Action{System.Threading.Tasks.Task})" /> + </Member> + </ExtensionMethod> + </ExtensionMethods> </Overview> diff --git a/mcs/class/Mono.Parallel/Makefile b/mcs/class/Mono.Parallel/Makefile index 32f76c531c0..d20194b5ff5 100644 --- a/mcs/class/Mono.Parallel/Makefile +++ b/mcs/class/Mono.Parallel/Makefile @@ -6,4 +6,4 @@ LIBRARY = Mono.Parallel.dll include ../../build/library.make -LIB_MCS_FLAGS += -d:INSIDE_MONO_PARALLEL +LIB_MCS_FLAGS += -d:INSIDE_MONO_PARALLEL -r:$(corlib) -r:System.Core.dll diff --git a/mcs/class/Mono.Parallel/Mono.Parallel.dll.sources b/mcs/class/Mono.Parallel/Mono.Parallel.dll.sources index 691336cf4e8..84f989278a7 100644 --- a/mcs/class/Mono.Parallel/Mono.Parallel.dll.sources +++ b/mcs/class/Mono.Parallel/Mono.Parallel.dll.sources @@ -9,3 +9,4 @@ Mono.Threading/ReaderWriterLockSlimmer.cs ../corlib/System.Threading/AtomicBoolean.cs ../corlib/System.Collections.Concurrent/ConcurrentOrderedList.cs ../corlib/System.Threading.Tasks/CyclicDeque.cs +Mono.Threading.Tasks/MonoTaskExtensions.cs diff --git a/mcs/class/Mono.Parallel/Mono.Parallel_test.dll.sources b/mcs/class/Mono.Parallel/Mono.Parallel_test.dll.sources index d1ba72de323..f7386d2ef70 100644 --- a/mcs/class/Mono.Parallel/Mono.Parallel_test.dll.sources +++ b/mcs/class/Mono.Parallel/Mono.Parallel_test.dll.sources @@ -1,4 +1,5 @@ Mono.Collections.Concurrent/CollectionStressTestHelper.cs Mono.Collections.Concurrent/ConcurrentSkipListTests.cs Mono.Threading/ParallelTestHelper.cs -Mono.Threading/SnziTests.cs
\ No newline at end of file +Mono.Threading/SnziTests.cs +Mono.Threading.Tasks/MonoTaskExtensionsTests.cs diff --git a/mcs/class/Mono.Parallel/Mono.Threading.Tasks/MonoTaskExtensions.cs b/mcs/class/Mono.Parallel/Mono.Threading.Tasks/MonoTaskExtensions.cs new file mode 100644 index 00000000000..d1e6bffe1e6 --- /dev/null +++ b/mcs/class/Mono.Parallel/Mono.Threading.Tasks/MonoTaskExtensions.cs @@ -0,0 +1,52 @@ +// +// MonoTaskExtensions.cs +// +// Author: +// Jérémie "Garuma" Laval <jeremie.laval@gmail.com> +// +// Copyright (c) 2011 Jérémie "Garuma" Laval +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#if NET_4_0 + +using System; +using System.Threading.Tasks; +using System.Reflection; + +namespace Mono.Threading.Tasks +{ + public static class MonoTaskExtensions + { + static Action<Task, Action<Task>> internalExecute = null; + + // Allow external worker to call into the otherwise internal corresponding method of Task + public static void Execute (this Task task, Action<Task> childWorkAdder) + { + if (internalExecute == null) { + var method = typeof(Task).GetMethod ("Execute", BindingFlags.Instance | BindingFlags.NonPublic); + internalExecute = (Action<Task, Action<Task>>)Delegate.CreateDelegate (typeof(Action<Task, Action<Task>>), method); + } + + internalExecute (task, childWorkAdder); + } + } +} + +#endif diff --git a/mcs/class/Mono.Parallel/Test/Mono.Threading.Tasks/MonoTaskExtensionsTests.cs b/mcs/class/Mono.Parallel/Test/Mono.Threading.Tasks/MonoTaskExtensionsTests.cs new file mode 100644 index 00000000000..62f939b963a --- /dev/null +++ b/mcs/class/Mono.Parallel/Test/Mono.Threading.Tasks/MonoTaskExtensionsTests.cs @@ -0,0 +1,67 @@ +// +// MonoTaskExtensionsTests.cs +// +// Author: +// Jérémie "Garuma" Laval <jeremie.laval@gmail.com> +// +// Copyright (c) 2011 Jérémie "Garuma" Laval +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#if NET_4_0 + +using System; +using System.Threading; +using System.Threading.Tasks; + +using Mono.Threading.Tasks; + +using NUnit.Framework; + +namespace MonoTests.Mono.Threading.Tasks +{ + [TestFixtureAttribute] + public class MonoTaskExtensionsTests + { + [Test] + public void SimpleExecutionTest () + { + bool executed = false; + Task t = new Task (() => executed = true); + t.Execute (delegate {}); + + Assert.IsTrue (executed); + } + + [Test] + public void ExecutionWithChildCreationTest () + { + bool executed = false; + bool childRetrieved = false; + + Task t = new Task (() => { Task.Factory.StartNew (() => Console.WriteLine ("execution")); executed = true; }); + t.Execute ((child) => childRetrieved = child != null); + + Assert.IsTrue (executed); + Assert.IsTrue (childRetrieved); + } + } +} + +#endif |