diff --git a/include/posix/pthread.h b/include/posix/pthread.h index cd9f949431fc..f412adc8fdbc 100644 --- a/include/posix/pthread.h +++ b/include/posix/pthread.h @@ -18,6 +18,7 @@ struct timespec { s32_t tv_nsec; }; #endif +#include "pthread_sched.h" static inline s32_t _ts_to_ms(const struct timespec *to) { diff --git a/include/posix/pthread_sched.h b/include/posix/pthread_sched.h new file mode 100644 index 000000000000..80bb80b8fefb --- /dev/null +++ b/include/posix/pthread_sched.h @@ -0,0 +1,15 @@ +/* + * Copyright (c) 2017 Intel Corporation + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +#define SCHED_COOP 0 +#define SCHED_PREMPT 1 + +extern int sched_get_priority_min(int policy); +extern int sched_get_priority_max(int policy); +extern int sched_yield(void); + diff --git a/kernel/posix/CMakeLists.txt b/kernel/posix/CMakeLists.txt index a82c866d8db0..cfb02abb13ad 100644 --- a/kernel/posix/CMakeLists.txt +++ b/kernel/posix/CMakeLists.txt @@ -2,3 +2,4 @@ target_sources(kernel PRIVATE posix/pthread_common.c) target_sources(kernel PRIVATE posix/pthread_cond.c) target_sources(kernel PRIVATE posix/pthread_mutex.c) target_sources(kernel PRIVATE posix/pthread_barrier.c) +target_sources(kernel PRIVATE posix/pthread_sched.c) diff --git a/kernel/posix/pthread_sched.c b/kernel/posix/pthread_sched.c new file mode 100644 index 000000000000..8fab347667d9 --- /dev/null +++ b/kernel/posix/pthread_sched.c @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2017 Intel Corporation + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +/* + * ======== sched_get_priority_min ======== + * Provide Minimum Priority for the given policy + * + * In case of Cooperative priority value returned + * is negative of the priority value. + * + * returns: priority corresponing to policy + * -EINVAL for Error + */ +int sched_get_priority_min(int policy) +{ + + int priority = -EINVAL; + + if (!((policy == CONFIG_COOP_ENABLED) || (policy == SCHED_PREMPT))) { + return -EINVAL; + } +#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED) + priority = (policy == SCHED_COOP) ? (-1 * -1) : + K_LOWEST_APPLICATION_THREAD_PRIO; +#elif defined(CONFIG_COOP_ENABLED) + priority = (policy == SCHED_COOP) ? + (-1 * K_LOWEST_APPLICATION_THREAD_PRIO) : -EINVAL; +#elif defined(CONFIG_PREEMPT_ENABLED) + priority = (policy == SCHED_PREMPT) ? K_LOWEST_APPLICATION_THREAD_PRIO : + -EINVAL; +#endif + return priority; +} + +/* + * ======== sched_get_priority_max ======== + * Provide Maximum Priority for the given policy + * + * In case of Cooperative priority value returned + * is negative of the priority value. + * + * returns: priority corresponing to policy + * -EINVAL for Error + */ +int sched_get_priority_max(int policy) +{ + int priority = -EINVAL; + + if (!((policy == CONFIG_COOP_ENABLED) || (policy == SCHED_PREMPT))) { + return -EINVAL; + } +#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED) + priority = (policy == SCHED_COOP) ? + (-1 * K_HIGHEST_APPLICATION_THREAD_PRIO) : 0; +#elif defined(CONFIG_COOP_ENABLED) + priority = (policy == SCHED_COOP) ? + (-1 * K_HIGHEST_APPLICATION_THREAD_PRIO) : -EINVAL; +#elif defined(CONFIG_PREEMPT_ENABLED) + priority = (policy == SCHED_PREMPT) ? + K_HIGHEST_APPLICATION_THREAD_PRIO : -EINVAL; +#endif + return priority; +} + +/* + * ======== sched_yield ======== + */ +int sched_yield(void) +{ + k_yield(); + return 0; +} +