Overview ¶
Purpose ¶
The Thread Safe LibC utility provides the locking hooks required to make the C library (libc) thread‑safe on STM32 targets. It supplies toolchain‑specific glue for newlib (GCC), ARMlibc (ARMCC), and IAR DLib, while allowing each project to select an appropriate locking strategy for its execution model (bare‑metal or FreeRTOS, with configurable permission or restriction of lock usage in ISRs).
Key Features ¶
The main features are:
Configurable locking strategies via preprocessor macros: bare‑metal allow/deny, FreeRTOS allow/deny, or user‑defined locks.
Multi-toolchain C library integration: ready‑to‑use glue layers for newlib, ARMlibc, and IAR DLib, providing thread‑safe hooks for malloc/free, the C runtime environment, timezone handling, file I/O locking, and C++ static initialization guards.
FreeRTOS integration using
taskENTER_CRITICAL/taskENTER_CRITICAL_FROM_ISRwith bounded nesting.Minimal footprint.
Architecture ¶
The following diagram illustrates the software components involved in the Thread Safe LibC utility. It shows the interactions between the user application, the C library runtime (newlib, ARMlibc, IAR DLib), the Thread Safe LibC utility, and the STM32 target platform.
Component Structure ¶
Core lock primitives (
stm32_lock.h): Defines the configurable lock API (stm32_lock_init,stm32_lock_acquire,stm32_lock_release) and selects the lock strategy (user-defined, bare-metal, or FreeRTOS; allowing or denying lock usage in interrupt context) based on compile‑time macros.Toolchain glue:
GCC/newlib (
GCC/newlib_lock_glue.c): Implements the newlib lock interface for GCC to protect selected C library functions and provides the__cxa_guard_*hooks for thread-safe one-time initialization of local static objects in C++.ARMCC/ARMlibc (
ARM/armlib_lock_glue.c): Provides the ARM C library mutex implementation based on a fixed pool of statically allocated locks and routes_mutex_*operations through the STM32 lock API.IAR/DLib (
IAR/dlib_lock_glue.c): Implements the IAR DLib lock interface for system, file, and C++ dynamic locks and performs early thread‑safety initialization beforemain.All these toolchain-specific lock implementations rely on the common
stm32_lockprimitives selected by the STM32 thread-safety strategy.
User template (
stm32_lock_user.h): Provides a template for defining a customLockingData_ttype and implementingstm32_lock_init,stm32_lock_acquire, andstm32_lock_releasewhenSTM32_THREAD_SAFE_USER_LOCKSis selected, enabling projects to plug in their own thread‑safety mechanism.
Modules and Files ¶
The following diagram illustrates the Thread Safe LibC module and its associated files.
Configuration table ¶
The following table lists the configuration defines for the Thread Safe LibC module:
|
Config Defines |
Where |
Description |
|---|---|---|
|
|
Preprocessor environment |
Enables a user-defined locking implementation (
|
|
|
Preprocessor environment |
Enables the bare-metal strategy that allows lock usage from interrupts |
|
|
Preprocessor environment |
Enables the bare-metal strategy that denies lock usage from interrupts |
|
|
Preprocessor environment |
Enables the FreeRTOS-based strategy that allows lock usage from tasks and ISRs |
|
|
Preprocessor environment |
Enables the FreeRTOS-based strategy that denies lock usage from tasks and ISRs |
|
|
Preprocessor environment |
Legacy selector: values 1..5 to choose one of the strategies above (1 = user, 2 = bare‑metal allow, 3 = bare‑metal deny, 4 = FreeRTOS allow, 5 = FreeRTOS deny) |