]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/lldb/tools/lldb-mi/MICmnThreadMgrStd.h
MFC r355940:
[FreeBSD/FreeBSD.git] / contrib / llvm-project / lldb / tools / lldb-mi / MICmnThreadMgrStd.h
1 //===-- MICmnThreadMgrStd.h -------------------------------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8
9 #pragma once
10
11 // Third party headers:
12 #include <vector>
13
14 // In-house headers:
15 #include "MICmnBase.h"
16 #include "MICmnResources.h"
17 #include "MIUtilSingletonBase.h"
18 #include "MIUtilThreadBaseStd.h"
19
20 //++
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
26 //          shutdown.
27 //          Singleton class.
28 //--
29 class CMICmnThreadMgrStd : public CMICmnBase,
30                            public MI::ISingleton<CMICmnThreadMgrStd> {
31   friend MI::ISingleton<CMICmnThreadMgrStd>;
32
33   // Methods:
34 public:
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
39                         // our behalf
40                         bool ThreadStart(T &vrwObject);
41
42   // Typedef:
43 private:
44   typedef std::vector<CMIUtilThreadActiveObjBase *> ThreadList_t;
45
46   // Methods:
47 private:
48   /* ctor */ CMICmnThreadMgrStd();
49   /* ctor */ CMICmnThreadMgrStd(const CMICmnThreadMgrStd &);
50   void operator=(const CMICmnThreadMgrStd &);
51   //
52   bool AddThread(const CMIUtilThreadActiveObjBase &
53                      vrObj); // Add a thread for monitoring by the threadmanager
54
55   // Overridden:
56 private:
57   // From CMICmnBase
58   /* dtor */ ~CMICmnThreadMgrStd() override;
59
60   // Attributes:
61 private:
62   CMIUtilThreadMutex m_mutex;
63   ThreadList_t m_threadList;
64 };
65
66 //++
67 // Details: Given a thread object start its (worker) thread to do work. The
68 // object is
69 //          added to the *this manager for housekeeping and deletion of all
70 //          thread objects.
71 // Type:    Template method.
72 // Args:    vrwThreadObj      - (RW) A CMIUtilThreadActiveObjBase derived
73 // object.
74 // Return:  MIstatus::success - Functional succeeded.
75 //          MIstatus::failure - Functional failed.
76 // Throws:  None.
77 //--
78 template <typename T> bool CMICmnThreadMgrStd::ThreadStart(T &vrwThreadObj) {
79   bool bOk = MIstatus::success;
80
81   // Grab a reference to the base object type
82   CMIUtilThreadActiveObjBase &rObj =
83       static_cast<CMIUtilThreadActiveObjBase &>(vrwThreadObj);
84
85   // Add to the thread managers internal database
86   bOk &= AddThread(rObj);
87   if (!bOk) {
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;
93   }
94
95   // Grab a reference on behalf of the caller
96   bOk &= vrwThreadObj.Acquire();
97   if (!bOk) {
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;
103   }
104
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;
110   }
111
112   // Start the thread running
113   bOk &= vrwThreadObj.ThreadExecute();
114   if (!bOk) {
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;
120   }
121
122   return MIstatus::success;
123 }