blob: d9bf3a25b5d4d2fe9b529f0cb340d4a4c4adbbb6 [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
10//++
11// File: MICmnThreadMgr.cpp
12//
13// Overview: CMICmnThreadMgr implementation.
14//
15// Environment: Compilers: Visual C++ 12.
16// gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1
17// Libraries: See MIReadmetxt.
18//
19// Copyright: None.
20//--
21
22// In-house headers:
23#include "MICmnConfig.h"
24#include "MICmnThreadMgrStd.h"
25#include "MICmnLog.h"
26#include "MICmnResources.h"
27#include "MIUtilSingletonHelper.h"
28
29//++ ------------------------------------------------------------------------------------
30// Details: CMICmnThreadMgr constructor.
31// Type: Method.
32// Args: None.
33// Return: None.
34// Throws: None.
35//--
36CMICmnThreadMgrStd::CMICmnThreadMgrStd( void )
37{
38}
39
40//++ ------------------------------------------------------------------------------------
41// Details: CMICmnThreadMgr destructor.
42// Type: Method.
43// Args: None.
44// Return: None.
45// Throws: None.
46//--
47CMICmnThreadMgrStd::~CMICmnThreadMgrStd( void )
48{
49 Shutdown();
50}
51
52//++ ------------------------------------------------------------------------------------
53// Details: Initialize resources for *this thread manager.
54// Type: Method.
55// Args: None.
56// Return: MIstatus::success - Functional succeeded.
57// MIstatus::failure - Functional failed.
58// Throws: None.
59//--
60bool CMICmnThreadMgrStd::Initialize( void )
61{
62 m_clientUsageRefCnt++;
63
64 if( m_bInitialized )
65 return MIstatus::success;
66
67 bool bOk = MIstatus::success;
68
69 ClrErrorDescription();
70 CMIUtilString errMsg;
71
72 // Note initialization order is important here as some resources depend on previous
73 MI::ModuleInit< CMICmnLog > ( IDS_MI_INIT_ERR_LOG , bOk, errMsg );
74 MI::ModuleInit< CMICmnResources >( IDS_MI_INIT_ERR_RESOURCES, bOk, errMsg );
75
76 m_bInitialized = bOk;
77
78 if( !bOk )
79 {
80 CMIUtilString strInitError( CMIUtilString::Format( MIRSRC( IDS_MI_INIT_ERR_THREADMGR ), errMsg.c_str() ) );
81 SetErrorDescription( strInitError );
82 return MIstatus::failure;
83 }
84
85 return bOk;
86}
87
88//++ ------------------------------------------------------------------------------------
89// Details: Release resources for *this thread manager.
90// Type: Method.
91// Args: None.
92// Return: MIstatus::success - Functional succeeded.
93// MIstatus::failure - Functional failed.
94// Throws: None.
95//--
96bool CMICmnThreadMgrStd::Shutdown( void )
97{
98 if( --m_clientUsageRefCnt > 0 )
99 return MIstatus::success;
100
101 if( !m_bInitialized )
102 return MIstatus::success;
103
104 m_bInitialized = false;
105
106 ClrErrorDescription();
107
108 bool bOk = MIstatus::success;
109 CMIUtilString errMsg;
110
111 // Tidy up
112 ThreadAllTerminate();
113
114 // Note shutdown order is important here
115 MI::ModuleShutdown< CMICmnResources >( IDE_MI_SHTDWN_ERR_RESOURCES, bOk, errMsg );
116 MI::ModuleShutdown< CMICmnLog > ( IDS_MI_SHTDWN_ERR_LOG , bOk, errMsg );
117
118 if( !bOk )
119 {
120 SetErrorDescriptionn( MIRSRC( IDS_MI_SHUTDOWN_ERR ), errMsg.c_str() );
121 }
122
123 return bOk;
124}
125
126//++ ------------------------------------------------------------------------------------
127// Details: Ask the thread manager to kill all threads and wait until they have died
128// Type: Method.
129// Args: None.
130// Return: MIstatus::success - Functional succeeded.
131// MIstatus::failure - Functional failed.
132// Throws: None.
133//--
134bool CMICmnThreadMgrStd::ThreadAllTerminate( void )
135{
136 // Find an iterator object for the list
137 ThreadList_t::const_iterator it = m_threadList.begin();
138
139 // Loop over all entries in the list
140 for( ; it != m_threadList.end(); ++it )
141 {
142 // Get the thread object from the list
143 CMIUtilThreadActiveObjBase * pThread = *it;
144
145 // If the thread is still running
146 if( pThread->ThreadIsActive() )
147 {
148 // Ask this thread to kill itself
149 pThread->ThreadKill();
150
151 // Wait for this thread to die
152 pThread->ThreadJoin();
153 }
154 }
155
156 return MIstatus::success;
157}
158
159//++ ------------------------------------------------------------------------------------
160// Details: Add a thread object to *this manager's list of thread objects. The list to
161// used to manage thread objects centrally.
162// Type: Method.
163// Args: vrObj - (R) A thread object.
164// Return: MIstatus::success - Functional succeeded.
165// MIstatus::failure - Functional failed.
166// Throws: None.
167//--
168bool CMICmnThreadMgrStd::AddThread( const CMIUtilThreadActiveObjBase & vrObj )
169{
170 // Push this thread onto the thread list
171 m_threadList.push_back( const_cast< CMIUtilThreadActiveObjBase * >( &vrObj ) );
172
173 return MIstatus::success;
174}