ZenLib

Thread.h

Go to the documentation of this file.
00001 /*  Copyright (c) MediaArea.net SARL. All Rights Reserved.
00002  *
00003  *  Use of this source code is governed by a zlib-style license that can
00004  *  be found in the License.txt file in the root of the source tree.
00005  */
00006 
00007 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00008 //
00009 // Thread functions
00010 //
00011 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00012 
00013 //---------------------------------------------------------------------------
00014 #ifndef ZenLib_ThreadH
00015 #define ZenLib_ThreadH
00016 //---------------------------------------------------------------------------
00017 #include "ZenLib/Conf.h"
00018 #include "ZenLib/CriticalSection.h"
00019 #ifdef _WINDOWS
00020     #undef Yield
00021 #endif
00022 //---------------------------------------------------------------------------
00023 
00024 namespace ZenLib
00025 {
00026 
00027 //***************************************************************************
00028 /// @brief Thread manipulation
00029 //***************************************************************************
00030 
00031 class Thread
00032 {
00033 public :
00034     //Constructor/Destructor
00035     Thread  ();
00036     virtual ~Thread ();
00037 
00038     //Control
00039     enum returnvalue
00040     {
00041         Ok,
00042         IsNotRunning,
00043         Incoherent,
00044         Resource,
00045     };
00046     returnvalue Run();
00047     returnvalue RunAgain();
00048     returnvalue Pause();
00049     returnvalue RequestTerminate();
00050     returnvalue ForceTerminate();
00051 
00052     //Status
00053     bool        IsRunning();
00054     bool        IsTerminating();
00055     bool        IsExited();
00056 
00057     //Configuration
00058     void        Priority_Set(int8s Priority); //-100 to +100
00059 
00060     //Main Entry
00061     virtual void Entry();
00062 
00063     //Internal
00064     returnvalue Internal_Exit(); //Do not use it
00065 
00066 protected :
00067 
00068     //Communicating
00069     void    Sleep(std::size_t Millisecond);
00070     void    Yield();
00071 
00072 private :
00073     //Internal
00074     void*   ThreadPointer;
00075 
00076     //The possible states of the thread ("-->" shows all possible transitions from this state)
00077     enum state
00078     {
00079         State_New,              // didn't start execution yet (--> Running)
00080         State_Running,          // thread is running (--> Paused, Terminating)
00081         State_Paused,           // thread is temporarily suspended (--> Running)
00082         State_Terminating,      // thread should terminate a.s.a.p. (--> Terminated)
00083         State_Terminated,       // thread is terminated
00084     };
00085     state State;
00086     CriticalSection C;
00087 };
00088 
00089 } //NameSpace
00090 
00091 #endif