Skip to content

Commit 7eabf10

Browse files
youvedeep-singhAnas Nashif
authored andcommitted
kernel: POSIX: Compatibility layer for scheduler APIs.
This patch provides scheduler APIs for POSIX 1003.1 PSE52 standard. Signed-off-by: Youvedeep Singh <[email protected]>
1 parent c8aa657 commit 7eabf10

File tree

6 files changed

+103
-10
lines changed

6 files changed

+103
-10
lines changed

arch/posix/include/posix_cheats.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
#define pthread_barrierattr_destroy(...) \
6060
zap_pthread_barrierattr_destroy(__VA_ARGS__)
6161

62-
/* pthread */
62+
/* Pthread */
6363
#define pthread_attr_init(...) zap_pthread_attr_init(__VA_ARGS__)
6464
#define pthread_attr_destroy(...) zap_pthread_attr_destroy(__VA_ARGS__)
6565
#define pthread_attr_getschedparam(...) \
@@ -90,6 +90,11 @@
9090
#define pthread_setcancelstate(...) zap_pthread_setcancelstate(__VA_ARGS__)
9191
#define pthread_setschedparam(...) zap_pthread_setschedparam(__VA_ARGS__)
9292

93+
/* Scheduler */
94+
#define sched_yield(...) zap_sched_yield(__VA_ARGS__)
95+
#define sched_get_priority_min(...) zap_sched_get_priority_min(__VA_ARGS__)
96+
#define sched_get_priority_max(...) zap_sched_get_priority_max(__VA_ARGS__)
97+
9398
#endif /* CONFIG_ARCH_POSIX */
9499

95100
#endif

include/posix/sched.h renamed to include/posix/posix_sched.h

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,34 @@
1010
extern "C" {
1111
#endif
1212

13-
#include_next <sched.h>
14-
1513
/* Cooperative scheduling policy */
1614
#ifndef SCHED_FIFO
1715
#define SCHED_FIFO 0
1816
#endif /* SCHED_FIFO */
1917

20-
/* Priority based prempetive scheduling policy */
18+
/* Priority based preemptive scheduling policy */
2119
#ifndef SCHED_RR
2220
#define SCHED_RR 1
2321
#endif /* SCHED_RR */
2422

2523
struct sched_param {
26-
int priority; /* Thread execution priority */
24+
int priority;
2725
};
2826

27+
/**
28+
* @brief Yield the processor
29+
*
30+
* See IEEE 1003.1
31+
*/
32+
static inline int sched_yield(void)
33+
{
34+
k_yield();
35+
return 0;
36+
}
37+
38+
int sched_get_priority_min(int policy);
39+
int sched_get_priority_max(int policy);
40+
2941
#ifdef __cplusplus
3042
}
3143
#endif

include/posix/pthread.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ struct timespec {
2121
#endif /* CONFIG_NEWLIB_LIBC */
2222

2323
#include "sys/types.h"
24-
#include "sched.h"
24+
#include "posix_sched.h"
2525

2626
enum pthread_state {
2727
/* The thread is running and joinable. */

kernel/posix/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ target_sources(kernel PRIVATE posix/pthread_cond.c)
33
target_sources(kernel PRIVATE posix/pthread_mutex.c)
44
target_sources(kernel PRIVATE posix/pthread_barrier.c)
55
target_sources(kernel PRIVATE posix/pthread.c)
6+
target_sources(kernel PRIVATE posix/pthread_sched.c)

kernel/posix/pthread.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,8 @@ K_MEM_POOL_DEFINE(posix_thread_pool, sizeof(struct posix_thread),
3030
sizeof(struct posix_thread), CONFIG_MAX_PTHREAD_COUNT, 4);
3131
static bool is_posix_prio_valid(u32_t priority, int policy)
3232
{
33-
if ((policy == SCHED_RR &&
34-
priority <= CONFIG_NUM_PREEMPT_PRIORITIES) ||
35-
(policy == SCHED_FIFO &&
36-
priority < CONFIG_NUM_COOP_PRIORITIES)) {
33+
if (priority >= sched_get_priority_min(policy) &&
34+
priority <= sched_get_priority_max(policy)) {
3735
return true;
3836
}
3937

kernel/posix/pthread_sched.c

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright (c) 2018 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <kernel.h>
8+
#include <posix_sched.h>
9+
10+
static bool valid_posix_policy(int policy)
11+
{
12+
if (policy != SCHED_FIFO && policy != SCHED_RR) {
13+
return false;
14+
}
15+
16+
return true;
17+
}
18+
19+
/**
20+
* @brief Get minimum priority value for a given policy
21+
*
22+
* See IEEE 1003.1
23+
*/
24+
int sched_get_priority_min(int policy)
25+
{
26+
if (valid_posix_policy(policy) == false) {
27+
errno = EINVAL;
28+
return -1;
29+
}
30+
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;
45+
}
46+
47+
/**
48+
* @brief Get maximum priority value for a given policy
49+
*
50+
* See IEEE 1003.1
51+
*/
52+
int sched_get_priority_max(int policy)
53+
{
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+
}
73+
}
74+
75+
errno = EINVAL;
76+
return -1;
77+
}

0 commit comments

Comments
 (0)