|
|
//----------------------------------------------------------------------------
// Laser Guard Ltd.
// Copyright � 2000-2002. All Rights Reserved.
// Version 1.0 01/01/2000
// Last modification: 08/10/2003
// PROJECT:
// SUBSYSTEM:
// FILE: Thread.cpp
// AUTHOR: Sergey Chizhevskiy
// OVERVIEW
// ~~~~~~~~
// Source file of Thread.cpp
//----------------------------------------------------------------------------
#include "StdAfx.h"
#include "..\LgLib\Version.h"
#include "..\LgLib\LgLogger.h"
#include <process.h>
#include "Thread.h"
CLgThread::CLgThread(){ // erase all data
m_dwThreadId = 0UL; m_hEventStopThread = NULL; m_hThread = NULL; m_bSelfDestroy = FALSE;
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CThread::CThread" );}
CLgThread::~CLgThread(){ // if it is not self destroy
if( !m_bSelfDestroy ) Stop( TRUE );
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CThread::~CThread" );}
BOOL CLgThread::Start( BOOL bSelfDestroy ){ // blocking stop if thread was ran before
if( !Stop( TRUE ) ) { // log error
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgThread::Start"), _T("Stop failed") ); return FALSE; }
// store for future use
m_bSelfDestroy = bSelfDestroy;
// create event for thread's stopping
m_hEventStopThread = ::CreateEvent( NULL, TRUE, FALSE, NULL ); if( !m_hEventStopThread ) return FALSE;
// create new thread
m_hThread = (HANDLE)_beginthreadex( NULL, 0, ThreadProc_, this, 0, (PUINT)&m_dwThreadId ); if( !m_hThread ) return FALSE;
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CThread::Create", "thread id %08X", m_dwThreadId );
// all work was done successefully
return TRUE;}
BOOL CLgThread::Stop( BOOL bWaitForComplete ){ // force thread to close
if( m_hEventStopThread ) ::SetEvent( m_hEventStopThread );
// if don't need to wait until thread will closed
if( !bWaitForComplete ) { LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CThread::Stop", "thread id %08X", m_dwThreadId ); return TRUE; }
// prevent dead lock if tryed to close from thread itself
if( m_dwThreadId == ::GetCurrentThreadId() ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_WARNING, this, "CThread::Stop", "thread stop with wait from itself (deadlock)", m_dwThreadId ); return FALSE; }
// assume success
BOOL bResult = TRUE;
// if thread exists and works
if( m_hThread ) { LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CThread::Stop", "thread id %08X", m_dwThreadId );
// wait until thread will closed
bResult = ::WaitForSingleObject( m_hThread, MAX_THREAD_CLOSE_TIME ) == WAIT_OBJECT_0;
// if thread doesn't closed normally
if( !bResult ) { // log error
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgThread::Stop"), _T("WaitForSingleObject failed") ); // force thread stop
_endthreadex( (UINT)m_hThread ); } // close thread handle
::CloseHandle( m_hThread ); // erase old value
m_hThread = NULL; }
// if event handle is valid
if( m_hEventStopThread ) { // close event handle
::CloseHandle( m_hEventStopThread );
// erase old value
m_hEventStopThread = NULL; }
// return result of close operation
return bResult;}
UINT CALLBACK CLgThread::ThreadProc_( LPVOID lpvParam ){ // get pointer to this
CLgThread *pThread = (CLgThread*) lpvParam;
__try { // call thread procedure
UINT nResult = pThread->ThreadProc();
// ensure to close thread normally
// _endthreadex( (UINT)nResult );
// self destroy if nessessary
if( pThread->m_bSelfDestroy ) { // close event handle
if( pThread->m_hEventStopThread ) ::CloseHandle( pThread->m_hEventStopThread );
// close thread handle
if( pThread->m_hThread ) ::CloseHandle( pThread->m_hThread );
// delete itself
delete pThread; }
LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, pThread, "CThread::ThreadProc_" );
//return with result
return nResult; } __except( EXCEPTION_EXECUTE_HANDLER ) { // show information
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, pThread, _T("CThread::ThreadProc_"), _T("Exception 0x%08X"), _exception_code() ); }
return -1;}
|