blob: 06f838458d00fb34e6118891faa7a22361e51448 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
2// Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
daniel@transgaming.combbf56f72010-04-20 18:52:13 +00007#include "compiler/InitializeDll.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00008
alokp@chromium.orgea0e1af2010-03-22 19:33:14 +00009#include "GLSLANG/ShaderLang.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000010
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000011#include "compiler/InitializeGlobals.h"
12#include "compiler/InitializeParseContext.h"
13
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000014OS_TLSIndex ThreadInitializeIndex = OS_INVALID_TLS_INDEX;
15
16bool InitProcess()
17{
18 if (ThreadInitializeIndex != OS_INVALID_TLS_INDEX) {
19 //
20 // Function is re-entrant.
21 //
22 return true;
23 }
24
25 ThreadInitializeIndex = OS_AllocTLSIndex();
26
27 if (ThreadInitializeIndex == OS_INVALID_TLS_INDEX) {
28 assert(0 && "InitProcess(): Failed to allocate TLS area for init flag");
29 return false;
30 }
31
32
33 if (!InitializePoolIndex()) {
34 assert(0 && "InitProcess(): Failed to initalize global pool");
35 return false;
36 }
37
38 if (!InitializeParseContextIndex()) {
39 assert(0 && "InitProcess(): Failed to initalize parse context");
40 return false;
41 }
42
43 InitThread();
44 return true;
45}
46
47
48bool InitThread()
49{
50 //
51 // This function is re-entrant
52 //
53 if (ThreadInitializeIndex == OS_INVALID_TLS_INDEX) {
54 assert(0 && "InitThread(): Process hasn't been initalised.");
55 return false;
56 }
57
58 if (OS_GetTLSValue(ThreadInitializeIndex) != 0)
59 return true;
60
61 InitializeGlobalPools();
62
63 if (!InitializeGlobalParseContext())
64 return false;
65
66 if (!OS_SetTLSValue(ThreadInitializeIndex, (void *)1)) {
67 assert(0 && "InitThread(): Unable to set init flag.");
68 return false;
69 }
70
71 return true;
72}
73
74
75bool DetachThread()
76{
77 bool success = true;
78
79 if (ThreadInitializeIndex == OS_INVALID_TLS_INDEX)
80 return true;
81
82 //
83 // Function is re-entrant and this thread may not have been initalised.
84 //
85 if (OS_GetTLSValue(ThreadInitializeIndex) != 0) {
86 if (!OS_SetTLSValue(ThreadInitializeIndex, (void *)0)) {
87 assert(0 && "DetachThread(): Unable to clear init flag.");
88 success = false;
89 }
90
91 FreeGlobalPools();
92
93 if (!FreeParseContext())
94 success = false;
95 }
96
97 return success;
98}
99
100bool DetachProcess()
101{
102 bool success = true;
103
104 if (ThreadInitializeIndex == OS_INVALID_TLS_INDEX)
105 return true;
106
107 ShFinalize();
108
109 success = DetachThread();
110
111 FreePoolIndex();
112
113 if (!FreeParseContextIndex())
114 success = false;
115
116 OS_FreeTLSIndex(ThreadInitializeIndex);
117 ThreadInitializeIndex = OS_INVALID_TLS_INDEX;
118
119 return success;
120}