Commit graph

396 commits

Author SHA1 Message Date
Christopher Friedt 57057b141c posix: add a top-level menu
Add a top-level menu to prevent POSIX API options from
cluttering the menuconfig view under
"Additional libraries".

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-01-25 05:54:06 -05:00
Christopher Friedt 3ff7c04f30 posix: timer: use async pthread cancellation
Previously, Zephyr's POSIX API did not differentiate between
deferred and asynchronous pthread cancellation. In fact all
pthread cancellation was asynchronous. According to the spec,
all pthreads should be created with deferred cancellation by
default.

Note: PTHREAD_CANCEL_ASYNCHRONOUS means cancel asynchronously
with respect to cancellation points (but synchronously with
respect to the thread that callse pthread_cancel(), which is
perhaps unintuitive).

The POSIX timer relied on this non-standard convention.

Oddly, this change prevents what would have otherwise been a
regression that would have been caused by fixing pthread
behaviour (in a separate commit).

We are effectively uncovering bugs which were probably always
present in the pthread.c and timer.c implementations going
back quite a few years.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-01-22 06:48:55 -05:00
Christopher Friedt 689dc4a45b posix: timer: support other clocks
There is no requirement that says e.g. CLOCK_REALTIME cannot be
used for timer_create(). In fact, the spec explicitly requires
it. It might not be ideal, but users should still be able to
use it.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-01-22 06:48:55 -05:00
Christopher Friedt b0a83a9177 posix: rwlock: make pthread_rwlock_t a pooled ipc type
Like mutex, barrier, cond, spinlock, etc, make
pthread_rwlock_t a pooled ipc type.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-01-18 09:22:35 -05:00
Yong Cong Sin 7c42c01f10 posix: pthread: use is_posix_policy_prio_valid to check POSIX priority
Use the existing `is_posix_policy_prio_valid()` function to
verify the POSIX's priority in the conversion functions.

Changed the `priority` arg of `is_posix_policy_prio_valid` to
`int` since that is the output of `sched_get_priority_min` &
`sched_get_priority_max`.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2024-01-18 10:54:47 +01:00
Yong Cong Sin c3cc2e4e6d posix: pthread: test the priority conversion functions
Made the conversion functions non-static and added ztests for
them to make sure that they work across the full range of
Zephyr priorities.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2024-01-18 10:54:47 +01:00
Yong Cong Sin 5ad7f4b314 posix: pthread: priority should be of int type
Changed the variable type of the priority in the args and the
return type of the conversion functions to `int`, as both
Zephyr's priority & POSIX's `sched_priority` has type `int`.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2024-01-18 10:54:47 +01:00
Yong Cong Sin b2caec8601 posix: pthread: fix typos
- 'piority' should be 'priority'
- COOP should start from `-CONFIG_NUM_COOP_PRIORITIES`

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2024-01-18 10:54:47 +01:00
Yong Cong Sin 66fdd39ec5 posix: pthread: fix zephyr_to_posix_priority assert test
If `z_prio` is negative and we want to make sure that it is
within `[-CONFIG_NUM_COOP_PRIORITIES, -1]`, we should invert
its sign and make sure that it is `<=`
`CONFIG_NUM_COOP_PRIORITIES` instead of `<`.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2024-01-18 10:54:47 +01:00
Yong Cong Sin 45c554d082 posix: shell: introduce top level posix command
Added a top level `posix` shell command for other POSIX
commands. Currently only `uname` is supported.

New POSIX commands can be added by including the
`posix_shell.h` header and use the `POSIX_CMD_ADD` helper
macro.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2024-01-18 10:54:32 +01:00
Yong Cong Sin 635dabf07a posix: uname: shell: update help message with more information
Populate the help message with more information when an error
occurs.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2024-01-18 10:54:32 +01:00
Yong Cong Sin 48c16f9052 posix: uname: move uname shell from sample
Relocate the `uname` shell implementation from uname sample, so
that it can be reused by other application and the uname sample
only uses POSIX APIs.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2024-01-18 10:54:32 +01:00
Yong Cong Sin 98a631b5bc posix: pthread: allow oldstate & oldtype to be NULL
The POSIX standard doesn't specify if the argument to store
previous state/type in
`pthread_setcancelstate`/`pthread_setcancelstate` can be
`NULL`, but threading implementations in Linux & Apache NuttX
permit the arguments to be `NULL`.

This commit changes Zephyr's implementation to mimic that of
Linux & NuttX, so that user do not get caught off-guard by
NULL pointer dereferencing when porting code over from those
OSes.

Updated test accordingly.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2024-01-17 14:42:50 +01:00
Yong Cong Sin 73e20a8967 posix: clock: add ztest rule to reset clock base
Make sure that the POSIX clock base is reset after every
testsuite so that the initial time is the same for every test.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2024-01-17 05:41:37 -05:00
Christopher Friedt a6dab1a85e posix: pthread: additional null checks on parameters
Instead of relying on a fault occurring when NULL
parameters are passed in, explicitly check and return
EINVAL.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-01-16 00:35:45 -05:00
Dmitrii Golovanov 68d1a52417 posix: sched: Implement get APIs for scheduling parameters
Initial implementation of `sched_getparam()` and `sched_getscheduler()`
POSIX APIs as a part of PSE53 `_POSIX_PRIORITY_SCHEDULING` option group.
Both functions are actually placeholders and just return `ENOSYS`
since Zephyr does not yet support processes or process scheduling.

Signed-off-by: Dmitrii Golovanov <dmitrii.golovanov@intel.com>
2024-01-15 09:57:44 +01:00
Dmitrii Golovanov b184fc3a62 posix: sched: Add CONFIG_POSIX_PRIORITY_SCHEDULING
Add `CONFIG_POSIX_PRIORITY_SCHEDULING` Kconfig option to select
APIs from PSE53 `_POSIX_PRIORITY_SCHEDULING` option group.

Signed-off-by: Dmitrii Golovanov <dmitrii.golovanov@intel.com>
2024-01-15 09:57:44 +01:00
Yong Cong Sin 73da1e80f4 posix: signal: implement sigprocmask()
Implement `sigprocmask()` by simply redirecting call to the
`pthread_sigmask()` as they are identical.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2024-01-11 07:49:26 -05:00
Nicolas Pitre cf74f22524 posix: semaphore: optimize named semaphore implementation a bit
- Regroup refcount decrement and semaphore destruction by making the
  linked state into a counted reference for it. This allows for
  simplifying the code and cleanly adding a few assertions in a common
  location.

- Remove redundant initialization to NULL on memory about to be freed
  and local pointer in nsem_cleanup().

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2024-01-10 07:18:19 -05:00
Yong Cong Sin abb21d48d9 posix: semaphore: implement sem_open(), sem_unlink() & sem_close()
Implements `sem_open()`, `sem_unlink()` & `sem_close()`
functions and added tests for them.

Updated existing tests and POSIX docs.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2024-01-10 07:18:19 -05:00
Jai Arora 5c34726ab1 posix: patch to implement get_pid function
patch to implement get_pid function

Signed-off-by: Jai Arora <infolinesoni@gmail.com>
2024-01-08 12:44:45 +01:00
Jai Arora a3573a9e30 posix: implement clock_getcpuclockid function
Implements clock_getcpuclockid function

Fixes #59954

Signed-off-by: Jai Arora <infolinesoni@gmail.com>
2024-01-08 12:44:45 +01:00
Alberto Escolar Piedras b802e42c6f posix kconfig: Improve depends on host libC
Improve a depends on the host libC.
It is technically correct, but NATIVE_LIBC
is shorter and clearer than "ARCH_POSIX && EXTERNAL_LIBC"

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2024-01-05 11:38:57 -06:00
Alberto Escolar Piedras c58710c602 lib/posix getopt: Fix include
Let's try to use the host unistd.h when building with
the host library only, instead of assuming
that the native boards are always built with it.
This fixes a build error when building for native boards
using minimal libc.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2024-01-05 11:38:57 -06:00
Christopher Friedt 95a22b1217 posix: clock: clock_gettime() should not be a syscall
We try to implement Zephyr's POSIX API as regular library
functions, so remove the __syscall annotation from
clock_gettime() and implement the syscall portion of it under
the hood.

This also adds a bit of a micro-optimization in that
we can do a lot of processing outside of the system call.

In fact, processing CLOCK_MONOTONIC likely does not
require any syscall other than k_uptime_ticks().

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-01-04 08:27:29 +09:00
Christopher Friedt e0383a6f9d posix: clock: check for invalid ns in clock_settime()
The clock_settime() function should not accept an input timespec
with nanosecond values < 0 or >= NSEC_PER_SEC.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2024-01-04 08:27:29 +09:00
Martin Åberg fdacddda08 posix: timer: Fix timer_obj alignment
This makes sure the heap posix_timer_slab provides objects aligned
compatible with the type timer_obj. It was previously set to align at 4
bytes. One example where this failed was on the SPARC which requires
access to int64_t to be 8-byte aligned.

In particular, struct timer_obj contains fields of type k_timer_t and
struct _timeout.

With this commit we now get the information on required alignment for
struct timer_obj from the compiler by using __alignof__().

Signed-off-by: Martin Åberg <martin.aberg@gaisler.com>
2024-01-02 10:09:00 +01:00
Christopher Friedt 10156f5f1d posix: mqueue: pop mode as int with va_arg()
There was some discussion about whether it was suitable to have
an architecture-specific workaround in mqueue.c after that
workaround was copied to a different source file in a PR.

The original issue was that newlib and picolibc declare mode_t
to be unsigned short instead of unsigned long when __svr4__
is not defined along with __sparc__. This is specifically
impactful, because va_arg() deals (mainly) with 32-bit and
64-bit values that are all naturally aligned to 4 bytes.

#if defined(__sparc__) && !defined(__sparc_v9__)
#ifdef __svr4__
typedef unsigned long __mode_t;
#else
typedef unsigned short __mode_t;
#endif

A uint16_t is naturally aligned to 2 bytes, so not only would
a 16-bit mode_t be corrupted, it would also generate a warning
with recent gcc versions which is promoted to error (rightfully
so) when run through CI.

mqueue.c:61:35: error: 'mode_t' {aka 'short unsigned int'} is
  promoted to 'int' when passed through '...' [-Werror]
   61 |                 mode = va_arg(va, mode_t);

Instead of using an architecture-specific workaround, simply
add a build assert that the size of mode_t is less than or
equal to the size of an int, and use an int to retrieve it
via va_arg().

Signed-off-by: Christopher Friedt <cfriedt@meta.com>

# Please enter the commit message for your changes. Lines starting
# with '#' will be kept; you may remove them yourself if you want to.
# An empty message aborts the commit.
#
# Date:      Fri Dec 29 10:06:44 2023 -0500
#
# On branch posix-mqueue-always-use-int-for-mode-t-va-arg
# Changes to be committed:
#	modified:   lib/posix/mqueue.c
#	modified:   tests/posix/common/testcase.yaml
#
2024-01-01 16:36:31 -05:00
Fabio Baltieri e67b12e636 lib: posix: tag the fallthrough case as __fallthrough
Use the __fallthrough directive on the switch fallthrough case and drop
the comment instead. This informs the compiler that the fallthrough is
intentional and silence any possible warning about it.

Drop the not reached case as I think that that part can actually be
reached since there are breaks in some of the cases.

Signed-off-by: Fabio Baltieri <fabiobaltieri@google.com>
2023-12-30 14:35:41 -05:00
Christopher Friedt ec69e64d4c posix: pthread_setspecific: fix for coverity issue cid 334906
* return EINVAL if pthread_self() is not a valid pthread

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-12-28 12:19:10 +00:00
Christopher Friedt 3458527a2c posix: pthread_getspecific: fix for coverity issue cid 334909
* remove unneeded line of code that duplicated the first part
  of the SYS_SLIST_FOR_EACH_NODE() expansion.
* return NULL if pthread_self() is not a valid pthread

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-12-28 12:19:10 +00:00
Christopher Friedt fcd139d7af posix: pthread: support stack sizes larger than 65k
A previous size optimization capped the pthread_attr_t stacksize
property at 65536. Some Zephyr users felt that was not large
enough for specific use cases.

Modify struct pthread_attr to support large stack sizes by
default with the flexibility to allow users to vary the number
of bits used for both stacksizes and guardsizes.

The default guardsize remains zero sinze Zephyr's stack
allocators already pad stacks with a guard area based on other
config parameters, and since Zephyr is already designed to
support both SW and HW stack protection at the kernel layer.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-12-23 22:29:41 -05:00
Christopher Friedt a7d2c5cb32 posix: implement pthread_getguardsize() pthread_setguardsize()
Implement pthread_getguardsize() and pthread_setguardsize().

pthread_getguardsize() and pthread_setguardsize() are required
by the POSIX_THREADS_EXT Option Group as detailed in Section
E.1 of IEEE-1003.1-2017. However, they were formerly part of
XSI_THREADS_EXT.

The XSI_THREADS_EXT Option Group was required for PSE51, PSE52,
PSE53, and PSE54 conformance.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-12-20 14:20:58 +00:00
Christopher Friedt eb7e4bed98 posix: pthread_attr: use space in struct more efficiently
This change reduces the space occupied by struct pthread_attr
which is the internal type used for pthread_attr_t.

We cap the stack size at 16 bits (so up to 65536 bytes) and
since a stack size of 0 is invalid, we can encode the stack
size by simply subtracting 1 or adding 1 when setting or
getting.

The schedpolicy is capped at 2 bits and initialized,
cancellable, and detached are given 1 bit.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-12-20 14:20:58 +00:00
Johan Hedberg 7bb16c779b posix: mqueue: Remove custom default for HEAP_MEM_POOL_SIZE
Use the new HEAP_MEM_POOL_ADD_SIZE_ prefix to construct a minimum
requirement for posix message queue usage. This way we can remove the
"special case" default values from the HEAP_MEM_POOL_SIZE Kconfig
definition.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
2023-12-20 11:01:42 +01:00
Christopher Friedt 04c9903055 posix: pthread_once: simplify and reduce size of pthread_once_t
Since pthread_once() is both the initializer and executor of
pthread_once_t, it can have maximally two states. Since the
implementation in Zephyr previously aimed to maximize libc
compatibility, we opted to use the definition of pthread_once_t
from newlib, which is a structure with two ints.

It does not make sense to use 64 bits to manage 2 possible
states. The control for that should effectively be a bool.

We maintain compatibility with newlib by asserting (at build
time), that newlib's pthread_once_t is larger than Zephyr's
new struct pthread_once (which just contains a bool).

This allows us to delete the non-standard pthread_key.h
header file (finally).

Reuse the pthread_pool_lock in order to synchronize the related
init function (so that it is only called maximally once from any
thread). The spinlock is only used to test the state and the
init function is not called with the spinlock held.

The alternative was to use an atomic inside of
struct pthread_once. But again, that could be up to 64-bits with
Zephyr's atomics implementation.

Ideally we would use C11 generics or something to support atomics
on 8, 16, 32, and 64-bit primitives.

Lastly, also update declarations for C11 threads as they mostly
mirror our pthread implementation.

This needed to be done as a single commit in order to ensure
continuity of build.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-12-14 09:32:58 +01:00
Christopher Friedt c2721805f0 posix: pthread: implement pthread_sigmask()
pthread_sigmask() is required by the POSIX_THREADS_BASE
Option Group as detailed in Section E.1 of IEEE-1003.1-2017.

The POSIX_THREADS_BASE Option Group is required for PSE51,
PSE52, PSE53, and PSE54 conformance, and is otherwise mandatory
for any POSIX conforming system as per Section A.2.1.3 of
IEEE-1003-1.2017.

Currently, setting a pthread signal mask has no effect.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-12-04 20:49:35 -05:00
Christopher Friedt 73930c25f2 posix: signal: define max rt signals as zero when not enabled
When CONFIG_POSIX_SIGNAL=n, default CONFIG_POSIX_RTSIG_MAX to
0 so that the `sigset_t` type can be defined.

Also define RTSIG_MAX.

This allows native_sim and other platforms to build without
error.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-12-04 20:49:35 -05:00
Christopher Friedt 742de21eef posix: pthread: add logging to pthread key
To align with other supported POSIX features, add logging to
pthread key to provide better error reporting and diagnostics.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-11-30 11:24:09 -05:00
Christopher Friedt 5dc561f1b4 posix: key: remove unnecessary else / indent
Remove the unnecessary else clause and indentation in
pthread_setspecific().

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-11-30 11:24:09 -05:00
Christopher Friedt fb695c42fb posix: pthread: implement pthread_cleanup_push() / pop()
pthread_cleanup_push() and pthread_cleanup_pop() are required
by the POSIX_THREADS_BASE Option Group as detailed in Section
E.1 of IEEE-1003.1-2017.

The POSIX_THREADS_BASE Option Group is required for PSE51,
PSE52, PSE53, and PSE54 conformance, and is otherwise mandatory
for any POSIX conforming system as per Section A.2.1.3 of
IEEE-1003-1.2017.

In this change, we require the addition of a dedicated
pthread_key_t that will not be available for applilcation usage.

Rather than including that as part of
CONFIG_MAX_PTHREAD_KEY_COUNT, we increase the storage by 1 in
order to be least invasive from the application perspective.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-11-29 10:08:44 +01:00
Christopher Friedt 5cd2e17424 posix: pthread: fix warning about uninitialized variable
Initialize the variable to zero. I don't think there is any way
out of this function without it being initialized, so IMHO it's
a false positive.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-11-27 19:57:42 -05:00
Christopher Friedt e49ae776cc posix: pthread: implement pthread_getconcurrency()
Zephyr must support all functionality of the XSI_THREADS_EXT
subprofiling option group in order to claim it supports that
subprofiling option group.

The XSI_THREADS_EXT option group is critical to be able to
run POSIX threads with statically allocated thread stacks, which
has been a feature of the implementation since it was initially
added.

The pthread_getconcurrency() and pthread_setconcurrency()
functions are the only remaining, unimplemented functions of
the XSI_THREADS_EXT option group.

Implement pthread_getconcurrency() and pthread_setconcurrency()
via the more "posixly correct" interpretation of the
specification.

I.e. as the pthread_t:k_thread relationship is 1:1 and not M:N,
Zephyr does not support multiplexing of user threads on top of
schedulable kernel entities (i.e. "user threads" are directly
mapped to native threads, just like linuxthreads or NPTL are in
Linux).

For that reason, to be "posixly correct", we should save the
provided value via pthread_setconcurrency(), in the absense of
errors, and also return that same value back via
pthread_getconcurrency(), even though that serves zero purpose
in Zephyr for the foreseeable future.

Note: the specification also states
"an implementation can always ignore any calls to
pthread_setconcurrency() and return a constant for
pthread_getconcurrency()."

For that reason, the implementation may be revisited at a later
time when when considering optimizations and when there is a
better system in place for documenting deviations.

Any such optimization should be explicitly controlled via
Kconfig.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-11-27 08:26:31 -05:00
Christopher Friedt 1e7eb7a6da posix: pthread: support for pthread_setcanceltype()
pthread_setcanceltype() is required by the POSIX_THREADS_BASE
Option Group as detailed in Section E.1 of IEEE-1003.1-2017.

The POSIX_THREADS_BASE Option Group is required for PSE51,
PSE52, PSE53, and PSE54 conformance, and is otherwise mandatory
for any POSIX conforming system as per Section A.2.1.3 of
IEEE-1003-1.2017.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-11-26 05:55:08 -05:00
Christopher Friedt 87635dd34a posix: do not define _PTHREAD_CANCEL_POS
_PTHREAD_CANCEL_POS is an implementation detail and should not
be defined in the global scope. Furthermore, _PTHREAD_CANCEL_POS
uses a reserved identifier (underscore followed by capital
letter).

Adjust definitions so that the implementation detail is only
used in the implementation and not in the interface.

Additionally, modify naming so that the non-standard macro does
not use a reserved identifier.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-11-25 06:29:16 -05:00
Christopher Friedt 19ef279dc3 posix: define PTHREAD_CANCELED globally
Move the definition of PTHREAD_CANCELED from pthread.c to
pthread.h.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-11-25 06:29:16 -05:00
Christopher Friedt ad5441bbdd posix: pthread: move pthread_equal() closer to pthread_self()
* "identity" functions grouped more closely
* posix_thread_pool_init() should be adjacent to the SYS_INIT()

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-11-25 06:29:16 -05:00
Christopher Friedt a89fa32dac posix: pthread: ensure pthread_key_delete() removes correct key
Previously, `pthread_key_delete()` was only ever deleting key 0
rather than the key corresponding to the provided argument.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-11-24 10:47:28 -05:00
Christopher Friedt a0c307c0a5 posix: pthread: implement pthread_atfork()
pthread_atfork() is required by the POSIX_THREADS_BASE
Option Group as detailed in Section E.1 of IEEE-1003.1-2017.

The POSIX_THREADS_BASE Option Group is required for PSE51,
PSE52, PSE53, and PSE54 conformance, and is otherwise
mandatory for any POSIX conforming system as per Section
A.2.1.3 of IEEE-1003-1.2017.

Since Zephyr does not yet support processes and (by extension)
fork(), this implementation includes a deviation and should be
categorized as producing undefined behaviour.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-11-23 00:59:32 -05:00
Anas Nashif a08bfeb49c syscall: rename Z_OOPS -> K_OOPS
Rename internal API to not use z_/Z_.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2023-11-03 11:46:52 +01:00
Anas Nashif 9c4d881183 syscall: rename Z_SYSCALL_ to K_SYSCALL_
Rename internal API to not use z_/Z_.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2023-11-03 11:46:52 +01:00
Anas Nashif 4e396174ce kernel: move syscall_handler.h to internal include directory
Move the syscall_handler.h header, used internally only to a dedicated
internal folder that should not be used outside of Zephyr.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2023-11-03 11:46:52 +01:00
Jukka Rissanen 83c875adab hostap: Move the relevant config options away from hostap
Moving the Zephyr specific config options from
modules/hostap/Kconfig to corresponding Kconfig where the
option is specified.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2023-10-26 09:48:47 +02:00
Alberto Escolar Piedras f8c202ec1c lib/posix clock: Add missing kconfig dependency
POSIX clock cannot be used with the host libC
when building with the POSIX architecture.
Let's ensure it via kconfig.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2023-10-25 09:58:07 +02:00
Flavio Ceolin 564adad952 treewide: Add CODE_UNREACHABLE after k_thread_abort(current)
Compiler can't tell that k_thread_abort() won't return and issues a
warning unless we tell it that control never gets this far.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2023-10-24 09:04:42 +02:00
Arunmani Alagarsamy 53f1af9bd7 timer: add support for sigev_thread flag in zephyr timer posix api
add support for the sigev_thread flag in zephyr timer posix api following
the behaviour described in the linux man page. with this enhancement,
a single thread is created to receive all notifications from timers

Signed-off-by: Arunmani Alagarsamy <arunmani@linumiz.com>
2023-10-20 14:52:07 +02:00
Christopher Friedt 7e8953ed7f posix: ensure that pooled ipc type is user-configurable
Previously it was not possible to n-select e.g.
CONFIG_PTHREAD_BARRIER because the Kconfig template for it
lacked a prompt. This made it impossible to disable some
unused POSIX features and unnecessarily increased code size
if unused code sections were not discarded by the linker.

Add a prompt so that each pooled IPC type is
user-configurable and can be disabled if unneeded.

Further, ensure that only the selected sources from lib/posix
are included in the cmake build.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-10-13 11:13:07 -04:00
Jaxson Han d489ec087e lib: posix: mutex: to_posix_mutex should be spinlocked
to_posix_mutex allocates a mutex which will race to change the value of
the lock, thus to_posix_mutex should be performed under the spinlock to
prevent the racy issue.

Signed-off-by: Jaxson Han <jaxson.han@arm.com>
2023-10-03 22:30:38 -04:00
Andrei Emeltchenko 06e8e9f5f5 posix: pthread: Reuse variable for return code
No need to have different variables for different function's return
codes.

Signed-off-by: Andrei Emeltchenko <andrei.emeltchenko@intel.com>
2023-10-03 15:23:43 +01:00
Andrei Emeltchenko 0ee0380643 posix: pthread: Move logging out of spinlock area
Move LOG_ERR() outside of K_SPINLOCK().

Signed-off-by: Andrei Emeltchenko <andrei.emeltchenko@intel.com>
2023-10-03 15:23:43 +01:00
Andrei Emeltchenko 3d82fbe357 posix: pthread: Remove dead code warning
It is better to put LOG_ERR() before K_SPINLOCK_BREAK().

Signed-off-by: Andrei Emeltchenko <andrei.emeltchenko@intel.com>
2023-10-03 15:23:43 +01:00
Anas Nashif 6d23a960db lib: os: build fdtable conditionally
Stop building fdtable by default, make it conditional and build it only
when needed.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2023-09-28 06:25:16 -04:00
Christopher Friedt b142e9dd5e posix: logging: use log module register instead of declare
Log modules should be registered with LOG_MODULE_REGISTER
rather than LOG_MODULE_DECLARE. It seems the latter works
on most platforms (at least with in minimal mode as configured
with ZTest).

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-09-19 16:11:58 +01:00
Christopher Friedt d9841ca9fb posix: cond: add logging for condition variables
Add logging for POSIX condition variable operations.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-09-19 06:16:10 -04:00
Christopher Friedt 0958c46e23 posix: mutex: add logging for POSIX mutexes
Add logging for POSIX mutex operations.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-09-19 06:16:10 -04:00
Christopher Friedt d2845abcb3 posix: pthread: add logging for POSIX threads
Add logging to POSIX threads.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-09-19 06:16:10 -04:00
Christopher Friedt 9cff600e25 posix: add logging to pooled resource templates
Support logging for all POSIX pooled resource types such as
pthread_t, pthread_mutex_t, and pthread_cond_t.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-09-19 06:16:10 -04:00
Christopher Friedt 1731010869 posix: pthread: do not assert in pthread_exit() on k_thread
If `pthread_exit()` is called from a `k_thread`, then we would
previously trigger an assertion. The problem with that, is that
is POSIX is acting as a compatibility layer.

Given that it is a reasonable expectation to have the calling
thread exit or abort when calling `pthread_exit()`, lets do just
that.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-09-16 18:56:33 -04:00
Anas Nashif 1fd3171f7b arch: remove wait_q.h include
This include is not used directly in those files, so remove it.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2023-09-12 12:55:36 -04:00
Carles Cufi 8c748fd005 kernel: Modify the signature of k_mem_slab_free()
Modify the signature of the k_mem_slab_free() function with a new one,
replacing the old void **mem with void *mem as a parameter.

The following function:
void k_mem_slab_free(struct k_mem_slab *slab, void **mem);

has the wrong signature. mem is only used as a regular pointer, so there
is no need to use a double-pointer. The correct signature should be:
void k_mem_slab_free(struct k_mem_slab *slab, void *mem);

The issue with the current signature, although functional, is that it is
extremely confusing. I myself, a veteran Zephyr developer, was confused
by this parameter when looking at it recently.

All in-tree uses of the function have been adapted.

Fixes #61888.

Signed-off-by: Carles Cufi <carles.cufi@nordicsemi.no>
2023-09-03 18:20:59 -04:00
Tom Finet d09a1c39f9 posix: implement clock_nanosleep
Implements the posix clock_nanosleep function, where both relative
and absolute sleeps are made as absolute sleeps.
The nanosleep() function is a special case of clock_nanosleep(),
and so has been refactored to simply call it.

Signed-off-by: Tom Finet <tom.codeninja@gmail.com>
2023-08-30 13:02:58 -07:00
Mateusz Marszalek 61219dacc6 POSIX: Implement pthread_condattr functions
Added:
pthread_condattr_init
pthread_condattr_destroy
pthread_condattr_getclock
pthread_condattr_setclock

Signed-off-by: Mateusz Marszalek <matti.marszalek@gmail.com>
2023-08-30 13:01:03 -07:00
Christopher Friedt 9f1e4fa86c posix: signal: type corrections for sigval, sigevent, notify attr
* `struct sigevent` is not type-defined
* `union sigval` is not type-defined
* `struct sigevent` must include `sigev_notify_attributes`

For more information, see https://bit.ly/3YfnELI

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-08-23 10:06:00 +02:00
Yong Cong Sin 43123e77c0 posix: signal: extend strsignal buf to cover entire INT range
extends the char buffer in the strsignal function to cover the
entire range of `int`

Had to use `-INT_MAX` as the compiler resolves

```
STRINGIFY(INT_MIN)
```

to:

```
(-2147483647 - 1)
```

instead of:

```
-2147483648
```

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2023-08-04 15:54:18 -04:00
Christopher Friedt f9ecc9a658 posix: mutex: return ETIMEDOUT from pthread_mutex_timedlock()
The normative spec for `pthread_mutex_timedlock()` says that
it should return `ETIMEDOUT` when a timeout occurs. However,
currently it returns `EAGAIN`, which reflects what is returned
by `k_mutex_lock()`.

Inspect and update the return value to account for this slight
difference.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-08-02 12:04:34 -04:00
Christopher Friedt 73bf557a95 posix: pthread: thread return value not set by pthread_join()
Ensure that the thread return value is set by `pthread_join()`
when `status` is non-NULL.

Additionally, we have an opportunity to synchronously clean
up thread stacks in `pthread_join()`, which is preferable.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-07-30 19:17:49 -04:00
Christopher Friedt 52f00882d1 posix: pthread: report appropriate return value instead of 0
Discovered this while implementing c11 threads, but there
was a regression recently that made it so that `pthread_join()`
would report success when attempting to join a thread that had
been detached with `pthread_detach()`.

Technically now that is undefined behaviour, but historically,
we have reported `EINVAL`, which was the older specified
return value.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-07-30 12:51:12 -04:00
Christopher Friedt 115efa2e35 pthread: facilitate dynamically allocated thread stacks
This change allows users to call pthread_create() with
the pthread_attr_t argument equal to NULL.

If Zephyr is configured with `CONFIG_DYNAMIC_THREAD`, then a
suitable thread stack will be allocated via
k_thread_stack_alloc(). The allocated thread stack is
automatically freed via k_thread_stack_free().

This makes the Zephyr implementation of pthread_create()
compliant with the normative spec.

Signed-off-by: Christopher Friedt <chrisfriedt@gmail.com>
2023-07-24 12:59:43 -04:00
Christopher Friedt 748683fae6 posix: eventfd: remove redundant conditional
Since the argument is a 32-bit unsigned int, all possible
values satisfy the condition that intval < UINT64_MAX - 1.

Remove the redundant conditional.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-07-21 06:33:19 -04:00
Jai Arora 82f5db37d2 posix: Changes pthread_equal to regular type function
Patch changes pthread_equal function from static inline to regular
function type

Fix for issue#59942

Signed-off-by: Jai Arora <infolinesoni@gmail.com>
2023-07-20 16:24:16 +02:00
Yong Cong Sin 43c549305b posix: signal: check RTSIG limit
Use build assert to make sure that the realtime signal
constants are configured properly in the Kconfig.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2023-07-18 15:35:42 -04:00
Yong Cong Sin 335acf817d posix: signal: implement strsignal
Implementation and ztest for strsignal.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2023-07-18 15:35:42 -04:00
Yong Cong Sin ff4b81e5e5 posix: signal: implement sigismember
Implementation and ztest for sigismember.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2023-07-18 15:35:42 -04:00
Yong Cong Sin 88cd494430 posix: signal: implement sigdelset
Implementation and ztest for sigdelset.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2023-07-18 15:35:42 -04:00
Yong Cong Sin 8011449542 posix: signal: implement sigaddset
Implementation and ztest for sigaddset.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2023-07-18 15:35:42 -04:00
Yong Cong Sin b9720e27c0 posix: signal: implement sigfillset
Implementation and ztest for sigfillset.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2023-07-18 15:35:42 -04:00
Yong Cong Sin 6910eb4a9b posix: signal: implement sigemptyset
Implementation and ztest for sigemptyset.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2023-07-18 15:35:42 -04:00
Yong Cong Sin e3c49b9573 posix: signal: add initial header
Initial header for the signal APIs.
APIs to be implemented in later commit.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2023-07-18 15:35:42 -04:00
Jaxson Han 0df7bd26ed lib: posix: pthread_rwlock: Fix a racy issue
Multiple reader threads unlocking the read lock simultaneously might
cause the program hang because it's possible that no thread is
identified as the last one to active the writer thread.

To fix the issue, swap the k_sem_give sequence.

Signed-off-by: Jaxson Han <jaxson.han@arm.com>
2023-07-12 09:25:26 +02:00
Harshil Bhatt 8b2a41904d posix: implement timer_getoverrun()
Address missing POSIX function and tests #59956

Signed-off-by: Harshil Bhatt <harshilbhatt2001@gmail.com>
2023-07-10 09:30:33 +02:00
Yong Cong Sin 4580708f54 posix: implement uname
Add implementation for posix uname.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
2023-07-09 16:57:16 -04:00
Harshil Bhatt 78c8176c4d posix: Implement pthread_barrieratter functions
Added pthread_barrieratter_init() #59936,
pthread_barrieratter_destroy() #59935,
pthread_barrieratter_getpshared() #59937 and
pthread_barrieratter_setpshared() #59939.

Signed-off-by: Harshil Bhatt <harshilbhatt2001@gmail.com>
2023-07-09 10:40:57 -04:00
Christopher Friedt 694cd58647 posix: pthread: fixes for coverity 321140 and 321092
The `pthread_once_lock` `k_mutex` is statically initialized and
only visible within file scope. Coverity identified it as unsafe
because the return values of `pthread_mutex_lock()` and
`pthread_mutex_unlock()` were unchecked. However, if those
functions were to fail here, it would be indicative that
something far worse has happened.

In any case, we add assertions that these functions
succeed rather than silently ignoring with `(void)`, which
ensures that we have coverage when assertions are enabled,
in test, while removing unneeded code with assertions disable,
in production.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-07-09 08:20:32 -04:00
Christopher Friedt 1434296915 posix: spinlock: additional kconfig for k_spinlock being size 0
The `struct k_spinlock` size is zero bytes under certain
circumstances. This is a bit of a problem, because it breaks a
number of assumptions about things in C.

That should be fixed when #59922 is addressed.

This change is just a hotfix to correct the specific condition
where we will need workarounds in other source files.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-07-06 16:45:26 +00:00
Christopher Friedt 13dcf2e4ed posix: split supported features into separate Kconfig files
This change is setting up for switching over to proper POSIX
option requirements, feature test macros, and a dependency
structure that is reflective of the standard.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-07-06 11:47:07 -04:00
Christopher Friedt 4fcf197c50 posix: rename some files for brevity
Most of the posix source files can be easily identified by a
short name. I.e. most of the `pthread_` prefixed files do not
need the `pthread_` prefix.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-07-06 11:47:07 -04:00
Christopher Friedt e11856fbfe posix: add Meta copyright as appropriate
A significant enough portion of these files has been
changed to justify adding Meta copyright as well as
that of the original author.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-07-06 11:47:07 -04:00
Christopher Friedt 452205ff61 posix: pthread: implement pthread spinlocks
pthread_spinlock_t support is part of the base threading
requirement.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-07-05 21:06:55 -04:00
Vincent van Beveren 2ae521a5f5 posix: fs: Fixes stat command to return file information
Fixes #58911. Previously the stat command returned
information on the filesystem, but not the file itself.
Because block size is still set this function is
backwards compatible with the previous faulty
behavior.

Signed-off-by: Vincent van Beveren <v.van.beveren@nikhef.nl>
2023-06-23 17:04:34 -04:00
Alberto Escolar Piedras 3454d8f727 lib POSIX: Enable selectively for POSIX ARCH
The POSIX API compatibility shim can be used for some
of the POSIX ARCH targets.
Narrow the Kconfig filtering accordingly.

Note that the recommended configuration when building
with the native simulator is still to use an embedded
C library. Using the host C library will in some cases
cause undesired behaviour.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2023-06-22 19:46:43 -04:00
Marcin Niestroj 44d61bde84 posix: eventfd: fix waking up poll()
Fix a regression introduced by commit e6eb0a705b ("posix: eventfd: revise
locking, signaling, and allocation"), which was a complete rewrite stating
that:

  The `wait_q` and `k_poll_signal` entries were removed from
  `struct eventfd` as they were unnecessary.

In fact, `k_poll_signal` (both `read_sig` and `write_sig`) were used to
wake-up blocking `poll()` invocation in another thread. This is no longer
the case now, i.e. `poll(..., POLLIN)` does not return after calling
`eventfd_write()` on the observed (polled) FD.

Fix this regression by bringing back `read_sig` and `write_sig` to very
similar state as it was before.

Fixes: e6eb0a705b ("posix: eventfd: revise locking, signaling, and
  allocation")
Signed-off-by: Marcin Niestroj <m.niestroj@emb.dev>
2023-06-22 16:09:42 -04:00
Daniel Leung 1d4d718a9b lib: syscalls: use zephyr_syscall_header
This adds a few line use zephyr_syscall_header() to include
headers containing syscall function prototypes.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2023-06-17 07:57:45 -04:00
Christopher Friedt 89cf4cea56 posix: pthread: mitigate include order sensitivity
Previously, the `posix_internal.h` header needed to be exposed
to the application because we had non-trivial details for
most posix types (pthread, mutex, cond, ...). Since most of
those have been simplified to a typedef'ed integer, we
no longer need to expose that header to the applicaiton.

Additionally, it means that we can adopt normalized
header order in posix.

Additionally, keep more implementation details hidden
and prefer the static keyword on internal symbols where
possible.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-06-09 12:27:04 -04:00
Christopher Friedt e1f8ea1ad7 posix: pthread: add option for pthread_create() barrier
To enable testing, introduce `CONFIG_PTHREAD_CREATE_BARRIER`.

Some observations were made that running several Qemu SMP targets
concurrently could lead to synchronization problems. On such
targets, it was found that the synchronization issues were
mitigated by introducing a `pthread_barrier_t` shared between
`pthread_create()` and the spawned thread.

It is suggested to enable the option when running many
SMP tests concurrently in several parallel Qemu processes,
e.g. with `twister`.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-06-09 12:27:04 -04:00
Christopher Friedt 85e18746b8 posix: pthread: rework pthreads to use zephyr api
Previously, pthreads suffered from some race conditions.
This was almost inevitable given that it was maintained in
parallel to Zephyr's threading and synchronization API.

The unfortunate side-effect of with that is that it did not
receive the reliability and other improvements that
`k_thread`s did.

Here, we perform a significant update of pthread code so
that it depends directly on public Zephyr API. With that,
we reuse as many concepts as possible and pthreads benefits for
free from any improvement made to Zephyr's threading and
synchronization APIs.

Included with this change, we
* implement state with `ready_q`, `run_q`, and `done_q`
* use `pthread_barrier_wait()` to sync `pthread_create()`
* synchronize internal state with a spinlock

These pthreads are considerably more reliable than
before.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-06-09 12:27:04 -04:00
Christopher Friedt 08ba17683b posix: cond: use k_condvar instead of posix_cond
The internal representation of `pthread_cond_t`,
`struct posix_cond`, is an identical clone of `struct k_condvar`
but without the benefit of being able to use all of the
existing `k_condvar_*()` suite of functions.

The first step in the right direction was switching
the external representation of `pthread_cond_t` to a simple
`int`. Let's take the next step in the right direction, which
is getting rid of `struct posix_cond`.

For now, let's keep this change as a simple type
substitution. Eventually, we should be able to fully switch
to Zephyr API internally.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-06-09 12:27:04 -04:00
Christopher Friedt 0d7ef2297b posix: mutex: use k_mutex instead of posix_mutex
The internal representation of `pthread_mutex_t`,
`struct posix_mutex`, is basically a clone of `struct k_mutex`
but without the benefit of being able to use all of the
existing `k_mutex_*()` suite of functions.

The first step in the right direction was switching
the external representation of `pthread_mutex_t` to a simple
`int`. Let's take the next step in the right direction, which
is getting rid of `struct posix_mutex`.

The only significant difference between `struct k_mutex` and
`struct posix_mutex` is that the latter needs a `type` field.

Since there were a fixed number of `struct posix_mutex`, we
can just externalize the `type` field and reuse
`struct k_mutex` as-is.

For now, let's keep this change as a simple type
substitution. Eventually, we should be able to fully switch
to Zephyr API internally.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-06-09 12:27:04 -04:00
Christopher Friedt f35fb33b94 posix: pthread: reimplement pthread_barrier using zephyr objects
Previously pthread_barrier_t was implemented in terms of wait
queues and internal scheduler functions.

This introduced some obstacles and inconsistency. In order
to be more consistent, rely only on Zephyr's public API and
reuse as many concepts as possible.

Deprecate `PTHREAD_BARRIER_DEFINE()` since it's non-standard.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-06-09 12:27:04 -04:00
Christopher Friedt 3e27c7f4a7 posix: eventfd: deprecate non-public EFD macros
Deprecate `EFD_IN_USE` and `EFD_FLAGS_SET` as they are not
specified as part of any public `eventfd()` API.

While those are being deprecated, use `_INTERNAL` variants.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-06-02 18:45:46 -04:00
Christopher Friedt e6eb0a705b posix: eventfd: revise locking, signaling, and allocation
TL;DR - a complete rewrite.

Previously, the prototypical `eventfd()` usage (one thread
performing a blocking `read()`, followed by another thread
performing a `write()`) would deadlock Zephyr. This shortcoming
has existed in Zephyr's `eventfd()` implementation from the
start and the suggested workaround was to use `poll()`.

However, that is not sufficient for integrating 3rd-party
libraries that may rely on proper `eventfd()` blocking
operations such as `eventfd_read()` and `eventfd_write()`.

The culprit was the per-fdtable-entry `struct k_mutex`.

Here we perform a minor revision of the locking strategy
and employ `k_condvar_broadcast()` and `k_condvar_wait()`
to signal and wait on the holder of a given `struct k_mutex`.

It is important to note, however, that the primary means of
synchronizing the eventfd state is actually the eventfd
spinlock. The fdtable mutex and condition variable are mainly
used for the purposes of blocking io (r,w,close) and are not
used in the code path of non-blocking reads.

The `wait_q` and `k_poll_signal` entries were removed from
`struct eventfd` as they were unnecessary.

Additionally, switch to using a bitarray because it is
possibly faster than linear search for allocating and
deallocating eventfd resources.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-06-02 18:45:46 -04:00
Christopher Friedt 90343a1f6d posix: eventfd: un-inline eventfd_read() and eventfd_write()
There is no major benefit to haveing `eventfd_read()` and
`eventfd_write()` as `static inline` functions and it is
arguably much easier to solve the deadlock bug when they
are not `inline`.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-06-02 18:45:46 -04:00
Gerard Marull-Paretas dacb3dbfeb iterable_sections: move to specific header
Until now iterable sections APIs have been part of the toolchain
(common) headers. They are not strictly related to a toolchain, they
just rely on linker providing support for sections. Most files relied on
indirect includes to access the API, now, it is included as needed.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2023-05-22 10:42:30 +02:00
Kristoffer Rist Skøien 5974617db8 lib: getopt: Fix compiler warning.
current_dash may be used uninitialized

Signed-off-by: Kristoffer Rist Skøien <kristoffer.skoien@nordicsemi.no>
2023-05-09 11:03:44 -04:00
Kumar Gala f215e4494c posix: eventfd: depends on polling
Have eventfd Kconfig select POLL is the code utilizes the polling
API.  We get a link error for tests/lib/fdtable/libraries.os.fdtable
when building on arm-clang without this.

Signed-off-by: Kumar Gala <kumar.gala@intel.com>
2023-05-09 23:32:57 +09:00
Chris Friedt b9828a74ab posix: sched: add support for SCHED_OTHER
The `SCHED_OTHER` scheduling priority is mandatory as part of
POSIX. It must be numerically distinct from `SCHED_FIFO`,
`SCHED_RR`, and `SCHED_SPORADIC`, but is implementation-
defined and may behave identically to `SCHED_FIFO` or
`SCHED_RR`.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2023-04-29 13:14:40 -07:00
Chris Friedt 2b2cbf8107 posix: sched: ensure min and max priority are schedulable
Previously, there was an off-by-one error for SCHED_RR.

Fixes #56729

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2023-04-29 13:14:40 -07:00
Gerard Marull-Paretas 667eeb11fb shell: fix MISRA 5.7 violations on struct shell
MISRA Rule 5.7 requires uniqueness of tag identifiers. Shell is
frequently problematic because many code uses `const struct shell
*shell`. This causes CI noise every time one of these shell files is
edited, so let's update all of them with `const struct shell *sh`
instead.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2023-04-14 12:21:08 +02:00
Gerard Marull-Paretas a5fd0d184a init: remove the need for a dummy device pointer in SYS_INIT functions
The init infrastructure, found in `init.h`, is currently used by:

- `SYS_INIT`: to call functions before `main`
- `DEVICE_*`: to initialize devices

They are all sorted according to an initialization level + a priority.
`SYS_INIT` calls are really orthogonal to devices, however, the required
function signature requires a `const struct device *dev` as a first
argument. The only reason for that is because the same init machinery is
used by devices, so we have something like:

```c
struct init_entry {
	int (*init)(const struct device *dev);
	/* only set by DEVICE_*, otherwise NULL */
	const struct device *dev;
}
```

As a result, we end up with such weird/ugly pattern:

```c
static int my_init(const struct device *dev)
{
	/* always NULL! add ARG_UNUSED to avoid compiler warning */
	ARG_UNUSED(dev);
	...
}
```

This is really a result of poor internals isolation. This patch proposes
a to make init entries more flexible so that they can accept sytem
initialization calls like this:

```c
static int my_init(void)
{
	...
}
```

This is achieved using a union:

```c
union init_function {
	/* for SYS_INIT, used when init_entry.dev == NULL */
	int (*sys)(void);
	/* for DEVICE*, used when init_entry.dev != NULL */
	int (*dev)(const struct device *dev);
};

struct init_entry {
	/* stores init function (either for SYS_INIT or DEVICE*)
	union init_function init_fn;
	/* stores device pointer for DEVICE*, NULL for SYS_INIT. Allows
	 * to know which union entry to call.
	 */
	const struct device *dev;
}
```

This solution **does not increase ROM usage**, and allows to offer clean
public APIs for both SYS_INIT and DEVICE*. Note that however, init
machinery keeps a coupling with devices.

**NOTE**: This is a breaking change! All `SYS_INIT` functions will need
to be converted to the new signature. See the script offered in the
following commit.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>

init: convert SYS_INIT functions to the new signature

Conversion scripted using scripts/utils/migrate_sys_init.py.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>

manifest: update projects for SYS_INIT changes

Update modules with updated SYS_INIT calls:

- hal_ti
- lvgl
- sof
- TraceRecorderSource

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>

tests: devicetree: devices: adjust test

Adjust test according to the recently introduced SYS_INIT
infrastructure.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>

tests: kernel: threads: adjust SYS_INIT call

Adjust to the new signature: int (*init_fn)(void);

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2023-04-12 14:28:07 +00:00
Gerard Marull-Paretas 0ebe14beb4 sys: util: migrate all files to DIV_ROUND_UP
ceiling_fraction is deprecated, use DIV_ROUND_UP.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2023-04-11 12:00:37 +02:00
Chris Friedt 5e9d3c7522 posix: fnmatch: correct (many) compliance issues
* add opening and closing braces around several blocks
* convert dos line endings to unix
* move assignments out of conditionals
* ensure adequate spacing after control blocks

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2023-03-03 17:40:14 +09:00
Chris Friedt f923441146 lib: posix: move fnmatch from lib/util to lib/posix
The `fnmatch()` function is specified by POSIX and should be
a part of `lib/posix`.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2023-03-03 17:40:14 +09:00
Chris Friedt ed11614a87 posix: mqueue: do not typedef mq_attr
The specification actually requires this to be a plain
`struct mq_attr`, without the typedef.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2023-01-23 09:57:31 -08:00
Chris Friedt 3aff1ff0c2 posix: fs: avoid adding open() alias with newlib and picolibc
Newlib and PicoLibc both already alias `open` to `_open`.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2023-01-23 09:57:31 -08:00
Chris Friedt e431cfbbe5 posix: eventfd: select eventfd by default with CONFIG_POSIX_API
Although the eventfd API is not (yet) a part of POSIX,
it's pretty well ubiquitous on POSIX systems now.

Enable it by default when `CONFIG_POSIX_API=y`.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2023-01-23 09:57:31 -08:00
Chris Friedt bf5d47a5ce posix: getopt: select getopt by default with CONFIG_POSIX_API
The `getopt()` function is part of POSIX and should be
available when applications choose to enable general POSIX
API support.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2023-01-23 09:57:31 -08:00
Chris Friedt e93f3c9b6d posix: rename posix_sched.h to sched.h
Rename the `posix_sched.h` to conform to the spec.

IEEE Std 1003.1

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2023-01-23 09:57:31 -08:00
Chris Friedt 43110ddb71 lib: posix: standard include paths with CONFIG_POSIX_API
With the `<zephyr/posix/...> prefix, it became
exponentially more difficult to integrate 3rd-party
libraries that depend on the POSIX API.

Standard POSIX headers should be available in standard
include paths - and that should most certainly the case
when `CONFIG_POSIX_API=y`.

With this change:

* When `CONFIG_POSIX_API=y`
  - applications have explicitly chosen to use
    POSIX APIs.
  - all standard POSIX includes are in the default
    include path.
* When `CONFIG_POSIX_API=n`
  - applications *may* include POSIX headers
    explicitly with the namespaced prefix
  - e.g. `#include <zephyr/posix/unistd.h>`
  - individual Kconfig options can be used to
    enable POSIX features selectively, such as
    `getopt` or `eventfd`.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2023-01-23 09:57:31 -08:00
Chris Friedt 0697c7b3fe posix: headers: harmonize remaining types with newlib
The remaining types that needed to be harmonized between
Newlib and Zephyr's POSIX definitions are:

* `struct sched_param`
  - don't re-define if using minimal libc
* `pthread_attr_t`
  - convert to `struct pthread_attr`
  - define type if using minimal libc
  - assert acceptible object size
* `pthread_mutexattr_t`
  - convert to `struct pthread_mutexattr`
  - define type if using minimal libc
  - assert acceptible object size
* `pthred_condattr_t`
  - convert to `struct pthread_condattr`
  - define type if using minimal libc
  - assert acceptible object size
* `pthread_once_t`
  - adopt newlib definition
  - define type if using minimal libc
Signed-off-by: Chris Friedt <cfriedt@meta.com>
2023-01-10 09:02:21 +09:00
Chris Friedt ecb82784b7 posix: newlib compatible PTHREAD_CREATE_DETACHED and JOINABLE
Define `PTHREAD_CREATE_DETACHED` and
`PTHREAD_CREATE_JOINABLE` to be compatible with the Newlib
definitions.

This is a temporary workaround for #51211 until Newlib
headers are pulled in.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2023-01-10 09:02:21 +09:00
Chris Friedt 8659e2f69e libc: minimal: include: move fcntl.h to posix
The `fcntl.h` header has never been a part of ISO C so move it to
`include/zephyr/posix`.

To ensure a smooth migration, a header was left in
`lib/libc/minimal/include` that prints a deprecation warning.

Users should either include `<zephyr/posix/fcntl.h>` or switch to
`CONFIG_POSIX_API=y`.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2023-01-10 09:02:21 +09:00
Chris Friedt 6f4e96bc24 libc: minimal: include: move sys/stat.h to posix
The `sys/stat.h` header has never been a part of ISO C so move it
to `zephyr/include/posix/sys/`.

To ensure a smooth migration, leave a stub header in
`lib/libc/minimal/include/sys/` that prints a deprecation warning
suggesting developers either include `<zephyr/posix/sys/stat.h>`
or use `CONFIG_POSIX_API=y`.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2023-01-10 09:02:21 +09:00
Chris Friedt 44b72bf801 posix: clock: fix seconds calculation
The previous method used to calculate seconds in `clock_gettime()`
seemed to have an inaccuracy that grew with time causing the
seconds to be off by an order of magnitude when ticks would roll
over.

This change fixes the method used to calculate seconds.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2023-01-04 21:12:58 +01:00
Nicholas Lowell 51f83185b0 posix: Kconfig for timer_create wait time
should be able to configure the time spent waiting
for available resources when calling timer_create()
to not cause a hiccup in applications that require
faster response times than the original hard-coded 100 ms.

Signed-off-by: Nicholas Lowell <nlowell@lexmark.com>
2022-12-27 15:34:13 +01:00
Chris Friedt ff3aaa6ef3 posix: getopt: move declarations to unistd.h
Declarations for `getopt()` should be in `<unistd.h>`
according to the spec. The extended versions `getopt_long()`
and `getopt_long_only()` are declared in `<getopt.h>`.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2022-12-04 14:51:52 +01:00
Chris Friedt 4d906d944b Revert "posix: getopt: move declarations to unistd.h"
This seems to have caused build failures in spite of CI being
green in PR 52653.

This reverts commit fc92eb1b37.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2022-12-02 14:09:37 +09:00
Chris Friedt fc92eb1b37 posix: getopt: move declarations to unistd.h
Declarations for `getopt()` should be in `<unistd.h>`
according to the spec. The extended versions `getopt_long()`
and `getopt_long_only()` are declared in `<getopt.h>`.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2022-12-01 15:52:47 -05:00
Chris Friedt 7b95428fa0 lib: posix: update usleep() to follow the POSIX spec
The original implementation of `usleep()` was not compliant
to the POSIX spec in 3 ways.
- calling thread may not be suspended (because `k_busy_wait()`
  was previously used for short durations)
- if `usecs` > 1000000, previously we did not return -1 or set
  `errno` to `EINVAL`
- if interrupted, previously we did not return -1 or set
  `errno` to `EINTR`

This change addresses those issues to make `usleep()` more
POSIX-compliant.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2022-11-24 19:31:33 -05:00
Chris Friedt dcfcc6454b lib: posix: sleep() should report unslept time in seconds
In the case that `sleep()` is interrupted, the POSIX spec requires
it to return the number of "unslept" seconds (i.e. the number of
seconds requested minus the number of seconds actually slept).

Since `k_sleep()` already returns the amount of "unslept" time
in ms, we can simply use that.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2022-11-24 19:31:33 -05:00
Chris Friedt f1ececc682 posix: key: abstract pthread_key_t as uint32_t
Consistent with the change of `pthread_t`, `pthread_mutex_t`,
and `pthread_cond_t` to `uint32_t`, we can now also abstract
`pthread_key_t` as `uint32_t` and separate the implementation
detail, hidden from POSIX API consumers.

This change introduces `CONFIG_MAX_PTHREAD_KEY_COUNT`.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2022-11-24 04:19:01 +09:00
Chris Friedt 96b9d7cb6d lib: posix: pthread_key: use spinlock instead of semaphore
None of the operations that `pthread_key_sem` protected were
blocking, so simply make it a spinlock.

Also made the lock static.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2022-11-24 04:19:01 +09:00
Chris Friedt 187df899da lib: posix: pthread: rename pthread_key_lock to pthread_once_lock
Since this lock is only used in `pthread_once()` it makes sense
to name it accordingly.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2022-11-24 04:19:01 +09:00
Chris Friedt 3a56d276e9 posix: cond: abstract pthread_cond_t as uint32_t
Consistent with the change of `pthread_t` from
`struct posix_thread` to `uint32_t`, we can now also abstract
`pthread_cond_t` as `uint32_t` and separate `struct posix_cond`
as an implementation detail, hidden from POSIX API consumers.

This change deprecates `PTHREAD_COND_DEFINE()` in favour of the
(standardized) `PTHREAD_COND_INITIALIZER`.

This change introduces `CONFIG_MAX_PTHREAD_COND_COUNT`.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2022-11-19 14:16:38 +09:00
Chris Friedt afae448ff2 posix: pthread: take care with pthread cond resources
Previously, `pthread_cond_init()` could not actually fail, and
destroying condition variables was a no-op, and it was missing
in `pthread_exit()`.

However, with the change of `pthread_cond_t` to `uint32_t`, and
since those are embedded inside of `struct posix_thread` for the
time being, the pthread code needs to keep track that it is
relinquishes used condition variables when a thread completes.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2022-11-19 14:16:38 +09:00
Chris Friedt 1777a33558 lib: posix: internal: use a more generic INIT mask and inlines
Previously `PTHREAD_MUTEX_MASK_INIT` was used to mark a
`pthread_mutex_t` as initialized.

The same needs to be done for `pthread_cond_t` and likely others.

Rather than copy-pasting that and a number of inlines that
duplicate the same functionality, simply make it more generic.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2022-11-19 14:16:38 +09:00
Chris Friedt b0b4c9c3f1 posix: mutex: abstract pthread_mutex_t as uint32_t
Consistent with the change of `pthread_t` from
`struct posix_thread` to `uint32_t`, we can now also abstract
`pthread_mutex_t` as `uint32_t` and separate `struct posix_mutex`
as an implementation detail, hidden from POSIX API consumers.

This change deprecates `PTHREAD_MUTEX_DEFINE()` in favour of the
(standardized) `PTHREAD_MUTEX_INITIALIZER`.

This change introduces `CONFIG_MAX_PTHREAD_MUTEX_COUNT`.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2022-11-15 05:56:31 +09:00
Chris Friedt 6042acc1a9 posix: pthread: take care with pthread mutex resources
Previously, `pthread_mutex_init()` could not actually fail, and
destroying mutexes was a no-op, so it was missing in a couple of
places.

However, with the change of `pthread_mutex_t` to `uint32_t`, and
since those are embedded inside of `struct posix_thread` for the
time being, the pthread code needs to keep track that it is
relinquishes used mutex resources when a thread completes.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2022-11-15 05:56:31 +09:00
Chris Friedt 39b8b3ac8a posix: pthread_create: use spinlock for pthread_pool_lock
The `pthread_create()` function is not a cancellation point and
iterating over / mutating `posix_thread_pool` is not a blocking
operation, so use a spinlock for the internal `pthread_pool_lock`.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2022-11-08 16:10:26 -05:00
Chris Friedt 660a7db3f2 posix: pthread: use spinlock instead of mutex for cancel lock
The cancel lock will not block so a mutex is overkill.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2022-11-08 13:30:02 -05:00
Chris Friedt 4bae4f426f include: posix: move pthread impl detail to posix_internal.h
The `struct pthread` and `enum pthread_state` are actually
implementation details specific to Zephyr.

Let's limit the scope where that level of detail is visible.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2022-11-07 09:09:11 -05:00
Chris Friedt 2812f61e4e posix: pthread_once: use k_mutex for pthread_key_lock
In the interest of reducing any layering concerns,
avoid using POSIX locking primitives where necessary.

Note: it is not safe to use a spinlock here, as the
callback function to `pthread_once()` may itself be
a cancellation point.

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2022-11-07 09:04:01 -05:00