Skip to content

Commit

Permalink
add DefaultGraphStack
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpantyukhin committed Nov 18, 2017
1 parent 25e35d1 commit 9e17ab1
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion TensorFlowSharp/Tensorflow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,36 @@ internal static void CheckSize ()
}
}

internal class DefaultGraphStack
{
[ThreadStatic]
private static IList<TFGraph> stack;
private static Object lockObj;

internal TFGraph Instance => stack.Last();

internal static void SetGraph(TFGraph graph)
{
if (stack == null)
{
lock (lockObj)
{
if (stack == null)
{
stack = new List<TFGraph>();
}
}
}

stack.Add(graph);
}

internal static void RemoveGraph(TFGraph graph)
{
stack.Remove(graph);
}
}

/// <summary>
/// Base class for many TensorFlow data types that provides a common idiom to dispose and
/// release resources associated with the native data types. Generally, you do not need to use this.
Expand Down Expand Up @@ -473,19 +503,21 @@ public partial class TFGraph : TFDisposable
/// <summary>
/// Initializes a new instance of the <see cref="T:TensorFlow.TFGraph"/> class.
/// </summary>
public TFGraph () : base (TF_NewGraph ())
public TFGraph () : this (TF_NewGraph ())
{
}

internal TFGraph (IntPtr handle) : base (handle)
{
DefaultGraphStack.SetGraph(this);
}

// extern void TF_DeleteGraph (TF_Graph *);
[DllImport (NativeBinding.TensorFlowLibrary)]
static extern unsafe void TF_DeleteGraph (TF_Graph graph);
internal override void NativeDispose (IntPtr handle)
{
DefaultGraphStack.RemoveGraph(this);
TF_DeleteGraph (handle);
}

Expand Down

0 comments on commit 9e17ab1

Please sign in to comment.