Commit Graph

26 Commits

Author SHA1 Message Date
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
Daniel Leung c972ef1a0f kernel: mm: move kernel mm functions under kernel includes
This moves the k_* memory management functions from sys/ into
kernel/ includes, as there are kernel public APIs. The z_*
functions are further separated into the kernel internal
header directory.

Also made a quick change to doxygen to group sys_mem_* into
the OS Memory Management group so they will appear in doc.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2023-11-20 09:19:14 +01:00
Christopher Friedt 4c58c6b4c4 libc: common: support for C11 call_once()
Add C11 call_once() support to go with C11 threads
and mutexes.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-11-14 18:23:42 +09:00
Christopher Friedt b9db7df628 libc: common: support for C11 thread-specific storage
Add C11 thread-specific storage (tss) support to go with C11 threads
and mutexes.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-11-14 18:23:42 +09:00
Christopher Friedt 7e539e2706 libc: common: support for C11 condition variables
Add C11 condition variable support to go with C11 threads
and mutexes.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-11-14 18:23:42 +09:00
Christopher Friedt 576ae7f677 lib: libc: common: add C11 mutex implementation
Add support for C11 mutexes to go with C11 threads.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-11-14 18:23:42 +09:00
Christopher Friedt 97668b8b09 libc: common: add support for iso c11 threads
This change capitalizes on newly added support for dynamic
thread stacks and the existing pthread support to provide
an implementation of the ISO C11 `<threads.h>` API.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2023-11-14 18:23:42 +09:00
Grant Ramsay a3ff19a39e cmake: compiler: Add compiler property for no-builtin
Abstracts these flags for multiple toolchain support

Signed-off-by: Grant Ramsay <gramsay@enphaseenergy.com>
2023-11-13 10:21:41 +01:00
Grant Ramsay 679d82c484 libc: Add GCC fno-builtin-malloc flag to common stdlib compilation
This prevents the compiler from optimizing calloc into an
infinite recursive call.

For example a call to malloc + memset zero at GCC -O2 will be
replaced by a call to calloc. This causes infinite recursion
if the function being implemented *is* calloc.

fno-builtin-malloc forces the compiler to avoid this optimization.

Signed-off-by: Grant Ramsay <gramsay@enphaseenergy.com>
2023-11-13 10:21:41 +01:00
Keith Packard e7126b5d84 libc/common: Place malloc data structures in libc partition
Leave the malloc partition so that it only contains the heap itself; this
lets the initialization code adjust the address range when configuring the
arena at startup.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-10-10 23:39:40 +03:00
Keith Packard ae92df1e4a libc/common: Don't use malloc mutex when CONFIG_MULTITHREADING=n
When multithreading is disabled, the whole mutex infrastructure isn't
available. The common malloc code wasn't checking for this case which
caused build failures.

Signed-off-by: Keith Packard <keithp@keithp.com>
Tested-by: Detlev Zundel dzu@member.fsf.org
2023-09-25 18:42:21 -04:00
Keith Packard 101cdcd86e libc/common: Add memalign
Memalign is another name for the posix aligned_alloc function, although it
has weaker restrictions on the relationship between the alignment and size.

memalign() is used internally by the libstdc++ when built for 'newlib'
targets (which includes picolibc) instead of aligned_alloc() due to a bug
in gcc, so we need to provide an implementation of this when using that
library, even though it's not part of the Zephyr C library API.

When a fix for the libstdc++ is merged upstream and can be consider a
reasonable dependency for Zephyr, this work-around can be removed.

Closes: #57899

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-08-10 11:35:17 -04:00
Alberto Escolar Piedras 0517c85dcc libC COMMON_LIBC_MALLOC_ARENA_SIZE: provide default for POSIX ARCH
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>
2023-06-22 17:16:17 -04:00
Keith Packard b97a4a52a8 libc/common: Increase USERSPACE + POT MPU default arena to 2048 bytes
This matches the size that would be used with the newlib_nano configuration
and allows several tests to complete which would otherwise fail due to
insufficient heap space.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-05-31 10:02:53 -04:00
Keith Packard 3103f94c6a libc/common: Require at least double alignment for common malloc arena
arm, arc and riscv all have special cases for malloc arena alignment that
might be smaller than the minimum required for a C allocator. In
particular, the riscv value might actually be zero, which turns out to be
an invalid alignment value.

Make sure all of these have alignment that meets the C language
requirements for allocation alignment.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-05-31 10:02:53 -04:00
Keith Packard 5279acba9d libc/common: Make sure malloc arena is PoT aligned on Risc-V
The malloc arena needs to be aligned to a suitable size on Risc-V, and the
Z_RISCV_STACK_GUARD_SIZE variable is a helpful proxy for what size that
is. However, that is not always a power of two, so round it up to make the
linker capable of performing the necessary allocation.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-05-11 01:25:01 +09:00
Keith Packard 69f4f39aef libc/common: Always build aligned_alloc
This function may be used by system libraries (like libstdc++), so instead
of gating compile on target language, always build this function and gate
API visibility in header files instead.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-05-11 01:25:01 +09:00
Keith Packard f74455127d libc/common: Refine check for xtensa _heap_sentry
Not all xtensa targets define the top of usable RAM via the _heap_sentry
address; it looks like the list is limited to esp32, esp32s2, esp32s3 and
intel parts. The first three all define HAS_ESPRESSIF_HAL, so key the test
using that or SOC_FAMILY_INTEL_ADSP.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-05-11 01:25:01 +09:00
Keith Packard 706d9f6db2 libc/common: Use static initializer for z_malloc_heap_mutex
Instead of explicitly initializing the mutex at runtime, use
SYS_MUTEX_DEFINE to initialize it at build time instead.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-05-11 01:25:01 +09:00
Keith Packard 0a44b419ab libc/common: Allocate default malloc arena for non-minimal-libc uses
When using the common malloc implementation on systems not using the
minimal C library, allocate a reasonable default malloc heap according to
the following rules (adopted from the Picolibc heap size defaults):

* When an MMU is available, allocate 16kB.
* When USERSPACE is enabled for a device with an MPU require PoT alignment,
  allocate 1024 bytes
* Otherwise, use all available memory.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-05-09 01:29:16 +09:00
Keith Packard 32a437cbc8 libc/common: Handle MMU/MPU/USERSPACE and alignment requirements in malloc
When CONFIG_COMMON_LIBC_MALLOC_ARENA_SIZE is set to -1, make the malloc
arena use all remaining RAM. When an MMU exists, allocate the arena at
startup using k_mem_map. Otherwise, compute the available memory
automatically and use that.

When an MPU is being used to manage the malloc arena, make sure the heap
respects any MPU alignment requirements. Otherwise, align the heap to
sizeof(double).

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-05-09 01:29:16 +09:00
Kumar Gala 161e4a05a2 libc: Move abort into common
Move the abort implementation into common so its shared among the
libc.  As part of this start using the common abort on newlib.

Signed-off-by: Kumar Gala <kumar.gala@intel.com>
2023-05-08 09:59:27 +02:00
Kumar Gala ca4dc50ec1 libc: Move strnlen into common
Move the strnlen implementation into common so its available to any
libc that may not implement strnlen.

Signed-off-by: Kumar Gala <kumar.gala@intel.com>
2023-05-08 09:59:27 +02:00
Keith Packard a9c62caab7 lib/libc: Move malloc from minimal to common library
This will (eventually) permit use of a common malloc implementation with
other C libraries, reducing the amount of Zephyr-specific code required
to support each C library.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-04-27 20:17:24 +09:00
Keith Packard 52e57ef17f lib/libc: Allow common library to be empty
This allows the C library common library to be empty if no pieces are
needed, eliminating cmake warnings.

Signed-off-by: Keith Packard <keithp@keithp.com>
2023-04-27 20:17:24 +09:00
Kumar Gala 672aeace88 libc: share time() between minimal libc and armclang libc
Introduce a place to share implementations of libc functions that
are needed by different libc versions.  Place time() in this common
location so it can be shared when building for either minimal libc or
armclang libc.

Signed-off-by: Kumar Gala <kumar.gala@intel.com>
2023-03-27 09:49:40 +02:00