1 : //
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 :
7 : #include "compiler/InitializeDll.h"
8 :
9 : #include "compiler/InitializeGlobals.h"
10 : #include "compiler/InitializeParseContext.h"
11 : #include "compiler/osinclude.h"
12 :
13 : OS_TLSIndex ThreadInitializeIndex = OS_INVALID_TLS_INDEX;
14 :
15 0 : bool InitProcess()
16 : {
17 0 : if (ThreadInitializeIndex != OS_INVALID_TLS_INDEX) {
18 : //
19 : // Function is re-entrant.
20 : //
21 0 : return true;
22 : }
23 :
24 0 : ThreadInitializeIndex = OS_AllocTLSIndex();
25 :
26 0 : if (ThreadInitializeIndex == OS_INVALID_TLS_INDEX) {
27 0 : assert(0 && "InitProcess(): Failed to allocate TLS area for init flag");
28 : return false;
29 : }
30 :
31 :
32 0 : if (!InitializePoolIndex()) {
33 0 : assert(0 && "InitProcess(): Failed to initalize global pool");
34 : return false;
35 : }
36 :
37 0 : if (!InitializeParseContextIndex()) {
38 0 : assert(0 && "InitProcess(): Failed to initalize parse context");
39 : return false;
40 : }
41 :
42 0 : return InitThread();
43 : }
44 :
45 0 : bool DetachProcess()
46 : {
47 0 : bool success = true;
48 :
49 0 : if (ThreadInitializeIndex == OS_INVALID_TLS_INDEX)
50 0 : return true;
51 :
52 0 : success = DetachThread();
53 :
54 0 : if (!FreeParseContextIndex())
55 0 : success = false;
56 :
57 0 : FreePoolIndex();
58 :
59 0 : OS_FreeTLSIndex(ThreadInitializeIndex);
60 0 : ThreadInitializeIndex = OS_INVALID_TLS_INDEX;
61 :
62 0 : return success;
63 : }
64 :
65 0 : bool InitThread()
66 : {
67 : //
68 : // This function is re-entrant
69 : //
70 0 : if (ThreadInitializeIndex == OS_INVALID_TLS_INDEX) {
71 0 : assert(0 && "InitThread(): Process hasn't been initalised.");
72 : return false;
73 : }
74 :
75 0 : if (OS_GetTLSValue(ThreadInitializeIndex) != 0)
76 0 : return true;
77 :
78 0 : InitializeGlobalPools();
79 :
80 0 : if (!InitializeGlobalParseContext())
81 0 : return false;
82 :
83 0 : if (!OS_SetTLSValue(ThreadInitializeIndex, (void *)1)) {
84 0 : assert(0 && "InitThread(): Unable to set init flag.");
85 : return false;
86 : }
87 :
88 0 : return true;
89 : }
90 :
91 0 : bool DetachThread()
92 : {
93 0 : bool success = true;
94 :
95 0 : if (ThreadInitializeIndex == OS_INVALID_TLS_INDEX)
96 0 : return true;
97 :
98 : //
99 : // Function is re-entrant and this thread may not have been initalised.
100 : //
101 0 : if (OS_GetTLSValue(ThreadInitializeIndex) != 0) {
102 0 : if (!OS_SetTLSValue(ThreadInitializeIndex, (void *)0)) {
103 0 : assert(0 && "DetachThread(): Unable to clear init flag.");
104 : success = false;
105 : }
106 :
107 0 : if (!FreeParseContext())
108 0 : success = false;
109 :
110 0 : FreeGlobalPools();
111 : }
112 :
113 0 : return success;
114 : }
115 :
|