John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 1 | // |
John Kessenich | 927608b | 2017-01-06 12:34:14 -0700 | [diff] [blame] | 2 | // Copyright (C) 2002-2005 3Dlabs Inc. Ltd. |
| 3 | // All rights reserved. |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 4 | // |
John Kessenich | 927608b | 2017-01-06 12:34:14 -0700 | [diff] [blame] | 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions |
| 7 | // are met: |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 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 | // |
John Kessenich | 927608b | 2017-01-06 12:34:14 -0700 | [diff] [blame] | 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. |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 33 | // |
| 34 | |
| 35 | #define SH_EXPORTING |
| 36 | |
John Kessenich | 66ec80e | 2016-08-05 14:04:23 -0600 | [diff] [blame] | 37 | #include <cassert> |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 38 | |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 39 | #include "InitializeDll.h" |
baldurk | 42169c5 | 2015-07-08 15:11:59 +0200 | [diff] [blame] | 40 | #include "../glslang/Include/InitializeGlobals.h" |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 41 | |
baldurk | 42169c5 | 2015-07-08 15:11:59 +0200 | [diff] [blame] | 42 | #include "../glslang/Public/ShaderLang.h" |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 43 | |
John Kessenich | b603f91 | 2013-08-29 00:39:25 +0000 | [diff] [blame] | 44 | namespace glslang { |
| 45 | |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 46 | OS_TLSIndex ThreadInitializeIndex = OS_INVALID_TLS_INDEX; |
| 47 | |
| 48 | bool InitProcess() |
| 49 | { |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 50 | glslang::GetGlobalLock(); |
| 51 | |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 52 | if (ThreadInitializeIndex != OS_INVALID_TLS_INDEX) { |
John Kessenich | f75276b | 2015-05-08 02:28:33 +0000 | [diff] [blame] | 53 | // |
| 54 | // Function is re-entrant. |
| 55 | // |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 56 | |
| 57 | glslang::ReleaseGlobalLock(); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 58 | return true; |
John Kessenich | f75276b | 2015-05-08 02:28:33 +0000 | [diff] [blame] | 59 | } |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 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"); |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 65 | |
| 66 | glslang::ReleaseGlobalLock(); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 67 | return false; |
John Kessenich | f75276b | 2015-05-08 02:28:33 +0000 | [diff] [blame] | 68 | } |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 69 | |
John Kessenich | 7213324 | 2013-07-08 19:39:16 +0000 | [diff] [blame] | 70 | if (! InitializePoolIndex()) { |
John Kessenich | 55901ef | 2014-09-18 13:12:00 +0000 | [diff] [blame] | 71 | assert(0 && "InitProcess(): Failed to initialize global pool"); |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 72 | |
| 73 | glslang::ReleaseGlobalLock(); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 74 | return false; |
John Kessenich | f75276b | 2015-05-08 02:28:33 +0000 | [diff] [blame] | 75 | } |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 76 | |
Aaron Hamilton | ebde046 | 2015-12-02 07:39:26 +0000 | [diff] [blame] | 77 | if (! InitThread()) { |
| 78 | assert(0 && "InitProcess(): Failed to initialize thread"); |
| 79 | |
| 80 | glslang::ReleaseGlobalLock(); |
| 81 | return false; |
| 82 | } |
John Kessenich | 7213324 | 2013-07-08 19:39:16 +0000 | [diff] [blame] | 83 | |
John Kessenich | 2b07c7e | 2013-07-31 18:44:13 +0000 | [diff] [blame] | 84 | glslang::ReleaseGlobalLock(); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 85 | return true; |
| 86 | } |
| 87 | |
| 88 | |
| 89 | bool InitThread() |
| 90 | { |
John Kessenich | f75276b | 2015-05-08 02:28:33 +0000 | [diff] [blame] | 91 | // |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 92 | // This function is re-entrant |
John Kessenich | f75276b | 2015-05-08 02:28:33 +0000 | [diff] [blame] | 93 | // |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 94 | if (ThreadInitializeIndex == OS_INVALID_TLS_INDEX) { |
John Kessenich | f75276b | 2015-05-08 02:28:33 +0000 | [diff] [blame] | 95 | assert(0 && "InitThread(): Process hasn't been initalised."); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 96 | return false; |
John Kessenich | f75276b | 2015-05-08 02:28:33 +0000 | [diff] [blame] | 97 | } |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 98 | |
| 99 | if (OS_GetTLSValue(ThreadInitializeIndex) != 0) |
| 100 | return true; |
| 101 | |
John Kessenich | f75276b | 2015-05-08 02:28:33 +0000 | [diff] [blame] | 102 | InitializeMemoryPools(); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 103 | |
John Kessenich | 7213324 | 2013-07-08 19:39:16 +0000 | [diff] [blame] | 104 | if (! OS_SetTLSValue(ThreadInitializeIndex, (void *)1)) { |
John Kessenich | f75276b | 2015-05-08 02:28:33 +0000 | [diff] [blame] | 105 | assert(0 && "InitThread(): Unable to set init flag."); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 106 | return false; |
John Kessenich | f75276b | 2015-05-08 02:28:33 +0000 | [diff] [blame] | 107 | } |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 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 | |
John Kessenich | f75276b | 2015-05-08 02:28:33 +0000 | [diff] [blame] | 120 | // |
| 121 | // Function is re-entrant and this thread may not have been initialized. |
| 122 | // |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 123 | if (OS_GetTLSValue(ThreadInitializeIndex) != 0) { |
| 124 | if (!OS_SetTLSValue(ThreadInitializeIndex, (void *)0)) { |
John Kessenich | f75276b | 2015-05-08 02:28:33 +0000 | [diff] [blame] | 125 | assert(0 && "DetachThread(): Unable to clear init flag."); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 126 | success = false; |
John Kessenich | f75276b | 2015-05-08 02:28:33 +0000 | [diff] [blame] | 127 | } |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 128 | |
John Kessenich | f75276b | 2015-05-08 02:28:33 +0000 | [diff] [blame] | 129 | FreeGlobalPools(); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 130 | |
John Kessenich | f75276b | 2015-05-08 02:28:33 +0000 | [diff] [blame] | 131 | } |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 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 | |
John Kessenich | f75276b | 2015-05-08 02:28:33 +0000 | [diff] [blame] | 147 | FreePoolIndex(); |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 148 | |
John Kessenich | a0af473 | 2012-12-12 21:15:54 +0000 | [diff] [blame] | 149 | OS_FreeTLSIndex(ThreadInitializeIndex); |
| 150 | ThreadInitializeIndex = OS_INVALID_TLS_INDEX; |
| 151 | |
| 152 | return success; |
| 153 | } |
John Kessenich | b603f91 | 2013-08-29 00:39:25 +0000 | [diff] [blame] | 154 | |
| 155 | } // end namespace glslang |