blob: f0185b8fcf1d23045f5e197b54aec50cee4b2ccf [file] [log] [blame]
Deepak Panickal6f9c4682014-05-16 10:51:01 +00001//===-- MICmnThreadMgr.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
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
16//++ ------------------------------------------------------------------------------------
Zachary Turner1d6af022014-11-17 18:06:21 +000017// Details: CMICmnThreadMgr constructor.
18// Type: Method.
19// Args: None.
20// Return: None.
21// Throws: None.
Deepak Panickal6f9c4682014-05-16 10:51:01 +000022//--
Zachary Turner1d6af022014-11-17 18:06:21 +000023CMICmnThreadMgrStd::CMICmnThreadMgrStd(void)
Deepak Panickal6f9c4682014-05-16 10:51:01 +000024{
25}
26
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//--
Zachary Turner1d6af022014-11-17 18:06:21 +000034CMICmnThreadMgrStd::~CMICmnThreadMgrStd(void)
Deepak Panickal6f9c4682014-05-16 10:51:01 +000035{
Zachary Turner1d6af022014-11-17 18:06:21 +000036 Shutdown();
Deepak Panickal6f9c4682014-05-16 10:51:01 +000037}
38
39//++ ------------------------------------------------------------------------------------
Zachary Turner1d6af022014-11-17 18:06:21 +000040// Details: Initialise resources for *this thread manager.
41// Type: Method.
42// Args: None.
43// Return: MIstatus::success - Functional succeeded.
44// MIstatus::failure - Functional failed.
45// Throws: None.
Deepak Panickal6f9c4682014-05-16 10:51:01 +000046//--
Zachary Turner1d6af022014-11-17 18:06:21 +000047bool
48CMICmnThreadMgrStd::Initialize(void)
Deepak Panickal6f9c4682014-05-16 10:51:01 +000049{
Zachary Turner1d6af022014-11-17 18:06:21 +000050 m_clientUsageRefCnt++;
Deepak Panickal6f9c4682014-05-16 10:51:01 +000051
Zachary Turner1d6af022014-11-17 18:06:21 +000052 if (m_bInitialized)
53 return MIstatus::success;
Deepak Panickal6f9c4682014-05-16 10:51:01 +000054
Zachary Turner1d6af022014-11-17 18:06:21 +000055 bool bOk = MIstatus::success;
Deepak Panickal6f9c4682014-05-16 10:51:01 +000056
Zachary Turner1d6af022014-11-17 18:06:21 +000057 ClrErrorDescription();
58 CMIUtilString errMsg;
Deepak Panickal6f9c4682014-05-16 10:51:01 +000059
Zachary Turner1d6af022014-11-17 18:06:21 +000060 // Note initialisation order is important here as some resources depend on previous
61 MI::ModuleInit<CMICmnLog>(IDS_MI_INIT_ERR_LOG, bOk, errMsg);
62 MI::ModuleInit<CMICmnResources>(IDS_MI_INIT_ERR_RESOURCES, bOk, errMsg);
Deepak Panickal6f9c4682014-05-16 10:51:01 +000063
Zachary Turner1d6af022014-11-17 18:06:21 +000064 m_bInitialized = bOk;
65
66 if (!bOk)
67 {
68 CMIUtilString strInitError(CMIUtilString::Format(MIRSRC(IDS_MI_INIT_ERR_THREADMGR), errMsg.c_str()));
69 SetErrorDescription(strInitError);
70 return MIstatus::failure;
71 }
72
73 return bOk;
Deepak Panickal6f9c4682014-05-16 10:51:01 +000074}
75
76//++ ------------------------------------------------------------------------------------
Zachary Turner1d6af022014-11-17 18:06:21 +000077// Details: Release resources for *this thread manager.
78// Type: Method.
79// Args: None.
80// Return: MIstatus::success - Functional succeeded.
81// MIstatus::failure - Functional failed.
82// Throws: None.
Deepak Panickal6f9c4682014-05-16 10:51:01 +000083//--
Zachary Turner1d6af022014-11-17 18:06:21 +000084bool
85CMICmnThreadMgrStd::Shutdown(void)
Deepak Panickal6f9c4682014-05-16 10:51:01 +000086{
Zachary Turner1d6af022014-11-17 18:06:21 +000087 if (--m_clientUsageRefCnt > 0)
88 return MIstatus::success;
Deepak Panickal6f9c4682014-05-16 10:51:01 +000089
Zachary Turner1d6af022014-11-17 18:06:21 +000090 if (!m_bInitialized)
91 return MIstatus::success;
Deepak Panickal6f9c4682014-05-16 10:51:01 +000092
Zachary Turner1d6af022014-11-17 18:06:21 +000093 m_bInitialized = false;
Deepak Panickal6f9c4682014-05-16 10:51:01 +000094
Zachary Turner1d6af022014-11-17 18:06:21 +000095 ClrErrorDescription();
Deepak Panickal6f9c4682014-05-16 10:51:01 +000096
Zachary Turner1d6af022014-11-17 18:06:21 +000097 bool bOk = MIstatus::success;
98 CMIUtilString errMsg;
Deepak Panickal6f9c4682014-05-16 10:51:01 +000099
Zachary Turner1d6af022014-11-17 18:06:21 +0000100 // Tidy up
101 ThreadAllTerminate();
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000102
Zachary Turner1d6af022014-11-17 18:06:21 +0000103 // Note shutdown order is important here
104 MI::ModuleShutdown<CMICmnResources>(IDE_MI_SHTDWN_ERR_RESOURCES, bOk, errMsg);
105 MI::ModuleShutdown<CMICmnLog>(IDS_MI_SHTDWN_ERR_LOG, bOk, errMsg);
106
107 if (!bOk)
108 {
109 SetErrorDescriptionn(MIRSRC(IDS_MI_SHUTDOWN_ERR), errMsg.c_str());
110 }
111
112 return bOk;
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000113}
114
115//++ ------------------------------------------------------------------------------------
Zachary Turner1d6af022014-11-17 18:06:21 +0000116// Details: Ask the thread manager to kill all threads and wait until they have died
117// Type: Method.
118// Args: None.
119// Return: MIstatus::success - Functional succeeded.
120// MIstatus::failure - Functional failed.
121// Throws: None.
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000122//--
Zachary Turner1d6af022014-11-17 18:06:21 +0000123bool
124CMICmnThreadMgrStd::ThreadAllTerminate(void)
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000125{
Zachary Turner1d6af022014-11-17 18:06:21 +0000126 ThreadList_t::const_iterator it = m_threadList.begin();
127 for (; it != m_threadList.end(); ++it)
128 {
129 // If the thread is still running
130 CMIUtilThreadActiveObjBase *pThread = *it;
131 if (pThread->ThreadIsActive())
132 {
133 // Ask this thread to kill itself
134 pThread->ThreadKill();
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000135
Zachary Turner1d6af022014-11-17 18:06:21 +0000136 // Wait for this thread to die
137 pThread->ThreadJoin();
138 }
139 }
140
141 return MIstatus::success;
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000142}
143
144//++ ------------------------------------------------------------------------------------
Zachary Turner1d6af022014-11-17 18:06:21 +0000145// Details: Add a thread object to *this manager's list of thread objects. The list to
146// used to manage thread objects centrally.
147// Type: Method.
148// Args: vrObj - (R) A thread object.
149// Return: MIstatus::success - Functional succeeded.
150// MIstatus::failure - Functional failed.
151// Throws: None.
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000152//--
Zachary Turner1d6af022014-11-17 18:06:21 +0000153bool
154CMICmnThreadMgrStd::AddThread(const CMIUtilThreadActiveObjBase &vrObj)
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000155{
Zachary Turner1d6af022014-11-17 18:06:21 +0000156 m_threadList.push_back(const_cast<CMIUtilThreadActiveObjBase *>(&vrObj));
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000157
Zachary Turner1d6af022014-11-17 18:06:21 +0000158 return MIstatus::success;
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000159}