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>
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>
After commit 9a0aebc5fd,
the exclusion of qemu_x86_tiny is no longer and the "depends on"
option was removed. However, the comment about that remained.
Remove the comment as it is no longer valid.
Signed-off-by: Daniel Leung <daniel.leung@intel.com>
Skip child objects and arrays that are not specified in the given object
descriptor when parsing a JSON input string.
This patch adds support for extra child arrays which previously were not
supported by the parser as opposed to additional child objects.
Fixes#47988
Signed-off-by: Markus Fuchs <markus.fuchs@ch.sauter-bc.com>
Changed initial guess from a simple x/3 to dividing the exponent by 2.
This makes large or small numbers like 10e10 and 01e-10 converge in a few
loops.
Added a loop counter to ensure that the algorithm breaks out of the loop in
the case that the algorithm doesn't converge (toggling between two
numbers).
Added test cases for sqrt and sqrtf in libc. Tested with a range of numbers
between 10e10 and 10e-10. Verify good accuracy in test case.
Closes: #55962
Signed-off-by: Lawrence King <lawrencek52@gmail.com>
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>
Picolibc has both recursive and non-recursive mutex uses. The bulk of the
library locking uses the global libc lock, which is a recursive mutex as
that greatly simplifies the implementation.
The only use of non-recursive mutexes is in the stdio code when dealing
with file system I/O via fopen.
Using mutexes for both APIs is valid; the assumption picolibc makes is that
the non-recursive mutexes are somehow cheaper or faster and should be
preferred. However, in Zephyr, recursive mutexes are the default and the
non-recursive locks for picolibc were implemented using semaphores.
Switch the non-recursive picolibc locks to just invoking the existing
recursive functions using mutexes. This avoids pulling in another lock
implementation, saving a bit of space.
This also lets the kernel.memory_protection.mem_map test work on
qemu_x86_tiny where the amount of memory available is 320kB and that is
nearly filled by this test case, leaving too little space for allocating
pages in the k_mem_map_unmap test.
Signed-off-by: Keith Packard <keithp@keithp.com>
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>
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>
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>
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>
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>
Now that the gcc compiler driver uses the -T flag instead of -Wl,-T, we can
remove the hack here that kept the picolibc specs file from inserting the
picolibc linker script.
Signed-off-by: Keith Packard <keithp@keithp.com>
It is possible to build with the PICOLIBC_MODULE
with the POSIX arch targets which use the native
simulator as runner.
Update filtering accordingly.
Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
It is possible to build with STDOUT_CONSOLE with
the embedded C libraries with the POSIX arch.
Narrow down the filtering accordingly.
Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
So they depend or select on the right NATIVE_BUILD
instead of NATIVE_APPLICATION.
Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
Add ACPI support for Zephyr using acpica open source
project. ACPI subsystem use to discover and configure
hardware components, perform power management (e.g. putting
unused hardware components to sleep), auto configuration (e.g.
Plug and Play and hot swapping) etc.
Signed-off-by: Najumon Ba <najumon.ba@intel.com>
This patch fixes support for encoding and decoding multidimensional arrays
as described by the JSON_OBJ_DESCR_ARRAY_ARRAY() macro.
Currently, the JSON array encoding and decoding functions, arr_encode()
and arr_parse(), expect array elements to be of object or primitive type.
However, arrays may be nested and so an array's elements may also be
arrays.
In order to support nested arrays, two special cases must be considered:
1. The array of objects/arrays sub-descriptor is described by two
`json_obj_descr` structs and so two instead of one `json_obj_descr`
structs must be skipped when iterating over the JSON descriptor to get to
an array's elements.
2. The implicit array item count field has to be considered for the
parent itself and all its child array items when calculating an element's
size.
Fixes#50801
Signed-off-by: Markus Fuchs <markus.fuchs@ch.sauter-bc.com>
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>
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>
Provide a sensible default for the POSIX architecture,
as now it is possible to build with it.
Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
The spin loop in _exit() needs a Z_SPIN_DELAY() for the
POSIX architecture, so it does not hang the whole
executable on that infinite loop but only the thread
that exit'ed.
Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
The spin loop in _exit() needs a Z_SPIN_DELAY() for the
posix architecture, so it does not hang the whole
executable on that infinite loop but only the thread
that exit'ed.
Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
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>
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>
portability.posix.common.arcmwdtlib test fails with ARCMWDT libc.
This path fixes the test.
STDIN_FILENO and others macroses are used in libc-hooks.c only. So they
defined localy.
Signed-off-by: Nikolay Agishev <agishev@synopsys.com>
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>
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>
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>
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>
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>
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>
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>
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>
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>
Complement the `struct k_mutex` in each fdtable entry
with a `struct k_condvar`. The reasoning for this should be
self-evident.
For a bit of history, `fdtable` was introduced in
commit 06eb489c45 ("kernel: add condition variables")
which predates `struct k_condvar`, introduced in
commit f484bbaa26 ("lib: posix: Implement generic file descriptor table")
by almost 2 years.
Additionally, provide a new accessor function,
`z_get_obj_lock_and_cond()`, that (optionally) gets the mutex
and condition variable associated with the provided object and
vtable.
Signed-off-by: Christopher Friedt <cfriedt@meta.com>