-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable sending activation to dispatch queue threads (#102887)
* Enable sending activation to dispatch queue threads Until recently, it was not possible to send activation signal to Apple dispatch queue threads using pthread_kill. So in case a .NET code was running on such a thread, it could end up blocking runtime suspension in some cases. Recently, Apple has implemented a new API to enable sending specific signals to the dispatch queues, the dispatch_allow_send_signals. This change adds a call to that API to the PAL initialization code. * Workaround until CI can switch to newer xcode * Add unmasking activation signal on foreign threads The added coreclr test has revealed that while the signal could be sent now, the inject_activation_handler was not being invoked on the dispatch queue thread. The problem was that all signals are masked by default there. * Add test to verify the fix The test verifies that .NET code running on a dispatch queue thread in a tight loop can get the activation injected. * Fix build break * The test should be Apple only * Build the native part of the test on Apple only * Fix reverse polarity assert in nativeaot * Change usage of libdispatch to libSystem * Make sure the testing is performed only on OS that supports the new API * Add RequiresProcessIsolation for CLRTestTargetUnsupported
- Loading branch information
Showing
8 changed files
with
206 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# The test is valid only on Apple | ||
if (CLR_CMAKE_TARGET_APPLE) | ||
include_directories(${INC_PLATFORM_DIR}) | ||
|
||
add_library(nativetest102887 SHARED nativetest102887.cpp) | ||
|
||
# add the install targets | ||
install (TARGETS nativetest102887 DESTINATION bin) | ||
endif () |
27 changes: 27 additions & 0 deletions
27
src/tests/Regressions/coreclr/GitHub_102887/nativetest102887.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
#include <platformdefines.h> | ||
|
||
#include <dlfcn.h> | ||
#include <dispatch/dispatch.h> | ||
#include <dispatch/queue.h> | ||
|
||
extern "C" DLL_EXPORT void StartDispatchQueueThread(void (*work)(void* args)) | ||
{ | ||
dispatch_queue_global_t q = dispatch_get_global_queue(QOS_CLASS_UTILITY, 0); | ||
dispatch_async_f(q, NULL, work); | ||
} | ||
|
||
extern "C" DLL_EXPORT bool SupportsSendingSignalsToDispatchQueueThread() | ||
{ | ||
void *libSystem = dlopen("/usr/lib/libSystem.dylib", RTLD_LAZY); | ||
bool result = false; | ||
if (libSystem != NULL) | ||
{ | ||
int (*dispatch_allow_send_signals_ptr)(int) = (int (*)(int))dlsym(libSystem, "dispatch_allow_send_signals"); | ||
result = (dispatch_allow_send_signals_ptr != NULL); | ||
} | ||
|
||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
using System; | ||
using System.Runtime.InteropServices; | ||
using System.Threading; | ||
using Xunit; | ||
|
||
public class Test102887 | ||
{ | ||
delegate void DispatchQueueWork(IntPtr args); | ||
[DllImport("nativetest102887")] | ||
private static extern void StartDispatchQueueThread(DispatchQueueWork start); | ||
|
||
[DllImport("nativetest102887")] | ||
[return: MarshalAs(UnmanagedType.Bool)] | ||
private static extern bool SupportsSendingSignalsToDispatchQueueThread(); | ||
|
||
static volatile int s_cnt; | ||
static ManualResetEvent s_workStarted = new ManualResetEvent(false); | ||
|
||
private static void RunOnDispatchQueueThread(IntPtr args) | ||
{ | ||
s_workStarted.Set(); | ||
while (true) | ||
{ | ||
s_cnt++; | ||
} | ||
} | ||
|
||
[Fact] | ||
public static void TestEntryPoint() | ||
{ | ||
// Skip the test if the current OS doesn't support sending signals to the dispatch queue threads | ||
if (SupportsSendingSignalsToDispatchQueueThread()) | ||
{ | ||
Console.WriteLine("Sending signals to dispatch queue thread is supported, testing it now"); | ||
StartDispatchQueueThread(RunOnDispatchQueueThread); | ||
s_workStarted.WaitOne(); | ||
|
||
for (int i = 0; i < 100; i++) | ||
{ | ||
GC.Collect(2); | ||
} | ||
} | ||
} | ||
} | ||
|
17 changes: 17 additions & 0 deletions
17
src/tests/Regressions/coreclr/GitHub_102887/test102887.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<!-- Needed for CLRTestTargetUnsupported --> | ||
<RequiresProcessIsolation>true</RequiresProcessIsolation> | ||
<CLRTestTargetUnsupported Condition="'$(TargetsOSX)' != 'true' and '$(TargetsAppleMobile)' != 'true'">true</CLRTestTargetUnsupported> | ||
<CLRTestPriority>1</CLRTestPriority> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Compile Include="test102887.cs" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="$(TestSourceDir)Common/CoreCLRTestLibrary/CoreCLRTestLibrary.csproj" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<CMakeProjectReference Include="CMakeLists.txt" /> | ||
</ItemGroup> | ||
</Project> |