1 //===-- MIUtilThreadBaseStd.h -----------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
11 // Third party headers:
16 #include "MIDataTypes.h"
17 #include "MIUtilString.h"
20 //============================================================================
21 // Details: MI common code utility class. Handle thread mutual exclusion.
22 // Embed Mutexes in your Active Object and then use them through Locks.
24 class CMIUtilThreadMutex {
27 /* ctor */ CMIUtilThreadMutex() {}
29 void Lock(); // Wait until mutex can be obtained
30 void Unlock(); // Release the mutex
31 bool TryLock(); // Gain the lock if available
36 /* dtor */ virtual ~CMIUtilThreadMutex() {}
40 std::recursive_mutex m_mutex;
44 //============================================================================
45 // Details: MI common code utility class. Thread object.
50 typedef MIuint (*FnThreadProc)(void *vpThisClass);
54 /* ctor */ CMIUtilThread();
56 bool Start(FnThreadProc vpFn, void *vpArg); // Start execution of this thread
57 bool Join(); // Wait for this thread to stop
58 bool IsActive(); // Returns true if this thread is running
59 void Finish(); // Finish this thread
63 /* dtor */ virtual ~CMIUtilThread();
67 CMIUtilThreadMutex m_mutex;
68 std::thread *m_pThread;
73 //============================================================================
74 // Details: MI common code utility class. Base class for a worker thread active
75 // object. Runs an 'captive thread'.
77 class CMIUtilThreadActiveObjBase {
80 /* ctor */ CMIUtilThreadActiveObjBase();
82 bool Acquire(); // Obtain a reference to this object
83 bool Release(); // Release a reference to this object
84 bool ThreadIsActive(); // Return true if this object is running
85 bool ThreadJoin(); // Wait for this thread to stop running
86 bool ThreadKill(); // Force this thread to stop, regardless of references
87 bool ThreadExecute(); // Start this objects execution in another thread
92 /* dtor */ virtual ~CMIUtilThreadActiveObjBase();
94 // Each thread object must supple a unique name that can be used to locate it
95 virtual const CMIUtilString &ThreadGetName() const = 0;
99 static MIuint ThreadEntry(void *vpThisClass); // Thread entry point
103 virtual bool ThreadRun(bool &vrIsAlive) = 0; // Call the main worker method
104 virtual bool ThreadFinish() = 0; // Finish of what you were doing
108 volatile MIuint m_references; // Stores the current lifetime state of this
109 // thread, 0 = running, > 0 = shutting down
111 m_bHasBeenKilled; // Set to true when this thread has been killed
112 CMIUtilThread m_thread; // The execution thread
113 CMIUtilThreadMutex m_mutex; // This mutex allows us to safely communicate with
114 // this thread object across the interface from
119 //============================================================================
120 // Details: MI common code utility class. Handle thread resource locking.
121 // Put Locks inside all the methods of your Active Object that access
122 // data shared with the captive thread.
124 class CMIUtilThreadLock {
128 CMIUtilThreadLock(CMIUtilThreadMutex &vMutex) : m_rMutex(vMutex) {
135 virtual ~CMIUtilThreadLock() { m_rMutex.Unlock(); }
139 CMIUtilThreadMutex &m_rMutex;