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
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    return InitThread();
43}
44
45bool DetachProcess()
46{
47    bool success = true;
48
49    if (ThreadInitializeIndex == OS_INVALID_TLS_INDEX)
50        return true;
51
52    success = DetachThread();
53
54    if (!FreeParseContextIndex())
55        success = false;
56
57    FreePoolIndex();
58
59    OS_FreeTLSIndex(ThreadInitializeIndex);
60    ThreadInitializeIndex = OS_INVALID_TLS_INDEX;
61
62    return success;
63}
64
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
116