Skip to content

Instantly share code, notes, and snippets.

@devbharat
Created February 8, 2024 09:16
Show Gist options
  • Save devbharat/17a11229bd7df818f9a7fd65828f4ddd to your computer and use it in GitHub Desktop.
Save devbharat/17a11229bd7df818f9a7fd65828f4ddd to your computer and use it in GitHub Desktop.
mavlink callback manager comparison
void MavlinkMessageHandler::process_message(const mavlink_message_t& message)
{
std::lock_guard<std::mutex> lock(_mutex);
#if MESSAGE_DEBUGGING == 1
bool forwarded = false;
#endif
for (auto& entry : _table) {
if (entry.msg_id == message.msgid &&
(!entry.cmp_id.has_value() || entry.cmp_id == message.compid)) {
#if MESSAGE_DEBUGGING == 1
LogDebug() << "Forwarding msg " << int(message.msgid) << " to "
<< size_t(entry->cookie);
forwarded = true;
#endif
entry.callback(message);
}
}
#if MESSAGE_DEBUGGING == 1
if (!forwarded) {
LogDebug() << "Ignoring msg " << int(message.msgid);
}
#endif
}
vs
// Execute all callbacks for a specific message ID
void notify(MessageID msgid, Args... args)
{
CallbackList<Args...>* callbackListPtr = nullptr;
{
std::lock_guard<std::mutex> lock(_mutex);
auto it = _callbacks.find(msgid);
if (it != _callbacks.end()) {
callbackListPtr = &(it->second);
}
} // Unlock the map to avoid holding the mutex while executing callbacks
// If we have a valid CallbackList pointer, execute the callbacks
if (callbackListPtr) {
(*callbackListPtr)(args...);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment