ZenLib
Thread.h
Go to the documentation of this file.
00001 // ZenLib::Thread - Thread functions
00002 // Copyright (C) 2007-2011 MediaArea.net SARL, Info@MediaArea.net
00003 //
00004 // This software is provided 'as-is', without any express or implied
00005 // warranty.  In no event will the authors be held liable for any damages
00006 // arising from the use of this software.
00007 //
00008 // Permission is granted to anyone to use this software for any purpose,
00009 // including commercial applications, and to alter it and redistribute it
00010 // freely, subject to the following restrictions:
00011 //
00012 // 1. The origin of this software must not be misrepresented; you must not
00013 //    claim that you wrote the original software. If you use this software
00014 //    in a product, an acknowledgment in the product documentation would be
00015 //    appreciated but is not required.
00016 // 2. Altered source versions must be plainly marked as such, and must not be
00017 //    misrepresented as being the original software.
00018 // 3. This notice may not be removed or altered from any source distribution.
00019 //
00020 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00021 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00022 //
00023 // Thread functions
00024 //
00025 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00026 
00027 //---------------------------------------------------------------------------
00028 #ifndef ZenLib_ThreadH
00029 #define ZenLib_ThreadH
00030 //---------------------------------------------------------------------------
00031 #include "ZenLib/Conf.h"
00032 #include "ZenLib/CriticalSection.h"
00033 #ifdef _WINDOWS
00034     #undef Yield
00035 #endif
00036 //---------------------------------------------------------------------------
00037 
00038 #include <stdlib.h>
00039 
00040 namespace ZenLib
00041 {
00042 
00043 //***************************************************************************
00044 /// @brief Thread manipulation
00045 //***************************************************************************
00046 
00047 class Thread
00048 {
00049 public :
00050     //Constructor/Destructor
00051     Thread  ();
00052     virtual ~Thread ();
00053 
00054     //Control
00055     enum returnvalue
00056     {
00057         Ok,
00058         IsNotRunning,
00059         Incoherent,
00060         Resource,
00061     };
00062     returnvalue Run();
00063     returnvalue RunAgain();
00064     returnvalue Pause();
00065     returnvalue RequestTerminate();
00066     returnvalue ForceTerminate();
00067 
00068     //Status
00069     bool        IsRunning();
00070     bool        IsTerminating();
00071     bool        IsExited();
00072 
00073     //Configuration
00074     void        Priority_Set(int8s Priority); //-100 to +100
00075 
00076     //Main Entry
00077     virtual void Entry();
00078 
00079     //Internal
00080     returnvalue Internal_Exit(); //Do not use it
00081 
00082 protected :
00083 
00084     //Communicating
00085     void    Sleep(size_t Millisecond);
00086     void    Yield();
00087 
00088 private :
00089     //Internal
00090     void*   ThreadPointer;
00091 
00092     //The possible states of the thread ("-->" shows all possible transitions from this state)
00093     enum state
00094     {
00095         State_New,              // didn't start execution yet (--> Running)
00096         State_Running,          // thread is running (--> Paused, Terminating)
00097         State_Paused,           // thread is temporarily suspended (--> Running)
00098         State_Terminating,      // thread should terminate a.s.a.p. (--> Terminated)
00099         State_Terminated,       // thread is terminated
00100     };
00101     state State;
00102     CriticalSection C;
00103 };
00104 
00105 } //NameSpace
00106 
00107 #endif