1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
|
//===-- MIUtilThreadBaseStd.cpp ---------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//++
// File: MIUtilThreadBaseStd.cpp
//
// Overview: CMIUtilThread implementation.
// CMIUtilThreadActiveObjBase implementation.
// CMIUtilThreadMutex implementation.
//
// Environment: Compilers: Visual C++ 12.
// gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1
// Libraries: See MIReadmetxt.
//
// Copyright: None.
//--
// Include compiler configuration
#include "MICmnConfig.h"
// Third Party Headers:
#include <assert.h>
// In-house headers:
#include "MIUtilThreadBaseStd.h"
#include "MICmnThreadMgrStd.h"
//++ ------------------------------------------------------------------------------------
// Details: Constructor.
// Type: None.
// Args: None.
// Return: None.
// Throws: None.
//--
CMIUtilThreadActiveObjBase::CMIUtilThreadActiveObjBase( void )
: m_references( 0 )
, m_bHasBeenKilled( false )
{
}
//++ ------------------------------------------------------------------------------------
// Details: Destructor.
// Type: None.
// Args: None.
// Return: None.
// Throws: None.
//--
CMIUtilThreadActiveObjBase::~CMIUtilThreadActiveObjBase( void )
{
// Make sure our thread is not alive before we die
m_thread.Join();
}
//++ ------------------------------------------------------------------------------------
// Details: Check if an object is already running.
// Type: Method.
// Args: None.
// Return: MIstatus::success - Functional succeeded.
// MIstatus::failure - Functional failed.
// Throws: None.
//--
bool CMIUtilThreadActiveObjBase::ThreadIsActive( void )
{
// Create a new thread to occupy this threads Run() function
return m_thread.IsActive();
}
//++ ------------------------------------------------------------------------------------
// Details: Set up *this thread.
// Type: Mrthod.
// Args: None.
// Return: MIstatus::success - Functional succeeded.
// MIstatus::failure - Functional failed.
// Throws: None.
//--
bool CMIUtilThreadActiveObjBase::ThreadExecute( void )
{
// Create a new thread to occupy this threads Run() function
return m_thread.Start( ThreadEntry, this );
}
//++ ------------------------------------------------------------------------------------
// Details: Aquire a reference to CMIUtilThreadActiveObjBase.
// Type: Method.
// Args: None.
// Return: MIstatus::success - Functional succeeded.
// MIstatus::failure - Functional failed.
// Throws: None.
//--
bool CMIUtilThreadActiveObjBase::Acquire( void )
{
// Access to this function is serial
CMIUtilThreadLock serial( m_mutex );
// >0 == *this thread is alive
m_references++;
return MIstatus::success;
}
//++ ------------------------------------------------------------------------------------
// Details: Release a reference to CMIUtilThreadActiveObjBase.
// Type: Method.
// Args: None.
// Return: MIstatus::success - Functional succeeded.
// MIstatus::failure - Functional failed.
// Throws: None.
//--
bool CMIUtilThreadActiveObjBase::Release( void )
{
// Access to this function is serial
CMIUtilThreadLock serial( m_mutex );
// 0 == kill off *this thread
m_references--;
return MIstatus::success;
}
//++ ------------------------------------------------------------------------------------
// Details: Force this thread to stop, regardless of references
// Type: Method.
// Args: None.
// Return: MIstatus::success - Functional succeeded.
// MIstatus::failure - Functional failed.
// Throws: None.
//--
bool CMIUtilThreadActiveObjBase::ThreadKill( void )
{
// Access to this function is serial
CMIUtilThreadLock serial( m_mutex );
// Set this thread to killed status
m_bHasBeenKilled = true;
return MIstatus::success;
}
//++ ------------------------------------------------------------------------------------
// Details: Proxy to thread join.
// Type: Method.
// Args: None.
// Return: MIstatus::success - Functional succeeded.
// MIstatus::failure - Functional failed.
// Throws: None.
//--
bool CMIUtilThreadActiveObjBase::ThreadJoin( void )
{
return m_thread.Join();
}
//++ ------------------------------------------------------------------------------------
// Details: This function is the entry point of this object thread.
// It is a trampoline to an instances operation manager.
// Type: Static method.
// Args: vpThisClass - (R) From the system (our CMIUtilThreadActiveObjBase from the ctor).
// Return: MIuint - 0 = success.
// Throws: None.
//--
MIuint CMIUtilThreadActiveObjBase::ThreadEntry( void * vpThisClass )
{
// The argument is a pointer to a CMIUtilThreadActiveObjBase class
// as passed from the initialize function, so we can safely cast it.
assert( vpThisClass != nullptr );
CMIUtilThreadActiveObjBase * pActive = reinterpret_cast< CMIUtilThreadActiveObjBase * >( vpThisClass );
// Start the management routine of this object
pActive->ThreadManage();
// Thread death
return 0;
}
//++ ------------------------------------------------------------------------------------
// Details: This function forms a small management routine, to handle the thread's running.
// Type: Method.
// Args: None.
// Return: None.
// Throws: None.
//--
void CMIUtilThreadActiveObjBase::ThreadManage( void )
{
bool bAlive = true;
// Infinite loop
while( bAlive )
{
// Scope the lock while we access m_isDying
{
// Lock down access to the interface
CMIUtilThreadLock serial( m_mutex );
// Quit the run loop if we are dying
if( m_references == 0 )
break;
}
// Execute the run routine
if( !ThreadRun( bAlive ) )
// Thread's run function failed (MIstatus::failure)
break;
// We will die if we have been signaled to die
bAlive &= !m_bHasBeenKilled;
}
// Execute the finish routine just before we die
// to give the object a chance to clean up
ThreadFinish();
}
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
//
CMIUtilThread::CMIUtilThread( void )
: m_pThread( nullptr )
{
}
//++ ------------------------------------------------------------------------------------
// Details: CMIUtilThread destructor.
// Type: Method.
// Args: None.
// Return: None.
// Throws: None.
//--
CMIUtilThread::~CMIUtilThread( void )
{
Join();
}
//++ ------------------------------------------------------------------------------------
// Details: Wait for thread to stop.
// Type: Method.
// Args: None.
// Return: MIstatus::success - Functional succeeded.
// MIstatus::failure - Functional failed.
// Throws: None.
//--
bool CMIUtilThread::Join( void )
{
if( m_pThread != nullptr )
{
// Wait for this thread to die
m_pThread->join();
// Scope the thread lock while we modify the pointer
{
CMIUtilThreadLock _lock( m_mutex );
delete m_pThread;
m_pThread = nullptr;
}
}
return MIstatus::success;
}
//++ ------------------------------------------------------------------------------------
// Details: Is the thread doing work.
// Type: Method.
// Args: None.
// Return: bool - True = Yes active, false = not active.
// Throws: None.
//--
bool CMIUtilThread::IsActive( void )
{
// Lock while we access the thread pointer
CMIUtilThreadLock _lock( m_mutex );
if( m_pThread == nullptr )
return false;
else
return true;
}
//++ ------------------------------------------------------------------------------------
// Details: Set up *this thread.
// Type: Method.
// Args: vpFn (R) - Function pointer to thread's main function.
// vpArg (R) - Pointer arguments to pass to the thread.
// Return: MIstatus::success - Functional succeeded.
// MIstatus::failure - Functional failed.
// Throws: None.
//--
bool CMIUtilThread::Start( FnThreadProc vpFn, void * vpArg )
{
// Create the std thread, which starts immediately
m_pThread = new std::thread( vpFn, vpArg );
// We expect to always be able to create one
assert( m_pThread != nullptr );
return MIstatus::success;
}
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
//++ ------------------------------------------------------------------------------------
// Details: Take resource.
// Type: Method.
// Args: None.
// Return: None.
// Throws: None.
//--
void CMIUtilThreadMutex::Lock( void )
{
m_mutex.lock();
}
//++ ------------------------------------------------------------------------------------
// Details: Release resource.
// Type: Method.
// Args: None.
// Return: None.
// Throws: None.
//--
void CMIUtilThreadMutex::Unlock( void )
{
m_mutex.unlock();
}
//++ ------------------------------------------------------------------------------------
// Details: Take resource if available. Immediately return in either case.
// Type: Method.
// Args: None.
// Return: True - mutex has been locked.
// False - mutex could not be locked.
// Throws: None.
//--
bool CMIUtilThreadMutex::TryLock( void )
{
return m_mutex.try_lock();
}
|