Skip to content

Commit c4d5505

Browse files
authored
[browser][MT] enable MT PLINQ (#94214)
* wip * keep ActiveIssue #91579 * more * more * more * more * feedback
1 parent f609282 commit c4d5505

File tree

7 files changed

+21
-59
lines changed

7 files changed

+21
-59
lines changed

src/libraries/System.Linq.Parallel/src/System.Linq.Parallel.csproj

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
66
<UseCompilerGeneratedDocXmlFile>false</UseCompilerGeneratedDocXmlFile>
77
</PropertyGroup>
8-
8+
<PropertyGroup>
9+
<TargetPlatformIdentifier>$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)'))</TargetPlatformIdentifier>
10+
<FeatureWasmThreads Condition="'$(TargetPlatformIdentifier)' == 'browser' and '$(MonoWasmBuildVariant)' == 'multithread'">true</FeatureWasmThreads>
11+
<DefineConstants Condition="'$(FeatureWasmThreads)' == 'true'" >$(DefineConstants);FEATURE_WASM_THREADS</DefineConstants>
12+
</PropertyGroup>
13+
<!-- Compiled Source Files -->
914
<ItemGroup>
1015
<Compile Include="System\Linq\Parallel\Channels\AsynchronousChannel.cs" />
1116
<Compile Include="System\Linq\Parallel\Channels\SynchronousChannel.cs" />

src/libraries/System.Linq.Parallel/src/System/Linq/ParallelEnumerable.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,12 @@ public static class ParallelEnumerable
6464

6565
// When running in single partition mode, PLINQ operations will occur on a single partition and will not
6666
// be executed in parallel, but will retain PLINQ semantics (exceptions wrapped as aggregates, etc).
67+
#if !FEATURE_WASM_THREADS
6768
[System.Runtime.Versioning.SupportedOSPlatformGuard("browser")]
6869
internal static bool SinglePartitionMode => OperatingSystem.IsBrowser();
70+
#else
71+
internal static bool SinglePartitionMode => false;
72+
#endif
6973

7074
//-----------------------------------------------------------------------------------
7175
// Converts any IEnumerable<TSource> into something that can be the target of parallel

src/libraries/System.Threading.Tasks.Parallel/src/System.Threading.Tasks.Parallel.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
66
<UseCompilerGeneratedDocXmlFile>false</UseCompilerGeneratedDocXmlFile>
77
</PropertyGroup>
8-
8+
<PropertyGroup>
9+
<TargetPlatformIdentifier>$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)'))</TargetPlatformIdentifier>
10+
<FeatureWasmThreads Condition="'$(TargetPlatformIdentifier)' == 'browser' and '$(MonoWasmBuildVariant)' == 'multithread'">true</FeatureWasmThreads>
11+
<DefineConstants Condition="'$(FeatureWasmThreads)' == 'true'" >$(DefineConstants);FEATURE_WASM_THREADS</DefineConstants>
12+
</PropertyGroup>
913
<ItemGroup>
1014
<Compile Include="System\Threading\Tasks\Parallel.cs" />
1115
<Compile Include="System\Threading\Tasks\Parallel.ForEachAsync.cs" />

src/libraries/System.Threading.Tasks.Parallel/src/System/Threading/Tasks/Parallel.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,12 @@ public static void Invoke(ParallelOptions parallelOptions, params Action[] actio
238238
{
239239
// If we've gotten this far, it's time to process the actions.
240240

241+
#if !FEATURE_WASM_THREADS
241242
// Web browsers need special treatment that is implemented in TaskReplicator
242243
if (OperatingSystem.IsBrowser() ||
244+
#else
245+
if (
246+
#endif
243247
// This is more efficient for a large number of actions, or for enforcing MaxDegreeOfParallelism:
244248
(actionsCopy.Length > SMALL_ACTIONCOUNT_LIMIT) ||
245249
(parallelOptions.MaxDegreeOfParallelism != -1 && parallelOptions.MaxDegreeOfParallelism < actionsCopy.Length)

src/libraries/System.Threading.Tasks.Parallel/src/System/Threading/Tasks/TaskReplicator.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ public static void Run<TState>(ReplicatableUserAction<TState> action, ParallelOp
131131
{
132132
// Browser hosts do not support synchronous Wait so we want to run the
133133
// replicated task directly instead of going through Task infrastructure
134+
#if !FEATURE_WASM_THREADS
134135
if (OperatingSystem.IsBrowser())
135136
{
136137
// Since we are running on a single thread, we don't want the action to time out
@@ -142,6 +143,7 @@ public static void Run<TState>(ReplicatableUserAction<TState> action, ParallelOp
142143
throw new Exception("Replicated tasks cannot yield in this single-threaded browser environment");
143144
}
144145
else
146+
#endif
145147
{
146148
int maxConcurrencyLevel = (options.EffectiveMaxConcurrencyLevel > 0) ? options.EffectiveMaxConcurrencyLevel : int.MaxValue;
147149

0 commit comments

Comments
 (0)