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