Skip to content

posix: sched: Implement sched_getparam() and sched_getscheduler() #67028

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

Merged
merged 2 commits into from
Jan 15, 2024
Merged
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
2 changes: 2 additions & 0 deletions arch/posix/include/posix_cheats.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ extern "C" int _posix_zephyr_main(void);
#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__)
#define sched_getparam(...) zap_sched_getparam(__VA_ARGS__)
#define sched_getscheduler(...) zap_sched_getscheduler(__VA_ARGS__)
Comment on lines +154 to +155
Copy link
Member

Choose a reason for hiding this comment

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

I don't know if this is necessary as part of this PR. I would maybe suggest making it in a separate PR.

Copy link
Member Author

Choose a reason for hiding this comment

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

i thought to do it same way how other APIs were introduced back then (6 years ago!) keeping related changes together in the same PR

@aescolar, what do you think ?

Copy link
Member

Choose a reason for hiding this comment

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

It is not necessary but quite harmless.
Background: This POSIX API renames were used at the time when building the POSIX API together with the host libC was allowed (the POSIX API function names were renamed with this trick to avoid them colliding with the host libC). That combination is not supported or permitted anymore, so there is no need to add more (or keep the majority of these).

Copy link
Member Author

Choose a reason for hiding this comment

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

This POSIX API renames were used at the time when building the POSIX API together with the host libC was allowed (the POSIX API function names were renamed with this trick to avoid them colliding with the host libC). That combination is not supported or permitted anymore, so there is no need to add more (or keep the majority of these).

is a bulk cleanup already planned for posix_cheats.h ? or should it be done gradually for API groups like sched_* ?
in scope of this PR it looks confusing to have only some of these APIs there.

Copy link
Member

Choose a reason for hiding this comment

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

It is in my low priority TODO. So it should happen eventually, probably when I need to change something around there.


/* Sleep */
#define sleep(...) zap_sleep(__VA_ARGS__)
Expand Down
2 changes: 1 addition & 1 deletion doc/services/portability/posix/aep/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Minimal Realtime System Profile (PSE51)
_POSIX_THREAD_CPUTIME, -1,
_POSIX_THREAD_PRIO_INHERIT, 200809L, :kconfig:option:`CONFIG_PTHREAD_MUTEX`
_POSIX_THREAD_PRIO_PROTECT, -1,
_POSIX_THREAD_PRIORITY_SCHEDULING, -1,
_POSIX_THREAD_PRIORITY_SCHEDULING, -1, :kconfig:option:`CONFIG_POSIX_PRIORITY_SCHEDULING` (will fail with ``ENOSYS``:ref:`†<posix_undefined_behaviour>`)
_POSIX_THREAD_SPORADIC_SERVER, -1,
_POSIX_TIMEOUTS, 200809L, :kconfig:option:`CONFIG_PTHREAD_IPC`
_POSIX_TIMERS, 200809L, :kconfig:option:`CONFIG_POSIX_CLOCK`
Expand Down
2 changes: 1 addition & 1 deletion doc/services/portability/posix/conformance/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ POSIX System Interfaces
:ref:`_POSIX_MESSAGE_PASSING<posix_option_message_passing>`, 200809L, :kconfig:option:`CONFIG_POSIX_MQUEUE`
_POSIX_MONOTONIC_CLOCK, 200809L, :kconfig:option:`CONFIG_POSIX_CLOCK`
_POSIX_PRIORITIZED_IO, -1,
:ref:`_POSIX_PRIORITY_SCHEDULING<posix_option_priority_scheduling>`, -1, :kconfig:option:`CONFIG_PTHREAD`
:ref:`_POSIX_PRIORITY_SCHEDULING<posix_option_priority_scheduling>`, -1, :kconfig:option:`CONFIG_POSIX_PRIORITY_SCHEDULING` (will fail with ``ENOSYS``:ref:`†<posix_undefined_behaviour>`)
_POSIX_RAW_SOCKETS, 200809L, :kconfig:option:`CONFIG_NET_SOCKETS`
_POSIX_SHARED_MEMORY_OBJECTS, -1,
_POSIX_SPAWN, -1,
Expand Down
7 changes: 6 additions & 1 deletion include/zephyr/posix/sched.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018 Intel Corporation
* Copyright (c) 2018-2023 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand All @@ -8,6 +8,8 @@

#include <zephyr/kernel.h>

#include "posix_types.h"

#ifdef __cplusplus
extern "C" {
#endif
Expand Down Expand Up @@ -46,6 +48,9 @@ static inline int sched_yield(void)
int sched_get_priority_min(int policy);
int sched_get_priority_max(int policy);

int sched_getparam(pid_t pid, struct sched_param *param);
int sched_getscheduler(pid_t pid);

#ifdef __cplusplus
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion lib/posix/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ zephyr_library_sources_ifdef(CONFIG_PTHREAD_KEY key.c)
zephyr_library_sources_ifdef(CONFIG_PTHREAD_MUTEX mutex.c)
zephyr_library_sources_ifdef(CONFIG_PTHREAD pthread.c)
zephyr_library_sources_ifdef(CONFIG_PTHREAD_IPC rwlock.c)
zephyr_library_sources_ifdef(CONFIG_PTHREAD_IPC sched.c)
zephyr_library_sources_ifdef(CONFIG_POSIX_PRIORITY_SCHEDULING sched.c)
zephyr_library_sources_ifdef(CONFIG_PTHREAD_IPC semaphore.c)
zephyr_library_sources_ifdef(CONFIG_PTHREAD_SPINLOCK spinlock.c)

Expand Down
1 change: 1 addition & 0 deletions lib/posix/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ source "lib/posix/Kconfig.limits"
source "lib/posix/Kconfig.mqueue"
source "lib/posix/Kconfig.mutex"
source "lib/posix/Kconfig.pthread"
source "lib/posix/Kconfig.sched"
source "lib/posix/Kconfig.semaphore"
source "lib/posix/Kconfig.signal"
source "lib/posix/Kconfig.spinlock"
Expand Down
11 changes: 11 additions & 0 deletions lib/posix/Kconfig.sched
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Copyright (c) 2023 Intel Corporation
#
# SPDX-License-Identifier: Apache-2.0

config POSIX_PRIORITY_SCHEDULING
bool "_POSIX_PRIORITY_SCHEDULING API support"
default y if PTHREAD
default y if POSIX_API
depends on PTHREAD
help
This enables POSIX scheduling APIs (_POSIX_PRIORITY_SCHEDULING).
31 changes: 30 additions & 1 deletion lib/posix/sched.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018 Intel Corporation
* Copyright (c) 2018-2023 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -41,3 +41,32 @@ int sched_get_priority_max(int policy)
errno = EINVAL;
return -1;
}

/**
* @brief Get scheduling parameters
*
* See IEEE 1003.1
*/
int sched_getparam(pid_t pid, struct sched_param *param)
{
ARG_UNUSED(pid);
ARG_UNUSED(param);

errno = ENOSYS;

return -1;
}

/**
* @brief Get scheduling policy
*
* See IEEE 1003.1
*/
int sched_getscheduler(pid_t pid)
{
ARG_UNUSED(pid);

errno = ENOSYS;

return -1;
}
1 change: 1 addition & 0 deletions tests/posix/common/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CONFIG_MAX_PTHREAD_COUNT=10
CONFIG_ZTEST=y
CONFIG_SEM_VALUE_MAX=32767
CONFIG_POSIX_MQUEUE=y
CONFIG_POSIX_PRIORITY_SCHEDULING=y
CONFIG_HEAP_MEM_POOL_SIZE=4096
CONFIG_MAX_THREAD_BYTES=4
CONFIG_THREAD_NAME=y
Expand Down
19 changes: 18 additions & 1 deletion tests/posix/common/src/pthread.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018 Intel Corporation
* Copyright (c) 2018-2023 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -644,6 +644,23 @@ ZTEST(posix_apis, test_pthread_descriptor_leak)
}
}

ZTEST(posix_apis, test_sched_getparam)
{
struct sched_param param;
int rc = sched_getparam(0, &param);
int err = errno;

zassert_true((rc == -1 && err == ENOSYS));
}

ZTEST(posix_apis, test_sched_getscheduler)
{
int rc = sched_getscheduler(0);
int err = errno;

zassert_true((rc == -1 && err == ENOSYS));
}

ZTEST(posix_apis, test_sched_policy)
{
/*
Expand Down
4 changes: 2 additions & 2 deletions tests/posix/headers/src/sched_h.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ ZTEST(posix_headers, test_sched_h)
zassert_not_null(sched_get_priority_max);
zassert_not_null(sched_get_priority_min);

/* zassert_not_null(sched_getparam); */ /* not implemented */
/* zassert_not_null(sched_getscheduler); */ /* not implemented */
zassert_not_null(sched_getparam);
zassert_not_null(sched_getscheduler);

/* zassert_not_null(sched_rr_get_interval); */ /* not implemented */

Expand Down