Skip to content

Commit 1b768c4

Browse files
committed
Only change process priority if necessary (Windows only)
1 parent 73ebf4b commit 1b768c4

File tree

4 files changed

+93
-87
lines changed

4 files changed

+93
-87
lines changed

Modelica_DeviceDrivers/Blocks/OperatingSystem.mo

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,23 @@ package OperatingSystem
1616
Modelica_DeviceDrivers.OperatingSystem.ProcessPriority procPrio = Modelica_DeviceDrivers.OperatingSystem.ProcessPriority() if setPriority;
1717
Real dummyState(start = 0, fixed=true)
1818
"dummy state to be integrated, to force synchronization in every integration step";
19-
initial algorithm
20-
if setPriority then
21-
Modelica_DeviceDrivers.OperatingSystem.setProcessPriority(procPrio,
22-
if
23-
(priority == "Idle") then -2 else
24-
if
25-
(priority == "Below normal") then -1 else
26-
if
27-
(priority == "Normal") then 0 else
28-
if
29-
(priority == "High priority") then 1 else
30-
if
31-
(priority == "Realtime") then 2 else
32-
0);
33-
end if;
3419
equation
20+
when initial() then
21+
if setPriority then
22+
Modelica_DeviceDrivers.OperatingSystem.setProcessPriority(procPrio,
23+
if
24+
(priority == "Idle") then -2 else
25+
if
26+
(priority == "Below normal") then -1 else
27+
if
28+
(priority == "Normal") then 0 else
29+
if
30+
(priority == "High priority") then 1 else
31+
if
32+
(priority == "Realtime") then 2 else
33+
0);
34+
end if;
35+
end when;
3536
(calculationTime,availableTime) = Modelica_DeviceDrivers.OperatingSystem.realtimeSynchronize(time,resolution);
3637
der(dummyState) = calculationTime;
3738
annotation (preferredView="info",

Modelica_DeviceDrivers/Resources/Include/MDDRealtimeSynchronize.h

Lines changed: 77 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -43,56 +43,52 @@ DllExport void* MDD_ProcessPriorityConstructor(void) {
4343
DllExport void MDD_ProcessPriorityDestructor(void* prioObj) {
4444
ProcPrio* prio = (ProcPrio*) prioObj;
4545
if (prio) {
46-
BOOL ret = 0;
47-
ModelicaFormatMessage("setting...\n");
48-
switch (prio->lastPrio) {
49-
case IDLE_PRIORITY_CLASS:
50-
ret = SetPriorityClass(GetCurrentProcess(), IDLE_PRIORITY_CLASS);
51-
if (ret) {
52-
ModelicaFormatMessage("ProcessPriority set to idle.\n");
53-
}
54-
break;
55-
56-
case BELOW_NORMAL_PRIORITY_CLASS:
57-
ret = SetPriorityClass(GetCurrentProcess(), BELOW_NORMAL_PRIORITY_CLASS);
58-
if (ret) {
59-
ModelicaFormatMessage("ProcessPriority set to below normal.\n");
60-
}
61-
break;
62-
63-
case NORMAL_PRIORITY_CLASS:
64-
ret = SetPriorityClass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS);
65-
if (ret) {
66-
ModelicaFormatMessage("ProcessPriority set to normal.\n");
67-
}
68-
break;
69-
70-
case HIGH_PRIORITY_CLASS:
71-
ret = SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
72-
if (ret) {
73-
ModelicaFormatMessage("ProcessPriority set to high.\n");
74-
}
75-
break;
76-
77-
case REALTIME_PRIORITY_CLASS:
78-
ret = SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS);
79-
if (ret) {
80-
ModelicaFormatMessage("ProcessPriority set to realtime.\n");
81-
}
82-
break;
46+
DWORD currentPrio = GetPriorityClass(GetCurrentProcess());
47+
if (currentPrio != prio->lastPrio) {
48+
BOOL ret = 1;
49+
ModelicaFormatMessage("setting...\n");
50+
switch (prio->lastPrio) {
51+
case IDLE_PRIORITY_CLASS:
52+
ret = SetPriorityClass(GetCurrentProcess(), IDLE_PRIORITY_CLASS);
53+
if (ret) {
54+
ModelicaFormatMessage("ProcessPriority set to idle.\n");
55+
}
56+
break;
57+
58+
case BELOW_NORMAL_PRIORITY_CLASS:
59+
ret = SetPriorityClass(GetCurrentProcess(), BELOW_NORMAL_PRIORITY_CLASS);
60+
if (ret) {
61+
ModelicaFormatMessage("ProcessPriority set to below normal.\n");
62+
}
63+
break;
64+
65+
case HIGH_PRIORITY_CLASS:
66+
ret = SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
67+
if (ret) {
68+
ModelicaFormatMessage("ProcessPriority set to high.\n");
69+
}
70+
break;
71+
72+
case REALTIME_PRIORITY_CLASS:
73+
ret = SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS);
74+
if (ret) {
75+
ModelicaFormatMessage("ProcessPriority set to realtime.\n");
76+
}
77+
break;
78+
79+
default:
80+
ret = SetPriorityClass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS);
81+
if (ret) {
82+
ModelicaFormatMessage("ProcessPriority set to normal.\n");
83+
}
84+
break;
85+
}
8386

84-
default:
85-
ret = SetPriorityClass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS);
86-
if (ret) {
87-
ModelicaFormatMessage("ProcessPriority set to normal.\n");
87+
if (ret == 0) {
88+
DWORD dw = GetLastError();
89+
if (dw) {
90+
ModelicaFormatMessage("LastError: %d\n", dw);
8891
}
89-
break;
90-
}
91-
92-
if (ret == 0) {
93-
DWORD dw = GetLastError();
94-
if (dw) {
95-
ModelicaFormatMessage("LastError: %d\n", dw);
9692
}
9793
}
9894

@@ -101,48 +97,56 @@ DllExport void MDD_ProcessPriorityDestructor(void* prioObj) {
10197
}
10298

10399
DllExport void MDD_setPriority(void* dummyPrioObj, int priority) {
104-
BOOL ret = 0;
105-
ModelicaFormatMessage("setting...\n");
100+
BOOL ret = 1;
101+
DWORD currentPrio = GetPriorityClass(GetCurrentProcess());
106102
switch (priority) {
107103
case -2:
108-
ret = SetPriorityClass(GetCurrentProcess(), IDLE_PRIORITY_CLASS);
109-
if (ret) {
110-
ModelicaFormatMessage("ProcessPriority set to idle.\n");
104+
if (currentPrio != IDLE_PRIORITY_CLASS) {
105+
ModelicaFormatMessage("setting...\n");
106+
ret = SetPriorityClass(GetCurrentProcess(), IDLE_PRIORITY_CLASS);
107+
if (ret) {
108+
ModelicaFormatMessage("ProcessPriority set to idle.\n");
109+
}
111110
}
112111
break;
113112

114113
case -1:
115-
ret = SetPriorityClass(GetCurrentProcess(), BELOW_NORMAL_PRIORITY_CLASS);
116-
if (ret) {
117-
ModelicaFormatMessage("ProcessPriority set to below normal.\n");
118-
}
119-
break;
120-
121-
case 0:
122-
ret = SetPriorityClass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS);
123-
if (ret) {
124-
ModelicaFormatMessage("ProcessPriority set to normal.\n");
114+
if (currentPrio != BELOW_NORMAL_PRIORITY_CLASS) {
115+
ModelicaFormatMessage("setting...\n");
116+
ret = SetPriorityClass(GetCurrentProcess(), BELOW_NORMAL_PRIORITY_CLASS);
117+
if (ret) {
118+
ModelicaFormatMessage("ProcessPriority set to below normal.\n");
119+
}
125120
}
126121
break;
127122

128123
case 1:
129-
ret = SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
130-
if (ret) {
131-
ModelicaFormatMessage("ProcessPriority set to high.\n");
124+
if (currentPrio != HIGH_PRIORITY_CLASS) {
125+
ModelicaFormatMessage("setting...\n");
126+
ret = SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
127+
if (ret) {
128+
ModelicaFormatMessage("ProcessPriority set to high.\n");
129+
}
132130
}
133131
break;
134132

135133
case 2:
136-
ret = SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS);
137-
if (ret) {
138-
ModelicaFormatMessage("ProcessPriority set to realtime.\n");
134+
if (currentPrio != REALTIME_PRIORITY_CLASS) {
135+
ModelicaFormatMessage("setting...\n");
136+
ret = SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS);
137+
if (ret) {
138+
ModelicaFormatMessage("ProcessPriority set to realtime.\n");
139+
}
139140
}
140141
break;
141142

142143
default:
143-
ret = SetPriorityClass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS);
144-
if (ret) {
145-
ModelicaFormatMessage("ProcessPriority set to normal.\n");
144+
if (currentPrio != NORMAL_PRIORITY_CLASS) {
145+
ModelicaFormatMessage("setting...\n");
146+
ret = SetPriorityClass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS);
147+
if (ret) {
148+
ModelicaFormatMessage("ProcessPriority set to normal.\n");
149+
}
146150
}
147151
break;
148152
}
@@ -330,6 +334,7 @@ DllExport void MDD_ProcessPriorityDestructor(void* prioObj) {
330334
*
331335
* Function maps directly on windows API. For linux a mapping was chosen that seemed
332336
* to be reasonable.
337+
* @param[in] Process priority external object (dummy)
333338
* @param[in] priority range: (-2: idle, -1: below normal, 0: normal, 1: high, 2: realtime)
334339
*/
335340
void MDD_setPriority(void* dummyPrioObj, int priority) {
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)