From fa3d61f5b4b1229576c7ee03b62117b1e8e94c25 Mon Sep 17 00:00:00 2001 From: David Wells Date: Sun, 7 Aug 2022 13:00:37 -0400 Subject: [PATCH] Move Threads::Mutex to a dedicated header. Most files don't need anything in Threads other than Threads::Mutex so we can decouple them from TBB et. al. by moving this class to its own header. --- examples/step-48/step-48.cc | 1 + include/deal.II/base/flow_function.h | 2 +- include/deal.II/base/function_cspline.h | 2 +- include/deal.II/base/incremental_function.h | 2 +- include/deal.II/base/index_set.h | 2 +- include/deal.II/base/mutex.h | 84 ++++++++++++++ include/deal.II/base/parallel.h | 4 +- include/deal.II/base/polynomials_abf.h | 2 +- include/deal.II/base/polynomials_bdm.h | 2 +- include/deal.II/base/thread_management.h | 106 +++++------------- include/deal.II/base/timer.h | 2 +- include/deal.II/fe/fe_dgq.h | 2 +- include/deal.II/fe/fe_nedelec.h | 2 +- include/deal.II/fe/fe_poly_tensor.h | 4 +- include/deal.II/fe/fe_q_base.h | 2 +- include/deal.II/fe/fe_raviart_thomas.h | 1 + include/deal.II/fe/fe_simplex_p.h | 1 + include/deal.II/fe/mapping_fe_field.h | 4 +- include/deal.II/fe/mapping_q_eulerian.h | 2 +- include/deal.II/lac/block_matrix_base.h | 5 +- include/deal.II/lac/la_parallel_vector.h | 1 - include/deal.II/lac/lapack_full_matrix.h | 4 +- include/deal.II/lac/precondition.h | 2 +- include/deal.II/lac/scalapack.h | 2 +- include/deal.II/lac/tensor_product_matrix.h | 2 +- include/deal.II/lac/vector_memory.h | 2 +- .../deal.II/matrix_free/dof_info.templates.h | 2 - .../mapping_info_storage.templates.h | 2 - .../matrix_free/matrix_free.templates.h | 1 + include/deal.II/matrix_free/task_info.h | 1 - source/dofs/dof_handler.cc | 1 + source/fe/fe_q_base.cc | 1 + source/fe/fe_system.cc | 1 + source/fe/fe_values.cc | 1 + source/grid/tria.cc | 1 + source/non_matching/fe_immersed_values.cc | 2 + 36 files changed, 148 insertions(+), 110 deletions(-) create mode 100644 include/deal.II/base/mutex.h diff --git a/examples/step-48/step-48.cc b/examples/step-48/step-48.cc index 9f1f8acdff..6cca69fcf1 100644 --- a/examples/step-48/step-48.cc +++ b/examples/step-48/step-48.cc @@ -20,6 +20,7 @@ // The necessary files from the deal.II library. #include +#include #include #include #include diff --git a/include/deal.II/base/flow_function.h b/include/deal.II/base/flow_function.h index eb37be7c8b..33b213ca6b 100644 --- a/include/deal.II/base/flow_function.h +++ b/include/deal.II/base/flow_function.h @@ -20,8 +20,8 @@ #include #include +#include #include -#include DEAL_II_NAMESPACE_OPEN diff --git a/include/deal.II/base/function_cspline.h b/include/deal.II/base/function_cspline.h index 0e853940c4..5ce2920069 100644 --- a/include/deal.II/base/function_cspline.h +++ b/include/deal.II/base/function_cspline.h @@ -20,8 +20,8 @@ #ifdef DEAL_II_WITH_GSL # include +# include # include -# include # include diff --git a/include/deal.II/base/incremental_function.h b/include/deal.II/base/incremental_function.h index 992672fd92..b959b182a0 100644 --- a/include/deal.II/base/incremental_function.h +++ b/include/deal.II/base/incremental_function.h @@ -20,7 +20,7 @@ #include #include -#include +#include #include diff --git a/include/deal.II/base/index_set.h b/include/deal.II/base/index_set.h index b3c3ceec2a..9afd33f187 100644 --- a/include/deal.II/base/index_set.h +++ b/include/deal.II/base/index_set.h @@ -19,7 +19,7 @@ #include #include -#include +#include #include DEAL_II_DISABLE_EXTRA_DIAGNOSTICS diff --git a/include/deal.II/base/mutex.h b/include/deal.II/base/mutex.h new file mode 100644 index 0000000000..4776b6bef6 --- /dev/null +++ b/include/deal.II/base/mutex.h @@ -0,0 +1,84 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2000 - 2022 by the deal.II authors +// +// This file is part of the deal.II library. +// +// The deal.II library is free software; you can use it, 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 2.1 of the License, or (at your option) any later version. +// The full text of the license can be found in the file LICENSE.md at +// the top level directory of deal.II. +// +// --------------------------------------------------------------------- + +#ifndef dealii_mutex_h +#define dealii_mutex_h + + +#include + +#include + +DEAL_II_NAMESPACE_OPEN + +/** + * @addtogroup threads + * @{ + */ + +namespace Threads +{ + /** + * A class implementing a mutex. + * Mutexes are used to lock data structures to ensure that only a + * single thread of execution can access them at the same time. + * + * This class is a thin wrapper around `std::mutex`. The only difference + * is that this class is copyable when `std::mutex` is not. Indeed, when + * copied, the receiving object does not copy any state from the object + * being copied, i.e. an entirely new mutex is created. These semantics + * are consistent with the common use case if a mutex is used as a member + * variable to lock the other member variables of a class: in that case, + * the mutex of the copied-to object should only guard the members of the + * copied-to object, not the members of both the copied-to and + * copied-from object. Since at the time when the class is copied, the + * destination's member variable is not used yet, its corresponding mutex + * should also remain in its original state. + */ + class Mutex : public std::mutex + { + public: + /** + * Default constructor. + */ + Mutex() = default; + + /** + * Copy constructor. As discussed in this class's documentation, no state + * is copied from the object given as argument. + */ + Mutex(const Mutex &) + : std::mutex() + {} + + /** + * Copy operators. As discussed in this class's documentation, no state + * is copied from the object given as argument. + */ + Mutex & + operator=(const Mutex &) + { + return *this; + } + }; +} // namespace Threads + +/** + * @} + */ + +DEAL_II_NAMESPACE_CLOSE +#endif diff --git a/include/deal.II/base/parallel.h b/include/deal.II/base/parallel.h index 066b372806..2940140076 100644 --- a/include/deal.II/base/parallel.h +++ b/include/deal.II/base/parallel.h @@ -20,9 +20,9 @@ #include #include +#include #include #include -#include #include #include @@ -612,7 +612,7 @@ namespace parallel /** * A mutex to guard the access to the in_use flag. */ - std::mutex mutex; + Threads::Mutex mutex; #endif }; } // namespace internal diff --git a/include/deal.II/base/polynomials_abf.h b/include/deal.II/base/polynomials_abf.h index b3a0e76797..8a55c0215a 100644 --- a/include/deal.II/base/polynomials_abf.h +++ b/include/deal.II/base/polynomials_abf.h @@ -20,13 +20,13 @@ #include #include +#include #include #include #include #include #include #include -#include #include #include diff --git a/include/deal.II/base/polynomials_bdm.h b/include/deal.II/base/polynomials_bdm.h index cba0e91000..8e6e296d2f 100644 --- a/include/deal.II/base/polynomials_bdm.h +++ b/include/deal.II/base/polynomials_bdm.h @@ -20,12 +20,12 @@ #include #include +#include #include #include #include #include #include -#include #include diff --git a/include/deal.II/base/thread_management.h b/include/deal.II/base/thread_management.h index 2929308f3d..b6e85a446f 100644 --- a/include/deal.II/base/thread_management.h +++ b/include/deal.II/base/thread_management.h @@ -14,31 +14,31 @@ // --------------------------------------------------------------------- #ifndef dealii_thread_management_h -# define dealii_thread_management_h +#define dealii_thread_management_h -# include +#include -# include -# include -# include -# include +#include +#include +#include +#include +#include -# include -# include -# include -# include -# include -# include -# include -# include -# include +#include +#include +#include +#include +#include +#include +#include +#include -# ifdef DEAL_II_WITH_TBB +#ifdef DEAL_II_WITH_TBB DEAL_II_DISABLE_EXTRA_DIAGNOSTICS -# include +# include DEAL_II_ENABLE_EXTRA_DIAGNOSTICS -# endif +#endif DEAL_II_NAMESPACE_OPEN @@ -54,55 +54,6 @@ DEAL_II_NAMESPACE_OPEN * * @ingroup threads */ -namespace Threads -{ - /** - * A class implementing a mutex. - * Mutexes are used to lock data structures to ensure that only a - * single thread of execution can access them at the same time. - * - * This class is a thin wrapper around `std::mutex`. The only difference - * is that this class is copyable when `std::mutex` is not. Indeed, when - * copied, the receiving object does not copy any state from the object - * being copied, i.e. an entirely new mutex is created. These semantics - * are consistent with the common use case if a mutex is used as a member - * variable to lock the other member variables of a class: in that case, - * the mutex of the copied-to object should only guard the members of the - * copied-to object, not the members of both the copied-to and - * copied-from object. Since at the time when the class is copied, the - * destination's member variable is not used yet, its corresponding mutex - * should also remain in its original state. - */ - class Mutex : public std::mutex - { - public: - /** - * Default constructor. - */ - Mutex() = default; - - /** - * Copy constructor. As discussed in this class's documentation, no state - * is copied from the object given as argument. - */ - Mutex(const Mutex &) - : std::mutex() - {} - - /** - * Copy operators. As discussed in this class's documentation, no state - * is copied from the object given as argument. - */ - Mutex & - operator=(const Mutex &) - { - return *this; - } - }; -} // namespace Threads - - namespace Threads { /** @@ -185,7 +136,7 @@ namespace Threads } // namespace Threads /* ----------- implementation of functions in namespace Threads ---------- */ -# ifndef DOXYGEN +#ifndef DOXYGEN namespace Threads { template @@ -236,7 +187,7 @@ namespace Threads } } // namespace Threads -# endif // DOXYGEN +#endif // DOXYGEN namespace Threads { @@ -1034,7 +985,7 @@ namespace Threads { if (MultithreadInfo::n_threads() > 1) { -# ifdef DEAL_II_WITH_TBB +#ifdef DEAL_II_WITH_TBB // Create a promise object and from it extract a future that // we can use to refer to the outcome of the task. For reasons // explained below, we can't just create a std::promise object, @@ -1102,7 +1053,7 @@ namespace Threads } }); -# else +#else // If no threading library is supported, just fall back onto C++11 // facilities. The problem with this is that the standard does // not actually say what std::async should do. The first @@ -1123,7 +1074,7 @@ namespace Threads task_data = std::make_shared( std::async(std::launch::async | std::launch::deferred, function_object)); -# endif +#endif } else { @@ -1410,7 +1361,7 @@ namespace Threads return; else { -# ifdef DEAL_II_WITH_TBB +#ifdef DEAL_II_WITH_TBB // If we build on the TBB, then we can't just wait for the // std::future object to get ready. Apparently the TBB happily // enqueues a task into an arena and then just sits on it without @@ -1419,7 +1370,7 @@ namespace Threads // tbb::task_group, and then here wait for the single task // associated with that task group. task_group.wait(); -# endif +#endif // Wait for the task to finish and then move its // result. (We could have made the set_from() function @@ -1488,14 +1439,14 @@ namespace Threads */ internal::return_value returned_object; -# ifdef DEAL_II_WITH_TBB +#ifdef DEAL_II_WITH_TBB /** * A task group object we can wait for. */ tbb::task_group task_group; friend class Task; -# endif +#endif }; /** @@ -1744,8 +1695,5 @@ namespace Threads */ -//--------------------------------------------------------------------------- DEAL_II_NAMESPACE_CLOSE -// end of #ifndef dealii_thread_management_h #endif -//--------------------------------------------------------------------------- diff --git a/include/deal.II/base/timer.h b/include/deal.II/base/timer.h index 3a6befaffa..89d6e7e483 100644 --- a/include/deal.II/base/timer.h +++ b/include/deal.II/base/timer.h @@ -20,7 +20,7 @@ #include #include -#include +#include #include #include diff --git a/include/deal.II/fe/fe_dgq.h b/include/deal.II/fe/fe_dgq.h index 7665731953..0cffacef27 100644 --- a/include/deal.II/fe/fe_dgq.h +++ b/include/deal.II/fe/fe_dgq.h @@ -18,8 +18,8 @@ #include +#include #include -#include #include diff --git a/include/deal.II/fe/fe_nedelec.h b/include/deal.II/fe/fe_nedelec.h index ec2f223935..7b7ac333e2 100644 --- a/include/deal.II/fe/fe_nedelec.h +++ b/include/deal.II/fe/fe_nedelec.h @@ -19,12 +19,12 @@ #include #include +#include #include #include #include #include #include -#include #include #include diff --git a/include/deal.II/fe/fe_poly_tensor.h b/include/deal.II/fe/fe_poly_tensor.h index bafc92911d..6d1730c316 100644 --- a/include/deal.II/fe/fe_poly_tensor.h +++ b/include/deal.II/fe/fe_poly_tensor.h @@ -20,9 +20,9 @@ #include #include +#include #include #include -#include #include @@ -536,7 +536,7 @@ protected: /** * A mutex to be used to guard access to the variables below. */ - mutable std::mutex cache_mutex; + mutable Threads::Mutex cache_mutex; /** * If a shape function is computed at a single point, we must compute all of diff --git a/include/deal.II/fe/fe_q_base.h b/include/deal.II/fe/fe_q_base.h index e9c70005ae..86e3511c6e 100644 --- a/include/deal.II/fe/fe_q_base.h +++ b/include/deal.II/fe/fe_q_base.h @@ -18,7 +18,7 @@ #include -#include +#include #include diff --git a/include/deal.II/fe/fe_raviart_thomas.h b/include/deal.II/fe/fe_raviart_thomas.h index e2852b4e6f..0550fae6dd 100644 --- a/include/deal.II/fe/fe_raviart_thomas.h +++ b/include/deal.II/fe/fe_raviart_thomas.h @@ -18,6 +18,7 @@ #include +#include #include #include diff --git a/include/deal.II/fe/fe_simplex_p.h b/include/deal.II/fe/fe_simplex_p.h index 3bbadd90e4..5f53aacf44 100644 --- a/include/deal.II/fe/fe_simplex_p.h +++ b/include/deal.II/fe/fe_simplex_p.h @@ -18,6 +18,7 @@ #include +#include #include #include diff --git a/include/deal.II/fe/mapping_fe_field.h b/include/deal.II/fe/mapping_fe_field.h index 4bf581d982..9562157930 100644 --- a/include/deal.II/fe/mapping_fe_field.h +++ b/include/deal.II/fe/mapping_fe_field.h @@ -20,7 +20,7 @@ #include #include -#include +#include #include @@ -648,7 +648,7 @@ private: /** * A variable to guard access to the fe_values variable. */ - mutable std::mutex fe_values_mutex; + mutable Threads::Mutex fe_values_mutex; void compute_data(const UpdateFlags update_flags, diff --git a/include/deal.II/fe/mapping_q_eulerian.h b/include/deal.II/fe/mapping_q_eulerian.h index e5802040c4..c2e3956e56 100644 --- a/include/deal.II/fe/mapping_q_eulerian.h +++ b/include/deal.II/fe/mapping_q_eulerian.h @@ -19,8 +19,8 @@ #include +#include #include -#include #include #include diff --git a/include/deal.II/lac/block_matrix_base.h b/include/deal.II/lac/block_matrix_base.h index a478a68f3b..787c0a5a5e 100644 --- a/include/deal.II/lac/block_matrix_base.h +++ b/include/deal.II/lac/block_matrix_base.h @@ -20,9 +20,9 @@ #include #include +#include #include #include -#include #include #include @@ -33,6 +33,7 @@ #include #include +#include DEAL_II_NAMESPACE_OPEN @@ -1038,7 +1039,7 @@ private: * A mutex variable used to guard access to the member variables of this * structure; */ - std::mutex mutex; + Threads::Mutex mutex; /** * Copy operator. This is needed because the default copy operator of this diff --git a/include/deal.II/lac/la_parallel_vector.h b/include/deal.II/lac/la_parallel_vector.h index adbd4f5ae0..137ff816e0 100644 --- a/include/deal.II/lac/la_parallel_vector.h +++ b/include/deal.II/lac/la_parallel_vector.h @@ -26,7 +26,6 @@ #include #include #include -#include #include #include diff --git a/include/deal.II/lac/lapack_full_matrix.h b/include/deal.II/lac/lapack_full_matrix.h index ee214db2b1..fe7154a183 100644 --- a/include/deal.II/lac/lapack_full_matrix.h +++ b/include/deal.II/lac/lapack_full_matrix.h @@ -19,9 +19,9 @@ #include +#include #include #include -#include #include #include @@ -972,7 +972,7 @@ private: /** * Thread mutex. */ - mutable std::mutex mutex; + mutable Threads::Mutex mutex; }; diff --git a/include/deal.II/lac/precondition.h b/include/deal.II/lac/precondition.h index 035c86afc8..cc428ae8db 100644 --- a/include/deal.II/lac/precondition.h +++ b/include/deal.II/lac/precondition.h @@ -20,10 +20,10 @@ #include #include +#include #include #include #include -#include #include #include diff --git a/include/deal.II/lac/scalapack.h b/include/deal.II/lac/scalapack.h index e622910cbc..da65f8c183 100644 --- a/include/deal.II/lac/scalapack.h +++ b/include/deal.II/lac/scalapack.h @@ -22,8 +22,8 @@ # include # include +# include # include -# include # include # include diff --git a/include/deal.II/lac/tensor_product_matrix.h b/include/deal.II/lac/tensor_product_matrix.h index 55206de235..e8a434c184 100644 --- a/include/deal.II/lac/tensor_product_matrix.h +++ b/include/deal.II/lac/tensor_product_matrix.h @@ -20,7 +20,7 @@ #include #include -#include +#include #include diff --git a/include/deal.II/lac/vector_memory.h b/include/deal.II/lac/vector_memory.h index a397bd2668..f965bbe0b8 100644 --- a/include/deal.II/lac/vector_memory.h +++ b/include/deal.II/lac/vector_memory.h @@ -20,8 +20,8 @@ #include #include +#include #include -#include #include diff --git a/include/deal.II/matrix_free/dof_info.templates.h b/include/deal.II/matrix_free/dof_info.templates.h index 65d27043e5..9b462e475c 100644 --- a/include/deal.II/matrix_free/dof_info.templates.h +++ b/include/deal.II/matrix_free/dof_info.templates.h @@ -20,9 +20,7 @@ #include #include -#include #include -#include #include #include diff --git a/include/deal.II/matrix_free/mapping_info_storage.templates.h b/include/deal.II/matrix_free/mapping_info_storage.templates.h index fd468200c1..60d12ea87f 100644 --- a/include/deal.II/matrix_free/mapping_info_storage.templates.h +++ b/include/deal.II/matrix_free/mapping_info_storage.templates.h @@ -19,8 +19,6 @@ #include #include -#include -#include #include #include diff --git a/include/deal.II/matrix_free/matrix_free.templates.h b/include/deal.II/matrix_free/matrix_free.templates.h index 9919a0463e..31a64e5d87 100644 --- a/include/deal.II/matrix_free/matrix_free.templates.h +++ b/include/deal.II/matrix_free/matrix_free.templates.h @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include diff --git a/include/deal.II/matrix_free/task_info.h b/include/deal.II/matrix_free/task_info.h index 4cf5147256..3c66a98499 100644 --- a/include/deal.II/matrix_free/task_info.h +++ b/include/deal.II/matrix_free/task_info.h @@ -24,7 +24,6 @@ #include #include #include -#include #include #include diff --git a/source/dofs/dof_handler.cc b/source/dofs/dof_handler.cc index 6a3093f98c..bd2b7bc949 100644 --- a/source/dofs/dof_handler.cc +++ b/source/dofs/dof_handler.cc @@ -18,6 +18,7 @@ #include #include #include +#include #include #include diff --git a/source/fe/fe_q_base.cc b/source/fe/fe_q_base.cc index 281f698e41..651670c48d 100644 --- a/source/fe/fe_q_base.cc +++ b/source/fe/fe_q_base.cc @@ -22,6 +22,7 @@ #include #include #include +#include #include #include diff --git a/source/fe/fe_system.cc b/source/fe/fe_system.cc index d74476d1a8..78247de497 100644 --- a/source/fe/fe_system.cc +++ b/source/fe/fe_system.cc @@ -15,6 +15,7 @@ #include #include +#include #include diff --git a/source/fe/fe_values.cc b/source/fe/fe_values.cc index 5d1e7b9d0c..b8b5142b5b 100644 --- a/source/fe/fe_values.cc +++ b/source/fe/fe_values.cc @@ -19,6 +19,7 @@ #include #include #include +#include #include diff --git a/source/grid/tria.cc b/source/grid/tria.cc index 7c94de04ef..25733185f0 100644 --- a/source/grid/tria.cc +++ b/source/grid/tria.cc @@ -16,6 +16,7 @@ #include #include +#include #include diff --git a/source/non_matching/fe_immersed_values.cc b/source/non_matching/fe_immersed_values.cc index e789f12a0f..508f939716 100644 --- a/source/non_matching/fe_immersed_values.cc +++ b/source/non_matching/fe_immersed_values.cc @@ -13,6 +13,8 @@ // // --------------------------------------------------------------------- +#include + #include #include -- 2.39.5