diff --git a/src/drivers/uavcan/allocator.hpp b/src/drivers/uavcan/allocator.hpp index 3f11ee558da..25d704605e2 100644 --- a/src/drivers/uavcan/allocator.hpp +++ b/src/drivers/uavcan/allocator.hpp @@ -39,6 +39,7 @@ #include #include +#include #include // TODO: Entire UAVCAN application should be moved into a namespace later; this is the first step. @@ -46,8 +47,7 @@ namespace uavcan_node { struct AllocatorSynchronizer { - const ::irqstate_t state = ::enter_critical_section(); - ~AllocatorSynchronizer() { ::leave_critical_section(state); } + const UavcanCriticalSection lock; }; struct Allocator : public uavcan::HeapBasedPoolAllocator { diff --git a/src/drivers/uavcan/uavcan_drivers/critical_section.hpp b/src/drivers/uavcan/uavcan_drivers/critical_section.hpp new file mode 100644 index 00000000000..56fa7711c84 --- /dev/null +++ b/src/drivers/uavcan/uavcan_drivers/critical_section.hpp @@ -0,0 +1,67 @@ +/**************************************************************************** + * + * Copyright (c) 2026 PX4 Development Team. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name PX4 nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +#pragma once + +#if defined(__PX4_NUTTX) +#include +#elif defined(__PX4_POSIX) +#include +#endif + +/** + * Masks the interrupts in its scope, or, on POSIX, which has no interrupts, holds a process-wide lock. + */ +class UavcanCriticalSection +{ +public: +#if defined(__PX4_NUTTX) + UavcanCriticalSection() : _flags(enter_critical_section()) {} + ~UavcanCriticalSection() { leave_critical_section(_flags); } + +private: + const irqstate_t _flags; +#elif defined(__PX4_POSIX) + UavcanCriticalSection() { mutex().lock(); } + ~UavcanCriticalSection() { mutex().unlock(); } + +private: + static std::recursive_mutex &mutex() + { + static std::recursive_mutex m; + return m; + } +#else +#error "UavcanCriticalSection has no implementation for this system" +#endif +}; diff --git a/src/drivers/uavcan/uavcan_drivers/hrt_clock.hpp b/src/drivers/uavcan/uavcan_drivers/hrt_clock.hpp index 13805010d7c..168a2ec6445 100644 --- a/src/drivers/uavcan/uavcan_drivers/hrt_clock.hpp +++ b/src/drivers/uavcan/uavcan_drivers/hrt_clock.hpp @@ -35,7 +35,7 @@ #include #include -#include +#include "critical_section.hpp" namespace uavcan_hrt_clock { @@ -70,21 +70,27 @@ public: uint64_t utcUsec() const { - irqstate_t flags = enter_critical_section(); - const uint64_t utc = utcUsecFromInterrupt(); - leave_critical_section(flags); - return utc; + const UavcanCriticalSection lock; + return utcUsecFromInterrupt(); + } + + /** + * UTC at an earlier HRT time, for example when a frame was received. + */ + uint64_t utcUsecAt(uint64_t hrt) const + { + const UavcanCriticalSection lock; + return _set ? utcAt(hrt) : 0; } void setUtc(uint64_t utc_usec) { - irqstate_t flags = enter_critical_section(); + const UavcanCriticalSection lock; const uint64_t now = hrt_absolute_time(); _offset_usec = int64_t(utc_usec) - int64_t(now); _anchor = now; _last_adjustment_at = now; _set = true; - leave_critical_section(flags); } /** @@ -93,7 +99,7 @@ public: */ void adjustUtc(int64_t adjustment_usec) { - irqstate_t flags = enter_critical_section(); + const UavcanCriticalSection lock; const uint64_t now = hrt_absolute_time(); if (!_set) { @@ -125,16 +131,12 @@ public: _adjustments++; _last_adjustment_usec = int32_t(adjustment_usec); } - - leave_critical_section(flags); } SyncStatus status() const { - irqstate_t flags = enter_critical_section(); - const SyncStatus s{_adjustments, _last_adjustment_usec, int32_t((_rate_q32 * 1000000000LL) >> 32)}; - leave_critical_section(flags); - return s; + const UavcanCriticalSection lock; + return SyncStatus{_adjustments, _last_adjustment_usec, int32_t((_rate_q32 * 1000000000LL) >> 32)}; } private: diff --git a/src/drivers/uavcan/uavcan_main.cpp b/src/drivers/uavcan/uavcan_main.cpp index 1ddd4d62c70..7402fb65e70 100644 --- a/src/drivers/uavcan/uavcan_main.cpp +++ b/src/drivers/uavcan/uavcan_main.cpp @@ -54,8 +54,6 @@ #include #include -#include - #include #include diff --git a/src/drivers/uavcan/uavcan_servers.cpp b/src/drivers/uavcan/uavcan_servers.cpp index 3521e802ee8..834c120bb3c 100644 --- a/src/drivers/uavcan/uavcan_servers.cpp +++ b/src/drivers/uavcan/uavcan_servers.cpp @@ -34,8 +34,6 @@ #include #include -#include - #include #include #include @@ -47,8 +45,6 @@ #include #include -#include - #include "uavcan_main.hpp" #include "uavcan_servers.hpp"