]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/tools/lldb-mi/MIUtilThreadBaseStd.h
Merge llvm 3.6.0rc1 from ^/vendor/llvm/dist, merge clang 3.6.0rc1 from
[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 Lock( void );              // Wait until mutex can be obtained
52     void Unlock( void );        // Release the mutex
53         bool TryLock( void );   // Gain the lock if available
54
55 // Overrideable:
56 public:
57         // From CMICmnBase
58         /* dtor */ virtual ~CMIUtilThreadMutex( void ) { };
59    
60 // Attributes:
61 private:
62         std::recursive_mutex m_mutex;
63 };
64
65 //++ ============================================================================
66 // Details:     MI common code utility class. Thread object.
67 // Gotchas:     None.
68 // Authors:     Aidan Dodds 10/03/2014.
69 // Changes:     None.
70 //--
71 class CMIUtilThread
72 {
73 // Typedef:
74 public:
75         typedef MIuint (* FnThreadProc) (void * vpThisClass);
76         
77 // Methods:
78 public:
79         /* ctor */      CMIUtilThread( void );
80         //
81         bool Start( FnThreadProc vpFn, void * vpArg );  // Start execution of this thread
82         bool Join( void );                                                              // Wait for this thread to stop
83         bool IsActive( void );                                                  // Returns true if this thread is running
84
85 // Overrideable:
86 public:
87         /* dtor */ virtual ~CMIUtilThread( void );
88
89 // Methods:
90 private:
91         CMIUtilThreadMutex      m_mutex;
92         std::thread *           m_pThread;
93 };
94
95 //++ ============================================================================
96 // Details:     MI common code utility class. Base class for a worker thread active 
97 //                      object. Runs an 'captive thread'.
98 // Gotchas:     None.
99 // Authors:     Aidan Dodds 10/03/2014..
100 // Changes:     None.
101 //--
102 class CMIUtilThreadActiveObjBase
103 {
104 // Methods:
105 public:
106         /* ctor */      CMIUtilThreadActiveObjBase( void );
107         //
108         bool Acquire( void );                   // Obtain a reference to this object
109         bool Release( void );                   // Release a reference to this object
110         bool ThreadIsActive( void );    // Return true if this object is running
111         bool ThreadJoin( void );                // Wait for this thread to stop running
112         bool ThreadKill( void );                // Force this thread to stop, regardless of references
113         bool ThreadExecute( void );             // Start this objects execution in another thread
114         void ThreadManage( void );
115
116 // Overrideable:
117 public:
118         /* dtor */ virtual ~CMIUtilThreadActiveObjBase( void );
119         //      
120         // Each thread object must supple a unique name that can be used to locate it
121         virtual const CMIUtilString & ThreadGetName( void ) const = 0;
122
123 // Statics:
124 protected:
125         static MIuint ThreadEntry( void * vpThisClass );        // Thread entry point
126
127 // Overrideable:
128 protected:
129         virtual bool ThreadRun( bool &vrIsAlive ) = 0;  // Call the main worker method
130         virtual bool ThreadFinish( void ) = 0;                  // Finish of what you were doing
131
132 // Attributes:
133 protected:
134         volatile MIuint         m_references;           // Stores the current lifetime state of this thread, 0 = running, > 0 = shutting down
135         volatile bool           m_bHasBeenKilled;       // Set to true when this thread has been killed
136         CMIUtilThread           m_thread;                       // The execution thread
137         CMIUtilThreadMutex      m_mutex;                        // This mutex allows us to safely communicate with this thread object across the interface from multiple threads
138 };
139
140 //++ ============================================================================
141 // Details:     MI common code utility class. Handle thread resource locking.
142 //                      Put Locks inside all the methods of your Active Object that access 
143 //                      data shared with the captive thread.
144 // Gotchas:     None.
145 // Authors:     Aidan Dodds 10/03/2014.
146 // Changes:     None.
147 //--
148 class CMIUtilThreadLock
149 {
150 // Methods:
151 public:
152         /* ctor */
153         CMIUtilThreadLock( CMIUtilThreadMutex & vMutex )
154         :       m_rMutex( vMutex )
155         {
156                 m_rMutex.Lock();
157         }
158
159 // Overrideable:
160 public:
161         /* dtor */
162         virtual ~CMIUtilThreadLock( void )
163         {
164                 m_rMutex.Unlock();
165         }
166
167 // Attributes:
168 private:
169     CMIUtilThreadMutex & m_rMutex;
170 };