镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/workflow/building/libraries/webassembly-instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ The libraries build contains some native code. This includes shims over libc, op
./build.sh libs.native -os browser -c Debug|Release
```

For multithreaded Browser builds, pass `/p:WasmEnableThreads=true` to both runtime and
library builds. This enables `-pthread` for native library compilation and linking,
including minipal. Do not mix these binaries with a single-threaded build.

## Building individual libraries

Individual projects and libraries can be build by specifying the build configuration.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@
<ProjectReference Include="MTAMain\MTAMain.csproj" />
<ProjectReference Include="DefaultApartmentStateMain\DefaultApartmentStateMain.csproj" />
</ItemGroup>
<ItemGroup>
<Compile Include="$(CommonPath)Interop\Unix\Interop.Libraries.cs" />
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.Threading.cs" />
</ItemGroup>
</Project>
47 changes: 47 additions & 0 deletions src/libraries/System.Threading.Thread/tests/ThreadTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,53 @@ public static class ThreadTests
private static object t_threadStaticReference;
private static int s_globalStaticValue;

[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsMultithreadingSupported))]
[PlatformSpecific(TestPlatforms.AnyUnix | TestPlatforms.Browser)]
[InlineData(1)]
[InlineData(4)]
public static void NativeThreadIds_AreDistinctAndStable(int workerCount)
{
using var ready = new CountdownEvent(workerCount);
using var release = new ManualResetEventSlim();
ulong currentThreadId = Interop.Sys.GetCurrentThreadId();
ulong[] threadIds = new ulong[workerCount];
Action[] waitForThreads = new Action[workerCount];

for (int i = 0; i < workerCount; i++)
{
int index = i;
Thread thread = ThreadTestHelpers.CreateGuardedThread(out waitForThreads[i], () =>
{
threadIds[index] = Interop.Sys.GetCurrentThreadId();
ready.Signal();
Assert.True(release.Wait(UnexpectedTimeoutMilliseconds));
Assert.Equal(threadIds[index], Interop.Sys.GetCurrentThreadId());
});
thread.IsBackground = true;
thread.Start();
}

try
{
Assert.True(ready.Wait(UnexpectedTimeoutMilliseconds));
var uniqueIds = new HashSet<ulong> { currentThreadId };
Assert.NotEqual(0UL, currentThreadId);
foreach (ulong threadId in threadIds)
{
Assert.NotEqual(0UL, threadId);
Assert.True(uniqueIds.Add(threadId), $"Duplicate native thread ID: {threadId}");
}
}
finally
{
release.Set();
foreach (Action waitForThread in waitForThreads)
{
waitForThread();
}
}
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsMultithreadingSupported))]
public static void ConstructorTest()
{
Expand Down
1 change: 1 addition & 0 deletions src/libraries/tests.proj
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,7 @@
<SmokeTestProject Remove="@(SmokeTestProject)" />
<SmokeTestProject Include="$(MonoProjectRoot)sample\wasm\browser-threads\Wasm.Browser.Threads.Sample.csproj" />
<SmokeTestProject Include="$(MSBuildThisFileDirectory)Common/tests/Common.Tests.csproj" />
<SmokeTestProject Include="$(MSBuildThisFileDirectory)System.Threading.Thread\tests\System.Threading.Thread.Tests.csproj" />
<SmokeTestProject Include="$(MSBuildThisFileDirectory)System.Runtime.InteropServices.JavaScript\tests\System.Runtime.InteropServices.JavaScript.UnitTests\System.Runtime.InteropServices.JavaScript.Tests.csproj" />
<SmokeTestProject Include="$(MSBuildThisFileDirectory)System.Net.WebSockets.Client\tests\System.Net.WebSockets.Client.Tests.csproj" />
<SmokeTestProject Include="$(MSBuildThisFileDirectory)System.Net.Http\tests\FunctionalTests\System.Net.Http.Functional.Tests.csproj" />
Expand Down
5 changes: 5 additions & 0 deletions src/native/libs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ if ("${CMAKE_CURRENT_SOURCE_DIR}" STREQUAL "${CMAKE_SOURCE_DIR}")
include(../../../eng/native/configurepaths.cmake)
include(${CLR_ENG_NATIVE_DIR}/configurecompiler.cmake)

if (CLR_CMAKE_TARGET_BROWSER AND CLR_CMAKE_WASM_ENABLE_THREADS)
add_compile_options(-pthread)
add_link_options(-pthread)
endif ()

add_subdirectory(${CLR_SRC_NATIVE_DIR}/minipal minipal)
endif()

Expand Down
Loading