Skip to content
Closed
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
141 changes: 141 additions & 0 deletions DiskInfoToolkit/Core/Windows/IoControlRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* Copyright (c) 2026 Florian K.
*/

using Microsoft.Win32.SafeHandles;
using OS = BlackSharp.Core.Platform.OperatingSystem;

namespace DiskInfoToolkit.Core.Windows
{
internal sealed class IoControlRequest : IDisposable
{
#region Constructor

public IoControlRequest(SafeFileHandle handle, uint ioControlCode, byte[] inBuffer, byte[] outBuffer)
{
if (!OS.IsWindows())
{
throw new PlatformNotSupportedException();
}

Handle = handle;
IoControlCode = ioControlCode;
InBuffer = inBuffer;
OutBuffer = outBuffer;
}

#endregion

#region Fields

private readonly ManualResetEvent _completed = new ManualResetEvent(false);

private readonly object _syncRoot = new object();

private bool _completedState;

private bool _timedOut;

private bool _disposed;

#endregion

#region Properties

public SafeFileHandle Handle { get; }

public uint IoControlCode { get; }

public byte[] InBuffer { get; }

public byte[] OutBuffer { get; }

public bool Succeeded { get; private set; }

public int LastError { get; private set; }

public int BytesReturned { get; private set; }

public Exception Exception { get; private set; }

#endregion

#region Public

public bool Wait(int timeoutMilliseconds)
{
return _completed.WaitOne(timeoutMilliseconds);
}

public bool MarkTimedOut()
{
lock (_syncRoot)
{
if (_completedState)
{
return false;
}

_timedOut = true;
return true;
}
}

public void Complete(bool succeeded, int lastError, int bytesReturned, Exception exception)
{
bool shouldDispose = false;

lock (_syncRoot)
{
if (_timedOut)
{
return;
}

Succeeded = succeeded;
LastError = lastError;
BytesReturned = bytesReturned;
Exception = exception;
_completedState = true;

if (_disposed)
{
shouldDispose = true;
}
else
{
_completed.Set();
}
}

if (shouldDispose)
{
_completed.Dispose();
}
}

public void Dispose()
{
lock (_syncRoot)
{
if (_disposed)
{
return;
}

_disposed = true;

if (_completedState)
{
_completed.Dispose();
}
}
}

#endregion
}
}
160 changes: 160 additions & 0 deletions DiskInfoToolkit/Core/Windows/IoControlWorker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* Copyright (c) 2026 Florian K.
*/

using OS = BlackSharp.Core.Platform.OperatingSystem;

namespace DiskInfoToolkit.Core.Windows
{
internal sealed class IoControlWorker
{
#region Constructor

public IoControlWorker(int id)
{
if (!OS.IsWindows())
{
throw new PlatformNotSupportedException();
}

ID = id;

_thread = new Thread(ProcessRequests)
{
IsBackground = true,
Name = $"{nameof(WindowsStorageIoControl)}.{nameof(IoControlWorker)}"
};

_thread.Start();
}

#endregion

#region Fields

private readonly object _syncRoot = new object();

private readonly AutoResetEvent _requestAvailable = new AutoResetEvent(false);

private readonly Thread _thread;

private IoControlRequest _request;

private bool _abandoned;

#endregion

#region Properties

public int ID { get; }

public bool CanAcceptWork
{
get
{
lock (_syncRoot)
{
return !_abandoned;
}
}
}

#endregion

#region Public

public bool TryEnqueue(IoControlRequest request)
{
lock (_syncRoot)
{
if (_abandoned || _request != null)
{
return false;
}

_request = request;
_requestAvailable.Set();

return true;
}
}

public void MarkAbandoned()
{
lock (_syncRoot)
{
_abandoned = true;
}
}

#endregion

#region Private

private void ProcessRequests()
{
try
{
while (true)
{
_requestAvailable.WaitOne();

IoControlRequest request;
lock (_syncRoot)
{
request = _request;
_request = null;
}

if (request != null)
{
ProcessRequest(request);
}

lock (_syncRoot)
{
if (_abandoned)
{
break;
}
}
}
}
finally
{
_requestAvailable.Dispose();
}
}

private static void ProcessRequest(IoControlRequest request)
{
bool success = false;
int lastError = 0;
int bytesReturned = 0;
Exception exception = null;

try
{
success = WindowsStorageIoControl.SendRawIoControlCore(
request.Handle,
request.IoControlCode,
request.InBuffer,
request.OutBuffer,
out bytesReturned,
out lastError);
}
catch (Exception ex)
{
exception = ex;
}

request.Complete(success, lastError, bytesReturned, exception);
}

#endregion
}
}
Loading