Skip to content

Commit 7c42c01

Browse files
ycsincarlescufi
authored andcommitted
posix: pthread: use is_posix_policy_prio_valid to check POSIX priority
Use the existing `is_posix_policy_prio_valid()` function to verify the POSIX's priority in the conversion functions. Changed the `priority` arg of `is_posix_policy_prio_valid` to `int` since that is the output of `sched_get_priority_min` & `sched_get_priority_max`. Signed-off-by: Yong Cong Sin <[email protected]>
1 parent c3cc2e4 commit 7c42c01

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

lib/posix/pthread.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ void __z_pthread_cleanup_pop(int execute)
215215
}
216216
}
217217

218-
static bool is_posix_policy_prio_valid(uint32_t priority, int policy)
218+
static bool is_posix_policy_prio_valid(int priority, int policy)
219219
{
220220
if (priority >= sched_get_priority_min(policy) &&
221221
priority <= sched_get_priority_max(policy)) {
@@ -230,26 +230,25 @@ static bool is_posix_policy_prio_valid(uint32_t priority, int policy)
230230
/* Non-static so that they can be tested in ztest */
231231
int zephyr_to_posix_priority(int z_prio, int *policy)
232232
{
233+
int priority;
234+
233235
if (z_prio < 0) {
234236
__ASSERT_NO_MSG(-z_prio <= CONFIG_NUM_COOP_PRIORITIES);
235237
} else {
236238
__ASSERT_NO_MSG(z_prio < CONFIG_NUM_PREEMPT_PRIORITIES);
237239
}
238240

239241
*policy = (z_prio < 0) ? SCHED_FIFO : SCHED_RR;
240-
return ZEPHYR_TO_POSIX_PRIORITY(z_prio);
242+
priority = ZEPHYR_TO_POSIX_PRIORITY(z_prio);
243+
__ASSERT_NO_MSG(is_posix_policy_prio_valid(priority, *policy));
244+
245+
return priority;
241246
}
242247

243248
/* Non-static so that they can be tested in ztest */
244249
int posix_to_zephyr_priority(int priority, int policy)
245250
{
246-
if (policy == SCHED_FIFO) {
247-
/* COOP: highest [-CONFIG_NUM_COOP_PRIORITIES, -1] lowest */
248-
__ASSERT_NO_MSG(priority < CONFIG_NUM_COOP_PRIORITIES);
249-
} else {
250-
/* PREEMPT: lowest [0, CONFIG_NUM_PREEMPT_PRIORITIES - 1] highest */
251-
__ASSERT_NO_MSG(priority < CONFIG_NUM_PREEMPT_PRIORITIES);
252-
}
251+
__ASSERT_NO_MSG(is_posix_policy_prio_valid(priority, policy));
253252

254253
return POSIX_TO_ZEPHYR_PRIORITY(priority, policy);
255254
}

0 commit comments

Comments
 (0)