From 6480cf2205f73c75850fb0c7785ff47d2c48afb8 Mon Sep 17 00:00:00 2001 From: Simon Giraudot Date: Tue, 13 Mar 2018 15:41:12 +0100 Subject: [PATCH] Define a CGAL::cpp11::thread wrapper for TBB or STD --- Installation/include/CGAL/config.h | 4 ++ STL_Extension/include/CGAL/thread.h | 105 ++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 STL_Extension/include/CGAL/thread.h diff --git a/Installation/include/CGAL/config.h b/Installation/include/CGAL/config.h index 70cb2a2281f..c7fcad9736f 100644 --- a/Installation/include/CGAL/config.h +++ b/Installation/include/CGAL/config.h @@ -187,6 +187,10 @@ defined(BOOST_NO_CXX11_HDR_UNORDERED_MAP) #define CGAL_CFG_NO_CPP0X_UNORDERED 1 #endif +#if defined( BOOST_NO_0X_HDR_THREAD) || \ + defined( BOOST_NO_CXX11_HDR_THREAD) +#define CGAL_CFG_NO_STD_THREAD 1 +#endif #if defined(BOOST_NO_DECLTYPE) || \ defined(BOOST_NO_CXX11_DECLTYPE) || (BOOST_VERSION < 103600) #define CGAL_CFG_NO_CPP0X_DECLTYPE 1 diff --git a/STL_Extension/include/CGAL/thread.h b/STL_Extension/include/CGAL/thread.h new file mode 100644 index 00000000000..770c74bad02 --- /dev/null +++ b/STL_Extension/include/CGAL/thread.h @@ -0,0 +1,105 @@ +// Copyright (c) 2018 GeometryFactory Sarl (France). +// All rights reserved. +// +// This file is part of CGAL (www.cgal.org); you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public License as +// published by the Free Software Foundation; either version 3 of the License, +// or (at your option) any later version. +// +// Licensees holding a valid commercial license may use this file in +// accordance with the commercial license agreement provided with the software. +// +// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +// +// $URL$ +// $Id$ +// SPDX-License-Identifier: LGPL-3.0+ +// +// Author(s) : Simon Giraudot + +#ifndef CGAL_THREAD_H +#define CGAL_THREAD_H + +#include + +/* + This file defines the following: + - CGAL::cpp11::thread + - CGAL::cpp11::this_thread::sleep_for + - CGAL::cpp11::atomic + - CGAL::cpp11::chrono::seconds + + It uses either TBB or STD depending on what's available: as TBB can + quite often override `std::thread`, it is possible that TBB will be + used instead of STD even if the real CXX11 `std::thread` is + available. +*/ + +#if defined(CGAL_LINKED_WITH_TBB) +# include +# if TBB_IMPLEMENT_CPP0X +# include +# include +# include +# define CGAL_USE_TBB_THREADS 1 +# else +# define CGAL_USE_TBB_THREADS 0 +# endif +#else +# define CGAL_USE_TBB_THREADS 0 +#endif + +#if !CGAL_USE_TBB_THREADS +# if !defined(CGAL_CFG_NO_STD_THREAD) +# include +# include +# include +# define CGAL_USE_STD_THREADS 1 +# else +# define CGAL_USE_STD_THREADS 0 +# endif +#else +# define CGAL_USE_STD_THREADS 0 +#endif + +#if CGAL_USE_STD_THREADS || CGAL_USE_TBB_THREADS +# define CGAL_HAS_STD_THREADS // Useful define +#endif + +namespace CGAL { + +namespace cpp11 { + +#if CGAL_USE_TBB_THREADS + using std::thread; // std::thread is declared by TBB if TBB_IMPLEMENT_CPP0X == 1 + namespace this_thread + { + using std::this_thread::sleep_for; // std::this_thread::sleep_for is declared by TBB if TBB_IMPLEMENT_CPP0X == 1 + } + using tbb::atomic; + namespace chrono + { + typedef tbb::tick_count::interval_t seconds; + } +#elif CGAL_USE_STD_THREADS + using std::thread; + namespace this_thread + { + using std::this_thread::sleep_for; + } + using std::atomic; + namespace chrono + { + typedef std::chrono::duration > seconds; + } +#endif + +} // cpp11 + +} //namespace CGAL + +#undef CGAL_USE_STD_THREADS +#undef CGAL_USE_TBB_THREADS + +#endif // CGAL_THREAD_H