cgv
Loading...
Searching...
No Matches
priority.cxx
1#include "priority.h"
2#include <iostream>
3
4#ifdef WIN32
5#include <Windows.h>
6#endif
7
8namespace cgv {
9 namespace os {
10
11ExecutionPriority get_execution_priority()
12{
13#ifdef WIN32
14 int pp = GetPriorityClass(GetCurrentProcess());
15 int tp = GetThreadPriority(GetCurrentThread());
16
17 if (pp == NORMAL_PRIORITY_CLASS && tp == THREAD_PRIORITY_NORMAL)
18 return EP_NORMAL;
19
20 if (pp == IDLE_PRIORITY_CLASS && tp == THREAD_PRIORITY_IDLE)
21 return EP_IDLE;
22
23 if (pp == REALTIME_PRIORITY_CLASS && tp == THREAD_PRIORITY_TIME_CRITICAL)
24 return EP_HIGH;
25#endif
26
27 return EP_NORMAL;
28}
29
30
31bool set_execution_priority(ExecutionPriority p)
32{
33#ifdef WIN32
34 switch (p) {
35 case EP_IDLE :
36 if (!SetPriorityClass(GetCurrentProcess(), IDLE_PRIORITY_CLASS)) {
37 std::cerr << "could not set idle process priority" << std::endl;
38 return false;
39 }
40 if(!SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_IDLE)) {
41 std::cerr << "could not set idle thread priority" << std::endl;
42 return false;
43 }
44 break;
45 case EP_NORMAL :
46 if (!SetPriorityClass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS)) {
47 std::cerr << "could not set normal process priority" << std::endl;
48 return false;
49 }
50 if(!SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL)) {
51 std::cerr << "could not set normal thread priority" << std::endl;
52 return false;
53 }
54 break;
55 case EP_HIGH :
56 if (!SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS)) {
57 std::cerr << "could not set realtime process priority" << std::endl;
58 return false;
59 }
60 if(!SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL)) {
61 std::cerr << "could not set time critical thread priority" << std::endl;
62 return false;
63 }
64 break;
65 }
66 return true;
67#else
68 std::cerr << "set_execution_priority not implemented" << std::endl;
69 return false;
70#endif
71}
72
73 }
74}
the cgv namespace
Definition print.h:11