@@ -156,6 +156,31 @@ bool setProcessPriority(pprocess_t& process, DWORD priority)
156156 " REALTIME_PRIORITY_CLASS may fail. Try 'Run as Administrator'." );
157157 }
158158
159+ DWORD old_process_priority = ::GetPriorityClass (process);
160+ if (old_process_priority == 0 )
161+ {
162+ DWORD err = GetLastError ();
163+ URCL_LOG_ERROR (" Unsuccessful in retrieving the current process priority. Error: %lu (%s)" , err,
164+ getLastWindowsErrorMsg (err).c_str ());
165+ return false ;
166+ }
167+
168+ auto restore_priority = [&]()
169+ {
170+ if (!::SetPriorityClass (process, old_process_priority))
171+ {
172+ DWORD err = GetLastError ();
173+ URCL_LOG_ERROR (" Failed to restore previous process priority %s (0x%X). Error: %lu (%s)" ,
174+ processPriorityToString (old_process_priority), old_process_priority,
175+ err, getLastWindowsErrorMsg (err).c_str ());
176+ }
177+ else
178+ {
179+ URCL_LOG_INFO (" Previous process priority successfully restored to %s (0x%X)" ,
180+ processPriorityToString (old_process_priority), old_process_priority);
181+ }
182+ };
183+
159184 if (!::SetPriorityClass (process, priority))
160185 {
161186 DWORD err = GetLastError ();
@@ -170,6 +195,7 @@ bool setProcessPriority(pprocess_t& process, DWORD priority)
170195 DWORD err = GetLastError ();
171196 URCL_LOG_ERROR (" Unsuccessful in retrieving the process priority for verification. Error: %lu (%s)" , err,
172197 getLastWindowsErrorMsg (err).c_str ());
198+ restore_priority ();
173199 return false ;
174200 }
175201
@@ -180,6 +206,7 @@ bool setProcessPriority(pprocess_t& process, DWORD priority)
180206 {
181207 URCL_LOG_WARN (" Process priority mismatch. Expected %s (0x%X), got %s (0x%X)" , processPriorityToString (priority),
182208 priority, processPriorityToString (priority_applied), priority_applied);
209+ restore_priority ();
183210 return false ;
184211 }
185212
@@ -236,6 +263,31 @@ bool setThreadAffinity(pthread_t& thread, DWORD_PTR cpu_mask)
236263
237264bool setThreadPriority (pthread_t & thread, const int priority)
238265{
266+ int old_priority = ::GetThreadPriority (thread);
267+ if (old_priority == THREAD_PRIORITY_ERROR_RETURN )
268+ {
269+ DWORD err = GetLastError ();
270+ URCL_LOG_ERROR (" Unsuccessful in retrieving the current thread priority. Error: %lu (%s)" ,
271+ err, getLastWindowsErrorMsg (err).c_str ());
272+ return false ;
273+ }
274+
275+ auto restore_priority = [&]()
276+ {
277+ if (!::SetThreadPriority (thread, old_priority))
278+ {
279+ DWORD err = GetLastError ();
280+ URCL_LOG_ERROR (" Failed to restore previous thread priority %s (%d). Error: %lu (%s)" ,
281+ threadPriorityToString (old_priority), old_priority,
282+ err, getLastWindowsErrorMsg (err).c_str ());
283+ }
284+ else
285+ {
286+ URCL_LOG_INFO (" Previous thread priority successfully restored to %s (%d)" ,
287+ threadPriorityToString (old_priority), old_priority);
288+ }
289+ };
290+
239291 if (!::SetThreadPriority (thread, priority))
240292 {
241293 DWORD err = GetLastError ();
@@ -251,6 +303,7 @@ bool setThreadPriority(pthread_t& thread, const int priority)
251303 DWORD err = GetLastError ();
252304 URCL_LOG_ERROR (" Unsuccessful in retrieving the thread priority for verification. Error: %lu (%s)" , err,
253305 getLastWindowsErrorMsg (err).c_str ());
306+ restore_priority ();
254307 return false ;
255308 }
256309
@@ -260,6 +313,7 @@ bool setThreadPriority(pthread_t& thread, const int priority)
260313 {
261314 URCL_LOG_WARN (" Thread priority mismatch. Expected %s (%d), got %s (%d)" , threadPriorityToString (priority), priority,
262315 threadPriorityToString (applied), applied);
316+ restore_priority ();
263317 return false ;
264318 }
265319
@@ -358,11 +412,15 @@ bool setFiFoScheduling(pthread_t& thread, int priority)
358412 if (!setThreadPriority (thread, priority))
359413 {
360414 URCL_LOG_ERROR (" Unsuccessful in setting thread priority to %s (%d)" , threadPriorityToString (priority), priority);
415+ URCL_LOG_INFO (" Restoring previous process priority %s (0x%X)" ,
416+ processPriorityToString (old_process_priority), old_process_priority);
361417
362- if (!setProcessPriority (process, old_process_priority))
418+ if (!:: SetPriorityClass (process, old_process_priority))
363419 {
364- URCL_LOG_ERROR (" Failed to restore previous process priority %s (0x%X)" ,
365- processPriorityToString (old_process_priority), old_process_priority);
420+ DWORD err = GetLastError ();
421+ URCL_LOG_ERROR (" Failed to restore previous process priority %s (0x%X). Error: %lu (%s)" ,
422+ processPriorityToString (old_process_priority), old_process_priority,
423+ err, getLastWindowsErrorMsg (err).c_str ());
366424 }
367425
368426 return false ;
0 commit comments