Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fix to thread scheduling API #415

Merged
merged 4 commits into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions low_level_platform/impl/src/lf_linux_support.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,17 @@ int lf_thread_set_cpu(lf_thread_t thread, int cpu_number) {
}

int lf_thread_set_priority(lf_thread_t thread, int priority) {
int posix_policy, min_pri, max_pri, final_priority;
int posix_policy, min_pri, max_pri, final_priority, res;
struct sched_param schedparam;

if (priority > LF_SCHED_MAX_PRIORITY || priority < LF_SCHED_MIN_PRIORITY) {
return -1;
}

// Get the current scheduling policy
if (pthread_getschedparam(thread, &posix_policy, &schedparam) != 0) {
return -1;
res = pthread_getschedparam(thread, &posix_policy, &schedparam);
if (res != 0) {
return res;
}

min_pri = sched_get_priority_min(posix_policy);
Expand All @@ -87,39 +88,45 @@ int lf_thread_set_priority(lf_thread_t thread, int priority) {
}

int lf_thread_set_scheduling_policy(lf_thread_t thread, lf_scheduling_policy_t* policy) {
int posix_policy;
int posix_policy, res;
struct sched_param schedparam;

// Get the current scheduling policy
if (pthread_getschedparam(thread, &posix_policy, &schedparam) != 0) {
return -1;
res = pthread_getschedparam(thread, &posix_policy, &schedparam);
if (res != 0) {
return res;
}

// Update the policy
// Update the policy, and initially set the priority to max.
// The priority value is later updated. Initializing it
// is just to avoid code duplication.
switch (policy->policy) {
case LF_SCHED_FAIR:
posix_policy = SCHED_OTHER;
break;
case LF_SCHED_TIMESLICE:
posix_policy = SCHED_RR;
schedparam.sched_priority = sched_get_priority_max(SCHED_RR);
break;
case LF_SCHED_PRIORITY:
posix_policy = SCHED_FIFO;
schedparam.sched_priority = sched_get_priority_max(SCHED_FIFO);
break;
default:
return -1;
break;
}

// Write it back
if (pthread_setschedparam(thread, posix_policy, &schedparam) != 0) {
return -3;
res = pthread_setschedparam(thread, posix_policy, &schedparam);
if (res != 0) {
return res;
}

// Set the priority
if (lf_thread_set_priority(thread, policy->priority) != 0) {
return -1;
}
res = lf_thread_set_priority(thread, policy->priority);
if (res != 0)
return res;

return 0;
}
Expand Down
7 changes: 4 additions & 3 deletions low_level_platform/impl/src/lf_platform_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ int map_priorities(int priority, int dest_min, int dest_max) {
return -1;
}

// Perform the linear mapping
return dest_min +
((dest_max - dest_min) / (LF_SCHED_MAX_PRIORITY - LF_SCHED_MIN_PRIORITY)) * (priority - LF_SCHED_MIN_PRIORITY);
// Perform the linear mapping. Since we are working with integers, it is
// important to multiply before we divide
return dest_min + (((priority - LF_SCHED_MIN_PRIORITY) * (dest_max - dest_min)) /
(LF_SCHED_MAX_PRIORITY - LF_SCHED_MIN_PRIORITY));
}

#ifndef PLATFORM_ZEPHYR // on Zephyr, this is handled separately
Expand Down
Loading