Skip to content

Commit b9828a7

Browse files
cfriedtjgl-meta
authored andcommitted
posix: sched: add support for SCHED_OTHER
The `SCHED_OTHER` scheduling priority is mandatory as part of POSIX. It must be numerically distinct from `SCHED_FIFO`, `SCHED_RR`, and `SCHED_SPORADIC`, but is implementation- defined and may behave identically to `SCHED_FIFO` or `SCHED_RR`. Signed-off-by: Chris Friedt <[email protected]>
1 parent ad71b78 commit b9828a7

File tree

5 files changed

+40
-12
lines changed

5 files changed

+40
-12
lines changed

include/zephyr/posix/sched.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,19 @@
66
#ifndef ZEPHYR_INCLUDE_POSIX_SCHED_H_
77
#define ZEPHYR_INCLUDE_POSIX_SCHED_H_
88

9+
#include <zephyr/kernel.h>
10+
911
#ifdef __cplusplus
1012
extern "C" {
1113
#endif
1214

15+
/*
16+
* Other mandatory scheduling policy. Must be numerically distinct. May
17+
* execute identically to SCHED_RR or SCHED_FIFO. For Zephyr this is a
18+
* pseudonym for SCHED_RR.
19+
*/
20+
#define SCHED_OTHER 0
21+
1322
/* Cooperative scheduling policy */
1423
#define SCHED_FIFO 1
1524

lib/posix/posix_internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#ifndef ZEPHYR_LIB_POSIX_POSIX_INTERNAL_H_
88
#define ZEPHYR_LIB_POSIX_POSIX_INTERNAL_H_
99

10+
#include <zephyr/kernel.h>
11+
1012
/*
1113
* Bit used to mark a pthread object as initialized. Initialization status is
1214
* verified (against internal status) in lock / unlock / destroy functions.

lib/posix/pthread.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <zephyr/sys/slist.h>
1515

1616
#include "posix_internal.h"
17+
#include "pthread_sched.h"
1718

1819
#define PTHREAD_INIT_FLAGS PTHREAD_CANCEL_ENABLE
1920
#define PTHREAD_CANCELED ((void *) -1)
@@ -54,7 +55,7 @@ struct posix_thread *to_posix_thread(pthread_t pthread)
5455
return &posix_thread_pool[pthread];
5556
}
5657

57-
static bool is_posix_prio_valid(uint32_t priority, int policy)
58+
static bool is_posix_policy_prio_valid(uint32_t priority, int policy)
5859
{
5960
if (priority >= sched_get_priority_min(policy) &&
6061
priority <= sched_get_priority_max(policy)) {
@@ -108,7 +109,7 @@ int pthread_attr_setschedparam(pthread_attr_t *_attr, const struct sched_param *
108109
int priority = schedparam->sched_priority;
109110

110111
if ((attr == NULL) || (attr->initialized == 0U) ||
111-
(is_posix_prio_valid(priority, attr->schedpolicy) == false)) {
112+
(is_posix_policy_prio_valid(priority, attr->schedpolicy) == false)) {
112113
return EINVAL;
113114
}
114115

@@ -298,11 +299,11 @@ int pthread_setschedparam(pthread_t pthread, int policy,
298299
return ESRCH;
299300
}
300301

301-
if (policy != SCHED_RR && policy != SCHED_FIFO) {
302+
if (!valid_posix_policy(policy)) {
302303
return EINVAL;
303304
}
304305

305-
if (is_posix_prio_valid(param->sched_priority, policy) == false) {
306+
if (is_posix_policy_prio_valid(param->sched_priority, policy) == false) {
306307
return EINVAL;
307308
}
308309

@@ -565,8 +566,7 @@ int pthread_attr_setschedpolicy(pthread_attr_t *_attr, int policy)
565566
{
566567
struct pthread_attr *attr = (struct pthread_attr *)_attr;
567568

568-
if ((attr == NULL) || (attr->initialized == 0U) ||
569-
(policy != SCHED_RR && policy != SCHED_FIFO)) {
569+
if ((attr == NULL) || (attr->initialized == 0U) || !valid_posix_policy(policy)) {
570570
return EINVAL;
571571
}
572572

lib/posix/pthread_sched.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,11 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7+
#include "pthread_sched.h"
8+
79
#include <zephyr/kernel.h>
810
#include <zephyr/posix/sched.h>
911

10-
static inline bool valid_posix_policy(int policy)
11-
{
12-
return policy == SCHED_FIFO || policy == SCHED_RR;
13-
}
14-
1512
/**
1613
* @brief Get minimum priority value for a given policy
1714
*
@@ -36,7 +33,8 @@ int sched_get_priority_max(int policy)
3633
{
3734
if (IS_ENABLED(CONFIG_COOP_ENABLED) && policy == SCHED_FIFO) {
3835
return CONFIG_NUM_COOP_PRIORITIES - 1;
39-
} else if (IS_ENABLED(CONFIG_PREEMPT_ENABLED) && policy == SCHED_RR) {
36+
} else if (IS_ENABLED(CONFIG_PREEMPT_ENABLED) &&
37+
(policy == SCHED_RR || policy == SCHED_OTHER)) {
4038
return CONFIG_NUM_PREEMPT_PRIORITIES - 1;
4139
}
4240

lib/posix/pthread_sched.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright (c) 2023 Meta
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef ZEPHYR_LIB_POSIX_POSIX_PTHREAD_SCHED_H_
8+
#define ZEPHYR_LIB_POSIX_POSIX_PTHREAD_SCHED_H_
9+
10+
#include <stdbool.h>
11+
12+
#include <zephyr/posix/sched.h>
13+
14+
static inline bool valid_posix_policy(int policy)
15+
{
16+
return policy == SCHED_FIFO || policy == SCHED_RR || policy == SCHED_OTHER;
17+
}
18+
19+
#endif

0 commit comments

Comments
 (0)