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