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){};
52 Lock(void); // Wait until mutex can be obtained
54 Unlock(void); // Release the mutex
56 TryLock(void); // Gain the lock if available
61 /* dtor */ virtual ~CMIUtilThreadMutex(void){};
65 std::recursive_mutex m_mutex;
68 //++ ============================================================================
69 // Details: MI common code utility class. Thread object.
71 // Authors: Aidan Dodds 10/03/2014.
78 typedef MIuint (*FnThreadProc)(void *vpThisClass);
82 /* ctor */ CMIUtilThread(void);
85 Start(FnThreadProc vpFn, void *vpArg); // Start execution of this thread
87 Join(void); // Wait for this thread to stop
89 IsActive(void); // Returns true if this thread is running
93 /* dtor */ virtual ~CMIUtilThread(void);
97 CMIUtilThreadMutex m_mutex;
98 std::thread *m_pThread;
101 //++ ============================================================================
102 // Details: MI common code utility class. Base class for a worker thread active
103 // object. Runs an 'captive thread'.
105 // Authors: Aidan Dodds 10/03/2014..
108 class CMIUtilThreadActiveObjBase
112 /* ctor */ CMIUtilThreadActiveObjBase(void);
115 Acquire(void); // Obtain a reference to this object
117 Release(void); // Release a reference to this object
119 ThreadIsActive(void); // Return true if this object is running
121 ThreadJoin(void); // Wait for this thread to stop running
123 ThreadKill(void); // Force this thread to stop, regardless of references
125 ThreadExecute(void); // Start this objects execution in another thread
126 void ThreadManage(void);
130 /* dtor */ virtual ~CMIUtilThreadActiveObjBase(void);
132 // Each thread object must supple a unique name that can be used to locate it
133 virtual const CMIUtilString &ThreadGetName(void) const = 0;
138 ThreadEntry(void *vpThisClass); // Thread entry point
143 ThreadRun(bool &vrIsAlive) = 0; // Call the main worker method
145 ThreadFinish(void) = 0; // Finish of what you were doing
149 volatile MIuint m_references; // Stores the current lifetime state of this thread, 0 = running, > 0 = shutting down
150 volatile bool m_bHasBeenKilled; // Set to true when this thread has been killed
151 CMIUtilThread m_thread; // The execution thread
153 m_mutex; // This mutex allows us to safely communicate with this thread object across the interface from multiple threads
156 //++ ============================================================================
157 // Details: MI common code utility class. Handle thread resource locking.
158 // Put Locks inside all the methods of your Active Object that access
159 // data shared with the captive thread.
161 // Authors: Aidan Dodds 10/03/2014.
164 class CMIUtilThreadLock
169 CMIUtilThreadLock(CMIUtilThreadMutex &vMutex)
178 virtual ~CMIUtilThreadLock(void) { m_rMutex.Unlock(); }
182 CMIUtilThreadMutex &m_rMutex;