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 //===----------------------------------------------------------------------===//
12 // Third party headers:
17 #include "MIDataTypes.h"
18 #include "MIUtilString.h"
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.
25 class CMIUtilThreadMutex {
28 /* ctor */ CMIUtilThreadMutex() {}
30 void Lock(); // Wait until mutex can be obtained
31 void Unlock(); // Release the mutex
32 bool TryLock(); // Gain the lock if available
37 /* dtor */ virtual ~CMIUtilThreadMutex() {}
41 std::recursive_mutex m_mutex;
45 //============================================================================
46 // Details: MI common code utility class. Thread object.
51 typedef MIuint (*FnThreadProc)(void *vpThisClass);
55 /* ctor */ CMIUtilThread();
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
64 /* dtor */ virtual ~CMIUtilThread();
68 CMIUtilThreadMutex m_mutex;
69 std::thread *m_pThread;
74 //============================================================================
75 // Details: MI common code utility class. Base class for a worker thread active
76 // object. Runs an 'captive thread'.
78 class CMIUtilThreadActiveObjBase {
81 /* ctor */ CMIUtilThreadActiveObjBase();
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
93 /* dtor */ virtual ~CMIUtilThreadActiveObjBase();
95 // Each thread object must supple a unique name that can be used to locate it
96 virtual const CMIUtilString &ThreadGetName() const = 0;
100 static MIuint ThreadEntry(void *vpThisClass); // Thread entry point
104 virtual bool ThreadRun(bool &vrIsAlive) = 0; // Call the main worker method
105 virtual bool ThreadFinish() = 0; // Finish of what you were doing
109 volatile MIuint m_references; // Stores the current lifetime state of this
110 // thread, 0 = running, > 0 = shutting down
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
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.
125 class CMIUtilThreadLock {
129 CMIUtilThreadLock(CMIUtilThreadMutex &vMutex) : m_rMutex(vMutex) {
136 virtual ~CMIUtilThreadLock() { m_rMutex.Unlock(); }
140 CMIUtilThreadMutex &m_rMutex;