Merge pull request #4013 from lrineau/CGAL_Core-no_need_for_Boost.Thread-GF

Remove the need for Boost.Thread (but for CGAL_Core and gcc<9.1)
This commit is contained in:
Laurent Rineau 2019-07-10 14:22:53 +02:00
commit d3b9d89596
12 changed files with 72 additions and 144 deletions

View File

@ -408,7 +408,19 @@ public:
}
public:
static auto initialize_poly_0() {
Polynomial_2 poly_0;
return poly_0;
}
static Algebraic_curve_kernel_2& get_static_instance(){
// Useless reference to a `Polynomial_2` to force the creation
// of `CORE::MemoryPool<CORE::Bigfloat>` (and related type)
// before the static thread-local instance `ack_2_instance`.
// The issue is otherwise that the memory pool is created during
// the filling of the curves cache, and then destroyed too soon,
// before the destruction of `ack_2_instance`.
CGAL_STATIC_THREAD_LOCAL_VARIABLE(Polynomial_2, poly_0, initialize_poly_0());
CGAL_USE(poly_0);
// a default constructed ack_2 instance
CGAL_STATIC_THREAD_LOCAL_VARIABLE_0(Algebraic_curve_kernel_2, ack_2_instance);
return ack_2_instance;

View File

@ -2534,7 +2534,7 @@ protected:
Polycurve_basic_traits_2;
/*! The polycurve traits (in case it has state). */
const Polycurve_basic_traits_2& m_poly_traits;
const Polycurve_basic_traits_2 m_poly_traits;
const Point_2& m_point;

View File

@ -38,7 +38,7 @@
#include <CGAL/config.h>
#include <CGAL/tss.h>
#include <boost/config.hpp>
#if CGAL_STATIC_THREAD_LOCAL_USE_BOOST || (defined(CGAL_HAS_THREADS) && BOOST_GCC)
#if defined(CGAL_HAS_THREADS) && defined(BOOST_GCC) && BOOST_GCC < 90100
// Force the use of Boost.Thread with g++ and C++11, because of the PR66944
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66944
// See also CGAL PR #1888
@ -84,6 +84,10 @@ public:
::operator delete(blocks[i]);
}
}
// un-commenting the following line can help reproduce on Linux the
// assertion !blocks.empty() that is sometimes triggered with MSVC
// or AppleClang
// blocks.clear();
}
@ -92,10 +96,14 @@ public:
// Access the corresponding static global allocator.
static MemoryPool<T,nObjects>& global_allocator() {
#if CGAL_STATIC_THREAD_LOCAL_USE_BOOST || (defined(CGAL_HAS_THREADS) && BOOST_GCC)
#if defined(CGAL_HAS_THREADS) && defined(BOOST_GCC) && BOOST_GCC < 90100
if(memPool_ptr.get() == nullptr) {memPool_ptr.reset(new Self());}
Self& memPool = * memPool_ptr.get();
#endif
#elif defined(CGAL_HAS_THREADS) // use the C++11 implementation
static thread_local Self memPool;
#else // not CGAL_HAS_THREADS
static Self memPool;
#endif // not CGAL_HAS_THREADS
return memPool;
}
@ -103,25 +111,15 @@ private:
Thunk* head; // next available block in the pool
std::vector<void*> blocks;
#if CGAL_STATIC_THREAD_LOCAL_USE_BOOST || (defined(CGAL_HAS_THREADS) && BOOST_GCC)
#if defined(CGAL_HAS_THREADS) && defined(BOOST_GCC) && BOOST_GCC < 90100
static boost::thread_specific_ptr<Self> memPool_ptr;
#elif defined(CGAL_HAS_THREADS) // use the C++11 implementation
static thread_local Self memPool;
#else // not CGAL_HAS_THREADS
static Self memPool;
#endif // not CGAL_HAS_THREADS
};
#if CGAL_STATIC_THREAD_LOCAL_USE_BOOST || (defined(CGAL_HAS_THREADS) && BOOST_GCC)
#if defined(CGAL_HAS_THREADS) && defined(BOOST_GCC) && BOOST_GCC < 90100
template <class T, int nObjects >
boost::thread_specific_ptr<MemoryPool<T, nObjects> >
MemoryPool<T, nObjects>::memPool_ptr;
#else // use C++11 or without CGAL_HAS_THREADS
template <class T, int nObjects >
# ifdef CGAL_HAS_THREADS
thread_local
# endif
MemoryPool<T, nObjects> MemoryPool<T, nObjects>::memPool;
#endif
template< class T, int nObjects >

View File

@ -295,7 +295,7 @@ We next list the libraries and essential 3rd party software
| Library | CMake Variable | Functionality | Dependencies |
| :-------- | :------------- | :------------ | :----------- |
| `%CGAL` | none | Main library | \sc{Gmp}, \sc{Mpfr}, \sc{Boost} (headers), Boost.Thread and Boost.System (library) for compilers not supporting the keywords `thread_local` and the class `std::mutex` |
| `%CGAL` | none | Main library | \sc{Gmp}, \sc{Mpfr}, \sc{Boost} (headers) |
| `CGAL_Core` | `WITH_CGAL_Core` | The CORE library for algebraic numbers.\cgalFootnote{CGAL_Core is not part of \cgal, but a custom version of the \sc{Core} library distributed by \cgal for the user convenience and it has it's own license.} | \sc{Gmp} and \sc{Mpfr} |
| `CGAL_ImageIO` | `WITH_CGAL_ImageIO` | Utilities to read and write image files | \sc{zlib}, \sc{Vtk}(optional) |
| `CGAL_Qt5` | `WITH_CGAL_Qt5` | `QGraphicsView` support for \sc{Qt}5-based demos | \sc{Qt}5 |
@ -388,27 +388,16 @@ The \sc{Boost} libraries are a set of portable C++ source libraries. Most of
\sc{Boost} libraries are header-only, but a few of them need to be compiled or
installed as binaries.
\cgal requires the \sc{Boost} libraries. In particular the header files
and the threading library (`Boost.Thread` and
`Boost.System` binaries). Version 1.48 (or higher) are needed
for compilers not supporting the keyword `thread_local` and the class `std::mutex` (This is supported for \gcc 4.8 and later when using the \cpp 11 standard, and for Visual C++ starting with 2015, that is VC14).
\cgal only requires the headers of the \sc{Boost} libraries, but some demos and examples depend on the binary library `Boost.Program_options`.
As an exception, because of a bug in the \gcc compiler about the \cpp 11
keyword `thread_local`, the `CGAL_Core` library always requires
`Boost.Thread` if the \gcc compiler is used.
On Windows, as auto-linking is used, you also need the binaries of
`Boost.Serialization` and `Boost.DateTime`, but the
dependency is artificial and used only at link-time: the \cgal libraries do
not depend on the DLL's of those two libraries.
In \cgal some demos and examples depend on `Boost.Program_options`.
the binary library `Boost.Thread` if the \gcc compiler version 9.0 or
earlier is used.
In case the \sc{Boost} libraries are not installed on your system already, you
can obtain them from <A HREF="https://www.boost.org/">`https://www.boost.org/`</A>. For Visual C++ you can download precompiled libraries
from <A HREF="https://sourceforge.net/projects/boost/files/boost-binaries/">`https://sourceforge.net/projects/boost/files/boost-binaries/`</A>.
For Visual C++ versions prior to 2015 `Boost.Thread` is required, so make sure to either install the precompiled
libraries for your compiler or build `libboost-thread` and `libboost-system`.
As on Windows there is no canonical directory for where to find
\sc{Boost}, we recommend that you define the environment variable

View File

@ -31,7 +31,6 @@ CGAL packages, some are only needed for demos.
* Boost (>= 1.48)
Required for building CGAL and for applications using CGAL
Required compiled Boost library: Boost.Thread, Boost.System
Optional compiled Boost library: Boost.Program_options
http://www.boost.org/ or http://www.boostpro.com/products/free/
You need the former if you plan to compile the boost libraries yourself,

View File

@ -12,6 +12,9 @@ get_filename_component(CGAL_CONFIG_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
set(CGAL_HEADER_ONLY "@CGAL_HEADER_ONLY@" )
include(CMakeFindDependencyMacro)
find_dependency(Boost REQUIRED)
# The code for including exported targets is different from
# CGAL_Config_install.cmake. We do not have separate export files in
# an installed version and we need to make sure that we are not
@ -114,15 +117,6 @@ macro(check_cgal_component COMPONENT)
if ( "${CGAL_LIB}" STREQUAL "CGAL" )
set( CGAL_FOUND TRUE )
set( CHECK_CGAL_ERROR_TAIL "" )
get_property(CGAL_CGAL_is_imported TARGET CGAL::CGAL PROPERTY IMPORTED)
if(CGAL_CGAL_is_imported)
include("${CGAL_MODULES_DIR}/CGAL_SetupBoost.cmake")
get_property(CGAL_requires_Boost_libs
GLOBAL PROPERTY CGAL_requires_Boost_Thread)
if(CGAL_requires_Boost_libs AND TARGET Boost::thread)
set_property(TARGET CGAL::CGAL APPEND PROPERTY INTERFACE_LINK_LIBRARIES Boost::thread)
endif()
endif()
else( "${CGAL_LIB}" STREQUAL "CGAL" )
if ( WITH_${CGAL_LIB} )
if(TARGET CGAL::${CGAL_LIB})

View File

@ -12,6 +12,9 @@ get_filename_component(CGAL_CONFIG_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
set(CGAL_HEADER_ONLY "@CGAL_HEADER_ONLY@" )
include(CMakeFindDependencyMacro)
find_dependency(Boost REQUIRED)
# CGAL_DIR is the directory where this CGALConfig.cmake is installed
string(REPLACE "/@CGAL_INSTALL_CMAKE_DIR@" "" CGAL_INSTALL_PREFIX "${CGAL_CONFIG_DIR}")

View File

@ -290,13 +290,6 @@ if( NOT CGAL_MACROS_FILE_INCLUDED )
# To deal with imported targets of Qt5 and Boost, when CGAL
# targets are themselves imported by another project.
if(NOT CGAL_BUILDING_LIBS)
if (NOT MSVC AND "${component}" STREQUAL "Core")
# See the release notes of CGAL-4.10: CGAL_Core now requires
# Boost.Thread, with all compilers but MSVC.
find_package( Boost 1.48 REQUIRED thread system )
add_to_list( CGAL_3RD_PARTY_LIBRARIES ${Boost_LIBRARIES} )
endif()
if (${component} STREQUAL "Qt5")
find_package(Qt5 COMPONENTS OpenGL Gui Core Script ScriptTools QUIET)
endif()

View File

@ -17,38 +17,7 @@ set ( CGAL_Boost_Setup TRUE )
include(${CMAKE_CURRENT_LIST_DIR}/CGAL_TweakFindBoost.cmake)
function(CGAL_detect_if_Boost_Thread_is_required)
get_property(PROPERTY_CGAL_requires_Boost_Thread_IS_SET
GLOBAL PROPERTY CGAL_requires_Boost_Thread SET)
if(PROPERTY_CGAL_requires_Boost_Thread_IS_SET)
get_property(CGAL_requires_Boost_libs
GLOBAL PROPERTY CGAL_requires_Boost_Thread)
else()
set ( CGAL_requires_Boost_libs TRUE )
if ( DEFINED MSVC_VERSION AND "${MSVC_VERSION}" GREATER 1800)
set ( CGAL_requires_Boost_libs FALSE )
else()
try_run( CGAL_test_cpp_version_RUN_RES CGAL_test_cpp_version_COMPILE_RES
"${CMAKE_BINARY_DIR}"
"${CGAL_MODULES_DIR}/config/support/CGAL_test_cpp_version.cpp"
RUN_OUTPUT_VARIABLE CGAL_cplusplus)
message(STATUS "__cplusplus is ${CGAL_cplusplus}")
if(NOT CGAL_test_cpp_version_RUN_RES)
set ( CGAL_requires_Boost_libs FALSE )
message(STATUS " --> Do not link with Boost.Thread")
endif()
endif()
endif()
set_property(GLOBAL PROPERTY CGAL_requires_Boost_Thread ${CGAL_requires_Boost_libs})
set(CGAL_requires_Boost_libs ${CGAL_requires_Boost_libs} PARENT_SCOPE)
endfunction(CGAL_detect_if_Boost_Thread_is_required)
CGAL_detect_if_Boost_Thread_is_required()
if (CGAL_requires_Boost_libs)
find_package( Boost 1.48 REQUIRED thread system )
else()
find_package( Boost 1.48 REQUIRED )
endif()
find_package( Boost 1.48 REQUIRED )
if(Boost_FOUND AND Boost_VERSION VERSION_LESS 1.70)
if(DEFINED Boost_DIR AND NOT Boost_DIR)
@ -94,9 +63,6 @@ function(use_CGAL_Boost_support target)
endif()
if(TARGET Boost::boost)
target_link_libraries(${target} ${keyword} Boost::boost)
if (CGAL_requires_Boost_libs)
target_link_libraries(${target} ${keyword} Boost::thread)
endif()
else()
target_include_directories(${target} SYSTEM ${keyword} ${Boost_INCLUDE_DIRS})
target_link_libraries(${target} ${keyword} ${Boost_LIBRARIES})

View File

@ -54,8 +54,8 @@ endif()
#
# See the release notes of CGAL-4.10: CGAL_Core now requires
# Boost.Thread, with GNU G++.
if (CMAKE_CXX_COMPILER_ID STREQUAL GNU)
# Boost.Thread, with GNU G++ < 9.1.
if (CMAKE_CXX_COMPILER_ID STREQUAL GNU AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9.1)
include(${CMAKE_CURRENT_LIST_DIR}/CGAL_TweakFindBoost.cmake)
find_package( Boost 1.48 REQUIRED COMPONENTS thread system )
endif()
@ -73,7 +73,7 @@ function(CGAL_setup_CGAL_Core_dependencies target)
# See the release notes of CGAL-4.10: CGAL_Core now requires
# Boost.Thread, with GNU G++.
if (CMAKE_CXX_COMPILER_ID STREQUAL GNU)
if (CMAKE_CXX_COMPILER_ID STREQUAL GNU AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9.1)
if(TARGET Boost::thread)
target_link_libraries( CGAL_Core ${keyword} Boost::thread)
else()

View File

@ -22,46 +22,6 @@
#include <CGAL/config.h>
#if defined( CGAL_HAS_THREADS )
# ifdef CGAL_CAN_USE_CXX11_THREAD_LOCAL
//# pragma message ( "Use keyword thread_local" )
# else
//# pragma message ("Use thread_local from boost")
# define CGAL_USE_BOOST_THREAD
# include <boost/thread/tss.hpp>
# endif
# ifdef CGAL_USE_BOOST_THREAD
# define CGAL_STATIC_THREAD_LOCAL_USE_BOOST 1
# define CGAL_STATIC_THREAD_LOCAL_VARIABLE_0(TYPE, VAR) \
static boost::thread_specific_ptr<TYPE> VAR##_ptr; \
if(VAR##_ptr.get() == nullptr) {VAR##_ptr.reset(new TYPE());} \
TYPE& VAR = * VAR##_ptr.get()
# define CGAL_STATIC_THREAD_LOCAL_VARIABLE(TYPE, VAR, ARG1) \
static boost::thread_specific_ptr<TYPE> VAR##_ptr; \
if(VAR##_ptr.get() == nullptr) {VAR##_ptr.reset(new TYPE(ARG1));} \
TYPE& VAR = * VAR##_ptr.get()
# define CGAL_STATIC_THREAD_LOCAL_VARIABLE_2(TYPE, VAR, ARG1, ARG2) \
static boost::thread_specific_ptr<TYPE> VAR##_ptr; \
if(VAR##_ptr.get() == nullptr) {VAR##_ptr.reset(new TYPE(ARG1,ARG2));} \
TYPE& VAR = * VAR##_ptr.get()
# define CGAL_STATIC_THREAD_LOCAL_VARIABLE_3(TYPE, VAR, ARG1, ARG2, ARG3) \
static boost::thread_specific_ptr<TYPE> VAR##_ptr; \
if(VAR##_ptr.get() == nullptr) {VAR##_ptr.reset(new TYPE(ARG1,ARG2,ARG3));} \
TYPE& VAR = * VAR##_ptr.get()
# define CGAL_STATIC_THREAD_LOCAL_VARIABLE_4(TYPE, VAR, ARG1, ARG2, ARG3, ARG4) \
static boost::thread_specific_ptr<TYPE> VAR##_ptr; \
if(VAR##_ptr.get() == nullptr) {VAR##_ptr.reset(new TYPE(ARG1,ARG2,ARG3,ARG4));} \
TYPE& VAR = * VAR##_ptr.get()
# else // not CGAL_USE_BOOST_THREAD, -> use C++11 thread_local
# define CGAL_STATIC_THREAD_LOCAL_VARIABLE_0(TYPE, VAR) \
static thread_local TYPE VAR
@ -78,8 +38,6 @@
# define CGAL_STATIC_THREAD_LOCAL_VARIABLE_4(TYPE, VAR, ARG1, ARG2, ARG3, ARG4) \
static thread_local TYPE VAR(ARG1,ARG2,ARG3,ARG4)
# endif // not CGAL_USE_BOOST_THREAD
#else // not CGAL_HAS_THREADS
# define CGAL_STATIC_THREAD_LOCAL_VARIABLE_0(TYPE, VAR) static TYPE VAR

View File

@ -59,15 +59,8 @@ private:
Word _wrd;
static const Matrix& gmap(const std::string& s)
{
typedef std::map<std::string, Matrix> M;
CGAL_STATIC_THREAD_LOCAL_VARIABLE_0(M, m);
if(m.empty()){
static auto initialize_gmap() {
std::map<std::string, Matrix> m;
std::vector<Matrix> g;
Matrix::generators(g);
@ -128,7 +121,30 @@ private:
m["7"] = g[D];
m["72"] = g[D]*g[C];
m["725"] = g[D]*g[C]*g[B];
{ // This block abuses `operator<<` of numbers, to a null stream.
// That ensures that the following memory pool are correctly
// initialized:
// - `CORE::MemoryPool<CORE::Realbase_for<long, 1024>`
// - `CORE::MemoryPool<CORE::Realbase_for<double, 1024>`
// - `CORE::MemoryPool<CORE::BigFloatRep, 1024>`
// - `CORE::MemoryPool<CORE::BigIntRep, 1024>`
// otherwise, there is an assertion during the destruction of
// static (or `thread_local`) objects
struct NullBuffer : public std::streambuf {
int overflow(int c) { return c; }
};
NullBuffer null_buffer;
std::ostream null_stream(&null_buffer);
for(auto& pair: m) null_stream << pair.second;
}
return m;
}
static const Matrix& gmap(const std::string& s)
{
typedef std::map<std::string, Matrix> M;
CGAL_STATIC_THREAD_LOCAL_VARIABLE(M, m, initialize_gmap());
return m[s];
}