⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 warthread.cpp

📁 ftpserver very good sample
💻 CPP
字号:
#include "StdAfx.h"#include "WarThread.h"   // class implemented#ifndef WAR_ASSERT_H_INCLUDED#   include <assert.h>#endif#ifndef WAR_AUTO_LOCK_H#   include "WarAutoLock.h"#endif#ifndef WAR_LOG_H#   include "WarLog.h"#endif#ifndef WAR_THREAD_ENGINE_H#   include "WarThreadEngine.h"#endif/////////////////////////////// PUBLIC ///////////////////////////////////////WarPrioritiesDefE WarThread::msDefaultPriority = WAR_THRD_PRI_INVALID;#if HAVE_PTHREAD_H    pthread_key_t WarThread::mTsdKey;#elif WAR_USE_WIN32_TLS    DWORD WarThread::msTlsIndex;;#elif WAR_USE_WIN32_FAST_TD    WarThreadSpesificData *WarThread::mspThreadData;#endif//============================= LIFECYCLE ====================================    WarThread::WarThread(WarObjectGroup *pGroup)    throw(WarException)    :mDoAbort (false),    mDoExit (false),    mDoCancel (false),    mState (STATE_UNINITIALIZED),    mpEvent (new WarThreadEvent),    mpLock (new WarCriticalSection),    mStartPriority(msDefaultPriority),    mGroupRef(pGroup),    mThreadId(WAR_THERADID_NULL),#ifdef WIN32    mThreadHandle(NULL),#endif    mCurrentPriority(WAR_THRD_PRI_INVALID){#if WAR_USE_WIN32_FAST_TD    mspThreadData = NULL;#endif}WarThread::~WarThread(){    assert((mState == STATE_UNINITIALIZED) || (mState == STATE_DEAD));    if (mpLock)        delete mpLock;    if (mpEvent)        delete mpEvent;}//============================= OPERATORS ====================================void WarThread::Open(war_thread_id_t * pThreadId)throw(WarException){    WarPtrWrapper<WarThread> MySmartie(this); // Prevent destruction    war_thread_id_t thread_id = WAR_THERADID_NULL;    {        WarAutoLock MyLock(*mpLock);        CreateThread(thread_id);                    mpEvent->WaitForEvent();                if (mStartupError)            WarThrow(mStartupError, "Thread initialization failed");                if (pThreadId)            *pThreadId = thread_id;            }}//============================= OPERATIONS ===================================//============================= ACESS      ===================================//============================= INQUIRY    ===================================WarThread *WarThread::GetCurrentThread()throw(WarException){    return GetTsd()->mpThread;}std::string WarThread::GetThreadInfo() const{    static const war_ccstr_t StateNames[] =    {        {"UNINITIALIZED"},        {"IDLE"},        {"HOUSEKEEPING"},        {"BUSY"},        {"QUITTING"},        {"DEAD"}    };        static const war_ccstr_t PriNames[] =    {        {"VERYHIGH"},        {"HIGH"},        {"NORMAL"},        {"LOW"},        {"VERYLOW"},        {"IDLE"},        {"NOT_SPECIFIED"}    };         char buf[128];    try    {        sprintf(buf, "Thread ID: %08u. Flags: %c%c%c  Addr: %04xu  State: %s  Pri: %s",            (unsigned)GetThreadId(),            mDoExit ? 'e' : '-',            mDoAbort ? 'a' : '-',            mDoCancel ? 'c' : '-',            (unsigned)(const void *)this,            StateNames[GetState()],            PriNames[mStartPriority]);    }    catch (...)    {        return "WarThread::GetThreadInfo(): error examening thread!";    }        return buf;}WarPrioritiesDefE WarThread::GetPriority() const{    return mCurrentPriority;}/////////////////////////////// PROTECTED  ////////////////////////////////////////////////////////////////// PRIVATE    ///////////////////////////////////void WarThread::StartFunc()throw(WarException){    WarPtrWrapper<WarThread> MySmartie(this); // Prevent destruction    bool is_successfully_initialized = false;        // Initialize library    try    {        WarLog debug_log(WARLOG_THREADS, "WarThread::StartFunc()");        InitializeThread();                GetTsd()->mpThread = this;        mThreadId = GetCurrentThreadId();        debug_log << "New thread starting up. ID: "            << (unsigned int)GetThreadId()            << war_endl;                // Call the threads initializer. If that fails        // the issuer will get an exception        is_successfully_initialized = Initialize();    }    catch(WarException& ex)    {        WarLogError("WarThread::StartFunc()",             "Caught unexpected exception!", &ex);    }#if WAR_CATCH_ALL        catch(...)    {           WarLogError("WarThread::StartFunc()",             "Caught unknown exception", NULL);    }#endif    if (!is_successfully_initialized)    {        if (!mStartupError)            mStartupError = WAR_THREADERR_FAILED_TO_INITIALIZE;    }        try    {        mpEvent->SetEvent(WarThreadEvent::EVENT_SIGNALED);        // Wait for the starting thread to get the result from the initializer        mpLock->Lock();    }    catch(WarException& ex)    {        WarLogError("WarThread::StartFunc()",             "Caught unexpected exception!", &ex);        is_successfully_initialized = false;    }    try    {        mpLock->Unlock();    }    catch(WarException& ex)    {        WarLogError("WarThread::StartFunc()",             "Caught unexpected exception!", &ex);        is_successfully_initialized = false;    }    try    {        delete mpLock;        mpLock = NULL;        delete mpEvent;        mpEvent = NULL;    }    catch(WarException)    {    }#if WAR_CATCH_ALL        catch(...)    {    }#endif    try    {        WarLog debug_log(WARLOG_THREADS, "WarThread::StartFunc()");        if (is_successfully_initialized)        {            debug_log << "Thread ID: "                << (unsigned int)GetThreadId()                << " initialized ok. Calling Run()"                << war_endl;                        // Call the real start function            SetPriority(mStartPriority);        }        else        {            debug_log << "Thread ID: "                << (unsigned int)GetThreadId()                << " Failed to initialize."                << war_endl;                    }    }    catch(WarException& ex)    {        WarLogError("WarThread::StartFunc()",             "Caught unexpected exception!", &ex);    }    mState = STATE_IDLE;    try    {        WarThreadEngine::GetEngine().AddThread(this);    }    catch(WarException& ex)    {        WarLogError("WarThread::StartFunc()",             "Caught exception from WarThreadEngine::AddThread()", &ex);    }    try    {        Run();    }    catch(WarException& ex)    {        WarLogError("WarThread::StartFunc()",             "Caught exception from Run()", &ex);    }#if WAR_CATCH_ALL        catch(...)    {        WarLogError("WarThread::StartFunc()",             "Caught unknown exception from Run()", NULL);    }#endif    mState = STATE_QUITTING;    try    {        WarThreadEngine::GetEngine().RemoveThread(this);    }    catch(WarException& ex)    {        WarLogError("WarThread::StartFunc()",             "Caught exception from WarThreadEngine::RemoveThread()", &ex);    }    mState = STATE_DEAD;#if WAR_CATCH_ALL    try#endif    {        WarLog debug_log(WARLOG_THREADS, "WarThread::StartFunc()");        debug_log << "Thread ID: "                << (unsigned int)GetThreadId()                << " terminating."                << war_endl;    }#if WAR_CATCH_ALL        catch(...)    {        ; // Nothing    }#endif    try    {        // We must kill the TSD here, since we run as the therad that        // owns the TSD. Another thread may enter our destructor,         // creating an interesting race condition.        KillTsd();    }    catch(WarException)    {    }#if WAR_CATCH_ALL        catch(...)    {    }#endif}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -