Skip to content

Commit

Permalink
[release/7.0-rc1] Omit the JS sync context if threads are not enabled (
Browse files Browse the repository at this point in the history
…#74236)

* Omit the JS sync context if threads are not enabled

* define FEATURE_WASM_THREADS if threads are enabled

Co-authored-by: Katelyn Gadd <kg@luminance.org>
Co-authored-by: Aleksey Kliger <aleksey@lambdageek.org>
  • Loading branch information
3 people committed Aug 22, 2022
1 parent 754014e commit de3cd20
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
<Nullable>enable</Nullable>
</PropertyGroup>

<PropertyGroup>
<FeatureWasmThreads Condition="'$(TargetOS)' == 'browser' and '$(WasmEnableThreads)' == 'true'">true</FeatureWasmThreads>
</PropertyGroup>

<PropertyGroup>
<DefineConstants Condition="'$(FeatureWasmThreads)' == 'true'" >$(DefineConstants);FEATURE_WASM_THREADS</DefineConstants>
</PropertyGroup>

<!-- DesignTimeBuild requires all the TargetFramework Derived Properties to not be present in the first property group. -->
<PropertyGroup>
<TargetPlatformIdentifier>$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)'))</TargetPlatformIdentifier>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ public static void CompleteTask(JSMarshalerArgument* arguments_buffer)
}
}

#if FEATURE_WASM_THREADS

[MethodImpl(MethodImplOptions.NoInlining)] // https://github.com/dotnet/runtime/issues/71425
// the marshaled signature is:
// void InstallSynchronizationContext()
Expand All @@ -210,6 +212,8 @@ public static void InstallSynchronizationContext (JSMarshalerArgument* arguments
}
}

#endif

[MethodImpl(MethodImplOptions.NoInlining)] // https://github.com/dotnet/runtime/issues/71425
public static void StopProfile()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#if FEATURE_WASM_THREADS

using System;
using System.Threading;
using System.Threading.Channels;
Expand Down Expand Up @@ -141,3 +143,5 @@ private void Pump () {
}
}
}

#endif
31 changes: 17 additions & 14 deletions src/mono/wasm/runtime/managed-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ export function init_managed_exports(): void {
if (!runtimeHelpers.runtime_interop_exports_class)
throw "Can't find " + runtimeHelpers.runtime_interop_namespace + "." + runtimeHelpers.runtime_interop_exports_classname + " class";

const install_sync_context = get_method("InstallSynchronizationContext");
mono_assert(install_sync_context, "Can't find InstallSynchronizationContext method");
const install_sync_context = cwraps.mono_wasm_assembly_find_method(runtimeHelpers.runtime_interop_exports_class, "InstallSynchronizationContext", -1);
// mono_assert(install_sync_context, "Can't find InstallSynchronizationContext method");
const call_entry_point = get_method("CallEntrypoint");
mono_assert(call_entry_point, "Can't find CallEntrypoint method");
const release_js_owned_object_by_gc_handle_method = get_method("ReleaseJSOwnedObjectByGCHandle");
Expand Down Expand Up @@ -134,19 +134,22 @@ export function init_managed_exports(): void {
anyModule.stackRestore(sp);
}
};
runtimeHelpers.javaScriptExports.install_synchronization_context = () => {
const sp = anyModule.stackSave();
try {
const args = alloc_stack_frame(2);
invoke_method_and_handle_exception(install_sync_context, args);
} finally {
anyModule.stackRestore(sp);
}
};

if (!ENVIRONMENT_IS_PTHREAD)
// Install our sync context so that async continuations will migrate back to this thread (the main thread) automatically
runtimeHelpers.javaScriptExports.install_synchronization_context();
if (install_sync_context) {
runtimeHelpers.javaScriptExports.install_synchronization_context = () => {
const sp = anyModule.stackSave();
try {
const args = alloc_stack_frame(2);
invoke_method_and_handle_exception(install_sync_context, args);
} finally {
anyModule.stackRestore(sp);
}
};

if (!ENVIRONMENT_IS_PTHREAD)
// Install our sync context so that async continuations will migrate back to this thread (the main thread) automatically
runtimeHelpers.javaScriptExports.install_synchronization_context();
}
}

export function get_method(method_name: string): MonoMethod {
Expand Down

0 comments on commit de3cd20

Please sign in to comment.