priority_win.cpp
Go to the documentation of this file.
1 
8 /*****************************************************************************
9 ** Platform Check
10 *****************************************************************************/
11 
12 #include <ecl/config/ecl.hpp>
13 #if defined(ECL_IS_WIN32)
14 
15 /*****************************************************************************
16 ** Includes
17 *****************************************************************************/
18 
19 #include <iostream>
20 #include <ecl/errors/handlers.hpp>
21 #include "../../include/ecl/threads/priority_win.hpp"
22 
23 /*****************************************************************************
24 ** Namespaces
25 *****************************************************************************/
26 
27 namespace ecl {
28 
29 /*****************************************************************************
30 ** Using
31 *****************************************************************************/
32 
33 using std::ostringstream;
34 
35 /*****************************************************************************
36 ** Implementation [Process]
37 *****************************************************************************/
38 
39 bool set_priority(Priority priority_level)
40 {
41  if (priority_level >= RealTimePriority1) {
42  return threads::set_real_time_priority(0, priority_level);
43  }
44 
45  BOOL bResult = FALSE;
46  HANDLE hThread = GetCurrentThread();
47 
48  if (!hThread) {
49  ecl_debug_throw(threads::throwPriorityException(LOC));
50  return false;
51  }
52 
53  switch (priority_level) {
54  case CriticalPriority:
55  bResult = SetThreadPriority(hThread, THREAD_PRIORITY_HIGHEST);
56  break;
57  case HighPriority:
58  bResult = SetThreadPriority(hThread, THREAD_PRIORITY_ABOVE_NORMAL);
59  break;
60  case NormalPriority:
61  bResult = SetThreadPriority(hThread, THREAD_PRIORITY_NORMAL);
62  break;
63  case LowPriority:
64  bResult = SetThreadPriority(hThread, THREAD_PRIORITY_BELOW_NORMAL);
65  break;
66  case BackgroundPriority:
67  bResult = SetThreadPriority(hThread, THREAD_PRIORITY_IDLE);
68  break;
69  default:
70  break;
71  }
72 
73  if (!bResult) {
74  ecl_debug_throw(threads::throwPriorityException(LOC));
75  return false;
76  }
77 
78  return true;
79 }
80 
81 Priority get_priority()
82 {
83  HANDLE hThread = GetCurrentThread();
84 
85  if (!hThread) {
86  ecl_debug_throw(threads::throwPriorityException(LOC));
87  return UnknownPriority;
88  }
89 
90  DWORD dwPriority = GetThreadPriority(hThread);
91 
92  switch (dwPriority) {
93  case THREAD_PRIORITY_TIME_CRITICAL:
94  return RealTimePriority1; // representative of real-time priority group
95  case THREAD_PRIORITY_HIGHEST:
96  return CriticalPriority;
97  case THREAD_PRIORITY_ABOVE_NORMAL:
98  return HighPriority;
99  case THREAD_PRIORITY_NORMAL:
100  return NormalPriority;
101  case THREAD_PRIORITY_BELOW_NORMAL:
102  return LowPriority;
103  case THREAD_PRIORITY_IDLE:
104  return BackgroundPriority;
105  default:
106  break;
107  }
108 
109  return UnknownPriority;
110 }
111 
112 /*****************************************************************************
113 ** Implementation [Debugging]
114 *****************************************************************************/
115 
116 std::string print_priority_diagnostics() {
117 
118  ostringstream ostream;
119 
120  ostream << "\n";
121  ostream << "***********************************************************\n";
122  ostream << "* Priority Statistics\n";
123  ostream << "***********************************************************\n";
124  ostream << "\n";
125  switch ( get_priority() ) {
126  case ( BackgroundPriority ) : {
127  ostream << "Priority...................................Background\n";
128  break;
129  }
130  case ( LowPriority ) : {
131  ostream << "Priority...................................Low\n";
132  break;
133  }
134  case ( NormalPriority ) : {
135  ostream << "Priority...................................Normal\n";
136  break;
137  }
138  case ( HighPriority ) : {
139  ostream << "Priority...................................High\n";
140  break;
141  }
142  case ( CriticalPriority ) : {
143  ostream << "Priority...................................Critical\n";
144  break;
145  }
146  case ( RealTimePriority4 ) : {
147  ostream << "Priority...................................RealTime4\n";
148  break;
149  }
150  case ( RealTimePriority3 ) : {
151  ostream << "Priority...................................RealTime3\n";
152  break;
153  }
154  case ( RealTimePriority2 ) : {
155  ostream << "Priority...................................RealTime2\n";
156  break;
157  }
158  case ( RealTimePriority1 ) : {
159  ostream << "Priority...................................RealTime1\n";
160  break;
161  }
162  case ( DefaultPriority ) : {
163  ostream << "Priority...................................Default (Inherited)\n";
164  break;
165  }
166  case ( UnknownPriority ) : {
167  ostream << "Priority...................................Unknown\n";
168  break;
169  }
170  }
171  return ostream.str();
172 }
173 
174 
175 /*****************************************************************************
176 ** Hidden Implementations
177 *****************************************************************************/
178 
179 namespace threads {
180 
181 bool set_real_time_priority(int policy,int priority_level) {
182  // policy is ignored
183 
184  HANDLE hThread = GetCurrentThread();
185 
186  if (!hThread) {
187  ecl_debug_throw(threads::throwPriorityException(LOC));
188  return false;
189  }
190 
191  BOOL bResult = FALSE;
192 
193  if (priority_level >= RealTimePriority1) {
194  bResult = SetThreadPriority(hThread, THREAD_PRIORITY_TIME_CRITICAL);
195  }
196 
197  return bResult != FALSE;
198 }
199 } // namespace threads
200 }; // namespace ecl
201 
202 #endif /* ECL_IS_WIN32 */
ecl::RealTimePriority2
@ RealTimePriority2
Definition: priority_common.hpp:51
ecl::UnknownPriority
@ UnknownPriority
Definition: priority_common.hpp:44
ecl::HighPriority
@ HighPriority
Definition: priority_common.hpp:48
ecl::LowPriority
@ LowPriority
Definition: priority_common.hpp:46
ecl::DefaultPriority
@ DefaultPriority
Definition: priority_common.hpp:43
handlers.hpp
ecl::CriticalPriority
@ CriticalPriority
Definition: priority_common.hpp:49
ecl::NormalPriority
@ NormalPriority
Definition: priority_common.hpp:47
ecl::RealTimePriority1
@ RealTimePriority1
Definition: priority_common.hpp:50
ecl::RealTimePriority3
@ RealTimePriority3
Definition: priority_common.hpp:52
ecl::BackgroundPriority
@ BackgroundPriority
Definition: priority_common.hpp:45
ecl::RealTimePriority4
@ RealTimePriority4
Definition: priority_common.hpp:53
ecl
Embedded control libraries.
ecl::Priority
Priority
Shared abstraction of the scheduling priorities.
Definition: priority_common.hpp:36
ecl_debug_throw
#define ecl_debug_throw(exception)


ecl_threads
Author(s): Daniel Stonier
autogenerated on Wed Mar 2 2022 00:16:43