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 namespace ZenLib 00039 { 00040 00041 //*************************************************************************** 00042 /// @brief Thread manipulation 00043 //*************************************************************************** 00044 00045 class Thread 00046 { 00047 public : 00048 //Constructor/Destructor 00049 Thread (); 00050 virtual ~Thread (); 00051 00052 //Control 00053 enum returnvalue 00054 { 00055 Ok, 00056 IsNotRunning, 00057 Incoherent, 00058 Resource, 00059 }; 00060 returnvalue Run(); 00061 returnvalue RunAgain(); 00062 returnvalue Pause(); 00063 returnvalue RequestTerminate(); 00064 returnvalue ForceTerminate(); 00065 00066 //Status 00067 bool IsRunning(); 00068 bool IsTerminating(); 00069 bool IsExited(); 00070 00071 //Configuration 00072 void Priority_Set(int8s Priority); //-100 to +100 00073 00074 //Main Entry 00075 virtual void Entry(); 00076 00077 //Internal 00078 returnvalue Internal_Exit(); //Do not use it 00079 00080 protected : 00081 00082 //Communicating 00083 void Sleep(std::size_t Millisecond); 00084 void Yield(); 00085 00086 private : 00087 //Internal 00088 void* ThreadPointer; 00089 00090 //The possible states of the thread ("-->" shows all possible transitions from this state) 00091 enum state 00092 { 00093 State_New, // didn't start execution yet (--> Running) 00094 State_Running, // thread is running (--> Paused, Terminating) 00095 State_Paused, // thread is temporarily suspended (--> Running) 00096 State_Terminating, // thread should terminate a.s.a.p. (--> Terminated) 00097 State_Terminated, // thread is terminated 00098 }; 00099 state State; 00100 CriticalSection C; 00101 }; 00102 00103 } //NameSpace 00104 00105 #endif