Add MonoTaskExtensions to Mono.Parallel (with test and doc).
authorJérémie Laval <jeremie.laval@gmail.com>
Fri, 14 Jan 2011 17:20:48 +0000 (17:20 +0000)
committerJérémie Laval <jeremie.laval@gmail.com>
Fri, 14 Jan 2011 17:21:23 +0000 (17:21 +0000)
mcs/class/Mono.Parallel/Documentation/en/Mono.Threading.Tasks/MonoTaskExtensions.xml [new file with mode: 0644]
mcs/class/Mono.Parallel/Documentation/en/index.xml
mcs/class/Mono.Parallel/Makefile
mcs/class/Mono.Parallel/Mono.Parallel.dll.sources
mcs/class/Mono.Parallel/Mono.Parallel_test.dll.sources
mcs/class/Mono.Parallel/Mono.Threading.Tasks/MonoTaskExtensions.cs [new file with mode: 0644]
mcs/class/Mono.Parallel/Test/Mono.Threading.Tasks/MonoTaskExtensionsTests.cs [new file with mode: 0644]

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 (file)
index 0000000..24650d0
--- /dev/null
@@ -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&lt;System.Threading.Tasks.Task&gt; childWorkAdder);" />
+      <MemberSignature Language="ILAsm" Value=".method public static hidebysig void Execute(class System.Threading.Tasks.Task task, class System.Action`1&lt;class System.Threading.Tasks.Task&gt; 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&lt;System.Threading.Tasks.Task&gt;" />
+      </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&lt;Task&gt; deque = new CyclicDeque&lt;Task&gt; ();
+
+       if (deque.PopBottom (out task) == PopResult.Succeed))
+               task.Execute (deque.PushBottom);
+  </code>
+</example></remarks>
+      </Docs>
+    </Member>
+  </Members>
+</Type>
index 6739a805613cd76aea79f0b047bf833dedb05a3d..089df754a578932ea6ca693fe6c553886ab59a9d 100644 (file)
     <Namespace Name="Mono.Threading.Tasks">
       <Type Name="CyclicDeque`1" DisplayName="CyclicDeque&lt;T&gt;" Kind="Class" />
       <Type Name="IConcurrentDeque`1" DisplayName="IConcurrentDeque&lt;T&gt;" 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&lt;System.Threading.Tasks.Task&gt; childWorkAdder);" />
+        <MemberSignature Language="ILAsm" Value=".method public static hidebysig void Execute(class System.Threading.Tasks.Task task, class System.Action`1&lt;class System.Threading.Tasks.Task&gt; 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&lt;System.Threading.Tasks.Task&gt;" />
+        </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>
index 32f76c531c047bfa93fdabaa6e3bd947e9c3a6de..d20194b5ff59c52236df45f15766433d7dccd238 100644 (file)
@@ -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
index 691336cf4e82c625118b6fd5dc6ca895291dfd00..84f989278a74b020e7303ca2554651394dd282d3 100644 (file)
@@ -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
index d1ba72de323f9f14139f8cac3a25e63ed027960c..f7386d2ef701a9cb19e49a26c80b347110fd59a2 100644 (file)
@@ -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 (file)
index 0000000..d1e6bff
--- /dev/null
@@ -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 (file)
index 0000000..62f939b
--- /dev/null
@@ -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