From 96c180318de6f931925458f4ebce0b685ccb340c Mon Sep 17 00:00:00 2001 From: Miguel Company Date: Fri, 17 Dec 2021 07:07:51 +0100 Subject: [PATCH] Ensure correct boost singleton destruction order (#2356) * Refs 13287. Ensure correct boost singleton destruction order. Signed-off-by: Miguel Company * Refs 13288. Uncrustify. Signed-off-by: Miguel Company * Refs 13288. Fixed typo. Signed-off-by: Miguel Company * Refs 13288. Fixed link error on ListenerTests. Signed-off-by: Miguel Company (cherry picked from commit 80a44fe3435cdecbe9f5c7cf7144733d0d3a5045) # Conflicts: # test/unittest/dds/status/CMakeLists.txt --- .../domain/DomainParticipantFactory.cpp | 26 ++++++- test/unittest/dds/status/CMakeLists.txt | 73 +++++++++++++++++++ 2 files changed, 97 insertions(+), 2 deletions(-) diff --git a/src/cpp/fastdds/domain/DomainParticipantFactory.cpp b/src/cpp/fastdds/domain/DomainParticipantFactory.cpp index 15b81da502b..07705579b22 100644 --- a/src/cpp/fastdds/domain/DomainParticipantFactory.cpp +++ b/src/cpp/fastdds/domain/DomainParticipantFactory.cpp @@ -32,8 +32,9 @@ #include - - +// We include boost through this internal header, to ensure we use our custom boost config file +#include +#include using namespace eprosima::fastrtps::xmlparser; @@ -100,6 +101,27 @@ DomainParticipantFactory::~DomainParticipantFactory() DomainParticipantFactory* DomainParticipantFactory::get_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; + // Keep a reference to the topic payload pool to avoid it to be destroyed before our own instance using pool_registry_ref = eprosima::fastrtps::rtps::TopicPayloadPoolRegistry::reference; static pool_registry_ref topic_pool_registry = eprosima::fastrtps::rtps::TopicPayloadPoolRegistry::instance(); diff --git a/test/unittest/dds/status/CMakeLists.txt b/test/unittest/dds/status/CMakeLists.txt index 714803d32ce..ea8ad89a4ae 100644 --- a/test/unittest/dds/status/CMakeLists.txt +++ b/test/unittest/dds/status/CMakeLists.txt @@ -171,3 +171,76 @@ if(NOT ((MSVC OR MSVC_IDE) AND EPROSIMA_INSTALLER)) endif() endif() +<<<<<<< HEAD +======= + +# External sources +if(TINYXML2_SOURCE_DIR) + list(APPEND LISTENERTESTS_SOURCE + ${TINYXML2_SOURCE_DIR}/tinyxml2.cpp + ) +endif() + +include_directories(${TINYXML2_INCLUDE_DIR}) + +if(WIN32) + add_definitions(-D_WIN32_WINNT=0x0601) +endif() + +add_executable(ListenerTests ${LISTENERTESTS_SOURCE}) +target_compile_definitions(ListenerTests PRIVATE FASTRTPS_NO_LIB + BOOST_ASIO_STANDALONE + ASIO_STANDALONE + ASIO_DISABLE_VISIBILITY + SQLITE_WIN32_GETVERSIONEX=0 + $<$,$>,$>>:ASIO_DISABLE_STD_STRING_VIEW> + $<$>,$>:__DEBUG> + $<$:__INTERNALDEBUG> # Internal debug activated. + $<$:_ENABLE_ATOMIC_ALIGNMENT_FIX> + ) +target_include_directories(ListenerTests PRIVATE + ${Asio_INCLUDE_DIR} + ${PROJECT_SOURCE_DIR}/test/mock/rtps/DataSharingPayloadPool + ${PROJECT_SOURCE_DIR}/test/mock/rtps/RTPSReader + ${PROJECT_SOURCE_DIR}/test/mock/rtps/RTPSWriter + ${PROJECT_SOURCE_DIR}/test/mock/rtps/RTPSDomain + ${PROJECT_SOURCE_DIR}/test/mock/rtps/RTPSDomainImpl + ${PROJECT_SOURCE_DIR}/test/mock/rtps/RTPSParticipant + ${PROJECT_SOURCE_DIR}/test/mock/rtps/RTPSParticipantImpl + ${PROJECT_SOURCE_DIR}/test/mock/rtps/Endpoint + ${PROJECT_SOURCE_DIR}/test/mock/rtps/PDP + ${PROJECT_SOURCE_DIR}/test/mock/rtps/PDPSimple + ${PROJECT_SOURCE_DIR}/test/mock/rtps/StatefulWriter + ${PROJECT_SOURCE_DIR}/test/mock/rtps/StatelessWriter + ${PROJECT_SOURCE_DIR}/test/mock/rtps/StatefulReader + ${PROJECT_SOURCE_DIR}/test/mock/rtps/StatelessReader + ${PROJECT_SOURCE_DIR}/test/mock/rtps/WriterHistory + ${PROJECT_SOURCE_DIR}/test/mock/rtps/ReaderHistory + ${PROJECT_SOURCE_DIR}/test/mock/rtps/ResourceEvent + ${PROJECT_SOURCE_DIR}/test/mock/rtps/TimedEvent + ${PROJECT_SOURCE_DIR}/test/mock/rtps/UDPTransportDescriptor + ${PROJECT_SOURCE_DIR}/test/mock/rtps/UDPv4TransportDescriptor + ${PROJECT_SOURCE_DIR}/test/mock/rtps/UDPv6TransportDescriptor + ${PROJECT_SOURCE_DIR}/test/mock/rtps/TCPTransportDescriptor + ${PROJECT_SOURCE_DIR}/test/mock/rtps/TCPv4TransportDescriptor + ${PROJECT_SOURCE_DIR}/test/mock/rtps/TCPv6TransportDescriptor + ${PROJECT_SOURCE_DIR}/test/mock/rtps/SharedMemTransportDescriptor + ${PROJECT_SOURCE_DIR}/test/mock/rtps/TypeLookupManager + ${PROJECT_SOURCE_DIR}/test/mock/rtps/WLP + ${PROJECT_SOURCE_DIR}/include ${PROJECT_BINARY_DIR}/include + ${PROJECT_SOURCE_DIR}/src/cpp + ${THIRDPARTY_BOOST_INCLUDE_DIR} + ) + +target_link_libraries(ListenerTests fastcdr foonathan_memory + ${TINYXML2_LIBRARY} + GTest::gmock + ${CMAKE_DL_LIBS} + ${THIRDPARTY_BOOST_LINK_LIBS} + ) +if(MSVC OR MSVC_IDE) + target_link_libraries(ListenerTests iphlpapi Shlwapi ws2_32) +endif() + +add_gtest(ListenerTests SOURCES ${LISTENERTESTS_SOURCE}) +>>>>>>> 80a44fe34 (Ensure correct boost singleton destruction order (#2356))