Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[16647] Use shared_ptr for internal singletons #3185

Merged
merged 31 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
b960827
Refs #16647. Moved RTPSDomain::getNewId to source file.
MiguelCompany Jan 3, 2023
7561376
Refs #16647. Moved RTPSDomain implementation to RTPSDomainImpl.
MiguelCompany Jan 3, 2023
9770438
Refs #16647. Using shared_ptr for singleton data.
MiguelCompany Jan 3, 2023
49d4528
Refs #16647. DomainParticipantFactory keeps a reference to RTPSDomain…
MiguelCompany Jan 3, 2023
ad1fee9
Refs #16647. Use method to access Log resources.
MiguelCompany Jan 3, 2023
f93e587
Refs #16647. Refactor Log KillThread.
MiguelCompany Jan 3, 2023
7f456d3
Refs #16647. Using shared_ptr for Log Resouces.
MiguelCompany Jan 3, 2023
3b069bb
Refs #16647. Rename struct into LogResources.
MiguelCompany Jan 3, 2023
d6397a1
Refs #16647. Rename instance getter.
MiguelCompany Jan 3, 2023
5b8b875
Refs #16647. Make instance getter available on private header.
MiguelCompany Jan 3, 2023
94c8553
Refs #16647. DomainParticipantFactory keeps a reference to LogResources.
MiguelCompany Jan 4, 2023
786c2f6
Refs #16647. Removed unused method for_each_participant.
MiguelCompany Jan 4, 2023
a067bea
Refs #16647. Add instance getter to RTPSDomainImpl mock.
MiguelCompany Jan 4, 2023
5d0835d
Refs #16647. Moved mock for clientServerEnvironmentCreationOverride.
MiguelCompany Jan 4, 2023
a9e96a0
Refs #16647. Allow injection of custom exit work registration.
MiguelCompany Jan 5, 2023
dfc268c
Refs #16647. New helper singleton for registering atexit functions.
MiguelCompany Jan 5, 2023
e6a0c70
Refs #16647. Using new helper for registering boost at_exit_work.
MiguelCompany Jan 5, 2023
2bda4ac
Refs #16647. SharedSegmentBase keeps reference to BoostAtExitRegistry
MiguelCompany Jan 5, 2023
bc45757
Refs #16647. RobustInterprocessCondition keeps reference to BoostAtEx…
MiguelCompany Jan 5, 2023
2d07899
Refs #16647. Removed AuxiliaryBoostFunctor from DomainParticipantFact…
MiguelCompany Jan 5, 2023
603b666
Refs #16647. Add src/cpp to include directories when using try_compile
MiguelCompany Jan 5, 2023
67a32c0
Refs #16647. Keep raw reference to resources on Log::Run.
MiguelCompany Jan 12, 2023
fd1ad93
Refs #16647. Safety checks on LogResources::KillThread.
MiguelCompany Jan 12, 2023
cfbdfc3
Refs #16647. Kill log thread on test shutdown.
MiguelCompany Jan 13, 2023
2e2c6dc
Refs #16647. Moved Log::preprocess to LogResources.
MiguelCompany Jan 13, 2023
f24a144
Refs #16647. Moved Log::run to LogResources.
MiguelCompany Jan 13, 2023
20f52ef
Refs #16647. Moved Log implementation to LogResources.
MiguelCompany Jan 13, 2023
21b4ead
Refs #16647. Flush log on destruction of LogResources before killing …
MiguelCompany Jan 13, 2023
e406b53
Refs #16647. LogResources moved into detail namespace.
MiguelCompany Jan 13, 2023
8261245
Refs #16647. LogResources attributes made private.
MiguelCompany Jan 13, 2023
69f3834
Refs #16647. BoostAtExitRegistry allows registering a function during…
MiguelCompany Jan 16, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmake/modules/FindThirdpartyBoost.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ endif()
try_compile(IS_THIRDPARTY_BOOST_OK
${CMAKE_BINARY_DIR}
${PROJECT_SOURCE_DIR}/thirdparty/boost/test/ThirdpartyBoostCompile_test.cpp
CMAKE_FLAGS "-DINCLUDE_DIRECTORIES=${THIRDPARTY_BOOST_INCLUDE_DIR}"
CMAKE_FLAGS "-DINCLUDE_DIRECTORIES=${THIRDPARTY_BOOST_INCLUDE_DIR};${PROJECT_SOURCE_DIR}/src/cpp"
CXX_STANDARD 11
LINK_LIBRARIES ${THIRDPARTY_BOOST_LINK_LIBS}
OUTPUT_VARIABLE OUT
Expand Down
11 changes: 11 additions & 0 deletions include/fastdds/dds/domain/DomainParticipantFactory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ namespace eprosima {

namespace fastrtps {
namespace rtps {

class RTPSDomainImpl;

namespace detail {
class TopicPayloadPoolRegistry;
} // namespace detail
Expand All @@ -49,6 +52,10 @@ class DomainParticipantListener;
class DomainParticipant;
class DomainParticipantImpl;

namespace detail {
struct LogResources;
} // namespace detail

/**
* Class DomainParticipantFactory
*
Expand Down Expand Up @@ -303,6 +310,10 @@ class DomainParticipantFactory
DomainParticipantQos default_participant_qos_;

std::shared_ptr<fastrtps::rtps::detail::TopicPayloadPoolRegistry> topic_pool_;

std::shared_ptr<fastrtps::rtps::RTPSDomainImpl> rtps_domain_;

std::shared_ptr<detail::LogResources> log_resources_;
};

} // namespace dds
Expand Down
10 changes: 0 additions & 10 deletions include/fastdds/dds/log/Log.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,16 +172,6 @@ class Log
const std::string& message,
const Log::Context&,
Log::Kind);

private:

// Applies transformations to the entries compliant with the options selected (such as
// erasure of certain context information, or filtering by category. Returns false
// if the log entry is blacklisted.
static bool preprocess(
Entry&);

static void run();
};

/**
Expand Down
52 changes: 0 additions & 52 deletions include/fastdds/rtps/RTPSDomain.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ class RTPSDomainImpl;
*/
class RTPSDomain
{

friend class RTPSDomainImpl;

public:

/**
Expand Down Expand Up @@ -281,63 +278,14 @@ class RTPSDomain
RTPS_DllAPI static bool removeRTPSParticipant(
RTPSParticipant* p);

/**
* Set the maximum RTPSParticipantID.
* @param maxRTPSParticipantId ID.
*/
static inline void setMaxRTPSParticipantId(
uint32_t maxRTPSParticipantId)
{
m_maxRTPSParticipantID = maxRTPSParticipantId;
}

/**
* Creates a RTPSParticipant as default server or client if ROS_MASTER_URI environment variable is set.
* @param domain_id DDS domain associated
* @param enabled True if the RTPSParticipant should be enabled on creation. False if it will be enabled later with RTPSParticipant::enable()
* @param attrs RTPSParticipant Attributes.
* @param listen Pointer to the ParticipantListener.
* @return Pointer to the RTPSParticipant.
*
* \warning The returned pointer is invalidated after a call to removeRTPSParticipant() or stopAll(),
* so its use may result in undefined behaviour.
*/
static RTPSParticipant* clientServerEnvironmentCreationOverride(
uint32_t domain_id,
bool enabled,
const RTPSParticipantAttributes& attrs,
RTPSParticipantListener* listen /*= nullptr*/);

private:

typedef std::pair<RTPSParticipant*, RTPSParticipantImpl*> t_p_RTPSParticipant;

RTPSDomain() = delete;

/**
* DomainRTPSParticipant destructor
*/
~RTPSDomain() = delete;

/**
* @brief Get Id to create a RTPSParticipant.
* @return Different ID for each call.
*/
static inline uint32_t getNewId()
{
return m_maxRTPSParticipantID++;
}

static void removeRTPSParticipant_nts(
t_p_RTPSParticipant&);

static std::mutex m_mutex;

static std::atomic<uint32_t> m_maxRTPSParticipantID;

static std::vector<t_p_RTPSParticipant> m_RTPSParticipants;

static std::set<uint32_t> m_RTPSParticipantIDs;
};

} // namespace rtps
Expand Down
1 change: 1 addition & 0 deletions include/fastdds/rtps/participant/RTPSParticipant.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class RTPS_DllAPI RTPSParticipant
{
friend class RTPSParticipantImpl;
friend class RTPSDomain;
friend class RTPSDomainImpl;

private:

Expand Down
29 changes: 4 additions & 25 deletions src/cpp/fastdds/domain/DomainParticipantFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,14 @@
#include <fastrtps/xmlparser/XMLProfileManager.h>
#include <fastrtps/xmlparser/XMLEndpointParser.h>

#include <fastdds/log/LogResources.hpp>
#include <fastdds/domain/DomainParticipantImpl.hpp>
#include <fastdds/utils/QosConverters.hpp>
#include <rtps/RTPSDomainImpl.hpp>
#include <rtps/history/TopicPayloadPoolRegistry.hpp>
#include <statistics/fastdds/domain/DomainParticipantImpl.hpp>
#include <utils/SystemInfo.hpp>

// We include boost through this internal header, to ensure we use our custom boost config file
#include <utils/shared_memory/SharedMemSegment.hpp>
#include <boost/interprocess/sync/interprocess_mutex.hpp>

using namespace eprosima::fastrtps::xmlparser;

using eprosima::fastrtps::ParticipantAttributes;
Expand All @@ -54,6 +52,8 @@ DomainParticipantFactory::DomainParticipantFactory()
: default_xml_profiles_loaded(false)
, default_participant_qos_(PARTICIPANT_QOS_DEFAULT)
, topic_pool_(fastrtps::rtps::TopicPayloadPoolRegistry::instance())
, rtps_domain_(fastrtps::rtps::RTPSDomainImpl::get_instance())
, log_resources_(detail::get_log_resources())
{
}

Expand Down Expand Up @@ -88,27 +88,6 @@ DomainParticipantFactory* DomainParticipantFactory::get_instance()

std::shared_ptr<DomainParticipantFactory> DomainParticipantFactory::get_shared_instance()
{
/*
* The first time an interprocess synchronization object is created by boost, a singleton is instantiated and
* its destructor is registered with std::atexit(&atexit_work).
*
* We need to ensure that the boost singleton is destroyed after the instance of DomainParticipantFactory, to
* ensure that the interprocess objects keep working until all the participants are destroyed.
*
* We achieve this behavior by having an static instance of an auxiliary struct that instantiates a synchronization
* object on the constructor, just to ensure that the boost singleton is instantiated before the
* DomainParticipantFactory.
*/
struct AuxiliaryBoostFunctor
{
AuxiliaryBoostFunctor()
{
boost::interprocess::interprocess_mutex mtx;
}

};
static AuxiliaryBoostFunctor boost_functor;

// Note we need a custom deleter, since the destructor is protected.
static std::shared_ptr<DomainParticipantFactory> instance(
new DomainParticipantFactory(),
Expand Down
3 changes: 2 additions & 1 deletion src/cpp/fastdds/domain/DomainParticipantImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ using fastrtps::TopicAttributes;
using fastrtps::SubscriberAttributes;
using fastrtps::PublisherAttributes;
using fastrtps::rtps::RTPSDomain;
using fastrtps::rtps::RTPSDomainImpl;
using fastrtps::rtps::RTPSParticipant;
using fastrtps::rtps::ParticipantDiscoveryInfo;
#if HAVE_SECURITY
Expand Down Expand Up @@ -257,7 +258,7 @@ ReturnCode_t DomainParticipantImpl::enable()

// If DEFAULT_ROS2_MASTER_URI is specified then try to create default client if
// that already exists.
RTPSParticipant* part = RTPSDomain::clientServerEnvironmentCreationOverride(
RTPSParticipant* part = RTPSDomainImpl::clientServerEnvironmentCreationOverride(
domain_id_,
false,
rtps_attr,
Expand Down
Loading