MboThread.h

Go to the documentation of this file.
00001 /*****************************************************************************
00002 * Copyright (c) 2001 - 2008 Marcus Boerger.  All rights reserved.
00003 *
00004 * This library is free software; you can redistribute it and/or
00005 * modify it under the terms of the GNU Lesser General Public
00006 * License as published by the Free Software Foundation; either
00007 * version 2.1 of the License, or (at your option) any later version.
00008 *
00009 * This library is distributed in the hope that it will be useful,
00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 * Lesser General Public License for more details.
00013 *
00014 * You should have received a copy of the GNU Lesser General Public
00015 * License along with this library; if not, write to the Free Software
00016 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00017 * ========================================================================= */
00018 
00019 /* ------------------------------------------------------------------------ */
00020 /* Name:      MboThread.h / MboThread.cpp
00021  *
00022  * Requires:  
00023  * - Mbo.h
00024  * - MboSynch.h
00025  * - MboSynch.cpp
00026  */
00042 /* ------------------------------------------------------------------------ */
00043 
00044 #ifndef _MBOTHREAD_H_
00045 #define _MBOTHREAD_H_
00046 
00047 #include "Mbo.h"
00048 #include "MboSynch.h"
00049 #include <map>
00050 #include <list>
00051 #include <typeinfo.h>
00052 
00053 #if defined(_MSC_VER)
00054 # pragma warning(push)
00055 # pragma warning(disable: 4251) // disable warning C4251: '...' : class '...' needs to have dll-interface to be used by clients of class '...'
00056 # pragma warning(disable: 4275) // non dll-interface class '...' used as base for dll-interface class '...'
00057 # pragma warning(disable: 4290) // C++-exception-specification ignored
00058 # ifndef _CPPRTTI
00059 #  error You must compile using RTTI
00060 # endif // _CPPRTTI
00061 #endif // _MSC_VER
00062 
00063 namespace mbo
00064 {
00065 
00066 /* /def MBOTHREAD_USES_INSECURE_LOCK
00067  *
00068  * When defined an alternate version of determining whether or not the thread
00069  * is running will be used. Anyway there shouldn't be a need for that.
00070  */
00071 #undef MBOTHREAD_USES_INSECURE_LOCK
00072 
00073 /* ------------------------------------------------------------------------ */
00105 class MBO_API MboThread
00106 {
00107 public:
00108 
00111     MboThread();
00112 
00126     explicit MboThread(_IN const char * szThreadName);
00127 
00132     virtual ~MboThread();
00133 
00138     bool    IsRunning()  const;
00139 
00144     bool    IsStopping()  const;
00145 
00154     bool    WaitFor(_IN DWORD nMilliseconds = INFINITE) const;
00155 
00167     bool KillThread(_IN DWORD nMilliseconds = 0, _IN DWORD dwExitCode = -1);
00168 
00175     DWORD   GetThreadId() const;
00176 
00180     enum EThreadPriority
00181     {
00182         PRIORITY_IDLE     = THREAD_PRIORITY_IDLE,
00183         PRIORITY_LOWEST   = THREAD_PRIORITY_LOWEST,
00184         PRIORITY_LOW      = THREAD_PRIORITY_ABOVE_NORMAL,
00185         PRIORITY_NORMAL   = THREAD_PRIORITY_NORMAL,
00186         PRIORITY_HIGH     = THREAD_PRIORITY_ABOVE_NORMAL,
00187         PRIORITY_HIGHEST  = THREAD_PRIORITY_HIGHEST,
00188         PRIORITY_CRITICAL = THREAD_PRIORITY_TIME_CRITICAL
00189     };
00190 
00195     void SetThreadPriority(_IN EThreadPriority ePriority);
00196 
00207     const char * GetThreadName();
00208 
00215     virtual void Action() = 0;
00216 
00221     virtual void OnActionException();
00222 
00228     virtual void Start();
00229 
00234     virtual void Stop();
00235 
00239     virtual void OnThreadStart();
00240 
00243     virtual void OnThreadStop();
00244 
00252     static void SetThreadName(
00253             _IN const char * szThreadName, 
00254             _IN DWORD        dwThreadID = -1
00255         );
00256 
00257 protected:
00260     virtual void ThreadProc();
00261 
00262     HANDLE      m_hThread;     
00263     HANDLE      m_hIsStopping; 
00264     DWORD       m_dwThreadId;  
00265     CCriticalSectionImpl  m_mxStartStop; 
00267 private:
00269     void ThreadProcCaller();
00271     void ThreadNamer();
00273     void ThreadProcRunner();
00274 
00275 
00285     virtual void ForceThreadName();
00286 
00287     char *      m_szThreadName;
00289 #ifdef MBOTHREAD_USES_INSECURE_LOCK
00290 
00294     CCriticalSectionImpl  m_mxIsRunning;
00295 
00300     bool        m_bIsRunning;
00301 #endif
00302 
00305     friend DWORD WINAPI MboThreadProc(LPVOID lpParameter);
00306 
00307 private:
00308 
00309     MboThread& operator = (_IN const MboThread&) __THROWS(logic_error)
00310     {
00311         throw logic_error("Assignment must not be used");
00312     }
00313 };
00314 
00315 /* ------------------------------------------------------------------------ */
00324 class MBO_API MboIntervalThread: public MboThread
00325 {
00326 public:
00341     explicit MboIntervalThread(
00342             _IN size_t       nMilliSeconds = 1000,
00343             _IN const char * szThreadName  = NULL
00344         );
00345 
00348     virtual ~MboIntervalThread();
00349 
00354     IncDecVal GetCounter() const;
00355 
00356 protected:
00361     virtual void SetInterval(_IN size_t nMilliSeconds = 1000);
00362 
00367     virtual size_t GetInterval() const;
00368 
00369 private:
00370     size_t      m_nInterval;  
00371     IncDecVal   m_nCounter;   
00375     virtual void ThreadProc();
00376 };
00377 
00378 /* ------------------------------------------------------------------------ */
00387 class MBO_API MboEventThread: public MboThread
00388 {
00389 friend class MboDelayedEventThread;
00390 public:
00405     explicit MboEventThread(
00406             _IN bool         bAutoStart    = true,
00407             _IN const char * szThreadName  = NULL
00408         );
00409 
00412     virtual ~MboEventThread();
00413 
00422     IncDecVal GetCounter() const;
00423 
00427     void SetEvent() const;
00428 
00435     virtual bool IsSignaled() const;
00436 
00442     virtual void Stop();
00443 
00444 private:
00445     IncDecVal   m_nCounter;   
00446     HANDLE      m_hEvent;     
00450     virtual void ThreadProc();
00451 };
00452 
00453 /* ------------------------------------------------------------------------ */
00463 class MBO_API MboDelayedEventThread: public MboEventThread
00464 {
00465 public:
00481     explicit MboDelayedEventThread(
00482             _IN size_t       nMilliSeconds = 0,
00483             _IN bool         bAutoStart    = true,
00484             _IN const char * szThreadName  = NULL
00485         );
00486 
00489     virtual ~MboDelayedEventThread();
00490 
00496     virtual void Stop();
00497 
00502     virtual void SetDelay(_IN size_t nMilliSeconds = 0);
00503 
00508     virtual size_t GetDelay() const;
00509 
00510 private:
00511     CCriticalSectionImpl  m_mxDelay;    
00512     size_t      m_nDelay;     
00513     HANDLE      m_hDelay;     
00517     virtual void ThreadProc();
00518 
00519 private:
00520 
00521     MboDelayedEventThread& operator = (_IN const MboDelayedEventThread&) __THROWS(logic_error)
00522     {
00523         throw logic_error("Assignment must not be used");
00524     }
00525 };
00526 
00527 /* ------------------------------------------------------------------------ */
00537 class MBO_API MboWinMsgHandler
00538     : public mbo::MboThread
00539 {
00540 friend LRESULT CALLBACK MboWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
00541 typedef std::map<HWND, MboWinMsgHandler*> MAP_HWND;
00542 typedef std::list<MboString> WndClassList;
00543 public:
00544 
00556     explicit MboWinMsgHandler(
00557             _IN const MboString& strWndClassName = "MboWinMsgHandler",
00558             _IN bool             bStart          = true
00559         );
00560 
00563     virtual ~MboWinMsgHandler();
00564 
00567     virtual HWND GetHWND() const;
00568 
00574     virtual void Stop();
00575 
00591     virtual LRESULT ProcessMessage(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
00592 
00608     virtual LRESULT ProcessDefault(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
00609 
00618     virtual bool PostMessage(UINT uMsg, WPARAM wParam, LPARAM lParam);
00619 
00636     virtual bool PostMessage(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
00637 
00640     virtual HINSTANCE GetModuleHandle() const;
00641 
00642 protected:
00643 
00652     virtual const MboString& WndClassName() const;
00653 
00656     virtual const MboString& WndTitleText() const;
00657 
00666     virtual void Action();
00667 
00674     virtual void OnWindowCreated();
00675 
00680     virtual void MessageHandler(MSG & msg);
00681 
00684     virtual bool WndCreate();
00685 
00689     virtual bool WndCalcMetrics();
00690 
00693     virtual void WndDestroy();
00694 
00697     virtual bool WndRegister();
00698 
00707     void AddHWND();
00708 
00709     HWND                m_hwnd;          
00711     const MboString     m_strWndClassName; 
00713     static WndClassList g_vWndClassList; 
00715 private:
00716 
00724     static MAP_HWND     g_mhwndList;
00725 
00726 #ifdef _MSC_VER
00727 # pragma warning(pop)
00728 #endif
00729 
00730 };
00731 
00732 /* ------------------------------------------------------------------------ */
00733 }; // namespace
00734 
00735 #endif // _MBOTHREAD_H_

  Hosted on code.google.com  
© Marcus Börger
Generated on Fri Jan 18 21:21:08 2008 for MBO-lib by doxygen 1.5.4