1 //===-- MIUtilThreadBaseStd.h -----------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 // File: MIUtilThreadBaseStd.h
13 // Overview: CMIUtilThread interface.
14 // CMIUtilThreadActiveObjBase interface.
15 // CMIUtilThreadMutex interface.
16 // CMIUtilThreadLock interface.
18 // Environment: Compilers: Visual C++ 12.
19 // gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1
20 // Libraries: See MIReadmetxt.
27 // Third party headers:
35 #include "MIDataTypes.h"
36 #include "MIUtilString.h"
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.
42 // Authors: Aidan Dodds 10/03/2014.
45 class CMIUtilThreadMutex
49 /* ctor */ CMIUtilThreadMutex( void ) { };
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
58 /* dtor */ virtual ~CMIUtilThreadMutex( void ) { };
62 std::recursive_mutex m_mutex;
65 //++ ============================================================================
66 // Details: MI common code utility class. Thread object.
68 // Authors: Aidan Dodds 10/03/2014.
75 typedef MIuint (* FnThreadProc) (void * vpThisClass);
79 /* ctor */ CMIUtilThread( void );
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
87 /* dtor */ virtual ~CMIUtilThread( void );
91 CMIUtilThreadMutex m_mutex;
92 std::thread * m_pThread;
95 //++ ============================================================================
96 // Details: MI common code utility class. Base class for a worker thread active
97 // object. Runs an 'captive thread'.
99 // Authors: Aidan Dodds 10/03/2014..
102 class CMIUtilThreadActiveObjBase
106 /* ctor */ CMIUtilThreadActiveObjBase( void );
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 );
118 /* dtor */ virtual ~CMIUtilThreadActiveObjBase( void );
120 // Each thread object must supple a unique name that can be used to locate it
121 virtual const CMIUtilString & ThreadGetName( void ) const = 0;
125 static MIuint ThreadEntry( void * vpThisClass ); // Thread entry point
129 virtual bool ThreadRun( bool &vrIsAlive ) = 0; // Call the main worker method
130 virtual bool ThreadFinish( void ) = 0; // Finish of what you were doing
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
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.
145 // Authors: Aidan Dodds 10/03/2014.
148 class CMIUtilThreadLock
153 CMIUtilThreadLock( CMIUtilThreadMutex & vMutex )
162 virtual ~CMIUtilThreadLock( void )
169 CMIUtilThreadMutex & m_rMutex;