blob: efa24d60250aaa893b89224887c503ab617cdaa5 [file] [log] [blame]
Bruce Mitchener39e88232015-07-21 13:09:39 +00001//===-- MICmnThreadMgrStd.cpp -----------------------------------*- C++ -*-===//
Deepak Panickal6f9c4682014-05-16 10:51:01 +00002//
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
Deepak Panickal6f9c4682014-05-16 10:51:01 +000010// In-house headers:
Deepak Panickal6f9c4682014-05-16 10:51:01 +000011#include "MICmnThreadMgrStd.h"
12#include "MICmnLog.h"
13#include "MICmnResources.h"
14#include "MIUtilSingletonHelper.h"
15
Kate Stoneb9c1b512016-09-06 20:57:50 +000016//++
17//------------------------------------------------------------------------------------
Zachary Turner1d6af022014-11-17 18:06:21 +000018// Details: CMICmnThreadMgr constructor.
19// Type: Method.
20// Args: None.
21// Return: None.
22// Throws: None.
Deepak Panickal6f9c4682014-05-16 10:51:01 +000023//--
Kate Stoneb9c1b512016-09-06 20:57:50 +000024CMICmnThreadMgrStd::CMICmnThreadMgrStd() {}
Deepak Panickal6f9c4682014-05-16 10:51:01 +000025
Kate Stoneb9c1b512016-09-06 20:57:50 +000026//++
27//------------------------------------------------------------------------------------
Zachary Turner1d6af022014-11-17 18:06:21 +000028// Details: CMICmnThreadMgr destructor.
29// Type: Method.
30// Args: None.
31// Return: None.
32// Throws: None.
Deepak Panickal6f9c4682014-05-16 10:51:01 +000033//--
Kate Stoneb9c1b512016-09-06 20:57:50 +000034CMICmnThreadMgrStd::~CMICmnThreadMgrStd() { Shutdown(); }
Deepak Panickal6f9c4682014-05-16 10:51:01 +000035
Kate Stoneb9c1b512016-09-06 20:57:50 +000036//++
37//------------------------------------------------------------------------------------
Zachary Turner1d6af022014-11-17 18:06:21 +000038// 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.
Deepak Panickal6f9c4682014-05-16 10:51:01 +000044//--
Kate Stoneb9c1b512016-09-06 20:57:50 +000045bool CMICmnThreadMgrStd::Initialize() {
46 m_clientUsageRefCnt++;
Deepak Panickal6f9c4682014-05-16 10:51:01 +000047
Kate Stoneb9c1b512016-09-06 20:57:50 +000048 if (m_bInitialized)
49 return MIstatus::success;
Deepak Panickal6f9c4682014-05-16 10:51:01 +000050
Kate Stoneb9c1b512016-09-06 20:57:50 +000051 bool bOk = MIstatus::success;
Deepak Panickal6f9c4682014-05-16 10:51:01 +000052
Kate Stoneb9c1b512016-09-06 20:57:50 +000053 ClrErrorDescription();
54 CMIUtilString errMsg;
Deepak Panickal6f9c4682014-05-16 10:51:01 +000055
Kate Stoneb9c1b512016-09-06 20:57:50 +000056 // 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);
Deepak Panickal6f9c4682014-05-16 10:51:01 +000060
Kate Stoneb9c1b512016-09-06 20:57:50 +000061 m_bInitialized = bOk;
Zachary Turner1d6af022014-11-17 18:06:21 +000062
Kate Stoneb9c1b512016-09-06 20:57:50 +000063 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 }
Zachary Turner1d6af022014-11-17 18:06:21 +000069
Kate Stoneb9c1b512016-09-06 20:57:50 +000070 return bOk;
Deepak Panickal6f9c4682014-05-16 10:51:01 +000071}
72
Kate Stoneb9c1b512016-09-06 20:57:50 +000073//++
74//------------------------------------------------------------------------------------
Zachary Turner1d6af022014-11-17 18:06:21 +000075// 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.
Deepak Panickal6f9c4682014-05-16 10:51:01 +000081//--
Kate Stoneb9c1b512016-09-06 20:57:50 +000082bool CMICmnThreadMgrStd::Shutdown() {
83 if (--m_clientUsageRefCnt > 0)
84 return MIstatus::success;
Deepak Panickal6f9c4682014-05-16 10:51:01 +000085
Kate Stoneb9c1b512016-09-06 20:57:50 +000086 if (!m_bInitialized)
87 return MIstatus::success;
Deepak Panickal6f9c4682014-05-16 10:51:01 +000088
Kate Stoneb9c1b512016-09-06 20:57:50 +000089 m_bInitialized = false;
Deepak Panickal6f9c4682014-05-16 10:51:01 +000090
Kate Stoneb9c1b512016-09-06 20:57:50 +000091 ClrErrorDescription();
Deepak Panickal6f9c4682014-05-16 10:51:01 +000092
Kate Stoneb9c1b512016-09-06 20:57:50 +000093 bool bOk = MIstatus::success;
94 CMIUtilString errMsg;
Deepak Panickal6f9c4682014-05-16 10:51:01 +000095
Kate Stoneb9c1b512016-09-06 20:57:50 +000096 // Tidy up
97 ThreadAllTerminate();
Deepak Panickal6f9c4682014-05-16 10:51:01 +000098
Kate Stoneb9c1b512016-09-06 20:57:50 +000099 // 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);
Zachary Turner1d6af022014-11-17 18:06:21 +0000102
Kate Stoneb9c1b512016-09-06 20:57:50 +0000103 if (!bOk) {
104 SetErrorDescriptionn(MIRSRC(IDS_MI_SHUTDOWN_ERR), errMsg.c_str());
105 }
Zachary Turner1d6af022014-11-17 18:06:21 +0000106
Kate Stoneb9c1b512016-09-06 20:57:50 +0000107 return bOk;
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000108}
109
Kate Stoneb9c1b512016-09-06 20:57:50 +0000110//++
111//------------------------------------------------------------------------------------
112// Details: Ask the thread manager to kill all threads and wait until they have
113// died
Zachary Turner1d6af022014-11-17 18:06:21 +0000114// Type: Method.
115// Args: None.
116// Return: MIstatus::success - Functional succeeded.
117// MIstatus::failure - Functional failed.
118// Throws: None.
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000119//--
Kate Stoneb9c1b512016-09-06 20:57:50 +0000120bool 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();
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000128
Kate Stoneb9c1b512016-09-06 20:57:50 +0000129 // Wait for this thread to die
130 pThread->ThreadJoin();
Zachary Turner1d6af022014-11-17 18:06:21 +0000131 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000132 }
Zachary Turner1d6af022014-11-17 18:06:21 +0000133
Kate Stoneb9c1b512016-09-06 20:57:50 +0000134 return MIstatus::success;
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000135}
136
Kate Stoneb9c1b512016-09-06 20:57:50 +0000137//++
138//------------------------------------------------------------------------------------
139// Details: Add a thread object to *this manager's list of thread objects. The
140// list to
Zachary Turner1d6af022014-11-17 18:06:21 +0000141// 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.
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000147//--
Kate Stoneb9c1b512016-09-06 20:57:50 +0000148bool CMICmnThreadMgrStd::AddThread(const CMIUtilThreadActiveObjBase &vrObj) {
149 m_threadList.push_back(const_cast<CMIUtilThreadActiveObjBase *>(&vrObj));
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000150
Kate Stoneb9c1b512016-09-06 20:57:50 +0000151 return MIstatus::success;
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000152}