aboutsummaryrefslogtreecommitdiff
path: root/tools/lldb-mi/MICmnThreadMgrStd.cpp
blob: ec223b802f8eeb5bbdd18a31eac022ea89b304fe (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//===-- MICmnThreadMgrStd.cpp -----------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

// In-house headers:
#include "MICmnThreadMgrStd.h"
#include "MICmnLog.h"
#include "MICmnResources.h"
#include "MIUtilSingletonHelper.h"

//++ ------------------------------------------------------------------------------------
// Details: CMICmnThreadMgr constructor.
// Type:    Method.
// Args:    None.
// Return:  None.
// Throws:  None.
//--
CMICmnThreadMgrStd::CMICmnThreadMgrStd()
{
}

//++ ------------------------------------------------------------------------------------
// Details: CMICmnThreadMgr destructor.
// Type:    Method.
// Args:    None.
// Return:  None.
// Throws:  None.
//--
CMICmnThreadMgrStd::~CMICmnThreadMgrStd()
{
    Shutdown();
}

//++ ------------------------------------------------------------------------------------
// Details: Initialise resources for *this thread manager.
// Type:    Method.
// Args:    None.
// Return:  MIstatus::success - Functional succeeded.
//          MIstatus::failure - Functional failed.
// Throws:  None.
//--
bool
CMICmnThreadMgrStd::Initialize()
{
    m_clientUsageRefCnt++;

    if (m_bInitialized)
        return MIstatus::success;

    bool bOk = MIstatus::success;

    ClrErrorDescription();
    CMIUtilString errMsg;

    // Note initialisation order is important here as some resources depend on previous
    MI::ModuleInit<CMICmnLog>(IDS_MI_INIT_ERR_LOG, bOk, errMsg);
    MI::ModuleInit<CMICmnResources>(IDS_MI_INIT_ERR_RESOURCES, bOk, errMsg);

    m_bInitialized = bOk;

    if (!bOk)
    {
        CMIUtilString strInitError(CMIUtilString::Format(MIRSRC(IDS_MI_INIT_ERR_THREADMGR), errMsg.c_str()));
        SetErrorDescription(strInitError);
        return MIstatus::failure;
    }

    return bOk;
}

//++ ------------------------------------------------------------------------------------
// Details: Release resources for *this thread manager.
// Type:    Method.
// Args:    None.
// Return:  MIstatus::success - Functional succeeded.
//          MIstatus::failure - Functional failed.
// Throws:  None.
//--
bool
CMICmnThreadMgrStd::Shutdown()
{
    if (--m_clientUsageRefCnt > 0)
        return MIstatus::success;

    if (!m_bInitialized)
        return MIstatus::success;

    m_bInitialized = false;

    ClrErrorDescription();

    bool bOk = MIstatus::success;
    CMIUtilString errMsg;

    // Tidy up
    ThreadAllTerminate();

    // Note shutdown order is important here
    MI::ModuleShutdown<CMICmnResources>(IDE_MI_SHTDWN_ERR_RESOURCES, bOk, errMsg);
    MI::ModuleShutdown<CMICmnLog>(IDS_MI_SHTDWN_ERR_LOG, bOk, errMsg);

    if (!bOk)
    {
        SetErrorDescriptionn(MIRSRC(IDS_MI_SHUTDOWN_ERR), errMsg.c_str());
    }

    return bOk;
}

//++ ------------------------------------------------------------------------------------
// Details: Ask the thread manager to kill all threads and wait until they have died
// Type:    Method.
// Args:    None.
// Return:  MIstatus::success - Functional succeeded.
//          MIstatus::failure - Functional failed.
// Throws:  None.
//--
bool
CMICmnThreadMgrStd::ThreadAllTerminate()
{
    ThreadList_t::const_iterator it = m_threadList.begin();
    for (; it != m_threadList.end(); ++it)
    {
        // If the thread is still running
        CMIUtilThreadActiveObjBase *pThread = *it;
        if (pThread->ThreadIsActive())
        {
            // Ask this thread to kill itself
            pThread->ThreadKill();

            // Wait for this thread to die
            pThread->ThreadJoin();
        }
    }

    return MIstatus::success;
}

//++ ------------------------------------------------------------------------------------
// Details: Add a thread object to *this manager's list of thread objects. The list to
//          used to manage thread objects centrally.
// Type:    Method.
// Args:    vrObj   - (R) A thread object.
// Return:  MIstatus::success - Functional succeeded.
//          MIstatus::failure - Functional failed.
// Throws:  None.
//--
bool
CMICmnThreadMgrStd::AddThread(const CMIUtilThreadActiveObjBase &vrObj)
{
    m_threadList.push_back(const_cast<CMIUtilThreadActiveObjBase *>(&vrObj));

    return MIstatus::success;
}