mirror of
https://github.com/libretro/xmil-libretro.git
synced 2024-11-23 08:09:42 +00:00
2ba22c1b46
refs #95 svn merge -r 183:185 https://amethyst.yui.ne.jp/svn-dev/x1/xmil/branches/yui/WORK_02 svn merge -r 187:188 https://amethyst.yui.ne.jp/svn-dev/x1/xmil/branches/yui/WORK_02 svn merge -r 190:205 https://amethyst.yui.ne.jp/svn-dev/x1/xmil/branches/yui/WORK_02
101 lines
1.5 KiB
C++
101 lines
1.5 KiB
C++
/*!
|
|
* @file threadbase.cpp
|
|
* @brief スレッド基底クラスの動作の定義を行います
|
|
*/
|
|
|
|
#include "compiler.h"
|
|
#include "threadbase.h"
|
|
#include <process.h>
|
|
|
|
/*!
|
|
* @brief コンストラクタ
|
|
*/
|
|
CThreadBase::CThreadBase()
|
|
: m_hThread(INVALID_HANDLE_VALUE)
|
|
, m_dwThreadId(0)
|
|
, m_bAbort(false)
|
|
, m_bDone(false)
|
|
, m_nStackSize(0)
|
|
{
|
|
}
|
|
|
|
/*!
|
|
* @brief デストラクタ
|
|
*/
|
|
CThreadBase::~CThreadBase()
|
|
{
|
|
Stop();
|
|
}
|
|
|
|
/*!
|
|
* @brief スレッド開始
|
|
*
|
|
* @retval true 成功
|
|
*/
|
|
bool CThreadBase::Start()
|
|
{
|
|
if (m_hThread != INVALID_HANDLE_VALUE)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
m_bAbort = false;
|
|
m_bDone = false;
|
|
DWORD dwThreadId = 0;
|
|
HANDLE hThread = ::CreateThread(NULL, m_nStackSize, &ThreadProc, this, 0, &dwThreadId);
|
|
if (hThread == INVALID_HANDLE_VALUE)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
m_hThread = hThread;
|
|
m_dwThreadId = dwThreadId;
|
|
return true;
|
|
}
|
|
|
|
/*!
|
|
* @brief スレッド終了
|
|
*
|
|
* @retval true 成功
|
|
*/
|
|
void CThreadBase::Stop()
|
|
{
|
|
if (m_hThread != INVALID_HANDLE_VALUE)
|
|
{
|
|
m_bAbort = true;
|
|
::WaitForSingleObject(m_hThread, INFINITE);
|
|
::CloseHandle(m_hThread);
|
|
m_hThread = INVALID_HANDLE_VALUE;
|
|
}
|
|
}
|
|
|
|
/*!
|
|
* @brief スレッド再開
|
|
*
|
|
* @retval true 成功
|
|
*/
|
|
bool CThreadBase::Restart()
|
|
{
|
|
if ((m_hThread != INVALID_HANDLE_VALUE) && (m_bDone))
|
|
{
|
|
Stop();
|
|
}
|
|
return Start();
|
|
}
|
|
|
|
/*!
|
|
* スレッド処理
|
|
* @param[in] pParam this ポインタ
|
|
* @retval 0 常に0
|
|
*/
|
|
DWORD __stdcall CThreadBase::ThreadProc(LPVOID pParam)
|
|
{
|
|
CThreadBase& obj = *(static_cast<CThreadBase*>(pParam));
|
|
while ((!obj.m_bAbort) && (obj.Task()))
|
|
{
|
|
}
|
|
|
|
obj.m_bDone = true;
|
|
return 0;
|
|
}
|