From 8351cc0046fcd01a87f0f058e94fd26dff3d3dff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Cab=C3=A9?= Date: Fri, 30 Oct 2026 19:09:20 +0100 Subject: [PATCH] refactor(cdev): release the node lock around allocations with macros MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CDev::poll() leaves the NuttX critical section around its heap allocations in __PX4_NUTTX blocks, as the allocator takes a semaphore. Name these as ATOMIC_PAUSE and ATOMIC_RESUME, which each platform defines next to ATOMIC_ENTER and ATOMIC_LEAVE: they leave and reenter the critical section on NuttX, and do nothing on POSIX, where the lock is a semaphore that can be held across an allocation. No functional change. Assisted-by: Claude:opus-5.5 Signed-off-by: Benjamin Cabé --- src/lib/cdev/CDev.cpp | 24 ++++++------------------ src/lib/cdev/nuttx/cdev_platform.hpp | 2 ++ src/lib/cdev/posix/cdev_platform.hpp | 2 ++ 3 files changed, 10 insertions(+), 18 deletions(-) diff --git a/src/lib/cdev/CDev.cpp b/src/lib/cdev/CDev.cpp index 7edbdafcabd..bcbdf2c303c 100644 --- a/src/lib/cdev/CDev.cpp +++ b/src/lib/cdev/CDev.cpp @@ -225,15 +225,11 @@ CDev::poll(file_t *filep, px4_pollfd_struct_t *fds, bool setup) const uint8_t new_count = _max_pollwaiters > 0 ? _max_pollwaiters * 2 : 1; px4_pollfd_struct_t **prev_pollset = _pollset; -#ifdef __PX4_NUTTX // malloc uses a semaphore, we need to call it enabled IRQ's - px4_leave_critical_section(flags); -#endif + ATOMIC_PAUSE; px4_pollfd_struct_t **new_pollset = new px4_pollfd_struct_t *[new_count]; -#ifdef __PX4_NUTTX - flags = px4_enter_critical_section(); -#endif + ATOMIC_RESUME; if (prev_pollset == _pollset) { // no one else updated the _pollset meanwhile, so we're good to go @@ -252,31 +248,23 @@ CDev::poll(file_t *filep, px4_pollfd_struct_t *fds, bool setup) _max_pollwaiters = new_count; // free the previous _pollset (we need to unlock here which is fine because we don't access _pollset anymore) -#ifdef __PX4_NUTTX - px4_leave_critical_section(flags); -#endif + ATOMIC_PAUSE; if (prev_pollset) { delete[](prev_pollset); } -#ifdef __PX4_NUTTX - flags = px4_enter_critical_section(); -#endif + ATOMIC_RESUME; // Success ret = PX4_OK; break; } -#ifdef __PX4_NUTTX - px4_leave_critical_section(flags); -#endif + ATOMIC_PAUSE; // We have to retry delete[] new_pollset; -#ifdef __PX4_NUTTX - flags = px4_enter_critical_section(); -#endif + ATOMIC_RESUME; } if (ret == PX4_OK) { diff --git a/src/lib/cdev/nuttx/cdev_platform.hpp b/src/lib/cdev/nuttx/cdev_platform.hpp index 7ec2b18bb59..6080c577346 100644 --- a/src/lib/cdev/nuttx/cdev_platform.hpp +++ b/src/lib/cdev/nuttx/cdev_platform.hpp @@ -9,6 +9,8 @@ #define ATOMIC_ENTER irqstate_t flags = px4_enter_critical_section() #define ATOMIC_LEAVE px4_leave_critical_section(flags) +#define ATOMIC_PAUSE px4_leave_critical_section(flags) +#define ATOMIC_RESUME flags = px4_enter_critical_section() namespace cdev { diff --git a/src/lib/cdev/posix/cdev_platform.hpp b/src/lib/cdev/posix/cdev_platform.hpp index c9d0fd15913..234f4c5fc8e 100644 --- a/src/lib/cdev/posix/cdev_platform.hpp +++ b/src/lib/cdev/posix/cdev_platform.hpp @@ -6,6 +6,8 @@ #define ATOMIC_ENTER lock() #define ATOMIC_LEAVE unlock() +#define ATOMIC_PAUSE +#define ATOMIC_RESUME namespace cdev {