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