]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/tools/lldb-mi/MIUtilThreadBaseStd.h
Update LLDB snapshot to upstream r225923 (git 2b588ecd)
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / tools / lldb-mi / MIUtilThreadBaseStd.h
1 //===-- MIUtilThreadBaseStd.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 //++
11 // File:        MIUtilThreadBaseStd.h
12 //
13 // Overview:    CMIUtilThread interface.
14 //              CMIUtilThreadActiveObjBase interface.
15 //              CMIUtilThreadMutex interface.
16 //              CMIUtilThreadLock interface.
17 //
18 // Environment: Compilers:  Visual C++ 12.
19 //                          gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1
20 //              Libraries:  See MIReadmetxt.
21 //
22 // Copyright:   None.
23 //--
24
25 #pragma once
26
27 // Third party headers:
28 #ifdef _MSC_VER
29 #include <eh.h>
30 #endif // _MSC_VER
31 #include <thread>
32 #include <mutex>
33
34 // In-house headers:
35 #include "MIDataTypes.h"
36 #include "MIUtilString.h"
37
38 //++ ============================================================================
39 // Details: MI common code utility class. Handle thread mutual exclusion.
40 //          Embed Mutexes in your Active Object and then use them through Locks.
41 // Gotchas: None.
42 // Authors: Aidan Dodds 10/03/2014.
43 // Changes: None.
44 //--
45 class CMIUtilThreadMutex
46 {
47     // Methods:
48   public:
49     /* ctor */ CMIUtilThreadMutex(void){};
50     //
51     void
52     Lock(void); // Wait until mutex can be obtained
53     void
54     Unlock(void); // Release the mutex
55     bool
56     TryLock(void); // Gain the lock if available
57
58     // Overrideable:
59   public:
60     // From CMICmnBase
61     /* dtor */ virtual ~CMIUtilThreadMutex(void){};
62
63     // Attributes:
64   private:
65     std::recursive_mutex m_mutex;
66 };
67
68 //++ ============================================================================
69 // Details: MI common code utility class. Thread object.
70 // Gotchas: None.
71 // Authors: Aidan Dodds 10/03/2014.
72 // Changes: None.
73 //--
74 class CMIUtilThread
75 {
76     // Typedef:
77   public:
78     typedef MIuint (*FnThreadProc)(void *vpThisClass);
79
80     // Methods:
81   public:
82     /* ctor */ CMIUtilThread(void);
83     //
84     bool
85     Start(FnThreadProc vpFn, void *vpArg); // Start execution of this thread
86     bool
87     Join(void); // Wait for this thread to stop
88     bool
89     IsActive(void); // Returns true if this thread is running
90
91     // Overrideable:
92   public:
93     /* dtor */ virtual ~CMIUtilThread(void);
94
95     // Methods:
96   private:
97     CMIUtilThreadMutex m_mutex;
98     std::thread *m_pThread;
99 };
100
101 //++ ============================================================================
102 // Details: MI common code utility class. Base class for a worker thread active
103 //          object. Runs an 'captive thread'.
104 // Gotchas: None.
105 // Authors: Aidan Dodds 10/03/2014..
106 // Changes: None.
107 //--
108 class CMIUtilThreadActiveObjBase
109 {
110     // Methods:
111   public:
112     /* ctor */ CMIUtilThreadActiveObjBase(void);
113     //
114     bool
115     Acquire(void); // Obtain a reference to this object
116     bool
117     Release(void); // Release a reference to this object
118     bool
119     ThreadIsActive(void); // Return true if this object is running
120     bool
121     ThreadJoin(void); // Wait for this thread to stop running
122     bool
123     ThreadKill(void); // Force this thread to stop, regardless of references
124     bool
125     ThreadExecute(void); // Start this objects execution in another thread
126     void ThreadManage(void);
127
128     // Overrideable:
129   public:
130     /* dtor */ virtual ~CMIUtilThreadActiveObjBase(void);
131     //
132     // Each thread object must supple a unique name that can be used to locate it
133     virtual const CMIUtilString &ThreadGetName(void) const = 0;
134
135     // Statics:
136   protected:
137     static MIuint
138     ThreadEntry(void *vpThisClass); // Thread entry point
139
140     // Overrideable:
141   protected:
142     virtual bool
143     ThreadRun(bool &vrIsAlive) = 0; // Call the main worker method
144     virtual bool
145     ThreadFinish(void) = 0; // Finish of what you were doing
146
147     // Attributes:
148   protected:
149     volatile MIuint m_references;   // Stores the current lifetime state of this thread, 0 = running, > 0 = shutting down
150     volatile bool m_bHasBeenKilled; // Set to true when this thread has been killed
151     CMIUtilThread m_thread;         // The execution thread
152     CMIUtilThreadMutex
153         m_mutex; // This mutex allows us to safely communicate with this thread object across the interface from multiple threads
154 };
155
156 //++ ============================================================================
157 // Details: MI common code utility class. Handle thread resource locking.
158 //          Put Locks inside all the methods of your Active Object that access
159 //          data shared with the captive thread.
160 // Gotchas: None.
161 // Authors: Aidan Dodds 10/03/2014.
162 // Changes: None.
163 //--
164 class CMIUtilThreadLock
165 {
166     // Methods:
167   public:
168     /* ctor */
169     CMIUtilThreadLock(CMIUtilThreadMutex &vMutex)
170         : m_rMutex(vMutex)
171     {
172         m_rMutex.Lock();
173     }
174
175     // Overrideable:
176   public:
177     /* dtor */
178     virtual ~CMIUtilThreadLock(void) { m_rMutex.Unlock(); }
179
180     // Attributes:
181   private:
182     CMIUtilThreadMutex &m_rMutex;
183 };