1 /*  Copyright (c) MediaArea.net SARL. All Rights Reserved.
2  *
3  *  Use of this source code is governed by a zlib-style license that can
4  *  be found in the License.txt file in the root of the source tree.
5  */
6 
7 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
8 //
9 // Thread functions
10 //
11 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
12 
13 //---------------------------------------------------------------------------
14 #ifndef ZenLib_ThreadH
15 #define ZenLib_ThreadH
16 //---------------------------------------------------------------------------
17 #include "ZenLib/Conf.h"
18 #include "ZenLib/CriticalSection.h"
19 #ifdef _WINDOWS
20     #undef Yield
21 #endif
22 //---------------------------------------------------------------------------
23 
24 namespace ZenLib
25 {
26 
27 //***************************************************************************
28 /// @brief Thread manipulation
29 //***************************************************************************
30 
31 class Thread
32 {
33 public :
34     //Constructor/Destructor
35     Thread  ();
36     virtual ~Thread ();
37 
38     //Control
39     enum returnvalue
40     {
41         Ok,
42         IsNotRunning,
43         Incoherent,
44         Resource,
45     };
46     returnvalue Run();
47     returnvalue RunAgain();
48     returnvalue Pause();
49     returnvalue RequestTerminate();
50     returnvalue ForceTerminate();
51 
52     //Status
53     bool        IsRunning();
54     bool        IsTerminating();
55     bool        IsExited();
56 
57     //Configuration
58     void        Priority_Set(int8s Priority); //-100 to +100
59 
60     //Main Entry
61     virtual void Entry();
62 
63     //Internal
64     returnvalue Internal_Exit(); //Do not use it
65 
66 protected :
67 
68     //Communicating
69     void    Sleep(std::size_t Millisecond);
70     void    Yield();
71 
72 private :
73     //Internal
74     void*   ThreadPointer;
75 
76     //The possible states of the thread ("-->" shows all possible transitions from this state)
77     enum state
78     {
79         State_New,              // didn't start execution yet (--> Running)
80         State_Running,          // thread is running (--> Paused, Terminating)
81         State_Paused,           // thread is temporarily suspended (--> Running)
82         State_Terminating,      // thread should terminate a.s.a.p. (--> Terminated)
83         State_Terminated,       // thread is terminated
84     };
85     state State;
86     CriticalSection C;
87 };
88 
89 } //NameSpace
90 
91 #endif
92