📄 warshutdownengine.cpp
字号:
#include "StdAfx.h"#include "WarShutdownEngine.h" // class implemented#ifndef WAR_LOG# include "WarLog.h"#endif#ifndef WAR_AUTO_LOCK_H# include "WarAutoLock.h"#endif#ifndef WAR_SHUTDOWN_THREAD_H# include "WarShutdownThread.h"#endif#define AUTO_LOCK WarAutoLock my_lock(mLock);/////////////////////////////// PUBLIC ///////////////////////////////////////WarShutdownEngine *WarShutdownEngine::mspEngine;//============================= LIFECYCLE ====================================WarShutdownEngine::WarShutdownEngine(): mIsShuttingDown(false), mHaveShutDown(false){ if (NULL != mspEngine) WarThrow(WarError(WAR_ERR_ALREADY_INITIALIZED), NULL); mspEngine = this;}// WarShutdownEngineWarShutdownEngine::~WarShutdownEngine(){ if (mspEngine != this) WarThrow(WarError(WAR_ERR_INTERNAL_DATA_NOT_INITIALIZED), NULL); Shutdown(); while(!mHaveShutDown) WarTime::Sleep(500); // Wait for another thread mspEngine = NULL;}// ~WarShutdownEngine//============================= OPERATORS ====================================//============================= OPERATIONS ===================================void WarShutdownEngine::WaitForShutdown(){ while(true) { if (mShutdownEvent.WaitForEvent(5000) || mHaveShutDown) break; } WarLog debug_log(WARLOG_DEBUG, "WarShutdownEngine::WaitForShutdown()"); if (debug_log) { debug_log << "Finished waiting for shutdown. mIsShuttingDown = " << (int)mIsShuttingDown << war_endl; }}void WarShutdownEngine::AddEvent(war_shutdown_task_ptr_t& taskPtr){ AUTO_LOCK mTasks.push_back(taskPtr);}void WarShutdownEngine::AddEvent(WarShutdownProc func, war_cptr_t parg){ war_shutdown_task_ptr_t new_task = new WarShutdownEngineTask(func, parg); AddEvent(new_task);}void WarShutdownEngine::DeleteEvent(war_shutdown_task_ptr_t& taskPtr){ AUTO_LOCKagain: for(tasklist_t::iterator P = mTasks.begin() ; P != mTasks.end() ; ++P) { if (P->IsSameObject(taskPtr)) { mTasks.erase(P); goto again; } }}void WarShutdownEngine::StartShutdown(){ if (mIsShuttingDown) return; WarPtrWrapper<WarShutdownThread> my_ptr = new WarShutdownThread; try { my_ptr->Open(); } catch(WarException& e) { WarLog err_log(WARLOG_ERROR, "WarShutdownEngine::StartShutdown()"); err_log << "Failed to start a seperate thread for shutting down. " << e << war_endl; }}void WarShutdownEngine::Shutdown(){ war_shutdown_task_ptr_t my_ptr; // Make sure we only enter Shutdown() once { AUTO_LOCK if (mIsShuttingDown) return; mIsShuttingDown = true; } while(true) { { AUTO_LOCK if (mTasks.empty()) { mShutdownEvent.Signal(); mHaveShutDown = true; return; } my_ptr = mTasks.front(); mTasks.pop_front(); } try { my_ptr->Shutdown(); } catch(WarException& e) { if (WarLogEngine::IsPresent()) { WarLog err_log(WARLOG_ERROR, "WarShutdownEngine::Shutdown()"); err_log << "Caught unexpected exception during shutdown. " << e.Explain() << war_endl; } }#if WAR_CATCH_ALL catch(...) { if (WarLogEngine::IsPresent()) { WarLog err_log(WARLOG_ERROR, "WarShutdownEngine::Shutdown()"); err_log << "Caught unknown exception during shutdown. " << war_endl; } }#endif // WAR_CATCH_ALL try { my_ptr = NULL; } catch(WarException& e) { WarLog err_log(WARLOG_ERROR, "WarShutdownEngine::Shutdown()"); err_log << "Caught unexpected exception during shutdown (2). " << e.Explain() << war_endl; }#if WAR_CATCH_ALL catch(...) { WarLog err_log(WARLOG_ERROR, "WarShutdownEngine::Shutdown()"); err_log << "Caught unknown exception during shutdown (2). " << war_endl; }#endif // WAR_CATCH_ALL }}//============================= ACCESS ===================================WarShutdownEngine& WarShutdownEngine::GetEngine(){ if (!mspEngine) WarThrow(WarError(WAR_ERR_INTERNAL_DATA_NOT_INITIALIZED), NULL); return *mspEngine;}//============================= INQUIRY ===================================/////////////////////////////// PROTECTED ////////////////////////////////////////////////////////////////// PRIVATE ///////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -