Skip to content

Commit 26ffb63

Browse files
committed
posix: sched: ensure min and max priority are schedulable
Previously, there was an off-by-one error for SCHED_RR. Fixes #56729 Signed-off-by: Chris Friedt <[email protected]> (cherry picked from commit 2b2cbf8)
1 parent 5db2717 commit 26ffb63

File tree

2 files changed

+15
-45
lines changed

2 files changed

+15
-45
lines changed

lib/posix/pthread.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@
1515
#define PTHREAD_INIT_FLAGS PTHREAD_CANCEL_ENABLE
1616
#define PTHREAD_CANCELED ((void *) -1)
1717

18-
#define LOWEST_POSIX_THREAD_PRIORITY 1
19-
2018
PTHREAD_MUTEX_DEFINE(pthread_key_lock);
2119

2220
static const pthread_attr_t init_pthread_attrs = {
23-
.priority = LOWEST_POSIX_THREAD_PRIORITY,
21+
.priority = 0,
2422
.stack = NULL,
2523
.stacksize = 0,
2624
.flags = PTHREAD_INIT_FLAGS,
@@ -54,9 +52,11 @@ static uint32_t zephyr_to_posix_priority(int32_t z_prio, int *policy)
5452
if (z_prio < 0) {
5553
*policy = SCHED_FIFO;
5654
prio = -1 * (z_prio + 1);
55+
__ASSERT_NO_MSG(prio < CONFIG_NUM_COOP_PRIORITIES);
5756
} else {
5857
*policy = SCHED_RR;
59-
prio = (CONFIG_NUM_PREEMPT_PRIORITIES - z_prio);
58+
prio = (CONFIG_NUM_PREEMPT_PRIORITIES - z_prio - 1);
59+
__ASSERT_NO_MSG(prio < CONFIG_NUM_PREEMPT_PRIORITIES);
6060
}
6161

6262
return prio;
@@ -68,9 +68,11 @@ static int32_t posix_to_zephyr_priority(uint32_t priority, int policy)
6868

6969
if (policy == SCHED_FIFO) {
7070
/* Zephyr COOP priority starts from -1 */
71+
__ASSERT_NO_MSG(priority < CONFIG_NUM_COOP_PRIORITIES);
7172
prio = -1 * (priority + 1);
7273
} else {
73-
prio = (CONFIG_NUM_PREEMPT_PRIORITIES - priority);
74+
__ASSERT_NO_MSG(priority < CONFIG_NUM_PREEMPT_PRIORITIES);
75+
prio = (CONFIG_NUM_PREEMPT_PRIORITIES - priority - 1);
7476
}
7577

7678
return prio;

lib/posix/pthread_sched.c

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,9 @@
77
#include <kernel.h>
88
#include <posix/posix_sched.h>
99

10-
static bool valid_posix_policy(int policy)
10+
static inline bool valid_posix_policy(int policy)
1111
{
12-
if (policy != SCHED_FIFO && policy != SCHED_RR) {
13-
return false;
14-
}
15-
16-
return true;
12+
return policy == SCHED_FIFO || policy == SCHED_RR;
1713
}
1814

1915
/**
@@ -23,25 +19,12 @@ static bool valid_posix_policy(int policy)
2319
*/
2420
int sched_get_priority_min(int policy)
2521
{
26-
if (valid_posix_policy(policy) == false) {
22+
if (!valid_posix_policy(policy)) {
2723
errno = EINVAL;
2824
return -1;
2925
}
3026

31-
if (IS_ENABLED(CONFIG_COOP_ENABLED)) {
32-
if (policy == SCHED_FIFO) {
33-
return 0;
34-
}
35-
}
36-
37-
if (IS_ENABLED(CONFIG_PREEMPT_ENABLED)) {
38-
if (policy == SCHED_RR) {
39-
return 0;
40-
}
41-
}
42-
43-
errno = EINVAL;
44-
return -1;
27+
return 0;
4528
}
4629

4730
/**
@@ -51,25 +34,10 @@ int sched_get_priority_min(int policy)
5134
*/
5235
int sched_get_priority_max(int policy)
5336
{
54-
if (valid_posix_policy(policy) == false) {
55-
errno = EINVAL;
56-
return -1;
57-
}
58-
59-
if (IS_ENABLED(CONFIG_COOP_ENABLED)) {
60-
if (policy == SCHED_FIFO) {
61-
/* Posix COOP priority starts from 0
62-
* whereas zephyr starts from -1
63-
*/
64-
return (CONFIG_NUM_COOP_PRIORITIES - 1);
65-
}
66-
67-
}
68-
69-
if (IS_ENABLED(CONFIG_PREEMPT_ENABLED)) {
70-
if (policy == SCHED_RR) {
71-
return CONFIG_NUM_PREEMPT_PRIORITIES;
72-
}
37+
if (IS_ENABLED(CONFIG_COOP_ENABLED) && policy == SCHED_FIFO) {
38+
return CONFIG_NUM_COOP_PRIORITIES - 1;
39+
} else if (IS_ENABLED(CONFIG_PREEMPT_ENABLED) && policy == SCHED_RR) {
40+
return CONFIG_NUM_PREEMPT_PRIORITIES - 1;
7341
}
7442

7543
errno = EINVAL;

0 commit comments

Comments
 (0)