using System;
using System.Collections.Generic;
using System.Linq;
namespace WhiteMagic.Thread;
///
/// A disposable scope that tracks a set of threads frozen by .
/// Disposing the scope resumes exactly those threads, in reverse order, even if the guarded
/// body throws.
///
public sealed class FrozenThread : IDisposable
{
private readonly IReadOnlyList _threads;
private bool _disposed;
internal FrozenThread(IReadOnlyList threads)
{
_threads = threads ?? throw new ArgumentNullException(nameof(threads));
}
/// The threads suspended by this freeze scope.
public IEnumerable Threads => _threads;
///
/// Resumes the frozen threads in reverse order. The original call is responsible for
/// disposing the instances afterwards.
///
public void Dispose()
{
if (_disposed)
return;
_disposed = true;
foreach (RemoteThread thread in _threads.Reverse())
{
try
{
thread.Resume();
}
catch
{
// Resume-on-dispose is best-effort; callers keep the thread handles so
// they can diagnose or recover separately.
}
}
}
}