Files
esp-idf/components/linux/CMakeLists.txt
Guillaume Souchere 445db75612 fix(linux): implement pthread-based locks for soft-preemption safety
The Linux target's sys/lock.h provided no-op inline stubs, which was
safe only under the assumption of single-threaded execution.  With the
new FreeRTOS Linux simulator using soft preemption, an outgoing task
can still run concurrently with the incoming task until it reaches a
yield point, making no-op locks unsafe.

Replace the no-op implementation with real pthread mutexes:

- Change _lock_t from `typedef int` to `typedef void *` (pointer to a
  heap-allocated pthread_mutex_t).
- Implement all _lock_* functions in a new lock.c, supporting both
  normal and recursive mutexes.
- Zero-initialized locks are lazily created on first acquire using
  double-checked locking, preserving newlib/esp_libc semantics.
- Add lock.c unconditionally to the linux component sources and link
  pthread.
2026-06-04 11:36:34 +02:00

27 lines
785 B
CMake

idf_build_get_property(target IDF_TARGET)
if(NOT "${target}" STREQUAL "linux")
return()
endif()
set(srcs "lock.c")
set(includes "include")
if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin")
list(APPEND srcs getrandom.c assert_func.c)
else()
list(APPEND srcs fls.c)
list(APPEND includes "linux_include")
endif()
idf_component_register(INCLUDE_DIRS ${includes}
REQUIRED_IDF_TARGETS linux
SRCS ${srcs})
target_link_libraries(${COMPONENT_LIB} PRIVATE pthread)
find_library(LIB_BSD bsd)
if(LIB_BSD)
target_link_libraries(${COMPONENT_LIB} PRIVATE ${LIB_BSD})
elseif(NOT CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin")
message(WARNING "Missing LIBBSD library. Install libbsd-dev package and/or check linker directories.")
endif()