InitializeDll.cpp 2.51 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
//
// Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//

#include "compiler/InitializeDll.h"

#include "compiler/InitializeGlobals.h"
#include "compiler/InitializeParseContext.h"
11
#include "compiler/osinclude.h"
12
13
14
15
16
17

OS_TLSIndex ThreadInitializeIndex = OS_INVALID_TLS_INDEX;

bool InitProcess()
{
    if (ThreadInitializeIndex != OS_INVALID_TLS_INDEX) {
18
19
20
        //
        // Function is re-entrant.
        //
21
        return true;
22
    }
23
24
25
26
27
28

    ThreadInitializeIndex = OS_AllocTLSIndex();

    if (ThreadInitializeIndex == OS_INVALID_TLS_INDEX) {
        assert(0 && "InitProcess(): Failed to allocate TLS area for init flag");
        return false;
29
    }
30
31
32
33
34


    if (!InitializePoolIndex()) {
        assert(0 && "InitProcess(): Failed to initalize global pool");
        return false;
35
    }
36
37
38
39

    if (!InitializeParseContextIndex()) {
        assert(0 && "InitProcess(): Failed to initalize parse context");
        return false;
40
    }
41

42
    return InitThread();
43
44
}

45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
bool DetachProcess()
{
    bool success = true;

    if (ThreadInitializeIndex == OS_INVALID_TLS_INDEX)
        return true;

    success = DetachThread();

    if (!FreeParseContextIndex())
        success = false;

    FreePoolIndex();

    OS_FreeTLSIndex(ThreadInitializeIndex);
    ThreadInitializeIndex = OS_INVALID_TLS_INDEX;

    return success;
}
64
65
66

bool InitThread()
{
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
76
77

    if (OS_GetTLSValue(ThreadInitializeIndex) != 0)
        return true;

78
    InitializeGlobalPools();
79

80
    if (!InitializeGlobalParseContext())
81
82
83
        return false;

    if (!OS_SetTLSValue(ThreadInitializeIndex, (void *)1)) {
84
        assert(0 && "InitThread(): Unable to set init flag.");
85
        return false;
86
    }
87
88
89
90
91
92
93
94
95
96
97

    return true;
}

bool DetachThread()
{
    bool success = true;

    if (ThreadInitializeIndex == OS_INVALID_TLS_INDEX)
        return true;

98
99
100
    //
    // Function is re-entrant and this thread may not have been initalised.
    //
101
102
    if (OS_GetTLSValue(ThreadInitializeIndex) != 0) {
        if (!OS_SetTLSValue(ThreadInitializeIndex, (void *)0)) {
103
            assert(0 && "DetachThread(): Unable to clear init flag.");
104
            success = false;
105
        }
106

107
        if (!FreeParseContext())
108
109
            success = false;

110
111
        FreeGlobalPools();
    }
112
113
114

    return success;
}
115