Skip to content

Commit

Permalink
Merge pull request #415 from lf-lang/thread-sched-fix
Browse files Browse the repository at this point in the history
Add fix to thread scheduling API
  • Loading branch information
lhstrh authored Apr 28, 2024
2 parents 5125110 + 68a7b8b commit b4bdaf8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
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

0 comments on commit b4bdaf8

Please sign in to comment.