using System.Collections.Generic; namespace Avalonia.Threading { internal class ThreadSafeObjectPool where T : class, new() { private readonly Stack _stack = new Stack(); public static ThreadSafeObjectPool Default { get; } = new ThreadSafeObjectPool(); public T Get() { lock (_stack) { if(_stack.Count == 0) return new T(); return _stack.Pop(); } } public void ReturnAndSetNull(ref T? obj) { if (obj == null) return; lock (_stack) { _stack.Push(obj); obj = null; } } } }