Skip to content

Commit

Permalink
[browser][MT] enable MT PLINQ (#94214)
Browse files Browse the repository at this point in the history
* wip

* keep ActiveIssue #91579

* more

* more

* more

* more

* feedback
  • Loading branch information
pavelsavara committed Nov 6, 2023
1 parent f609282 commit c4d5505
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<UseCompilerGeneratedDocXmlFile>false</UseCompilerGeneratedDocXmlFile>
</PropertyGroup>

<PropertyGroup>
<TargetPlatformIdentifier>$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)'))</TargetPlatformIdentifier>
<FeatureWasmThreads Condition="'$(TargetPlatformIdentifier)' == 'browser' and '$(MonoWasmBuildVariant)' == 'multithread'">true</FeatureWasmThreads>
<DefineConstants Condition="'$(FeatureWasmThreads)' == 'true'" >$(DefineConstants);FEATURE_WASM_THREADS</DefineConstants>
</PropertyGroup>
<!-- Compiled Source Files -->
<ItemGroup>
<Compile Include="System\Linq\Parallel\Channels\AsynchronousChannel.cs" />
<Compile Include="System\Linq\Parallel\Channels\SynchronousChannel.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,12 @@ public static class ParallelEnumerable

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

//-----------------------------------------------------------------------------------
// Converts any IEnumerable<TSource> into something that can be the target of parallel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<UseCompilerGeneratedDocXmlFile>false</UseCompilerGeneratedDocXmlFile>
</PropertyGroup>

<PropertyGroup>
<TargetPlatformIdentifier>$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)'))</TargetPlatformIdentifier>
<FeatureWasmThreads Condition="'$(TargetPlatformIdentifier)' == 'browser' and '$(MonoWasmBuildVariant)' == 'multithread'">true</FeatureWasmThreads>
<DefineConstants Condition="'$(FeatureWasmThreads)' == 'true'" >$(DefineConstants);FEATURE_WASM_THREADS</DefineConstants>
</PropertyGroup>
<ItemGroup>
<Compile Include="System\Threading\Tasks\Parallel.cs" />
<Compile Include="System\Threading\Tasks\Parallel.ForEachAsync.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,12 @@ public static void Invoke(ParallelOptions parallelOptions, params Action[] actio
{
// If we've gotten this far, it's time to process the actions.

#if !FEATURE_WASM_THREADS
// Web browsers need special treatment that is implemented in TaskReplicator
if (OperatingSystem.IsBrowser() ||
#else
if (
#endif
// This is more efficient for a large number of actions, or for enforcing MaxDegreeOfParallelism:
(actionsCopy.Length > SMALL_ACTIONCOUNT_LIMIT) ||
(parallelOptions.MaxDegreeOfParallelism != -1 && parallelOptions.MaxDegreeOfParallelism < actionsCopy.Length)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ public static void Run<TState>(ReplicatableUserAction<TState> action, ParallelOp
{
// Browser hosts do not support synchronous Wait so we want to run the
// replicated task directly instead of going through Task infrastructure
#if !FEATURE_WASM_THREADS
if (OperatingSystem.IsBrowser())
{
// Since we are running on a single thread, we don't want the action to time out
Expand All @@ -142,6 +143,7 @@ public static void Run<TState>(ReplicatableUserAction<TState> action, ParallelOp
throw new Exception("Replicated tasks cannot yield in this single-threaded browser environment");
}
else
#endif
{
int maxConcurrencyLevel = (options.EffectiveMaxConcurrencyLevel > 0) ? options.EffectiveMaxConcurrencyLevel : int.MaxValue;

Expand Down
Loading

0 comments on commit c4d5505

Please sign in to comment.