uORB: fix TSAN issues using atomics

This commit is contained in:
Julian Oes
2026-02-18 12:43:02 +13:00
parent 91e25df7c4
commit 22a32eaa55
13 changed files with 92 additions and 75 deletions
@@ -144,7 +144,7 @@ int uORBTest::UnitTest::pubsublatency_main()
PX4_INFO("max: %3i us", timing_max);
PX4_INFO("missed topic updates: %i", num_missed);
pubsubtest_passed = true;
pubsubtest_passed.store(true);
#if defined(CONFIG_ARCH_BOARD_PX4_SITL)
// relaxed on SITL (non-realtime)
@@ -154,13 +154,13 @@ int uORBTest::UnitTest::pubsublatency_main()
#endif
if (mean > kMaxMeanUs) {
pubsubtest_res = PX4_ERROR;
pubsubtest_res.store(PX4_ERROR);
} else {
pubsubtest_res = PX4_OK;
pubsubtest_res.store(PX4_OK);
}
return pubsubtest_res;
return pubsubtest_res.load();
}
int uORBTest::UnitTest::test()
@@ -412,7 +412,7 @@ int uORBTest::UnitTest::pub_test_multi2_main()
pub = orb_advertise_multi(ORB_ID(orb_test_medium_multi), &data_topic, &idx);
if (idx != i) {
_thread_should_exit = true;
_thread_should_exit.store(true);
PX4_ERR("Got wrong instance! should be: %i, but is %i", i, idx);
return -1;
}
@@ -440,7 +440,7 @@ int uORBTest::UnitTest::pub_test_multi2_main()
}
px4_usleep(100 * 1000);
_thread_should_exit = true;
_thread_should_exit.store(true);
for (int i = 0; i < num_instances; ++i) {
orb_unadvertise(orb_pub[i]);
@@ -454,7 +454,7 @@ int uORBTest::UnitTest::test_multi2()
test_note("Testing multi-topic 2 test (queue simulation)");
//test: first subscribe, then advertise
_thread_should_exit = false;
_thread_should_exit.store(false);
const int num_instances = 3;
int orb_data_fd[num_instances] {-1, -1, -1};
int orb_data_next = 0;
@@ -478,7 +478,7 @@ int uORBTest::UnitTest::test_multi2()
hrt_abstime last_time[num_instances] {};
while (!_thread_should_exit) {
while (!_thread_should_exit.load()) {
px4_usleep(1000);
@@ -945,7 +945,7 @@ int uORBTest::UnitTest::pub_test_queue_main()
const int queue_size = orb_get_queue_size(ORB_ID(orb_test_medium_queue_poll));
if ((ptopic = orb_advertise(ORB_ID(orb_test_medium_queue_poll), &t)) == nullptr) {
_thread_should_exit = true;
_thread_should_exit.store(true);
return test_fail("advertise failed: %d", errno);
}
@@ -968,9 +968,9 @@ int uORBTest::UnitTest::pub_test_queue_main()
px4_usleep(20 * 1000); //give subscriber a chance to catch up
}
_num_messages_sent = t.val;
_num_messages_sent.store(t.val);
px4_usleep(100 * 1000);
_thread_should_exit = true;
_thread_should_exit.store(true);
orb_unadvertise(ptopic);
return 0;
@@ -987,7 +987,7 @@ int uORBTest::UnitTest::test_queue_poll_notify()
return test_fail("subscribe failed: %d", errno);
}
_thread_should_exit = false;
_thread_should_exit.store(false);
char *const args[1] = { nullptr };
int pubsub_task = px4_task_spawn_cmd("uorb_test_queue",
@@ -1006,12 +1006,12 @@ int uORBTest::UnitTest::test_queue_poll_notify()
fds[0].fd = sfd;
fds[0].events = POLLIN;
while (!_thread_should_exit) {
while (!_thread_should_exit.load()) {
int poll_ret = px4_poll(fds, 1, 500);
if (poll_ret == 0) {
if (_thread_should_exit) {
if (_thread_should_exit.load()) {
break;
}
@@ -1032,9 +1032,9 @@ int uORBTest::UnitTest::test_queue_poll_notify()
}
}
if (_num_messages_sent != next_expected_val) {
if (_num_messages_sent.load() != next_expected_val) {
return test_fail("number of sent and received messages mismatch (sent: %i, received: %i)",
_num_messages_sent, next_expected_val);
_num_messages_sent.load(), next_expected_val);
}
return test_note("PASS orb queuing (poll & notify), got %i messages", next_expected_val);
@@ -1056,7 +1056,7 @@ int uORBTest::UnitTest::latency_test(bool print)
char *const args[1] = { nullptr };
pubsubtest_print = print;
pubsubtest_passed = false;
pubsubtest_passed.store(false);
/* test pub / sub latency */
@@ -1071,7 +1071,7 @@ int uORBTest::UnitTest::latency_test(bool print)
args);
/* give the test task some data */
while (!pubsubtest_passed) {
while (!pubsubtest_passed.load()) {
++t.val;
t.timestamp = hrt_absolute_time();
@@ -1089,7 +1089,7 @@ int uORBTest::UnitTest::latency_test(bool print)
orb_unadvertise(pfd0);
return pubsubtest_res;
return pubsubtest_res.load();
}
int uORBTest::UnitTest::test_fail(const char *fmt, ...)
@@ -42,6 +42,7 @@
#include <uORB/topics/orb_test_medium.h>
#include <uORB/topics/orb_test_large.h>
#include <px4_platform_common/atomic.h>
#include <px4_platform_common/defines.h>
#include <px4_platform_common/posix.h>
#include <px4_platform_common/time.h>
@@ -87,11 +88,11 @@ private:
static int pub_test_multi2_entry(int argc, char *argv[]);
int pub_test_multi2_main();
volatile bool _thread_should_exit;
px4::atomic_bool _thread_should_exit{false};
bool pubsubtest_passed{false};
px4::atomic_bool pubsubtest_passed{false};
bool pubsubtest_print{false};
int pubsubtest_res = OK;
px4::atomic<int> pubsubtest_res{OK};
orb_advert_t _pfd[4] {}; ///< used for test_multi and test_multi_reversed
@@ -113,7 +114,7 @@ private:
static int pub_test_queue_entry(int argc, char *argv[]);
int pub_test_queue_main();
int test_queue_poll_notify();
volatile int _num_messages_sent = 0;
px4::atomic<int> _num_messages_sent{0};
int test_fail(const char *fmt, ...);
int test_note(const char *fmt, ...);