Wrap uses of thread_mpi/mutex.h
authorTeemu Murtola <teemu.murtola@gmail.com>
Sun, 5 Jul 2015 04:12:43 +0000 (07:12 +0300)
committerGerrit Code Review <gerrit@gerrit.gromacs.org>
Tue, 7 Jul 2015 07:34:33 +0000 (09:34 +0200)
Introduce gmx::Mutex and gmx::lock_guard to hide the actual C++ mutex
implementation used, and make all code that was using tMPI::Mutex use
these.

For now, the implementation is just directly imported from thread-MPI,
but this allows changing the implementation (e.g., to C++11 std::mutex
or to a TBB mutex) when thread-MPI is no longer appropriate, with mainly
changing this single header.  Also, this makes it clearer to introduce
new C++ wrappers for things like tMPI_Lock_t, as they can now be written
in this wrapper layer, instead of in thread-MPI where they will
introduce additional such dependencies.

Change-Id: Ie4c91d4e74c5dbc5e4d2b5d7fb5a76b73ef5616b

src/gromacs/commandline/cmdlineprogramcontext.cpp
src/gromacs/fft/fft5d.cpp
src/gromacs/fft/fft_fftw3.cpp
src/gromacs/selection/nbsearch.cpp
src/gromacs/utility/errorcodes.cpp
src/gromacs/utility/mutex.h [new file with mode: 0644]
src/testutils/testoptions.cpp

index 84204246873d172544060c5df7002ac111816541..64308a1a4cf64137efd204d0461f5df06d52b3da 100644 (file)
 
 #include <boost/scoped_ptr.hpp>
 
-#include "thread_mpi/mutex.h"
-
 #include "buildinfo.h"
 #include "gromacs/utility/exceptions.h"
 #include "gromacs/utility/gmxassert.h"
+#include "gromacs/utility/mutex.h"
 #include "gromacs/utility/path.h"
 #include "gromacs/utility/stringutil.h"
 
@@ -321,7 +320,7 @@ class CommandLineProgramContext::Impl
         mutable std::string           fullBinaryPath_;
         mutable std::string           installationPrefix_;
         mutable bool                  bSourceLayout_;
-        mutable tMPI::mutex           binaryPathMutex_;
+        mutable Mutex                 binaryPathMutex_;
 };
 
 CommandLineProgramContext::Impl::Impl()
@@ -414,14 +413,14 @@ const char *CommandLineProgramContext::commandLine() const
 
 const char *CommandLineProgramContext::fullBinaryPath() const
 {
-    tMPI::lock_guard<tMPI::mutex> lock(impl_->binaryPathMutex_);
+    lock_guard<Mutex> lock(impl_->binaryPathMutex_);
     impl_->findBinaryPath();
     return impl_->fullBinaryPath_.c_str();
 }
 
 InstallationPrefixInfo CommandLineProgramContext::installationPrefix() const
 {
-    tMPI::lock_guard<tMPI::mutex> lock(impl_->binaryPathMutex_);
+    lock_guard<Mutex> lock(impl_->binaryPathMutex_);
     if (impl_->installationPrefix_.empty())
     {
         impl_->findBinaryPath();
index 5cd3dfae97cc6cdab74c4c113b4f5e62e636a22a..38b105a76164480d0a03a325a9bcbef4af3f0c9f 100644 (file)
@@ -78,12 +78,11 @@ FILE* debug = 0;
 #endif
 
 #ifdef GMX_FFT_FFTW3
-#include "thread_mpi/mutex.h"
-
 #include "gromacs/utility/exceptions.h"
+#include "gromacs/utility/mutex.h"
 /* none of the fftw3 calls, except execute(), are thread-safe, so
    we need to serialize them with this mutex. */
-static tMPI::mutex big_fftw_mutex;
+static gmx::Mutex big_fftw_mutex;
 #define FFTW_LOCK try { big_fftw_mutex.lock(); } GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR
 #define FFTW_UNLOCK try { big_fftw_mutex.unlock(); } GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR
 #endif /* GMX_FFT_FFTW3 */
index dee2ff284b473c41aa250438cbde4f824cf9dd53..2c1f0b80db0634243d2cb7942c7c249230aa7d03 100644 (file)
@@ -2,7 +2,7 @@
  * This file is part of the GROMACS molecular simulation package.
  *
  * Copyright (c) 1991-2003 David van der Spoel, Erik Lindahl, University of Groningen.
- * Copyright (c) 2013,2014, by the GROMACS development team, led by
+ * Copyright (c) 2013,2014,2015, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
 
 #include <fftw3.h>
 
-#include "thread_mpi/mutex.h"
-
 #include "gromacs/fft/fft.h"
 #include "gromacs/utility/exceptions.h"
 #include "gromacs/utility/fatalerror.h"
+#include "gromacs/utility/mutex.h"
 
 #ifdef GMX_DOUBLE
 #define FFTWPREFIX(name) fftw_ ## name
@@ -56,7 +55,7 @@
 
 /* none of the fftw3 calls, except execute(), are thread-safe, so
    we need to serialize them with this mutex. */
-static tMPI::mutex big_fftw_mutex;
+static gmx::Mutex big_fftw_mutex;
 #define FFTW_LOCK try { big_fftw_mutex.lock(); } GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR
 #define FFTW_UNLOCK try { big_fftw_mutex.unlock(); } GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR
 
index 768be462d87beaa763113edb6043f3e43ebb38dc..86fdcbefaed185a097378354f35f05ef366567f3 100644 (file)
@@ -62,8 +62,6 @@
 #include <algorithm>
 #include <vector>
 
-#include "thread_mpi/mutex.h"
-
 #include "gromacs/legacyheaders/names.h"
 #include "gromacs/math/vec.h"
 #include "gromacs/pbcutil/pbc.h"
@@ -72,6 +70,7 @@
 #include "gromacs/utility/arrayref.h"
 #include "gromacs/utility/exceptions.h"
 #include "gromacs/utility/gmxassert.h"
+#include "gromacs/utility/mutex.h"
 #include "gromacs/utility/stringutil.h"
 
 namespace gmx
@@ -309,7 +308,7 @@ class AnalysisNeighborhoodSearchImpl
         //! Data structure to hold the grid cell contents.
         CellList                cells_;
 
-        tMPI::mutex             createPairSearchMutex_;
+        Mutex                   createPairSearchMutex_;
         PairSearchList          pairSearchList_;
 
         friend class AnalysisNeighborhoodPairSearchImpl;
@@ -439,7 +438,7 @@ AnalysisNeighborhoodSearchImpl::~AnalysisNeighborhoodSearchImpl()
 AnalysisNeighborhoodSearchImpl::PairSearchImplPointer
 AnalysisNeighborhoodSearchImpl::getPairSearch()
 {
-    tMPI::lock_guard<tMPI::mutex> lock(createPairSearchMutex_);
+    lock_guard<Mutex> lock(createPairSearchMutex_);
     // TODO: Consider whether this needs to/can be faster, e.g., by keeping a
     // separate pool of unused search objects.
     PairSearchList::const_iterator i;
@@ -1252,7 +1251,7 @@ class AnalysisNeighborhood::Impl
 
         SearchImplPointer getSearch();
 
-        tMPI::mutex             createSearchMutex_;
+        Mutex                   createSearchMutex_;
         SearchList              searchList_;
         real                    cutoff_;
         const t_blocka         *excls_;
@@ -1263,7 +1262,7 @@ class AnalysisNeighborhood::Impl
 AnalysisNeighborhood::Impl::SearchImplPointer
 AnalysisNeighborhood::Impl::getSearch()
 {
-    tMPI::lock_guard<tMPI::mutex> lock(createSearchMutex_);
+    lock_guard<Mutex> lock(createSearchMutex_);
     // TODO: Consider whether this needs to/can be faster, e.g., by keeping a
     // separate pool of unused search objects.
     SearchList::const_iterator i;
index 00c57aa67c4ac86d3f71fba0b1c96aaedf72d883..59df7ce47edf9f71a23b65f437ffb2a3c5438bb4 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2010,2011,2012,2013,2014, by the GROMACS development team, led by
+ * Copyright (c) 2010,2011,2012,2013,2014,2015, by the GROMACS development team, led by
  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
  * and including many others, as listed in the AUTHORS file in the
  * top-level source directory and at http://www.gromacs.org.
@@ -45,7 +45,7 @@
 
 #include <cstdlib>
 
-#include "thread_mpi/mutex.h"
+#include "gromacs/utility/mutex.h"
 
 #include "errorformat.h"
 
@@ -100,7 +100,7 @@ void standardErrorHandler(int retcode, const char *msg,
 //! Global error handler set with setFatalErrorHandler().
 ErrorHandlerFunc g_errorHandler = standardErrorHandler;
 //! Mutex for protecting access to ::g_errorHandler.
-tMPI::mutex      handler_mutex;
+Mutex            handler_mutex;
 
 //! \}
 
@@ -117,8 +117,8 @@ const char *getErrorCodeString(int errorcode)
 
 ErrorHandlerFunc setFatalErrorHandler(ErrorHandlerFunc handler)
 {
-    tMPI::lock_guard<tMPI::mutex> lock(handler_mutex);
-    ErrorHandlerFunc              oldHandler = g_errorHandler;
+    lock_guard<Mutex> lock(handler_mutex);
+    ErrorHandlerFunc  oldHandler = g_errorHandler;
     g_errorHandler = handler;
     return oldHandler;
 }
@@ -131,7 +131,7 @@ void fatalError(int retcode, const char *msg, const char *file, int line)
 {
     ErrorHandlerFunc handler = NULL;
     {
-        tMPI::lock_guard<tMPI::mutex> lock(handler_mutex);
+        lock_guard<Mutex> lock(handler_mutex);
         handler = g_errorHandler;
     }
     if (handler != NULL)
diff --git a/src/gromacs/utility/mutex.h b/src/gromacs/utility/mutex.h
new file mode 100644 (file)
index 0000000..34c7375
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+ * This file is part of the GROMACS molecular simulation package.
+ *
+ * Copyright (c) 2015, by the GROMACS development team, led by
+ * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
+ * and including many others, as listed in the AUTHORS file in the
+ * top-level source directory and at http://www.gromacs.org.
+ *
+ * GROMACS is free software; 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 2.1
+ * of the License, or (at your option) any later version.
+ *
+ * GROMACS is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with GROMACS; if not, see
+ * http://www.gnu.org/licenses, or write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
+ *
+ * If you want to redistribute modifications to GROMACS, please
+ * consider that scientific software is very special. Version
+ * control is crucial - bugs must be traceable. We will be happy to
+ * consider code for inclusion in the official distribution, but
+ * derived work must not be called official GROMACS. Details are found
+ * in the README & COPYING files - if they are missing, get the
+ * official version at http://www.gromacs.org.
+ *
+ * To help us fund GROMACS development, we humbly ask that you cite
+ * the research papers on the package. Check out http://www.gromacs.org.
+ */
+/*! \libinternal \file
+ * \brief
+ * Declares C++11-style basic threading primitives
+ * (gmx::Mutex, gmx::lock_guard).
+ *
+ * For now, the implementation is imported from thread-MPI.
+ *
+ * \author Teemu Murtola <teemu.murtola@gmail.com>
+ * \inlibraryapi
+ * \ingroup module_utility
+ */
+#ifndef GMX_THREADING_MUTEX_H
+#define GMX_THREADING_MUTEX_H
+
+#include "thread_mpi/mutex.h"
+
+namespace gmx
+{
+
+//! \cond libapi
+/*! \libinternal \brief
+ * C++11-compatible basic mutex.
+ */
+typedef tMPI::mutex Mutex;
+//! \endcond
+using tMPI::lock_guard;
+
+} // namespace gmx
+
+#endif
index 9bc036f51dd9cd3463a9a4bade3bec64e62579f1..976b8d79892061c74a6ac35928e68428165fdd30 100644 (file)
@@ -45,9 +45,8 @@
 
 #include <list>
 
-#include "thread_mpi/mutex.h"
-
 #include "gromacs/utility/classhelpers.h"
+#include "gromacs/utility/mutex.h"
 
 namespace gmx
 {
@@ -75,7 +74,7 @@ class TestOptionsRegistry
         //! Adds a provider into the registry.
         void add(const char * /*name*/, TestOptionsProvider *provider)
         {
-            tMPI::lock_guard<tMPI::mutex> lock(listMutex_);
+            lock_guard<Mutex> lock(listMutex_);
             providerList_.push_back(provider);
         }
 
@@ -87,7 +86,7 @@ class TestOptionsRegistry
 
         typedef std::list<TestOptionsProvider *> ProviderList;
 
-        tMPI::mutex             listMutex_;
+        Mutex                   listMutex_;
         ProviderList            providerList_;
 
         GMX_DISALLOW_COPY_AND_ASSIGN(TestOptionsRegistry);
@@ -97,7 +96,7 @@ void TestOptionsRegistry::initOptions(Options *options)
 {
     // TODO: Have some deterministic order for the options; now it depends on
     // the order in which the global initializers are run.
-    tMPI::lock_guard<tMPI::mutex> lock(listMutex_);
+    lock_guard<Mutex>             lock(listMutex_);
     ProviderList::const_iterator  i;
     for (i = providerList_.begin(); i != providerList_.end(); ++i)
     {