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) ecl_debug_throw_decl(StandardException)
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() ecl_debug_throw_decl(StandardException)
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() ecl_debug_throw_decl(StandardException) {
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) ecl_debug_throw_decl(StandardException) {
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 */
Embedded control libraries.
#define ecl_debug_throw_decl(exception)
#define ecl_debug_throw(exception)
Priority
Shared abstraction of the scheduling priorities.


ecl_threads
Author(s): Daniel Stonier
autogenerated on Mon Jun 10 2019 13:08:44