blob: 8763cfeea8d760eadb241f2314fc2b7736aabe9c [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
daniel@transgaming.combbf56f72010-04-20 18:52:13 +00009#include "compiler/InitializeGlobals.h"
10#include "compiler/InitializeParseContext.h"
alokp@chromium.org34b99cd2010-07-27 18:37:55 +000011#include "compiler/osinclude.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000012
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000013OS_TLSIndex ThreadInitializeIndex = OS_INVALID_TLS_INDEX;
14
15bool InitProcess()
16{
17 if (ThreadInitializeIndex != OS_INVALID_TLS_INDEX) {
alokp@chromium.org34b99cd2010-07-27 18:37:55 +000018 //
19 // Function is re-entrant.
20 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000021 return true;
alokp@chromium.org34b99cd2010-07-27 18:37:55 +000022 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000023
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;
alokp@chromium.org34b99cd2010-07-27 18:37:55 +000029 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000030
31
32 if (!InitializePoolIndex()) {
33 assert(0 && "InitProcess(): Failed to initalize global pool");
34 return false;
alokp@chromium.org34b99cd2010-07-27 18:37:55 +000035 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000036
37 if (!InitializeParseContextIndex()) {
38 assert(0 && "InitProcess(): Failed to initalize parse context");
39 return false;
alokp@chromium.org34b99cd2010-07-27 18:37:55 +000040 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000041
alokp@chromium.org34b99cd2010-07-27 18:37:55 +000042 return InitThread();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000043}
44
45bool DetachProcess()
46{
47 bool success = true;
48
49 if (ThreadInitializeIndex == OS_INVALID_TLS_INDEX)
50 return true;
51
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000052 success = DetachThread();
53
alokp@chromium.org34b99cd2010-07-27 18:37:55 +000054 if (!FreeParseContextIndex())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000055 success = false;
56
alokp@chromium.org34b99cd2010-07-27 18:37:55 +000057 FreePoolIndex();
58
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000059 OS_FreeTLSIndex(ThreadInitializeIndex);
60 ThreadInitializeIndex = OS_INVALID_TLS_INDEX;
61
62 return success;
63}
alokp@chromium.org34b99cd2010-07-27 18:37:55 +000064
65bool InitThread()
66{
67 //
68 // This function is re-entrant
69 //
70 if (ThreadInitializeIndex == OS_INVALID_TLS_INDEX) {
71 assert(0 && "InitThread(): Process hasn't been initalised.");
72 return false;
73 }
74
75 if (OS_GetTLSValue(ThreadInitializeIndex) != 0)
76 return true;
77
78 InitializeGlobalPools();
79
80 if (!InitializeGlobalParseContext())
81 return false;
82
83 if (!OS_SetTLSValue(ThreadInitializeIndex, (void *)1)) {
84 assert(0 && "InitThread(): Unable to set init flag.");
85 return false;
86 }
87
88 return true;
89}
90
91bool DetachThread()
92{
93 bool success = true;
94
95 if (ThreadInitializeIndex == OS_INVALID_TLS_INDEX)
96 return true;
97
98 //
99 // Function is re-entrant and this thread may not have been initalised.
100 //
101 if (OS_GetTLSValue(ThreadInitializeIndex) != 0) {
102 if (!OS_SetTLSValue(ThreadInitializeIndex, (void *)0)) {
103 assert(0 && "DetachThread(): Unable to clear init flag.");
104 success = false;
105 }
106
107 if (!FreeParseContext())
108 success = false;
109
110 FreeGlobalPools();
111 }
112
113 return success;
114}
115