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:
20 #include "MIDataTypes.h"
21 #include "MIUtilString.h"
24 //============================================================================
25 // Details: MI common code utility class. Handle thread mutual exclusion.
26 // Embed Mutexes in your Active Object and then use them through Locks.
28 class CMIUtilThreadMutex {
31 /* ctor */ CMIUtilThreadMutex() {}
33 void Lock(); // Wait until mutex can be obtained
34 void Unlock(); // Release the mutex
35 bool TryLock(); // Gain the lock if available
40 /* dtor */ virtual ~CMIUtilThreadMutex() {}
44 std::recursive_mutex m_mutex;
48 //============================================================================
49 // Details: MI common code utility class. Thread object.
54 typedef MIuint (*FnThreadProc)(void *vpThisClass);
58 /* ctor */ CMIUtilThread();
60 bool Start(FnThreadProc vpFn, void *vpArg); // Start execution of this thread
61 bool Join(); // Wait for this thread to stop
62 bool IsActive(); // Returns true if this thread is running
63 void Finish(); // Finish this thread
67 /* dtor */ virtual ~CMIUtilThread();
71 CMIUtilThreadMutex m_mutex;
72 std::thread *m_pThread;
77 //============================================================================
78 // Details: MI common code utility class. Base class for a worker thread active
79 // object. Runs an 'captive thread'.
81 class CMIUtilThreadActiveObjBase {
84 /* ctor */ CMIUtilThreadActiveObjBase();
86 bool Acquire(); // Obtain a reference to this object
87 bool Release(); // Release a reference to this object
88 bool ThreadIsActive(); // Return true if this object is running
89 bool ThreadJoin(); // Wait for this thread to stop running
90 bool ThreadKill(); // Force this thread to stop, regardless of references
91 bool ThreadExecute(); // Start this objects execution in another thread
96 /* dtor */ virtual ~CMIUtilThreadActiveObjBase();
98 // Each thread object must supple a unique name that can be used to locate it
99 virtual const CMIUtilString &ThreadGetName() const = 0;
103 static MIuint ThreadEntry(void *vpThisClass); // Thread entry point
107 virtual bool ThreadRun(bool &vrIsAlive) = 0; // Call the main worker method
108 virtual bool ThreadFinish() = 0; // Finish of what you were doing
112 volatile MIuint m_references; // Stores the current lifetime state of this
113 // thread, 0 = running, > 0 = shutting down
115 m_bHasBeenKilled; // Set to true when this thread has been killed
116 CMIUtilThread m_thread; // The execution thread
117 CMIUtilThreadMutex m_mutex; // This mutex allows us to safely communicate with
118 // this thread object across the interface from
123 //============================================================================
124 // Details: MI common code utility class. Handle thread resource locking.
125 // Put Locks inside all the methods of your Active Object that access
126 // data shared with the captive thread.
128 class CMIUtilThreadLock {
132 CMIUtilThreadLock(CMIUtilThreadMutex &vMutex) : m_rMutex(vMutex) {
139 virtual ~CMIUtilThreadLock() { m_rMutex.Unlock(); }
143 CMIUtilThreadMutex &m_rMutex;