CARLA
Loading...
Searching...
No Matches
FastDDSSharedParticipant.h
Go to the documentation of this file.
1// Copyright (c) 2026 Computer Vision Center (CVC) at the Universitat Autonoma de Barcelona (UAB).
2// This work is licensed under the terms of the MIT license.
3// For a copy, see <https://opensource.org/licenses/MIT>.
4
5#pragma once
6
7#ifndef CARLA_ROS2_MIDDLEWARE_TESTING
8
9#include <fastdds/dds/domain/DomainParticipant.hpp>
10
11#include <cstdint>
12#include <mutex>
13#include <string>
14#include <unordered_map>
15
16namespace carla {
17namespace ros2 {
18
19/// Refcounted singleton DomainParticipant shared across all FastDDS
20/// publishers and subscribers in the process.
21///
22/// Using one DomainParticipant per publisher (the previous approach) caused a
23/// discovery storm on ROS2::Shutdown() when all N participants were destroyed
24/// back-to-back, triggering intermittent segfaults in remote rmw_fastrtps_cpp
25/// and rmw_cyclonedds_cpp clients. CycloneDDS already uses a
26/// process-lifetime shared participant (CycloneDDSSertype::carla_cdr_get_participant);
27/// this class mirrors that model for FastDDS.
28///
29/// Thread-safety: all methods acquire an internal mutex.
31 public:
32 /// Increment the refcount and return the shared DomainParticipant.
33 /// Creates the participant on the first call. Returns nullptr on failure.
34 static eprosima::fastdds::dds::DomainParticipant* acquire();
35
36 /// Decrement the refcount. Destroys the participant when count reaches 0.
37 /// Must be called exactly once for every successful acquire().
38 static void release();
39
40 /// Notify the singleton that one more endpoint is using type_name so that
41 /// unregister_type() is deferred until the last user releases it.
42 static void retain_type(const std::string& type_name);
43
44 /// Release one use of type_name. Calls participant->unregister_type() when
45 /// the count for that name drops to 0 (and the participant is still alive).
46 static void release_type(const std::string& type_name);
47
48 private:
50
51 static std::mutex _mutex;
52 static eprosima::fastdds::dds::DomainParticipant* _participant;
53 static uint32_t _refcount;
54 static std::unordered_map<std::string, uint32_t> _type_refcounts;
55};
56
57} // namespace ros2
58} // namespace carla
59
60#endif // !CARLA_ROS2_MIDDLEWARE_TESTING
Refcounted singleton DomainParticipant shared across all FastDDS publishers and subscribers in the pr...
static void release_type(const std::string &type_name)
Release one use of type_name.
static eprosima::fastdds::dds::DomainParticipant * _participant
static eprosima::fastdds::dds::DomainParticipant * acquire()
Increment the refcount and return the shared DomainParticipant.
static void retain_type(const std::string &type_name)
Notify the singleton that one more endpoint is using type_name so that unregister_type() is deferred ...
static std::unordered_map< std::string, uint32_t > _type_refcounts
static void release()
Decrement the refcount.
This file contains definitions of common data structures used in traffic manager.