InitializeDll.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // Copyright (C) 2002-2005 3Dlabs Inc. Ltd.
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions
  7. // are met:
  8. //
  9. // Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. //
  12. // Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following
  14. // disclaimer in the documentation and/or other materials provided
  15. // with the distribution.
  16. //
  17. // Neither the name of 3Dlabs Inc. Ltd. nor the names of its
  18. // contributors may be used to endorse or promote products derived
  19. // from this software without specific prior written permission.
  20. //
  21. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  28. // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  29. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. // POSSIBILITY OF SUCH DAMAGE.
  33. //
  34. #define SH_EXPORTING
  35. #include <cassert>
  36. #include "InitializeDll.h"
  37. #include "../glslang/Include/InitializeGlobals.h"
  38. #include "../glslang/Public/ShaderLang.h"
  39. #include "../glslang/Include/PoolAlloc.h"
  40. namespace glslang {
  41. OS_TLSIndex ThreadInitializeIndex = OS_INVALID_TLS_INDEX;
  42. // Per-process initialization.
  43. // Needs to be called at least once before parsing, etc. is done.
  44. // Will also do thread initialization for the calling thread; other
  45. // threads will need to do that explicitly.
  46. bool InitProcess()
  47. {
  48. glslang::GetGlobalLock();
  49. if (ThreadInitializeIndex != OS_INVALID_TLS_INDEX) {
  50. //
  51. // Function is re-entrant.
  52. //
  53. glslang::ReleaseGlobalLock();
  54. return true;
  55. }
  56. ThreadInitializeIndex = OS_AllocTLSIndex();
  57. if (ThreadInitializeIndex == OS_INVALID_TLS_INDEX) {
  58. assert(0 && "InitProcess(): Failed to allocate TLS area for init flag");
  59. glslang::ReleaseGlobalLock();
  60. return false;
  61. }
  62. if (! InitializePoolIndex()) {
  63. assert(0 && "InitProcess(): Failed to initialize global pool");
  64. glslang::ReleaseGlobalLock();
  65. return false;
  66. }
  67. if (! InitThread()) {
  68. assert(0 && "InitProcess(): Failed to initialize thread");
  69. glslang::ReleaseGlobalLock();
  70. return false;
  71. }
  72. glslang::ReleaseGlobalLock();
  73. return true;
  74. }
  75. // Per-thread scoped initialization.
  76. // Must be called at least once by each new thread sharing the
  77. // symbol tables, etc., needed to parse.
  78. bool InitThread()
  79. {
  80. //
  81. // This function is re-entrant
  82. //
  83. if (ThreadInitializeIndex == OS_INVALID_TLS_INDEX) {
  84. assert(0 && "InitThread(): Process hasn't been initalised.");
  85. return false;
  86. }
  87. if (OS_GetTLSValue(ThreadInitializeIndex) != 0)
  88. return true;
  89. if (! OS_SetTLSValue(ThreadInitializeIndex, (void *)1)) {
  90. assert(0 && "InitThread(): Unable to set init flag.");
  91. return false;
  92. }
  93. glslang::SetThreadPoolAllocator(nullptr);
  94. return true;
  95. }
  96. // Not necessary to call this: InitThread() is reentrant, and the need
  97. // to do per thread tear down has been removed.
  98. //
  99. // This is kept, with memory management removed, to satisfy any exiting
  100. // calls to it that rely on it.
  101. bool DetachThread()
  102. {
  103. bool success = true;
  104. if (ThreadInitializeIndex == OS_INVALID_TLS_INDEX)
  105. return true;
  106. //
  107. // Function is re-entrant and this thread may not have been initialized.
  108. //
  109. if (OS_GetTLSValue(ThreadInitializeIndex) != 0) {
  110. if (!OS_SetTLSValue(ThreadInitializeIndex, (void *)0)) {
  111. assert(0 && "DetachThread(): Unable to clear init flag.");
  112. success = false;
  113. }
  114. }
  115. return success;
  116. }
  117. // Not necessary to call this: InitProcess() is reentrant.
  118. //
  119. // This is kept, with memory management removed, to satisfy any exiting
  120. // calls to it that rely on it.
  121. //
  122. // Users of glslang should call shFinalize() or glslang::FinalizeProcess() for
  123. // process-scoped memory tear down.
  124. bool DetachProcess()
  125. {
  126. bool success = true;
  127. if (ThreadInitializeIndex == OS_INVALID_TLS_INDEX)
  128. return true;
  129. success = DetachThread();
  130. OS_FreeTLSIndex(ThreadInitializeIndex);
  131. ThreadInitializeIndex = OS_INVALID_TLS_INDEX;
  132. return success;
  133. }
  134. } // end namespace glslang