2014-05-16 18:51:01 +08:00
|
|
|
//===-- MIUtilThreadBaseStd.cpp ---------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// Third Party Headers:
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
// In-house headers:
|
|
|
|
#include "MIUtilThreadBaseStd.h"
|
|
|
|
#include "MICmnThreadMgrStd.h"
|
|
|
|
|
|
|
|
//++ ------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: Constructor.
|
|
|
|
// Type: None.
|
|
|
|
// Args: None.
|
|
|
|
// Return: None.
|
|
|
|
// Throws: None.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2015-08-04 18:24:20 +08:00
|
|
|
CMIUtilThreadActiveObjBase::CMIUtilThreadActiveObjBase()
|
2014-11-18 02:06:21 +08:00
|
|
|
: m_references(0)
|
|
|
|
, m_bHasBeenKilled(false)
|
2014-05-16 18:51:01 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//++ ------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: Destructor.
|
|
|
|
// Type: None.
|
|
|
|
// Args: None.
|
|
|
|
// Return: None.
|
|
|
|
// Throws: None.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2015-08-04 18:24:20 +08:00
|
|
|
CMIUtilThreadActiveObjBase::~CMIUtilThreadActiveObjBase()
|
2014-05-16 18:51:01 +08:00
|
|
|
{
|
2014-11-18 02:06:21 +08:00
|
|
|
// Make sure our thread is not alive before we die
|
|
|
|
m_thread.Join();
|
2014-05-16 18:51:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//++ ------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: Check if an object is already running.
|
|
|
|
// Type: Method.
|
|
|
|
// Args: None.
|
|
|
|
// Return: MIstatus::success - Functional succeeded.
|
|
|
|
// MIstatus::failure - Functional failed.
|
|
|
|
// Throws: None.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2014-11-18 02:06:21 +08:00
|
|
|
bool
|
2015-08-04 18:24:20 +08:00
|
|
|
CMIUtilThreadActiveObjBase::ThreadIsActive()
|
2014-05-16 18:51:01 +08:00
|
|
|
{
|
2014-11-18 02:06:21 +08:00
|
|
|
// Create a new thread to occupy this threads Run() function
|
|
|
|
return m_thread.IsActive();
|
2014-05-16 18:51:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//++ ------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: Set up *this thread.
|
2015-07-07 22:04:40 +08:00
|
|
|
// Type: Method.
|
2014-11-18 02:06:21 +08:00
|
|
|
// Args: None.
|
|
|
|
// Return: MIstatus::success - Functional succeeded.
|
|
|
|
// MIstatus::failure - Functional failed.
|
|
|
|
// Throws: None.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2014-11-18 02:06:21 +08:00
|
|
|
bool
|
2015-08-04 18:24:20 +08:00
|
|
|
CMIUtilThreadActiveObjBase::ThreadExecute()
|
2014-05-16 18:51:01 +08:00
|
|
|
{
|
2014-11-18 02:06:21 +08:00
|
|
|
// Create a new thread to occupy this threads Run() function
|
|
|
|
return m_thread.Start(ThreadEntry, this);
|
2014-05-16 18:51:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//++ ------------------------------------------------------------------------------------
|
2015-07-07 22:04:40 +08:00
|
|
|
// Details: Acquire a reference to CMIUtilThreadActiveObjBase.
|
2014-11-18 02:06:21 +08:00
|
|
|
// Type: Method.
|
|
|
|
// Args: None.
|
|
|
|
// Return: MIstatus::success - Functional succeeded.
|
|
|
|
// MIstatus::failure - Functional failed.
|
|
|
|
// Throws: None.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2014-11-18 02:06:21 +08:00
|
|
|
bool
|
2015-08-04 18:24:20 +08:00
|
|
|
CMIUtilThreadActiveObjBase::Acquire()
|
2014-05-16 18:51:01 +08:00
|
|
|
{
|
2014-11-18 02:06:21 +08:00
|
|
|
// Access to this function is serial
|
|
|
|
CMIUtilThreadLock serial(m_mutex);
|
2014-05-16 18:51:01 +08:00
|
|
|
|
2014-11-18 02:06:21 +08:00
|
|
|
// >0 == *this thread is alive
|
|
|
|
m_references++;
|
2014-05-16 18:51:01 +08:00
|
|
|
|
2014-11-18 02:06:21 +08:00
|
|
|
return MIstatus::success;
|
2014-05-16 18:51:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//++ ------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: Release a reference to CMIUtilThreadActiveObjBase.
|
|
|
|
// Type: Method.
|
|
|
|
// Args: None.
|
|
|
|
// Return: MIstatus::success - Functional succeeded.
|
|
|
|
// MIstatus::failure - Functional failed.
|
|
|
|
// Throws: None.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2014-11-18 02:06:21 +08:00
|
|
|
bool
|
2015-08-04 18:24:20 +08:00
|
|
|
CMIUtilThreadActiveObjBase::Release()
|
2014-05-16 18:51:01 +08:00
|
|
|
{
|
2014-11-18 02:06:21 +08:00
|
|
|
// Access to this function is serial
|
|
|
|
CMIUtilThreadLock serial(m_mutex);
|
2014-05-16 18:51:01 +08:00
|
|
|
|
2014-11-18 02:06:21 +08:00
|
|
|
// 0 == kill off *this thread
|
|
|
|
m_references--;
|
|
|
|
|
|
|
|
return MIstatus::success;
|
2014-05-16 18:51:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//++ ------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: Force this thread to stop, regardless of references
|
|
|
|
// Type: Method.
|
|
|
|
// Args: None.
|
|
|
|
// Return: MIstatus::success - Functional succeeded.
|
|
|
|
// MIstatus::failure - Functional failed.
|
|
|
|
// Throws: None.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2014-11-18 02:06:21 +08:00
|
|
|
bool
|
2015-08-04 18:24:20 +08:00
|
|
|
CMIUtilThreadActiveObjBase::ThreadKill()
|
2014-05-16 18:51:01 +08:00
|
|
|
{
|
2014-11-18 02:06:21 +08:00
|
|
|
// Access to this function is serial
|
|
|
|
CMIUtilThreadLock serial(m_mutex);
|
2014-05-16 18:51:01 +08:00
|
|
|
|
2014-11-18 02:06:21 +08:00
|
|
|
// Set this thread to killed status
|
|
|
|
m_bHasBeenKilled = true;
|
2014-05-16 18:51:01 +08:00
|
|
|
|
2014-11-18 02:06:21 +08:00
|
|
|
return MIstatus::success;
|
2014-05-16 18:51:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//++ ------------------------------------------------------------------------------------
|
|
|
|
// Details: Proxy to thread join.
|
2014-11-18 02:06:21 +08:00
|
|
|
// Type: Method.
|
|
|
|
// Args: None.
|
|
|
|
// Return: MIstatus::success - Functional succeeded.
|
|
|
|
// MIstatus::failure - Functional failed.
|
|
|
|
// Throws: None.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2014-11-18 02:06:21 +08:00
|
|
|
bool
|
2015-08-04 18:24:20 +08:00
|
|
|
CMIUtilThreadActiveObjBase::ThreadJoin()
|
2014-05-16 18:51:01 +08:00
|
|
|
{
|
2014-11-18 02:06:21 +08:00
|
|
|
return m_thread.Join();
|
2014-05-16 18:51:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//++ ------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// 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.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2014-11-18 02:06:21 +08:00
|
|
|
MIuint
|
|
|
|
CMIUtilThreadActiveObjBase::ThreadEntry(void *vpThisClass)
|
2014-05-16 18:51:01 +08:00
|
|
|
{
|
2014-11-18 02:06:21 +08:00
|
|
|
// 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);
|
2014-05-16 18:51:01 +08:00
|
|
|
|
2014-11-18 02:06:21 +08:00
|
|
|
// Start the management routine of this object
|
|
|
|
pActive->ThreadManage();
|
2014-05-16 18:51:01 +08:00
|
|
|
|
2014-11-18 02:06:21 +08:00
|
|
|
// Thread death
|
2014-05-16 18:51:01 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//++ ------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: This function forms a small management routine, to handle the thread's running.
|
|
|
|
// Type: Method.
|
|
|
|
// Args: None.
|
|
|
|
// Return: None.
|
|
|
|
// Throws: None.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2014-11-18 02:06:21 +08:00
|
|
|
void
|
2015-08-04 18:24:20 +08:00
|
|
|
CMIUtilThreadActiveObjBase::ThreadManage()
|
2014-05-16 18:51:01 +08:00
|
|
|
{
|
2014-11-18 02:06:21 +08:00
|
|
|
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();
|
2015-04-26 05:20:00 +08:00
|
|
|
|
|
|
|
m_thread.Finish();
|
2014-05-16 18:51:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------
|
|
|
|
//---------------------------------------------------------------------------------------
|
|
|
|
//---------------------------------------------------------------------------------------
|
|
|
|
|
2014-11-18 02:06:21 +08:00
|
|
|
//
|
2015-08-04 18:24:20 +08:00
|
|
|
CMIUtilThread::CMIUtilThread()
|
2014-11-18 02:06:21 +08:00
|
|
|
: m_pThread(nullptr)
|
2015-04-26 05:20:00 +08:00
|
|
|
, m_bIsActive(false)
|
2014-05-16 18:51:01 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//++ ------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: CMIUtilThread destructor.
|
|
|
|
// Type: Method.
|
|
|
|
// Args: None.
|
|
|
|
// Return: None.
|
|
|
|
// Throws: None.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2015-08-04 18:24:20 +08:00
|
|
|
CMIUtilThread::~CMIUtilThread()
|
2014-05-16 18:51:01 +08:00
|
|
|
{
|
2014-11-18 02:06:21 +08:00
|
|
|
Join();
|
2014-05-16 18:51:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//++ ------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: Wait for thread to stop.
|
|
|
|
// Type: Method.
|
|
|
|
// Args: None.
|
|
|
|
// Return: MIstatus::success - Functional succeeded.
|
|
|
|
// MIstatus::failure - Functional failed.
|
|
|
|
// Throws: None.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2014-11-18 02:06:21 +08:00
|
|
|
bool
|
2015-08-04 18:24:20 +08:00
|
|
|
CMIUtilThread::Join()
|
2014-05-16 18:51:01 +08:00
|
|
|
{
|
2014-11-18 02:06:21 +08:00
|
|
|
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;
|
2014-05-16 18:51:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//++ ------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: Is the thread doing work.
|
|
|
|
// Type: Method.
|
|
|
|
// Args: None.
|
|
|
|
// Return: bool - True = Yes active, false = not active.
|
|
|
|
// Throws: None.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2014-11-18 02:06:21 +08:00
|
|
|
bool
|
2015-08-04 18:24:20 +08:00
|
|
|
CMIUtilThread::IsActive()
|
2014-05-16 18:51:01 +08:00
|
|
|
{
|
2015-04-26 05:20:00 +08:00
|
|
|
// Lock while we access the thread status
|
|
|
|
CMIUtilThreadLock _lock(m_mutex);
|
|
|
|
return m_bIsActive;
|
|
|
|
}
|
|
|
|
|
|
|
|
//++ ------------------------------------------------------------------------------------
|
|
|
|
// Details: Finish this thread
|
|
|
|
// Type: Method.
|
|
|
|
// Args: None.
|
|
|
|
// Return: None.
|
|
|
|
// Throws: None.
|
|
|
|
//--
|
|
|
|
void
|
2015-08-04 18:24:20 +08:00
|
|
|
CMIUtilThread::Finish()
|
2015-04-26 05:20:00 +08:00
|
|
|
{
|
|
|
|
// Lock while we access the thread status
|
2014-11-18 02:06:21 +08:00
|
|
|
CMIUtilThreadLock _lock(m_mutex);
|
2015-04-26 05:20:00 +08:00
|
|
|
m_bIsActive = false;
|
2014-05-16 18:51:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//++ ------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// 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.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2014-11-18 02:06:21 +08:00
|
|
|
bool
|
|
|
|
CMIUtilThread::Start(FnThreadProc vpFn, void *vpArg)
|
2014-05-16 18:51:01 +08:00
|
|
|
{
|
2015-04-26 05:20:00 +08:00
|
|
|
// Lock while we access the thread pointer and status
|
|
|
|
CMIUtilThreadLock _lock(m_mutex);
|
|
|
|
|
|
|
|
// Create the std thread, which starts immediately and update its status
|
2014-11-18 02:06:21 +08:00
|
|
|
m_pThread = new std::thread(vpFn, vpArg);
|
2015-04-26 05:20:00 +08:00
|
|
|
m_bIsActive = true;
|
2014-11-18 02:06:21 +08:00
|
|
|
|
|
|
|
// We expect to always be able to create one
|
|
|
|
assert(m_pThread != nullptr);
|
|
|
|
|
|
|
|
return MIstatus::success;
|
2014-05-16 18:51:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------
|
|
|
|
//---------------------------------------------------------------------------------------
|
|
|
|
//---------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
//++ ------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: Take resource.
|
|
|
|
// Type: Method.
|
|
|
|
// Args: None.
|
|
|
|
// Return: None.
|
|
|
|
// Throws: None.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2014-11-18 02:06:21 +08:00
|
|
|
void
|
2015-08-04 18:24:20 +08:00
|
|
|
CMIUtilThreadMutex::Lock()
|
2014-05-16 18:51:01 +08:00
|
|
|
{
|
2014-11-18 02:06:21 +08:00
|
|
|
m_mutex.lock();
|
2014-05-16 18:51:01 +08:00
|
|
|
}
|
2014-11-18 02:06:21 +08:00
|
|
|
|
2014-05-16 18:51:01 +08:00
|
|
|
//++ ------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: Release resource.
|
|
|
|
// Type: Method.
|
|
|
|
// Args: None.
|
|
|
|
// Return: None.
|
|
|
|
// Throws: None.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2014-11-18 02:06:21 +08:00
|
|
|
void
|
2015-08-04 18:24:20 +08:00
|
|
|
CMIUtilThreadMutex::Unlock()
|
2014-05-16 18:51:01 +08:00
|
|
|
{
|
2014-11-18 02:06:21 +08:00
|
|
|
m_mutex.unlock();
|
2014-05-16 18:51:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//++ ------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// 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.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2014-11-18 02:06:21 +08:00
|
|
|
bool
|
2015-08-04 18:24:20 +08:00
|
|
|
CMIUtilThreadMutex::TryLock()
|
2014-05-16 18:51:01 +08:00
|
|
|
{
|
2014-11-18 02:06:21 +08:00
|
|
|
return m_mutex.try_lock();
|
2014-05-16 18:51:01 +08:00
|
|
|
}
|