mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-12-02 22:40:27 +08:00
refactor(cdev): release the node lock around allocations with macros
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é <benjamin@zephyrproject.org>
This commit is contained in:
1 parent
624d384ac7
commit
8351cc0046
3 files changed
+10
-18
No files matched your search
+6
-18
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
{
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
|
||||
#define ATOMIC_ENTER lock()
|
||||
#define ATOMIC_LEAVE unlock()
|
||||
#define ATOMIC_PAUSE
|
||||
#define ATOMIC_RESUME
|
||||
|
||||
namespace cdev
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user