kernel: POSIX: Compatibility layer for scheduler APIs.

This patch provides scheduler APIs for POSIX 1003.1 PSE52 standard.

Signed-off-by: Youvedeep Singh <youvedeep.singh@intel.com>
This commit is contained in:
Youvedeep Singh 2018-01-13 18:51:15 +05:30 committed by Anas Nashif
commit 7eabf1025c
6 changed files with 103 additions and 10 deletions

View file

@ -59,7 +59,7 @@
#define pthread_barrierattr_destroy(...) \
zap_pthread_barrierattr_destroy(__VA_ARGS__)
/* pthread */
/* Pthread */
#define pthread_attr_init(...) zap_pthread_attr_init(__VA_ARGS__)
#define pthread_attr_destroy(...) zap_pthread_attr_destroy(__VA_ARGS__)
#define pthread_attr_getschedparam(...) \
@ -90,6 +90,11 @@
#define pthread_setcancelstate(...) zap_pthread_setcancelstate(__VA_ARGS__)
#define pthread_setschedparam(...) zap_pthread_setschedparam(__VA_ARGS__)
/* Scheduler */
#define sched_yield(...) zap_sched_yield(__VA_ARGS__)
#define sched_get_priority_min(...) zap_sched_get_priority_min(__VA_ARGS__)
#define sched_get_priority_max(...) zap_sched_get_priority_max(__VA_ARGS__)
#endif /* CONFIG_ARCH_POSIX */
#endif

View file

@ -10,22 +10,34 @@
extern "C" {
#endif
#include_next <sched.h>
/* Cooperative scheduling policy */
#ifndef SCHED_FIFO
#define SCHED_FIFO 0
#endif /* SCHED_FIFO */
/* Priority based prempetive scheduling policy */
/* Priority based preemptive scheduling policy */
#ifndef SCHED_RR
#define SCHED_RR 1
#endif /* SCHED_RR */
struct sched_param {
int priority; /* Thread execution priority */
int priority;
};
/**
* @brief Yield the processor
*
* See IEEE 1003.1
*/
static inline int sched_yield(void)
{
k_yield();
return 0;
}
int sched_get_priority_min(int policy);
int sched_get_priority_max(int policy);
#ifdef __cplusplus
}
#endif

View file

@ -21,7 +21,7 @@ struct timespec {
#endif /* CONFIG_NEWLIB_LIBC */
#include "sys/types.h"
#include "sched.h"
#include "posix_sched.h"
enum pthread_state {
/* The thread is running and joinable. */

View file

@ -3,3 +3,4 @@ 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.c)
target_sources(kernel PRIVATE posix/pthread_sched.c)

View file

@ -30,10 +30,8 @@ K_MEM_POOL_DEFINE(posix_thread_pool, sizeof(struct posix_thread),
sizeof(struct posix_thread), CONFIG_MAX_PTHREAD_COUNT, 4);
static bool is_posix_prio_valid(u32_t priority, int policy)
{
if ((policy == SCHED_RR &&
priority <= CONFIG_NUM_PREEMPT_PRIORITIES) ||
(policy == SCHED_FIFO &&
priority < CONFIG_NUM_COOP_PRIORITIES)) {
if (priority >= sched_get_priority_min(policy) &&
priority <= sched_get_priority_max(policy)) {
return true;
}

View file

@ -0,0 +1,77 @@
/*
* Copyright (c) 2018 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <kernel.h>
#include <posix_sched.h>
static bool valid_posix_policy(int policy)
{
if (policy != SCHED_FIFO && policy != SCHED_RR) {
return false;
}
return true;
}
/**
* @brief Get minimum priority value for a given policy
*
* See IEEE 1003.1
*/
int sched_get_priority_min(int policy)
{
if (valid_posix_policy(policy) == false) {
errno = EINVAL;
return -1;
}
if (IS_ENABLED(CONFIG_COOP_ENABLED)) {
if (policy == SCHED_FIFO) {
return 0;
}
}
if (IS_ENABLED(CONFIG_PREEMPT_ENABLED)) {
if (policy == SCHED_RR) {
return 0;
}
}
errno = EINVAL;
return -1;
}
/**
* @brief Get maximum priority value for a given policy
*
* See IEEE 1003.1
*/
int sched_get_priority_max(int policy)
{
if (valid_posix_policy(policy) == false) {
errno = EINVAL;
return -1;
}
if (IS_ENABLED(CONFIG_COOP_ENABLED)) {
if (policy == SCHED_FIFO) {
/* Posix COOP priority starts from 0
* whereas zephyr starts from -1
*/
return (CONFIG_NUM_COOP_PRIORITIES - 1);
}
}
if (IS_ENABLED(CONFIG_PREEMPT_ENABLED)) {
if (policy == SCHED_RR) {
return CONFIG_NUM_PREEMPT_PRIORITIES;
}
}
errno = EINVAL;
return -1;
}