Skip to content

Commit

Permalink
Makes struct from Owned
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed Apr 11, 2024
1 parent a8d5f92 commit 4d527e3
Showing 1 changed file with 15 additions and 19 deletions.
34 changes: 15 additions & 19 deletions src/Pure.DI.Core/Components/Api.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1102,9 +1102,9 @@ public interface IOwned : global::System.IDisposable
[global::System.Diagnostics.DebuggerTypeProxy(typeof(global::Pure.DI.Owned.DebugView))]
internal partial struct Owned : global::Pure.DI.IOwned
{
private volatile object _lockObject;
private const int InitialSize = 8;
private int _count;
private global::System.IDisposable[] _disposables;
private volatile global::System.IDisposable[] _disposables;

/// <summary>
/// Adds a disposable instance.
Expand All @@ -1118,26 +1118,22 @@ public void Add(global::System.IDisposable disposable)
return;
}

if (_lockObject == null)
if (_disposables == null)
{
while (global::System.Threading.Interlocked.CompareExchange(ref _lockObject, new object(), null) == null) ;
global::System.Threading.Interlocked.CompareExchange(
ref _disposables,
new global::System.IDisposable[InitialSize],
null);
}

lock (_lockObject)
lock (_disposables)
{
if (_count == 0)
var size = _disposables.Length;
if (size == _count)
{
_disposables = new global::System.IDisposable[8];
}
else
{
var size = _disposables.Length;
if (size == _count)
{
var disposables = new global::System.IDisposable[size << 1];
global::System.Array.Copy(_disposables, disposables, size);
_disposables = disposables;
}
var disposables = new global::System.IDisposable[size << 1];
global::System.Array.Copy(_disposables, disposables, size);
_disposables = disposables;
}

_disposables[_count++] = disposable;
Expand All @@ -1148,14 +1144,14 @@ public void Add(global::System.IDisposable disposable)
[global::System.Runtime.CompilerServices.MethodImpl((global::System.Runtime.CompilerServices.MethodImplOptions)512)]
public void Dispose()
{
if (_lockObject == null)
if (_disposables == null)
{
return;
}

global::System.IDisposable[] disposables;
int count;
lock (_lockObject)
lock (_disposables)
{
disposables = new global::System.IDisposable[_count];
global::System.Array.Copy(_disposables, disposables, _count);
Expand Down

0 comments on commit 4d527e3

Please sign in to comment.