File tree Expand file tree Collapse file tree 3 files changed +45
-0
lines changed Expand file tree Collapse file tree 3 files changed +45
-0
lines changed Original file line number Diff line number Diff line change @@ -1741,6 +1741,8 @@ int main(int argc, char* argv[])
1741
1741
}
1742
1742
});
1743
1743
1744
+ set_real_time_priority (true );
1745
+
1744
1746
#ifdef PCM_FORCE_SILENT
1745
1747
null_stream nullStream1, nullStream2;
1746
1748
std::cout.rdbuf (&nullStream1);
Original file line number Diff line number Diff line change @@ -395,6 +395,48 @@ void restore_signal_handlers(void)
395
395
return ;
396
396
}
397
397
398
+ void set_real_time_priority (const bool & silent)
399
+ {
400
+ if (!silent)
401
+ {
402
+ std::cerr << " Setting real time priority for the process\n " ;
403
+ }
404
+ #ifdef _MSC_VER
405
+ if (!SetPriorityClass (GetCurrentProcess (), REALTIME_PRIORITY_CLASS))
406
+ {
407
+ std::cerr << " ERROR: SetPriorityClass with REALTIME_PRIORITY_CLASS failed with error " << GetLastError () << " \n " ;
408
+ }
409
+ if (!SetThreadPriority (GetCurrentThread (), THREAD_PRIORITY_TIME_CRITICAL))
410
+ {
411
+ std::cerr << " ERROR: SetThreadPriority with THREAD_PRIORITY_TIME_CRITICAL failed with error " << GetLastError () << " \n " ;
412
+ }
413
+ #elif __linux__
414
+ const auto priority = sched_get_priority_max (SCHED_RR);
415
+ if (priority == -1 )
416
+ {
417
+ std::cerr << " ERROR: Could not get SCHED_RR max priority: " << strerror (errno) << " \n " ;
418
+ }
419
+ else
420
+ {
421
+ struct sched_param sp = { .sched_priority = priority };
422
+ if (sched_setscheduler (0 , SCHED_RR, &sp) == -1 )
423
+ {
424
+ const auto errnosave = errno;
425
+ std::cerr << " ERROR: Could not set scheduler to realtime! Errno: " << errnosave << " Error message: \" " << strerror (errnosave) << " \"\n " ;
426
+ }
427
+ else
428
+ {
429
+ if (!silent)
430
+ {
431
+ std::cerr << " Scheduler changed to SCHED_RR and priority to " << priority << " \n " ;
432
+ }
433
+ }
434
+ }
435
+ #else
436
+ std::cerr << " Setting real time priority for the process not implemented on your OS.\n " ;
437
+ #endif
438
+ }
439
+
398
440
void set_post_cleanup_callback (void (*cb)(void ))
399
441
{
400
442
post_cleanup_callback = cb;
Original file line number Diff line number Diff line change @@ -45,6 +45,7 @@ namespace pcm {
45
45
46
46
void exit_cleanup (void );
47
47
void set_signal_handlers (void );
48
+ void set_real_time_priority (const bool & silent);
48
49
void restore_signal_handlers (void );
49
50
#ifndef _MSC_VER
50
51
void sigINT_handler (int signum);
You can’t perform that action at this time.
0 commit comments