]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/tools/lldb-mi/MICmnThreadMgrStd.h
Update lldb to upstream trunk r242221.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / tools / lldb-mi / MICmnThreadMgrStd.h
1 //===-- MICmnThreadMgrStd.h -------------------------------------*- 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 #pragma once
11
12 // Third party headers:
13 #include <vector>
14
15 // In-house headers:
16 #include "MICmnBase.h"
17 #include "MIUtilThreadBaseStd.h"
18 #include "MICmnResources.h"
19 #include "MIUtilSingletonBase.h"
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 // Gotchas: None.
29 // Authors: Aidan Dodds 12/03/2014.
30 // Changes: None.
31 //--
32 class CMICmnThreadMgrStd : public CMICmnBase, public MI::ISingleton<CMICmnThreadMgrStd>
33 {
34     friend MI::ISingleton<CMICmnThreadMgrStd>;
35
36     // Methods:
37   public:
38     bool Initialize(void) override;
39     bool Shutdown(void) override;
40     bool
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);
44
45     // Typedef:
46   private:
47     typedef std::vector<CMIUtilThreadActiveObjBase *> ThreadList_t;
48
49     // Methods:
50   private:
51     /* ctor */ CMICmnThreadMgrStd(void);
52     /* ctor */ CMICmnThreadMgrStd(const CMICmnThreadMgrStd &);
53     void operator=(const CMICmnThreadMgrStd &);
54     //
55     bool
56     AddThread(const CMIUtilThreadActiveObjBase &vrObj); // Add a thread for monitoring by the threadmanager
57
58     // Overridden:
59   private:
60     // From CMICmnBase
61     /* dtor */ ~CMICmnThreadMgrStd(void) override;
62
63     // Attributes:
64   private:
65     CMIUtilThreadMutex m_mutex;
66     ThreadList_t m_threadList;
67 };
68
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.
76 // Throws:  None.
77 //--
78 template <typename T>
79 bool
80 CMICmnThreadMgrStd::ThreadStart(T &vrwThreadObj)
81 {
82     bool bOk = MIstatus::success;
83
84     // Grab a reference to the base object type
85     CMIUtilThreadActiveObjBase &rObj = static_cast<CMIUtilThreadActiveObjBase &>(vrwThreadObj);
86
87     // Add to the thread managers internal database
88     bOk &= AddThread(rObj);
89     if (!bOk)
90     {
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;
95     }
96
97     // Grab a reference on behalf of the caller
98     bOk &= vrwThreadObj.Acquire();
99     if (!bOk)
100     {
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;
105     }
106
107     // Thread is already started
108     // This call must come after the reference count increment
109     if (vrwThreadObj.ThreadIsActive())
110     {
111         // Early exit on thread already running condition
112         return MIstatus::success;
113     }
114
115     // Start the thread running
116     bOk &= vrwThreadObj.ThreadExecute();
117     if (!bOk)
118     {
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;
123     }
124
125     return MIstatus::success;
126 }