Skip to content

Commit 888079a

Browse files
committed
pcm-raw: run with high priority
Change-Id: Ib72bfd8f9675db6a1f423e4fea509144ca844dc8
1 parent d37a225 commit 888079a

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

src/pcm-raw.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1741,6 +1741,8 @@ int main(int argc, char* argv[])
17411741
}
17421742
});
17431743

1744+
set_real_time_priority(true);
1745+
17441746
#ifdef PCM_FORCE_SILENT
17451747
null_stream nullStream1, nullStream2;
17461748
std::cout.rdbuf(&nullStream1);

src/utils.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,48 @@ void restore_signal_handlers(void)
395395
return;
396396
}
397397

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+
398440
void set_post_cleanup_callback(void(*cb)(void))
399441
{
400442
post_cleanup_callback = cb;

src/utils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ namespace pcm {
4545

4646
void exit_cleanup(void);
4747
void set_signal_handlers(void);
48+
void set_real_time_priority(const bool & silent);
4849
void restore_signal_handlers(void);
4950
#ifndef _MSC_VER
5051
void sigINT_handler(int signum);

0 commit comments

Comments
 (0)