Skip to content

Commit

Permalink
Remove redundant P/Invoke-s in S.D.Process on Apple platforms (#54273)
Browse files Browse the repository at this point in the history
* Remove P/Invoke to SystemNative_ConfigureTerminalForChildProcess which doesn't exist on Apple platforms

* Address the feedback based on the ifdef approach

* Add IsiOSLike property

* Use a partial method approach

* Address the feedback
  • Loading branch information
MaximLipnin authored Jun 21, 2021
1 parent 30f4269 commit f891033
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
<PropertyGroup>
<GeneratePlatformNotSupportedAssemblyMessage Condition="'$(TargetsAnyOS)' == 'true'">SR.Process_PlatformNotSupported</GeneratePlatformNotSupportedAssemblyMessage>
</PropertyGroup>
<PropertyGroup>
<IsiOSLike Condition="'$(TargetsMacCatalyst)' == 'true' or '$(TargetsiOS)' == 'true' or '$(TargetstvOS)' == 'true'">true</IsiOSLike>
</PropertyGroup>
<ItemGroup Condition="'$(TargetsAnyOS)' != 'true'">
<Compile Include="Microsoft\Win32\SafeHandles\SafeProcessHandle.cs" />
<Compile Include="System\Collections\Specialized\DictionaryWrapper.cs" />
Expand Down Expand Up @@ -234,8 +237,6 @@
Link="Common\Interop\Unix\Interop.GetHostName.cs" />
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.SysConf.cs"
Link="Common\Interop\Unix\Interop.SysConf.cs" />
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.ConfigureTerminalForChildProcess.cs"
Link="Common\Interop\Unix\Interop.ConfigureTerminalForChildProcess.cs" />
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.ForkAndExecProcess.cs"
Link="Common\Interop\Unix\Interop.ForkAndExecProcess.cs" />
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.GetGroupList.cs"
Expand Down Expand Up @@ -267,6 +268,11 @@
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.Access.cs"
Link="Common\Interop\Unix\System.Native\Interop.Access.cs" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetsUnix)' == 'true' and '$(IsiOSLike)' != 'true'">
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.ConfigureTerminalForChildProcess.cs"
Link="Common\Interop\Unix\Interop.ConfigureTerminalForChildProcess.cs" />
<Compile Include="System\Diagnostics\Process.Unix.ConfigureTerminalForChildProcesses.cs" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetsLinux)' == 'true'">
<Compile Include="System\Diagnostics\Process.Linux.cs" />
<Compile Include="System\Diagnostics\ProcessManager.Linux.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Threading;

namespace System.Diagnostics
{
public partial class Process
{
private static int s_childrenUsingTerminalCount;

static partial void ConfigureTerminalForChildProcessesInner(int increment)
{
Debug.Assert(increment != 0);

int childrenUsingTerminalRemaining = Interlocked.Add(ref s_childrenUsingTerminalCount, increment);
if (increment > 0)
{
Debug.Assert(s_processStartLock.IsReadLockHeld);

// At least one child is using the terminal.
Interop.Sys.ConfigureTerminalForChildProcess(childUsesTerminal: true);
}
else
{
Debug.Assert(s_processStartLock.IsWriteLockHeld);

if (childrenUsingTerminalRemaining == 0)
{
// No more children are using the terminal.
Interop.Sys.ConfigureTerminalForChildProcess(childUsesTerminal: false);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public partial class Process : IDisposable
private static volatile bool s_initialized;
private static readonly object s_initializedGate = new object();
private static readonly ReaderWriterLockSlim s_processStartLock = new ReaderWriterLockSlim();
private static int s_childrenUsingTerminalCount;

/// <summary>
/// Puts a Process component in state to interact with operating system processes that run in a
Expand Down Expand Up @@ -1051,26 +1050,9 @@ private static void OnSigChild(int reapAll)
/// </summary>
internal static void ConfigureTerminalForChildProcesses(int increment)
{
Debug.Assert(increment != 0);

int childrenUsingTerminalRemaining = Interlocked.Add(ref s_childrenUsingTerminalCount, increment);
if (increment > 0)
{
Debug.Assert(s_processStartLock.IsReadLockHeld);

// At least one child is using the terminal.
Interop.Sys.ConfigureTerminalForChildProcess(childUsesTerminal: true);
}
else
{
Debug.Assert(s_processStartLock.IsWriteLockHeld);

if (childrenUsingTerminalRemaining == 0)
{
// No more children are using the terminal.
Interop.Sys.ConfigureTerminalForChildProcess(childUsesTerminal: false);
}
}
ConfigureTerminalForChildProcessesInner(increment);
}

static partial void ConfigureTerminalForChildProcessesInner(int increment);
}
}

0 comments on commit f891033

Please sign in to comment.