mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-07-06 01:30:35 +08:00
add fast-path mavlink forwarding for single instance
Counts active mavlink instances atomically when instances are claimed or released and uses that value to early-exit the forwarding logic. It means on single-instance scenarios this will skip taking the mavlink_module_mutex lock and will not iterate over mavlink_module_instances on every received message. Signed-off-by: Onur Özkan <work@onurozkan.dev>
This commit is contained in:
@@ -54,6 +54,7 @@
|
||||
#include <lib/systemlib/mavlink_log.h>
|
||||
#include <lib/version/version.h>
|
||||
|
||||
#include <px4_platform_common/atomic.h>
|
||||
#include <px4_platform_common/events.h>
|
||||
|
||||
#include <uORB/topics/event.h>
|
||||
@@ -82,6 +83,8 @@
|
||||
|
||||
static pthread_mutex_t mavlink_module_mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
static pthread_mutex_t mavlink_event_buffer_mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
static px4::atomic<int> mavlink_instance_count {0};
|
||||
|
||||
events::EventBuffer *Mavlink::_event_buffer = nullptr;
|
||||
|
||||
Mavlink *mavlink_module_instances[MAVLINK_COMM_NUM_BUFFERS] {};
|
||||
@@ -161,6 +164,7 @@ Mavlink::~Mavlink()
|
||||
|
||||
if (_instance_id >= 0) {
|
||||
mavlink_module_instances[_instance_id] = nullptr;
|
||||
mavlink_instance_count.fetch_sub(1);
|
||||
}
|
||||
|
||||
// if this instance was responsible for checking events then select a new mavlink instance
|
||||
@@ -265,6 +269,7 @@ Mavlink::set_instance_id()
|
||||
if (mavlink_module_instances[instance_id] == nullptr) {
|
||||
mavlink_module_instances[instance_id] = this;
|
||||
_instance_id = instance_id;
|
||||
mavlink_instance_count.fetch_add(1);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -463,6 +468,11 @@ Mavlink::forward_message(const mavlink_message_t *msg, Mavlink *self)
|
||||
}
|
||||
}
|
||||
|
||||
// Avoid locking/iteration when there is no instance to forward to.
|
||||
if (mavlink_instance_count.load() <= 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
// If it's a message only for us, we keep it
|
||||
if (target_system_id == self->get_system_id() && target_component_id == self->get_component_id()) {
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user