Skip to content

Commit

Permalink
Refs #19436. Add MacOS implementation for setting scheduler and prior…
Browse files Browse the repository at this point in the history
…ity.

Signed-off-by: Miguel Company <MiguelCompany@eprosima.com>
  • Loading branch information
MiguelCompany committed Sep 26, 2023
1 parent fc928fe commit 1248c67
Showing 1 changed file with 54 additions and 1 deletion.
55 changes: 54 additions & 1 deletion src/cpp/utils/threading/threading_osx.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <string.h>
#include <stdio.h>

#include <fastdds/dds/log/Log.hpp>
#include <fastdds/rtps/attributes/ThreadSettings.hpp>

namespace eprosima {
Expand Down Expand Up @@ -50,9 +51,61 @@ void set_name_to_current_thread(
set_name_to_current_thread_impl(fmt, arg1, arg2);
}

static void configure_current_thread_scheduler(
int sched_class,
int sched_priority)
{
pthread_t self_tid = pthread_self();
sched_param param;
int result = 0;

memset(&param, 0, sizeof(param));
param.sched_priority = 0;

//
// Set Scheduler Class and Priority
//

if((sched_class == SCHED_OTHER) ||
(sched_class == SCHED_BATCH) ||
(sched_class == SCHED_IDLE))
{
//
// BATCH and IDLE do not have explicit priority values.
// - Requires priorty value to be zero (0).

result = pthread_setschedparam(self_tid, sched_class, &param);

//
// Sched OTHER has a nice value, that we pull from the priority parameter.
//

if(sched_class == SCHED_OTHER)
{
result = setpriority(PRIO_PROCESS, gettid(), sched_priority);
}
}
else if((sched_class == SCHED_FIFO) ||
(sched_class == SCHED_RR))
{
//
// RT Policies use a different priority numberspace.
//

param.sched_priority = sched_priority;
result = pthread_setschedparam(self_tid, sched_class, &param);
}

if (0 != result)
{
EPROSIMA_LOG_ERROR(SYSTEM, "Error '" << strerror(result) << "' configuring scheduler for thread " << self_tid);
}
}

void apply_thread_settings_to_current_thread(
const fastdds::rtps::ThreadSettings& /*settings*/)
const fastdds::rtps::ThreadSettings& settings)
{
configure_current_thread_scheduler(settings.scheduling_policy, settings.priority);
}

} // namespace eprosima

0 comments on commit 1248c67

Please sign in to comment.