Line |
Branch |
Exec |
Source |
1 |
|
|
/** |
2 |
|
|
* @file api.Thread.hpp |
3 |
|
|
* @author Sergey Baigudin, sergey@baigudin.software |
4 |
|
|
* @copyright 2014-2022, Sergey Baigudin, Baigudin Software |
5 |
|
|
*/ |
6 |
|
|
#ifndef API_THREAD_HPP_ |
7 |
|
|
#define API_THREAD_HPP_ |
8 |
|
|
|
9 |
|
|
#include "api.Object.hpp" |
10 |
|
|
|
11 |
|
|
namespace eoos |
12 |
|
|
{ |
13 |
|
|
namespace api |
14 |
|
|
{ |
15 |
|
|
|
16 |
|
|
/** |
17 |
|
|
* @class Thread |
18 |
|
|
* @brief Thread interface. |
19 |
|
|
*/ |
20 |
|
|
class Thread : public Object |
21 |
|
|
{ |
22 |
|
|
|
23 |
|
|
public: |
24 |
|
|
|
25 |
|
|
/** |
26 |
|
|
* @brief Wrong thead priority. |
27 |
|
|
*/ |
28 |
|
|
static const int32_t PRIORITY_WRONG = -1; |
29 |
|
|
|
30 |
|
|
/** |
31 |
|
|
* @brief Maximum thead priority. |
32 |
|
|
*/ |
33 |
|
|
static const int32_t PRIORITY_MAX = 10; |
34 |
|
|
|
35 |
|
|
/** |
36 |
|
|
* @brief Minimum thead priority. |
37 |
|
|
*/ |
38 |
|
|
static const int32_t PRIORITY_MIN = 1; |
39 |
|
|
|
40 |
|
|
/** |
41 |
|
|
* @brief Normal thead priority. |
42 |
|
|
*/ |
43 |
|
|
static const int32_t PRIORITY_NORM = 5; |
44 |
|
|
|
45 |
|
|
/** |
46 |
|
|
* @brief Thead locked on executing priority. |
47 |
|
|
*/ |
48 |
|
|
static const int32_t PRIORITY_LOCK = 0; |
49 |
|
|
|
50 |
|
|
/** |
51 |
|
|
* @enum Status |
52 |
|
|
* @brief Thread available statuses. |
53 |
|
|
* |
54 |
|
|
* Thread status state machine: |
55 |
|
|
* |-- On any error of creation -->| |
56 |
|
|
* (NEW)->(RUNNABLE)<->(RUNNING)->(DEAD) |
57 |
|
|
* |<-(SLEEPING)<-| |
58 |
|
|
* |<-(BLOCKED) <-| |
59 |
|
|
* |
60 |
|
|
* @todo Currently there is no function to get a thread status for OS, as statuses |
61 |
|
|
* of different OSs are different, and we need to align them to this. |
62 |
|
|
*/ |
63 |
|
|
enum Status |
64 |
|
|
{ |
65 |
|
|
STATUS_NEW = 0, //< Thread is constructed |
66 |
|
|
STATUS_RUNNABLE = 1, //< Thread is set with execute() |
67 |
|
|
STATUS_RUNNING = 2, //< Thread is run |
68 |
|
|
STATUS_BLOCKED = 4, //< Thread is blocked on a resource |
69 |
|
|
STATUS_SLEEPING = 5, //< Thread is sleeping on a resource |
70 |
|
|
STATUS_DEAD = 6 //< Thread is dead because it finished execution |
71 |
|
|
}; |
72 |
|
|
|
73 |
|
|
/** |
74 |
|
|
* @brief Destructor. |
75 |
|
|
*/ |
76 |
|
|
virtual ~Thread() = 0; |
77 |
|
|
|
78 |
|
|
/** |
79 |
|
|
* @brief Causes this thread to begin execution. |
80 |
|
|
* |
81 |
|
|
* @return True if execution of the thread initiated successfully. |
82 |
|
|
*/ |
83 |
|
|
virtual bool_t execute() = 0; |
84 |
|
|
|
85 |
|
|
/** |
86 |
|
|
* @brief Waits for this thread to die. |
87 |
|
|
* |
88 |
|
|
* @return True, if this thread is dead. |
89 |
|
|
*/ |
90 |
|
|
virtual bool_t join() = 0; |
91 |
|
|
|
92 |
|
|
/** |
93 |
|
|
* @brief Returns this thread priority. |
94 |
|
|
* |
95 |
|
|
* @return Priority value, or PRIORITY_WRONG if an error has been occurred. |
96 |
|
|
*/ |
97 |
|
|
virtual int32_t getPriority() const = 0; |
98 |
|
|
|
99 |
|
|
/** |
100 |
|
|
* @brief Sets this thread priority. |
101 |
|
|
* |
102 |
|
|
* @param priority Number of priority in range [PRIORITY_MIN, PRIORITY_MAX], or PRIORITY_LOCK. |
103 |
|
|
* @return True if priority is set. |
104 |
|
|
*/ |
105 |
|
|
virtual bool_t setPriority(int32_t priority) = 0; |
106 |
|
|
|
107 |
|
|
}; |
108 |
|
|
|
109 |
|
124 |
inline Thread::~Thread() {} |
110 |
|
|
|
111 |
|
|
} // namespace api |
112 |
|
|
} // namespace eoos |
113 |
|
|
#endif // API_THREAD_HPP_ |
114 |
|
|
|