Skip to content

Commit

Permalink
Ensure correct boost singleton destruction order (#2358)
Browse files Browse the repository at this point in the history
* Ensure correct boost singleton destruction order (#2356)

* Refs 13287. Ensure correct boost singleton destruction order.

Signed-off-by: Miguel Company <MiguelCompany@eprosima.com>

* Refs 13288. Uncrustify.

Signed-off-by: Miguel Company <MiguelCompany@eprosima.com>

* Refs 13288. Fixed typo.

Signed-off-by: Miguel Company <MiguelCompany@eprosima.com>

* Refs 13288. Fixed link error on ListenerTests.

Signed-off-by: Miguel Company <MiguelCompany@eprosima.com>
(cherry picked from commit 80a44fe)

# Conflicts:
#	test/unittest/dds/status/CMakeLists.txt

* Refs 13287. Fix conflicts.

Signed-off-by: Miguel Company <MiguelCompany@eprosima.com>

Co-authored-by: Miguel Company <miguelcompany@eprosima.com>
  • Loading branch information
mergify[bot] and MiguelCompany authored Jan 21, 2022
1 parent b5f7fbf commit dc95ea1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
26 changes: 24 additions & 2 deletions src/cpp/fastdds/domain/DomainParticipantFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@
#include <statistics/fastdds/domain/DomainParticipantImpl.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;

Expand Down Expand Up @@ -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();
Expand Down
8 changes: 7 additions & 1 deletion test/unittest/dds/status/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,12 @@ if(NOT ((MSVC OR MSVC_IDE) AND EPROSIMA_INSTALLER))
target_compile_definitions(ListenerTests PRIVATE FASTRTPS_NO_LIB
BOOST_ASIO_STANDALONE
ASIO_STANDALONE
ASIO_DISABLE_VISIBILITY
SQLITE_WIN32_GETVERSIONEX=0
$<$<AND:$<BOOL:${ANDROID}>,$<NOT:$<BOOL:${HAVE_CXX14}>>,$<NOT:$<BOOL:${HAVE_CXX1Y}>>>:ASIO_DISABLE_STD_STRING_VIEW>
$<$<AND:$<NOT:$<BOOL:${WIN32}>>,$<STREQUAL:"${CMAKE_BUILD_TYPE}","Debug">>:__DEBUG>
$<$<BOOL:${INTERNAL_DEBUG}>:__INTERNALDEBUG> # Internal debug activated.
$<$<BOOL:${WIN32}>:_ENABLE_ATOMIC_ALIGNMENT_FIX>
)
target_include_directories(ListenerTests PRIVATE
${Asio_INCLUDE_DIR}
Expand Down Expand Up @@ -157,12 +160,15 @@ if(NOT ((MSVC OR MSVC_IDE) AND EPROSIMA_INSTALLER))
${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})
${CMAKE_DL_LIBS}
${THIRDPARTY_BOOST_LINK_LIBS}
)
if(MSVC OR MSVC_IDE)
target_link_libraries(ListenerTests iphlpapi Shlwapi ws2_32)
endif()
Expand Down

0 comments on commit dc95ea1

Please sign in to comment.