From e2c15b4f6e9cf918f148d62b3798f4b45bd8c454 Mon Sep 17 00:00:00 2001 From: John Kessenich Date: Thu, 16 Nov 2017 22:48:41 -0700 Subject: [PATCH] Memory: Reference count number of clients, to support multiple independent clients in same process. Addresses 2nd item in #976. --- StandAlone/StandAlone.cpp | 15 +++++++++------ glslang/MachineIndependent/ShaderLang.cpp | 16 ++++++++++++++++ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/StandAlone/StandAlone.cpp b/StandAlone/StandAlone.cpp index 3abc8b51c..e7844eb21 100644 --- a/StandAlone/StandAlone.cpp +++ b/StandAlone/StandAlone.cpp @@ -1074,21 +1074,24 @@ int singleMain() if (Options & EOptionLinkProgram || Options & EOptionOutputPreprocessed) { glslang::InitializeProcess(); + glslang::InitializeProcess(); // also test reference counting of users + glslang::InitializeProcess(); // also test reference counting of users + glslang::FinalizeProcess(); // also test reference counting of users + glslang::FinalizeProcess(); // also test reference counting of users CompileAndLinkShaderFiles(workList); glslang::FinalizeProcess(); } else { ShInitialize(); + ShInitialize(); // also test reference counting of users + ShFinalize(); // also test reference counting of users bool printShaderNames = workList.size() > 1; - if (Options & EOptionMultiThreaded) - { + if (Options & EOptionMultiThreaded) { std::array threads; - for (unsigned int t = 0; t < threads.size(); ++t) - { + for (unsigned int t = 0; t < threads.size(); ++t) { threads[t] = std::thread(CompileShaders, std::ref(workList)); - if (threads[t].get_id() == std::thread::id()) - { + if (threads[t].get_id() == std::thread::id()) { fprintf(stderr, "Failed to create thread\n"); return EFailThreadCreate; } diff --git a/glslang/MachineIndependent/ShaderLang.cpp b/glslang/MachineIndependent/ShaderLang.cpp index ec5327d9c..62e079e55 100644 --- a/glslang/MachineIndependent/ShaderLang.cpp +++ b/glslang/MachineIndependent/ShaderLang.cpp @@ -69,6 +69,10 @@ namespace { // anonymous namespace for file-local functions and symbols +// Total number of successful initializers of glslang: a refcount +// Shared global; access should be protected by a global mutex/critical section. +int NumberOfClients = 0; + using namespace glslang; // Create a language specific version of parseables. @@ -1193,6 +1197,10 @@ int ShInitialize() if (! InitProcess()) return 0; + glslang::GetGlobalLock(); + ++NumberOfClients; + glslang::ReleaseGlobalLock(); + if (PerProcessGPA == nullptr) PerProcessGPA = new TPoolAllocator(); @@ -1259,6 +1267,14 @@ void ShDestruct(ShHandle handle) // int __fastcall ShFinalize() { + glslang::GetGlobalLock(); + --NumberOfClients; + assert(NumberOfClients >= 0); + bool finalize = NumberOfClients == 0; + glslang::ReleaseGlobalLock(); + if (! finalize) + return 1; + for (int version = 0; version < VersionCount; ++version) { for (int spvVersion = 0; spvVersion < SpvVersionCount; ++spvVersion) { for (int p = 0; p < ProfileCount; ++p) {