Skip to content

kernel: posix: implement schedule related APIs for POSIX. #5510

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/posix/pthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
15 changes: 15 additions & 0 deletions include/posix/pthread_sched.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These definitions are in a file named <sched.h>; shouldn't them be in a file with the same name so that code that assumes this will be able to find them without change?

* Copyright (c) 2017 Intel Corporation
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that it's 2018, these copyright headers need updating when (significant) changes are made, right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've personally never received any guidance that this is necessary.

*
* SPDX-License-Identifier: Apache-2.0
*/

#include <stdint.h>
Copy link
Collaborator

@lpereira lpereira Jan 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no need to include stdint.h here: this header file is not using any definition from this header. Add the #include directive to the implementation file, if required there. Also, please add an include guard.


#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);

1 change: 1 addition & 0 deletions kernel/posix/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
79 changes: 79 additions & 0 deletions kernel/posix/pthread_sched.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright (c) 2017 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <kernel.h>
#include <pthread.h>

/*
* ======== sched_get_priority_min ========
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide the documentation in the header file, using the Doxygen format.

* 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)
Copy link
Collaborator

@lpereira lpereira Jan 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function can be simplified by using the IS_ENABLED() macro. Not complete, of course, just something to consider:

int sched_get_priority_min(int policy)
{
    if (IS_ENABLED(CONFIG_COOP_ENABLED)) {
       if (priority == SCHED_COOP) {
            return K_LOWEST_APPLICATION_THREAD_PRIO;
       }
    }
    if (IS_ENABLED(CONFIG_PREEMPT_ENABLED)) {
        if (priority == SCHED_PREEMPT) {
            return K_LOWEST_APPLICATION_THREAD_PRIO;
        }
    }

    errno = EINVAL;
    return -1;
}

{

int priority = -EINVAL;

if (!((policy == CONFIG_COOP_ENABLED) || (policy == SCHED_PREMPT))) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CONFIG_COOP_ENABLED sounds like a Zephyr thing, rather than a POSIX thing. Is it correct here?

return -EINVAL;
}
#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
priority = (policy == SCHED_COOP) ? (-1 * -1) :
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why -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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These functions will return -1 on error, and the priority on success. It's returning a negative errno on error.

}

/*
* ======== 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;
}