|
@@ -1,18 +1,21 @@
|
|
|
|
|
+using Microsoft.Win32.SafeHandles;
|
|
|
using Ryujinx.Common.Logging;
|
|
using Ryujinx.Common.Logging;
|
|
|
using System;
|
|
using System;
|
|
|
using System.IO;
|
|
using System.IO;
|
|
|
using System.Runtime.InteropServices;
|
|
using System.Runtime.InteropServices;
|
|
|
using System.Runtime.Versioning;
|
|
using System.Runtime.Versioning;
|
|
|
using System.Threading;
|
|
using System.Threading;
|
|
|
|
|
+using System.Threading.Tasks;
|
|
|
|
|
|
|
|
namespace Ryujinx.Common.SystemInterop
|
|
namespace Ryujinx.Common.SystemInterop
|
|
|
{
|
|
{
|
|
|
public partial class StdErrAdapter : IDisposable
|
|
public partial class StdErrAdapter : IDisposable
|
|
|
{
|
|
{
|
|
|
private bool _disposable = false;
|
|
private bool _disposable = false;
|
|
|
- private UnixStream _pipeReader;
|
|
|
|
|
- private UnixStream _pipeWriter;
|
|
|
|
|
- private Thread _worker;
|
|
|
|
|
|
|
+ private Stream _pipeReader;
|
|
|
|
|
+ private Stream _pipeWriter;
|
|
|
|
|
+ private CancellationTokenSource _cancellationTokenSource;
|
|
|
|
|
+ private Task _worker;
|
|
|
|
|
|
|
|
public StdErrAdapter()
|
|
public StdErrAdapter()
|
|
|
{
|
|
{
|
|
@@ -31,37 +34,39 @@ namespace Ryujinx.Common.SystemInterop
|
|
|
(int readFd, int writeFd) = MakePipe();
|
|
(int readFd, int writeFd) = MakePipe();
|
|
|
dup2(writeFd, stdErrFileno);
|
|
dup2(writeFd, stdErrFileno);
|
|
|
|
|
|
|
|
- _pipeReader = new UnixStream(readFd);
|
|
|
|
|
- _pipeWriter = new UnixStream(writeFd);
|
|
|
|
|
|
|
+ _pipeReader = CreateFileDescriptorStream(readFd);
|
|
|
|
|
+ _pipeWriter = CreateFileDescriptorStream(writeFd);
|
|
|
|
|
|
|
|
- _worker = new Thread(EventWorker);
|
|
|
|
|
|
|
+ _cancellationTokenSource = new CancellationTokenSource();
|
|
|
|
|
+ _worker = Task.Run(async () => await EventWorkerAsync(_cancellationTokenSource.Token), _cancellationTokenSource.Token);
|
|
|
_disposable = true;
|
|
_disposable = true;
|
|
|
- _worker.Start();
|
|
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
[SupportedOSPlatform("linux")]
|
|
[SupportedOSPlatform("linux")]
|
|
|
[SupportedOSPlatform("macos")]
|
|
[SupportedOSPlatform("macos")]
|
|
|
- private void EventWorker()
|
|
|
|
|
|
|
+ private async Task EventWorkerAsync(CancellationToken cancellationToken)
|
|
|
{
|
|
{
|
|
|
- TextReader reader = new StreamReader(_pipeReader);
|
|
|
|
|
|
|
+ using TextReader reader = new StreamReader(_pipeReader, leaveOpen: true);
|
|
|
string line;
|
|
string line;
|
|
|
- while ((line = reader.ReadLine()) != null)
|
|
|
|
|
|
|
+ while (cancellationToken.IsCancellationRequested == false && (line = await reader.ReadLineAsync(cancellationToken)) != null)
|
|
|
{
|
|
{
|
|
|
Logger.Error?.PrintRawMsg(line);
|
|
Logger.Error?.PrintRawMsg(line);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
private void Dispose(bool disposing)
|
|
private void Dispose(bool disposing)
|
|
|
{
|
|
{
|
|
|
if (_disposable)
|
|
if (_disposable)
|
|
|
{
|
|
{
|
|
|
|
|
+ _disposable = false;
|
|
|
|
|
+
|
|
|
if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
|
|
if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
|
|
|
{
|
|
{
|
|
|
|
|
+ _cancellationTokenSource.Cancel();
|
|
|
|
|
+ _worker.Wait(0);
|
|
|
_pipeReader?.Close();
|
|
_pipeReader?.Close();
|
|
|
_pipeWriter?.Close();
|
|
_pipeWriter?.Close();
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- _disposable = false;
|
|
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -74,11 +79,11 @@ namespace Ryujinx.Common.SystemInterop
|
|
|
private static partial int dup2(int fd, int fd2);
|
|
private static partial int dup2(int fd, int fd2);
|
|
|
|
|
|
|
|
[LibraryImport("libc", SetLastError = true)]
|
|
[LibraryImport("libc", SetLastError = true)]
|
|
|
- private static unsafe partial int pipe(int* pipefd);
|
|
|
|
|
|
|
+ private static partial int pipe(Span<int> pipefd);
|
|
|
|
|
|
|
|
- private static unsafe (int, int) MakePipe()
|
|
|
|
|
|
|
+ private static (int, int) MakePipe()
|
|
|
{
|
|
{
|
|
|
- int *pipefd = stackalloc int[2];
|
|
|
|
|
|
|
+ Span<int> pipefd = stackalloc int[2];
|
|
|
|
|
|
|
|
if (pipe(pipefd) == 0)
|
|
if (pipe(pipefd) == 0)
|
|
|
{
|
|
{
|
|
@@ -89,5 +94,16 @@ namespace Ryujinx.Common.SystemInterop
|
|
|
throw new();
|
|
throw new();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ [SupportedOSPlatform("linux")]
|
|
|
|
|
+ [SupportedOSPlatform("macos")]
|
|
|
|
|
+ private static Stream CreateFileDescriptorStream(int fd)
|
|
|
|
|
+ {
|
|
|
|
|
+ return new FileStream(
|
|
|
|
|
+ new SafeFileHandle((IntPtr)fd, ownsHandle: true),
|
|
|
|
|
+ FileAccess.ReadWrite
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|