1 //===-- MICmnThreadMgrStd.cpp -----------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // In-house headers:
11 #include "MICmnThreadMgrStd.h"
12 #include "MICmnLog.h"
13 #include "MICmnResources.h"
14 #include "MIUtilSingletonHelper.h"
15
16 //++
17 //------------------------------------------------------------------------------------
18 // Details: CMICmnThreadMgr constructor.
19 // Type: Method.
20 // Args: None.
21 // Return: None.
22 // Throws: None.
23 //--
CMICmnThreadMgrStd()24 CMICmnThreadMgrStd::CMICmnThreadMgrStd() {}
25
26 //++
27 //------------------------------------------------------------------------------------
28 // Details: CMICmnThreadMgr destructor.
29 // Type: Method.
30 // Args: None.
31 // Return: None.
32 // Throws: None.
33 //--
~CMICmnThreadMgrStd()34 CMICmnThreadMgrStd::~CMICmnThreadMgrStd() { Shutdown(); }
35
36 //++
37 //------------------------------------------------------------------------------------
38 // Details: Initialise resources for *this thread manager.
39 // Type: Method.
40 // Args: None.
41 // Return: MIstatus::success - Functional succeeded.
42 // MIstatus::failure - Functional failed.
43 // Throws: None.
44 //--
Initialize()45 bool CMICmnThreadMgrStd::Initialize() {
46 m_clientUsageRefCnt++;
47
48 if (m_bInitialized)
49 return MIstatus::success;
50
51 bool bOk = MIstatus::success;
52
53 ClrErrorDescription();
54 CMIUtilString errMsg;
55
56 // Note initialisation order is important here as some resources depend on
57 // previous
58 MI::ModuleInit<CMICmnLog>(IDS_MI_INIT_ERR_LOG, bOk, errMsg);
59 MI::ModuleInit<CMICmnResources>(IDS_MI_INIT_ERR_RESOURCES, bOk, errMsg);
60
61 m_bInitialized = bOk;
62
63 if (!bOk) {
64 CMIUtilString strInitError(CMIUtilString::Format(
65 MIRSRC(IDS_MI_INIT_ERR_THREADMGR), errMsg.c_str()));
66 SetErrorDescription(strInitError);
67 return MIstatus::failure;
68 }
69
70 return bOk;
71 }
72
73 //++
74 //------------------------------------------------------------------------------------
75 // Details: Release resources for *this thread manager.
76 // Type: Method.
77 // Args: None.
78 // Return: MIstatus::success - Functional succeeded.
79 // MIstatus::failure - Functional failed.
80 // Throws: None.
81 //--
Shutdown()82 bool CMICmnThreadMgrStd::Shutdown() {
83 if (--m_clientUsageRefCnt > 0)
84 return MIstatus::success;
85
86 if (!m_bInitialized)
87 return MIstatus::success;
88
89 m_bInitialized = false;
90
91 ClrErrorDescription();
92
93 bool bOk = MIstatus::success;
94 CMIUtilString errMsg;
95
96 // Tidy up
97 ThreadAllTerminate();
98
99 // Note shutdown order is important here
100 MI::ModuleShutdown<CMICmnResources>(IDE_MI_SHTDWN_ERR_RESOURCES, bOk, errMsg);
101 MI::ModuleShutdown<CMICmnLog>(IDS_MI_SHTDWN_ERR_LOG, bOk, errMsg);
102
103 if (!bOk) {
104 SetErrorDescriptionn(MIRSRC(IDS_MI_SHUTDOWN_ERR), errMsg.c_str());
105 }
106
107 return bOk;
108 }
109
110 //++
111 //------------------------------------------------------------------------------------
112 // Details: Ask the thread manager to kill all threads and wait until they have
113 // died
114 // Type: Method.
115 // Args: None.
116 // Return: MIstatus::success - Functional succeeded.
117 // MIstatus::failure - Functional failed.
118 // Throws: None.
119 //--
ThreadAllTerminate()120 bool CMICmnThreadMgrStd::ThreadAllTerminate() {
121 ThreadList_t::const_iterator it = m_threadList.begin();
122 for (; it != m_threadList.end(); ++it) {
123 // If the thread is still running
124 CMIUtilThreadActiveObjBase *pThread = *it;
125 if (pThread->ThreadIsActive()) {
126 // Ask this thread to kill itself
127 pThread->ThreadKill();
128
129 // Wait for this thread to die
130 pThread->ThreadJoin();
131 }
132 }
133
134 return MIstatus::success;
135 }
136
137 //++
138 //------------------------------------------------------------------------------------
139 // Details: Add a thread object to *this manager's list of thread objects. The
140 // list to
141 // used to manage thread objects centrally.
142 // Type: Method.
143 // Args: vrObj - (R) A thread object.
144 // Return: MIstatus::success - Functional succeeded.
145 // MIstatus::failure - Functional failed.
146 // Throws: None.
147 //--
AddThread(const CMIUtilThreadActiveObjBase & vrObj)148 bool CMICmnThreadMgrStd::AddThread(const CMIUtilThreadActiveObjBase &vrObj) {
149 m_threadList.push_back(const_cast<CMIUtilThreadActiveObjBase *>(&vrObj));
150
151 return MIstatus::success;
152 }
153