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 35 #define SH_EXPORTING 36 37 #include <assert.h> 38 39 #include "InitializeDll.h" 40 #include "../glslang/Include/InitializeGlobals.h" 41 42 #include "../glslang/Public/ShaderLang.h" 43 44 namespace glslang { 45 46 OS_TLSIndex ThreadInitializeIndex = OS_INVALID_TLS_INDEX; 47 48 bool InitProcess() 49 { 50 glslang::GetGlobalLock(); 51 52 if (ThreadInitializeIndex != OS_INVALID_TLS_INDEX) { 53 // 54 // Function is re-entrant. 55 // 56 57 glslang::ReleaseGlobalLock(); 58 return true; 59 } 60 61 ThreadInitializeIndex = OS_AllocTLSIndex(); 62 63 if (ThreadInitializeIndex == OS_INVALID_TLS_INDEX) { 64 assert(0 && "InitProcess(): Failed to allocate TLS area for init flag"); 65 66 glslang::ReleaseGlobalLock(); 67 return false; 68 } 69 70 if (! InitializePoolIndex()) { 71 assert(0 && "InitProcess(): Failed to initialize global pool"); 72 73 glslang::ReleaseGlobalLock(); 74 return false; 75 } 76 77 if (! InitThread()) { 78 assert(0 && "InitProcess(): Failed to initialize thread"); 79 80 glslang::ReleaseGlobalLock(); 81 return false; 82 } 83 84 glslang::ReleaseGlobalLock(); 85 return true; 86 } 87 88 89 bool InitThread() 90 { 91 // 92 // This function is re-entrant 93 // 94 if (ThreadInitializeIndex == OS_INVALID_TLS_INDEX) { 95 assert(0 && "InitThread(): Process hasn't been initalised."); 96 return false; 97 } 98 99 if (OS_GetTLSValue(ThreadInitializeIndex) != 0) 100 return true; 101 102 InitializeMemoryPools(); 103 104 if (! OS_SetTLSValue(ThreadInitializeIndex, (void *)1)) { 105 assert(0 && "InitThread(): Unable to set init flag."); 106 return false; 107 } 108 109 return true; 110 } 111 112 113 bool DetachThread() 114 { 115 bool success = true; 116 117 if (ThreadInitializeIndex == OS_INVALID_TLS_INDEX) 118 return true; 119 120 // 121 // Function is re-entrant and this thread may not have been initialized. 122 // 123 if (OS_GetTLSValue(ThreadInitializeIndex) != 0) { 124 if (!OS_SetTLSValue(ThreadInitializeIndex, (void *)0)) { 125 assert(0 && "DetachThread(): Unable to clear init flag."); 126 success = false; 127 } 128 129 FreeGlobalPools(); 130 131 } 132 133 return success; 134 } 135 136 bool DetachProcess() 137 { 138 bool success = true; 139 140 if (ThreadInitializeIndex == OS_INVALID_TLS_INDEX) 141 return true; 142 143 ShFinalize(); 144 145 success = DetachThread(); 146 147 FreePoolIndex(); 148 149 OS_FreeTLSIndex(ThreadInitializeIndex); 150 ThreadInitializeIndex = OS_INVALID_TLS_INDEX; 151 152 return success; 153 } 154 155 } // end namespace glslang 156