1 //===-- MICmnThreadMgrStd.h -------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 // Third party headers:
16 #include "MICmnBase.h"
17 #include "MIUtilThreadBaseStd.h"
18 #include "MICmnResources.h"
19 #include "MIUtilSingletonBase.h"
21 //++ ============================================================================
22 // Details: MI's worker thread (active thread) manager.
23 // The manager creates threads and behalf of clients. Client are
24 // responsible for their threads and can delete them when necessary.
25 // This manager will stop and delete all threads on *this manager's
29 // Authors: Aidan Dodds 12/03/2014.
32 class CMICmnThreadMgrStd : public CMICmnBase, public MI::ISingleton<CMICmnThreadMgrStd>
34 friend MI::ISingleton<CMICmnThreadMgrStd>;
38 bool Initialize(void) override;
39 bool Shutdown(void) override;
41 ThreadAllTerminate(void); // Ask all threads to stop (caution)
42 template <typename T> // Ask the thread manager to start and stop threads on our behalf
43 bool ThreadStart(T &vrwObject);
47 typedef std::vector<CMIUtilThreadActiveObjBase *> ThreadList_t;
51 /* ctor */ CMICmnThreadMgrStd(void);
52 /* ctor */ CMICmnThreadMgrStd(const CMICmnThreadMgrStd &);
53 void operator=(const CMICmnThreadMgrStd &);
56 AddThread(const CMIUtilThreadActiveObjBase &vrObj); // Add a thread for monitoring by the threadmanager
61 /* dtor */ ~CMICmnThreadMgrStd(void) override;
65 CMIUtilThreadMutex m_mutex;
66 ThreadList_t m_threadList;
69 //++ ------------------------------------------------------------------------------------
70 // Details: Given a thread object start its (worker) thread to do work. The object is
71 // added to the *this manager for housekeeping and deletion of all thread objects.
72 // Type: Template method.
73 // Args: vrwThreadObj - (RW) A CMIUtilThreadActiveObjBase derived object.
74 // Return: MIstatus::success - Functional succeeded.
75 // MIstatus::failure - Functional failed.
80 CMICmnThreadMgrStd::ThreadStart(T &vrwThreadObj)
82 bool bOk = MIstatus::success;
84 // Grab a reference to the base object type
85 CMIUtilThreadActiveObjBase &rObj = static_cast<CMIUtilThreadActiveObjBase &>(vrwThreadObj);
87 // Add to the thread managers internal database
88 bOk &= AddThread(rObj);
91 const CMIUtilString errMsg(
92 CMIUtilString::Format(MIRSRC(IDS_THREADMGR_ERR_THREAD_FAIL_CREATE), vrwThreadObj.ThreadGetName().c_str()));
93 SetErrorDescription(errMsg);
94 return MIstatus::failure;
97 // Grab a reference on behalf of the caller
98 bOk &= vrwThreadObj.Acquire();
101 const CMIUtilString errMsg(
102 CMIUtilString::Format(MIRSRC(IDS_THREADMGR_ERR_THREAD_FAIL_CREATE), vrwThreadObj.ThreadGetName().c_str()));
103 SetErrorDescription(errMsg);
104 return MIstatus::failure;
107 // Thread is already started
108 // This call must come after the reference count increment
109 if (vrwThreadObj.ThreadIsActive())
111 // Early exit on thread already running condition
112 return MIstatus::success;
115 // Start the thread running
116 bOk &= vrwThreadObj.ThreadExecute();
119 const CMIUtilString errMsg(
120 CMIUtilString::Format(MIRSRC(IDS_THREADMGR_ERR_THREAD_FAIL_CREATE), vrwThreadObj.ThreadGetName().c_str()));
121 SetErrorDescription(errMsg);
122 return MIstatus::failure;
125 return MIstatus::success;