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