1 //===-- MICmnThreadMgrStd.h -------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
11 // Third party headers:
15 #include "MICmnBase.h"
16 #include "MICmnResources.h"
17 #include "MIUtilSingletonBase.h"
18 #include "MIUtilThreadBaseStd.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 class CMICmnThreadMgrStd : public CMICmnBase,
30 public MI::ISingleton<CMICmnThreadMgrStd> {
31 friend MI::ISingleton<CMICmnThreadMgrStd>;
35 bool Initialize() override;
36 bool Shutdown() override;
37 bool ThreadAllTerminate(); // Ask all threads to stop (caution)
38 template <typename T> // Ask the thread manager to start and stop threads on
40 bool ThreadStart(T &vrwObject);
44 typedef std::vector<CMIUtilThreadActiveObjBase *> ThreadList_t;
48 /* ctor */ CMICmnThreadMgrStd();
49 /* ctor */ CMICmnThreadMgrStd(const CMICmnThreadMgrStd &);
50 void operator=(const CMICmnThreadMgrStd &);
52 bool AddThread(const CMIUtilThreadActiveObjBase &
53 vrObj); // Add a thread for monitoring by the threadmanager
58 /* dtor */ ~CMICmnThreadMgrStd() override;
62 CMIUtilThreadMutex m_mutex;
63 ThreadList_t m_threadList;
67 // Details: Given a thread object start its (worker) thread to do work. The
69 // added to the *this manager for housekeeping and deletion of all
71 // Type: Template method.
72 // Args: vrwThreadObj - (RW) A CMIUtilThreadActiveObjBase derived
74 // Return: MIstatus::success - Functional succeeded.
75 // MIstatus::failure - Functional failed.
78 template <typename T> bool CMICmnThreadMgrStd::ThreadStart(T &vrwThreadObj) {
79 bool bOk = MIstatus::success;
81 // Grab a reference to the base object type
82 CMIUtilThreadActiveObjBase &rObj =
83 static_cast<CMIUtilThreadActiveObjBase &>(vrwThreadObj);
85 // Add to the thread managers internal database
86 bOk &= AddThread(rObj);
88 const CMIUtilString errMsg(
89 CMIUtilString::Format(MIRSRC(IDS_THREADMGR_ERR_THREAD_FAIL_CREATE),
90 vrwThreadObj.ThreadGetName().c_str()));
91 SetErrorDescription(errMsg);
92 return MIstatus::failure;
95 // Grab a reference on behalf of the caller
96 bOk &= vrwThreadObj.Acquire();
98 const CMIUtilString errMsg(
99 CMIUtilString::Format(MIRSRC(IDS_THREADMGR_ERR_THREAD_FAIL_CREATE),
100 vrwThreadObj.ThreadGetName().c_str()));
101 SetErrorDescription(errMsg);
102 return MIstatus::failure;
105 // Thread is already started
106 // This call must come after the reference count increment
107 if (vrwThreadObj.ThreadIsActive()) {
108 // Early exit on thread already running condition
109 return MIstatus::success;
112 // Start the thread running
113 bOk &= vrwThreadObj.ThreadExecute();
115 const CMIUtilString errMsg(
116 CMIUtilString::Format(MIRSRC(IDS_THREADMGR_ERR_THREAD_FAIL_CREATE),
117 vrwThreadObj.ThreadGetName().c_str()));
118 SetErrorDescription(errMsg);
119 return MIstatus::failure;
122 return MIstatus::success;