| | | 1 | | //======================================================================= |
| | | 2 | | // NavigationWorldGraphLease.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2024-present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using System; |
| | | 9 | | using System.Threading; |
| | | 10 | | |
| | | 11 | | namespace Trailblazer.Pathing; |
| | | 12 | | |
| | | 13 | | /// <summary>Owns one exact immutable graph generation checkout.</summary> |
| | | 14 | | internal sealed class NavigationWorldGraphLease : IDisposable |
| | | 15 | | { |
| | | 16 | | private NavigationWorldGraphStore? _owner; |
| | | 17 | | private NavigationWorldGraph? _graph; |
| | | 18 | | |
| | 1198 | 19 | | internal NavigationWorldGraphLease( |
| | 1198 | 20 | | NavigationWorldGraphStore owner, |
| | 1198 | 21 | | NavigationWorldGraph graph) |
| | | 22 | | { |
| | 1198 | 23 | | _owner = owner; |
| | 1198 | 24 | | _graph = graph; |
| | 1198 | 25 | | graph.Checkout(); |
| | 1198 | 26 | | } |
| | | 27 | | |
| | 52811 | 28 | | internal NavigationWorldGraph Graph => Volatile.Read(ref _graph)!; |
| | | 29 | | |
| | | 30 | | internal void Reinitialize(NavigationWorldGraphStore owner, NavigationWorldGraph graph) |
| | | 31 | | { |
| | 8223 | 32 | | _graph = graph; |
| | 8223 | 33 | | Volatile.Write(ref _owner, owner); |
| | 8223 | 34 | | graph.Checkout(); |
| | 8223 | 35 | | } |
| | | 36 | | |
| | 9420 | 37 | | internal NavigationWorldGraph DetachGraph() => Interlocked.Exchange(ref _graph, null)!; |
| | | 38 | | |
| | | 39 | | public void Dispose() |
| | | 40 | | { |
| | 9425 | 41 | | NavigationWorldGraphStore? owner = Interlocked.Exchange(ref _owner, null); |
| | 9425 | 42 | | owner?.Return(this); |
| | 9420 | 43 | | } |
| | | 44 | | } |