From 220651bc88c0c94dc64497ffe03bde4f585f59d3 Mon Sep 17 00:00:00 2001 From: Andreas Fabri Date: Fri, 4 Sep 2015 18:09:50 +0200 Subject: [PATCH] Use CGAL_THREAD_LOCAL as thread_local only comes with VC201 --- Filtered_kernel/include/CGAL/Lazy.h | 7 +- Installation/include/CGAL/mutex.h | 5 +- Installation/include/CGAL/tss.h | 15 ++++ .../CGAL/Modular_arithmetic/Residue_type.h | 29 ++++++-- Modular_arithmetic/src/CGAL/Residue_type.cpp | 8 ++- Number_types/include/CGAL/GMP/Gmpfi_type.h | 23 +++++- .../include/CGAL/Polynomial/Polynomial_type.h | 24 ++++--- .../include/CGAL/Delaunay_triangulation_2.h | 72 ++++++++++++++++++- 8 files changed, 154 insertions(+), 29 deletions(-) create mode 100644 Installation/include/CGAL/tss.h diff --git a/Filtered_kernel/include/CGAL/Lazy.h b/Filtered_kernel/include/CGAL/Lazy.h index 4251b748d64..4daa536683c 100644 --- a/Filtered_kernel/include/CGAL/Lazy.h +++ b/Filtered_kernel/include/CGAL/Lazy.h @@ -33,10 +33,7 @@ #include #include #include - -#if defined(CGAL_HAS_THREADS) && ! defined(BOOST_MSVC) -# include -#endif +#include #include #include @@ -789,7 +786,7 @@ private: { #ifdef CGAL_HAS_THREADS #if BOOST_MSVC - __declspec( thread ) static Self* z = NULL; + CGAL_THREAD_LOCAL static Self* z = NULL; if(z == NULL){ z = new Self(new Lazy_rep_0()); } diff --git a/Installation/include/CGAL/mutex.h b/Installation/include/CGAL/mutex.h index 0dbcdfed9f6..009bf1fa288 100644 --- a/Installation/include/CGAL/mutex.h +++ b/Installation/include/CGAL/mutex.h @@ -3,16 +3,15 @@ #include +#ifdef CGAL_HAS_THREADS #ifdef BOOST_MSVC #include #define CGAL_MUTEX std::mutex #define CGAL_SCOPED_LOCK(M) std::unique_lock scoped_lock(M) - #else #include #define CGAL_MUTEX boost::mutex #define CGAL_SCOPED_LOCK(M) boost::mutex::scoped_lock scoped_lock(M) - #endif - +#endif #endif // CGAL_MUTEX_H diff --git a/Installation/include/CGAL/tss.h b/Installation/include/CGAL/tss.h new file mode 100644 index 00000000000..fa14d291f60 --- /dev/null +++ b/Installation/include/CGAL/tss.h @@ -0,0 +1,15 @@ +#ifndef CGAL_TSS_H +#define CGAL_TSS_H + +#include + +#ifdef CGAL_HAS_THREADS +#ifdef BOOST_MSVC +#include +#define CGAL_THREAD_LOCAL __declspec( thread ) +#else +# include +#define CGAL_THREAD_LOCAL thread_local +#endif +#endif +#endif // CGAL_MUTEX_H diff --git a/Modular_arithmetic/include/CGAL/Modular_arithmetic/Residue_type.h b/Modular_arithmetic/include/CGAL/Modular_arithmetic/Residue_type.h index 18f05e7a6fd..36cfe593a52 100644 --- a/Modular_arithmetic/include/CGAL/Modular_arithmetic/Residue_type.h +++ b/Modular_arithmetic/include/CGAL/Modular_arithmetic/Residue_type.h @@ -22,16 +22,17 @@ #define CGAL_RESIDUE_TYPE_H #include +#include #include + #include -#ifdef CGAL_HAS_THREADS -# include -#endif namespace CGAL { + struct TT {int i;}; + class Residue; Residue operator + (const Residue&); @@ -63,6 +64,14 @@ private: CGAL_EXPORT static const double CST_CUT; #ifdef CGAL_HAS_THREADS +#ifdef BOOST_MSVC + CGAL_EXPORT CGAL_THREAD_LOCAL static int prime_int; + CGAL_EXPORT CGAL_THREAD_LOCAL static double prime; + CGAL_EXPORT CGAL_THREAD_LOCAL static double prime_inv; + static int get_prime_int(){ return prime_int;} + static double get_prime() { return prime;} + static double get_prime_inv(){ return prime_inv;} +#else CGAL_EXPORT static boost::thread_specific_ptr prime_int_; CGAL_EXPORT static boost::thread_specific_ptr prime_; CGAL_EXPORT static boost::thread_specific_ptr prime_inv_; @@ -93,10 +102,12 @@ private: init_class_for_thread(); return *prime_inv_.get(); } +#endif // ifdef BOOST_MSVC + #else - CGAL_EXPORT static int prime_int; - CGAL_EXPORT static double prime; - CGAL_EXPORT static double prime_inv; + CGAL_EXPORT static int prime_int; + CGAL_EXPORT static double prime; + CGAL_EXPORT static double prime_inv; static int get_prime_int(){ return prime_int;} static double get_prime() { return prime;} static double get_prime_inv(){ return prime_inv;} @@ -196,9 +207,15 @@ public: set_current_prime(int p){ int old_prime = get_prime_int(); #ifdef CGAL_HAS_THREADS +#ifdef BOOST_MSVC + prime_int = p; + prime = double(p); + prime_inv = 1.0 / prime; +#else *prime_int_.get() = p; *prime_.get() = double(p); *prime_inv_.get() = 1.0/double(p); +#endif #else prime_int = p; prime = double(p); diff --git a/Modular_arithmetic/src/CGAL/Residue_type.cpp b/Modular_arithmetic/src/CGAL/Residue_type.cpp index e221b51dc3e..24f37bf1307 100644 --- a/Modular_arithmetic/src/CGAL/Residue_type.cpp +++ b/Modular_arithmetic/src/CGAL/Residue_type.cpp @@ -22,13 +22,19 @@ namespace CGAL{ #ifdef CGAL_HAS_THREADS +#ifdef BOOST_MSVC +CGAL_THREAD_LOCAL int Residue::prime_int = 67111067; +CGAL_THREAD_LOCAL double Residue::prime = 67111067.0; +CGAL_THREAD_LOCAL double Residue::prime_inv = 1490067204.5640400859667452463541; +#else boost::thread_specific_ptr Residue::prime_int_; boost::thread_specific_ptr Residue::prime_; boost::thread_specific_ptr Residue::prime_inv_; +#endif #else int Residue::prime_int = 67111067; double Residue::prime = 67111067.0; -double Residue::prime_inv =1/67111067.0; +double Residue::prime_inv = 1/67111067.0; #endif diff --git a/Number_types/include/CGAL/GMP/Gmpfi_type.h b/Number_types/include/CGAL/GMP/Gmpfi_type.h index bb23d9b48dd..37453897c3c 100644 --- a/Number_types/include/CGAL/GMP/Gmpfi_type.h +++ b/Number_types/include/CGAL/GMP/Gmpfi_type.h @@ -26,9 +26,8 @@ #include #include #include -#ifdef CGAL_HAS_THREADS -# include -#endif +#include + #include #include @@ -81,7 +80,11 @@ Uncertain operator==(const Gmpfi&,const Gmpq&); // the default precision is a variable local to each thread in multithreaded // environments, or a global variable otherwise #ifdef CGAL_HAS_THREADS +#ifdef BOOST_MSVC + static CGAL_THREAD_LOCAL Gmpfi_default_precision_; +#else static boost::thread_specific_ptr Gmpfi_default_precision_; +#endif #else static mp_prec_t Gmpfi_default_precision=CGAL_GMPFI_DEFAULT_PRECISION; #endif @@ -403,18 +406,28 @@ CGAL_GMPFI_CONSTRUCTOR_FROM_SCALAR(Gmpz); #ifdef CGAL_HAS_THREADS inline void Gmpfi::init_precision_for_thread(){ +#ifdef BOOST_MSVC + Gmpfi_default_precision_ = new mp_prec_t(CGAL_GMPFI_DEFAULT_PRECISION); +#else CGAL_precondition(Gmpfi_default_precision_.get()==NULL); Gmpfi_default_precision_.reset( new mp_prec_t(CGAL_GMPFI_DEFAULT_PRECISION)); +#endif } #endif inline Gmpfi::Precision_type Gmpfi::get_default_precision(){ #ifdef CGAL_HAS_THREADS +#ifdef BOOST_MSVC + if(Gmpfi_default_precision_==NULL) + Gmpfi::init_precision_for_thread(); + return *Gmpfi_default_precision_; +#else if(Gmpfi_default_precision_.get()==NULL) Gmpfi::init_precision_for_thread(); return *Gmpfi_default_precision_.get(); +#endif #else return Gmpfi_default_precision; #endif @@ -425,7 +438,11 @@ Gmpfi::Precision_type Gmpfi::set_default_precision(Gmpfi::Precision_type prec){ Gmpfi::Precision_type old_prec=Gmpfi::get_default_precision(); CGAL_assertion(prec>=MPFR_PREC_MIN&&prec<=MPFR_PREC_MAX); #ifdef CGAL_HAS_THREADS +#ifdef BOOST_MSVC + *Gmpfi_default_precision_ = prec; +#else *Gmpfi_default_precision_.get()=prec; +#endif #else Gmpfi_default_precision=prec; #endif diff --git a/Polynomial/include/CGAL/Polynomial/Polynomial_type.h b/Polynomial/include/CGAL/Polynomial/Polynomial_type.h index 5a9090e6a54..26a1dd1788c 100644 --- a/Polynomial/include/CGAL/Polynomial/Polynomial_type.h +++ b/Polynomial/include/CGAL/Polynomial/Polynomial_type.h @@ -46,10 +46,7 @@ typename CGAL::internal::Innermost_coefficient_type::Type , 2>::Type #include #include - -#ifdef CGAL_HAS_THREADS -# include -#endif +#include namespace CGAL { @@ -268,11 +265,20 @@ protected: // private: static Self& get_default_instance(){ - #ifdef CGAL_HAS_THREADS - static boost::thread_specific_ptr< Self > safe_x_ptr; - if (safe_x_ptr.get() == NULL) - safe_x_ptr.reset(new Self(0)); - return *safe_x_ptr.get(); + #ifdef CGAL_HAS_THREADS + #if BOOST_MSVC + CGAL_THREAD_LOCAL static Self* safe_x_ptr = NULL; + if(safe_x_ptr == NULL){ + safe_x_ptr = new Self(0); + } + return *safe_x_ptr; + #else + static boost::thread_specific_ptr< Self > safe_x_ptr; + if (safe_x_ptr.get() == NULL){ + safe_x_ptr.reset(new Self(0)); + } + return *safe_x_ptr.get(); + #endif #else static Self x = Self(0); return x; diff --git a/Triangulation_2/include/CGAL/Delaunay_triangulation_2.h b/Triangulation_2/include/CGAL/Delaunay_triangulation_2.h index 1e1e3d1354c..efeb008810a 100644 --- a/Triangulation_2/include/CGAL/Delaunay_triangulation_2.h +++ b/Triangulation_2/include/CGAL/Delaunay_triangulation_2.h @@ -31,7 +31,7 @@ #ifndef CGAL_TRIANGULATION_2_DONT_INSERT_RANGE_OF_POINTS_WITH_INFO #include #include - +#include #include #include @@ -1053,7 +1053,23 @@ remove_and_give_new_faces(Vertex_handle v, OutputItFaces fit) afi++) *fit++ = afi; } else { - #ifdef CGAL_HAS_THREADS + #ifdef CGAL_HAS_THREADS + #ifdef BOOST_MSVC + CGAL_THREAD_LOCAL static int maxd = 30; + + CGAL_THREAD_LOCAL static std::vector* f_ptr; + CGAL_THREAD_LOCAL static std::vector* i_ptr; + CGAL_THREAD_LOCAL static std::vector* w_ptr; + if (f_ptr == NULL) { + f_ptr = new std::vector(maxd); + i_ptr = new std::vector(maxd); + w_ptr = new std::vector(maxd); + } + std::vector& f=*f_ptr; + std::vector& i=*i_ptr; + std::vector& w=*w_ptr; + #else + static boost::thread_specific_ptr< int > maxd_ptr; static boost::thread_specific_ptr< std::vector > f_ptr; static boost::thread_specific_ptr< std::vector > i_ptr; @@ -1068,6 +1084,7 @@ remove_and_give_new_faces(Vertex_handle v, OutputItFaces fit) std::vector& f=*f_ptr; std::vector& i=*i_ptr; std::vector& w=*w_ptr; +#endif #else static int maxd=30; static std::vector f(maxd); @@ -1098,6 +1115,22 @@ remove(Vertex_handle v) if ( this->dimension() <= 1) { Triangulation::remove(v); return; } #ifdef CGAL_HAS_THREADS + #ifdef BOOST_MSVC + CGAL_THREAD_LOCAL static int maxd = 30; + + CGAL_THREAD_LOCAL static std::vector* f_ptr; + CGAL_THREAD_LOCAL static std::vector* i_ptr; + CGAL_THREAD_LOCAL static std::vector* w_ptr; + if (f_ptr == NULL) { + f_ptr = new std::vector(maxd); + i_ptr = new std::vector(maxd); + w_ptr = new std::vector(maxd); + } + std::vector& f=*f_ptr; + std::vector& i=*i_ptr; + std::vector& w=*w_ptr; + #else + static boost::thread_specific_ptr< int > maxd_ptr; static boost::thread_specific_ptr< std::vector > f_ptr; static boost::thread_specific_ptr< std::vector > i_ptr; @@ -1112,6 +1145,7 @@ remove(Vertex_handle v) std::vector& f=*f_ptr; std::vector& i=*i_ptr; std::vector& w=*w_ptr; +#endif #else static int maxd=30; static std::vector f(maxd); @@ -2261,6 +2295,22 @@ move_if_no_collision(Vertex_handle v, const Point &p) { { int d; #ifdef CGAL_HAS_THREADS + #ifdef BOOST_MSVC + CGAL_THREAD_LOCAL static int maxd = 30; + + CGAL_THREAD_LOCAL static std::vector* f_ptr; + CGAL_THREAD_LOCAL static std::vector* i_ptr; + CGAL_THREAD_LOCAL static std::vector* w_ptr; + if (f_ptr == NULL) { + f_ptr = new std::vector(maxd); + i_ptr = new std::vector(maxd); + w_ptr = new std::vector(maxd); + } + std::vector& f=*f_ptr; + std::vector& i=*i_ptr; + std::vector& w=*w_ptr; + #else + static boost::thread_specific_ptr< int > maxd_ptr; static boost::thread_specific_ptr< std::vector > f_ptr; static boost::thread_specific_ptr< std::vector > i_ptr; @@ -2275,6 +2325,7 @@ move_if_no_collision(Vertex_handle v, const Point &p) { std::vector& f=*f_ptr; std::vector& i=*i_ptr; std::vector& w=*w_ptr; +#endif #else static int maxd=30; static std::vector f(maxd); @@ -2495,6 +2546,22 @@ move_if_no_collision_and_give_new_faces(Vertex_handle v, { #ifdef CGAL_HAS_THREADS + #ifdef BOOST_MSVC + CGAL_THREAD_LOCAL static int maxd = 30; + + CGAL_THREAD_LOCAL static std::vector* f_ptr; + CGAL_THREAD_LOCAL static std::vector* i_ptr; + CGAL_THREAD_LOCAL static std::vector* w_ptr; + if (f_ptr == NULL) { + f_ptr = new std::vector(maxd); + i_ptr = new std::vector(maxd); + w_ptr = new std::vector(maxd); + } + std::vector& f=*f_ptr; + std::vector& i=*i_ptr; + std::vector& w=*w_ptr; + #else + static boost::thread_specific_ptr< int > maxd_ptr; static boost::thread_specific_ptr< std::vector > f_ptr; static boost::thread_specific_ptr< std::vector > i_ptr; @@ -2509,6 +2576,7 @@ move_if_no_collision_and_give_new_faces(Vertex_handle v, std::vector& f=*f_ptr; std::vector& i=*i_ptr; std::vector& w=*w_ptr; +#endif #else static int maxd=30; static std::vector f(maxd);