blob: 2eb912c422f963dce6b098764cf923de82cce641 [file] [log] [blame]
John Kessenicha0af4732012-12-12 21:15:54 +00001//
John Kessenich927608b2017-01-06 12:34:14 -07002// Copyright (C) 2002-2005 3Dlabs Inc. Ltd.
3// All rights reserved.
John Kessenicha0af4732012-12-12 21:15:54 +00004//
John Kessenich927608b2017-01-06 12:34:14 -07005// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions
7// are met:
John Kessenicha0af4732012-12-12 21:15:54 +00008//
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 Kessenich927608b2017-01-06 12:34:14 -070021// 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 Kessenicha0af4732012-12-12 21:15:54 +000033//
34
35#define SH_EXPORTING
36
John Kessenich66ec80e2016-08-05 14:04:23 -060037#include <cassert>
John Kessenich2b07c7e2013-07-31 18:44:13 +000038
John Kessenicha0af4732012-12-12 21:15:54 +000039#include "InitializeDll.h"
baldurk42169c52015-07-08 15:11:59 +020040#include "../glslang/Include/InitializeGlobals.h"
John Kessenicha0af4732012-12-12 21:15:54 +000041
baldurk42169c52015-07-08 15:11:59 +020042#include "../glslang/Public/ShaderLang.h"
John Kessenicha0af4732012-12-12 21:15:54 +000043
John Kessenichb603f912013-08-29 00:39:25 +000044namespace glslang {
45
John Kessenicha0af4732012-12-12 21:15:54 +000046OS_TLSIndex ThreadInitializeIndex = OS_INVALID_TLS_INDEX;
47
48bool InitProcess()
49{
John Kessenich2b07c7e2013-07-31 18:44:13 +000050 glslang::GetGlobalLock();
51
John Kessenicha0af4732012-12-12 21:15:54 +000052 if (ThreadInitializeIndex != OS_INVALID_TLS_INDEX) {
John Kessenichf75276b2015-05-08 02:28:33 +000053 //
54 // Function is re-entrant.
55 //
John Kessenich2b07c7e2013-07-31 18:44:13 +000056
57 glslang::ReleaseGlobalLock();
John Kessenicha0af4732012-12-12 21:15:54 +000058 return true;
John Kessenichf75276b2015-05-08 02:28:33 +000059 }
John Kessenicha0af4732012-12-12 21:15:54 +000060
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 Kessenich2b07c7e2013-07-31 18:44:13 +000065
66 glslang::ReleaseGlobalLock();
John Kessenicha0af4732012-12-12 21:15:54 +000067 return false;
John Kessenichf75276b2015-05-08 02:28:33 +000068 }
John Kessenicha0af4732012-12-12 21:15:54 +000069
John Kessenich72133242013-07-08 19:39:16 +000070 if (! InitializePoolIndex()) {
John Kessenich55901ef2014-09-18 13:12:00 +000071 assert(0 && "InitProcess(): Failed to initialize global pool");
John Kessenich2b07c7e2013-07-31 18:44:13 +000072
73 glslang::ReleaseGlobalLock();
John Kessenicha0af4732012-12-12 21:15:54 +000074 return false;
John Kessenichf75276b2015-05-08 02:28:33 +000075 }
John Kessenicha0af4732012-12-12 21:15:54 +000076
Aaron Hamiltonebde0462015-12-02 07:39:26 +000077 if (! InitThread()) {
78 assert(0 && "InitProcess(): Failed to initialize thread");
79
80 glslang::ReleaseGlobalLock();
81 return false;
82 }
John Kessenich72133242013-07-08 19:39:16 +000083
John Kessenich2b07c7e2013-07-31 18:44:13 +000084 glslang::ReleaseGlobalLock();
John Kessenicha0af4732012-12-12 21:15:54 +000085 return true;
86}
87
88
89bool InitThread()
90{
John Kessenichf75276b2015-05-08 02:28:33 +000091 //
John Kessenicha0af4732012-12-12 21:15:54 +000092 // This function is re-entrant
John Kessenichf75276b2015-05-08 02:28:33 +000093 //
John Kessenicha0af4732012-12-12 21:15:54 +000094 if (ThreadInitializeIndex == OS_INVALID_TLS_INDEX) {
John Kessenichf75276b2015-05-08 02:28:33 +000095 assert(0 && "InitThread(): Process hasn't been initalised.");
John Kessenicha0af4732012-12-12 21:15:54 +000096 return false;
John Kessenichf75276b2015-05-08 02:28:33 +000097 }
John Kessenicha0af4732012-12-12 21:15:54 +000098
99 if (OS_GetTLSValue(ThreadInitializeIndex) != 0)
100 return true;
101
John Kessenichf75276b2015-05-08 02:28:33 +0000102 InitializeMemoryPools();
John Kessenicha0af4732012-12-12 21:15:54 +0000103
John Kessenich72133242013-07-08 19:39:16 +0000104 if (! OS_SetTLSValue(ThreadInitializeIndex, (void *)1)) {
John Kessenichf75276b2015-05-08 02:28:33 +0000105 assert(0 && "InitThread(): Unable to set init flag.");
John Kessenicha0af4732012-12-12 21:15:54 +0000106 return false;
John Kessenichf75276b2015-05-08 02:28:33 +0000107 }
John Kessenicha0af4732012-12-12 21:15:54 +0000108
109 return true;
110}
111
112
113bool DetachThread()
114{
115 bool success = true;
116
117 if (ThreadInitializeIndex == OS_INVALID_TLS_INDEX)
118 return true;
119
John Kessenichf75276b2015-05-08 02:28:33 +0000120 //
121 // Function is re-entrant and this thread may not have been initialized.
122 //
John Kessenicha0af4732012-12-12 21:15:54 +0000123 if (OS_GetTLSValue(ThreadInitializeIndex) != 0) {
124 if (!OS_SetTLSValue(ThreadInitializeIndex, (void *)0)) {
John Kessenichf75276b2015-05-08 02:28:33 +0000125 assert(0 && "DetachThread(): Unable to clear init flag.");
John Kessenicha0af4732012-12-12 21:15:54 +0000126 success = false;
John Kessenichf75276b2015-05-08 02:28:33 +0000127 }
John Kessenicha0af4732012-12-12 21:15:54 +0000128
John Kessenichf75276b2015-05-08 02:28:33 +0000129 FreeGlobalPools();
John Kessenicha0af4732012-12-12 21:15:54 +0000130
John Kessenichf75276b2015-05-08 02:28:33 +0000131 }
John Kessenicha0af4732012-12-12 21:15:54 +0000132
133 return success;
134}
135
136bool 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 Kessenichf75276b2015-05-08 02:28:33 +0000147 FreePoolIndex();
John Kessenicha0af4732012-12-12 21:15:54 +0000148
John Kessenicha0af4732012-12-12 21:15:54 +0000149 OS_FreeTLSIndex(ThreadInitializeIndex);
150 ThreadInitializeIndex = OS_INVALID_TLS_INDEX;
151
152 return success;
153}
John Kessenichb603f912013-08-29 00:39:25 +0000154
155} // end namespace glslang