IVGCVSW-6062 update ArmnnPreparedModels to have a single static instance of the threadpool

Signed-off-by: Finn Williams <Finn.Williams@arm.com>
Change-Id: Ie350a11eab5d677dd6a5571ea4094aa51b23c501
diff --git a/ArmnnPreparedModel_1_3.cpp b/ArmnnPreparedModel_1_3.cpp
index dcac281..16ea113 100644
--- a/ArmnnPreparedModel_1_3.cpp
+++ b/ArmnnPreparedModel_1_3.cpp
@@ -145,6 +145,9 @@
         ArmnnPreparedModel_1_3<HalVersion>::m_RequestThread;
 
 template<typename HalVersion>
+std::unique_ptr<armnn::Threadpool> ArmnnPreparedModel_1_3<HalVersion>::m_Threadpool(nullptr);
+
+template<typename HalVersion>
 template<typename TensorBindingCollection>
 void ArmnnPreparedModel_1_3<HalVersion>::DumpTensorsIfRequired(char const* tensorNamePrefix,
                                                                const TensorBindingCollection& tensorBindings)
@@ -183,7 +186,7 @@
     // Enable profiling if required.
     m_Runtime->GetProfiler(m_NetworkId)->EnableProfiling(m_GpuProfilingEnabled);
 
-    if (asyncModelExecutionEnabled)
+    if (m_AsyncModelExecutionEnabled)
     {
         std::vector<std::shared_ptr<armnn::IWorkingMemHandle>> memHandles;
         for (unsigned int i=0; i < numberOfThreads; ++i)
@@ -191,8 +194,16 @@
             memHandles.emplace_back(m_Runtime->CreateWorkingMemHandle(networkId));
         }
 
+        if (!m_Threadpool)
+        {
+            m_Threadpool = std::make_unique<armnn::Threadpool>(numberOfThreads, runtime, memHandles);
+        }
+        else
+        {
+            m_Threadpool->LoadMemHandles(memHandles);
+        }
+
         m_WorkingMemHandle = memHandles.back();
-        m_Threadpool = std::make_unique<armnn::Threadpool>(numberOfThreads, runtime, memHandles);
     }
 }
 
@@ -205,6 +216,12 @@
     // Unload the network associated with this model.
     m_Runtime->UnloadNetwork(m_NetworkId);
 
+    // Unload the network memhandles from the threadpool
+    if (m_AsyncModelExecutionEnabled)
+    {
+        m_Threadpool->UnloadMemHandles(m_NetworkId);
+    }
+
     // Dump the profiling info to a file if required.
     DumpJsonProfilingIfRequired(m_GpuProfilingEnabled, m_RequestInputsAndOutputsDumpDir, m_NetworkId, profiler.get());
 }