2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2017,2018, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
36 * \brief Tests for gmx::Mutex
38 * These tests ensure that basic mutual-exclusion properties hold.
39 * Note that no testing can prove there isn't a bug, but if one
40 * exists, then these tests might expose one.
42 * In particular, try_lock can be implemented differently on different
43 * platforms, or with different default mutex types, so we should
44 * check that the behaviour continues to conform with the thread-MPI
47 * \author Mark Abraham <mark.j.abraham@gmail.com>
48 * \ingroup module_utility
53 #include "gromacs/utility/mutex.h"
58 #include <gtest/gtest.h>
68 //! Convenience definition.
69 using Lock = gmx::lock_guard<Mutex>;
71 TEST(MutexBasicTest, CanBeMade)
76 TEST(MutexBasicTest, CanBeLocked)
79 ASSERT_NO_THROW(m.lock());
83 TEST(MutexBasicTest, CanBeTryLocked)
86 ASSERT_TRUE(m.try_lock());
90 TEST(MutexBasicTest, CanBeUsedInLockGuard)
96 //! A shared value for a mutex to protect
98 //! A mutex to protect a shared value
99 Mutex g_sharedValueMutex;
101 //! Function type for asynchronous tasks.
102 using TaskType = std::function<int(void)>;
104 //! A task that just does work.
105 int updateSharedValue()
107 return ++g_sharedValue;
110 //! A task that does work after it gets the mutex.
111 int updateSharedValueWithLock()
113 Lock guard(g_sharedValueMutex);
114 return updateSharedValue();
117 //! A task that does work only if it can get the mutex immediately.
118 int updateSharedValueWithTryLock()
120 // Special return value to signal when work was not done because
121 // the lock was not acquired.
123 if (g_sharedValueMutex.try_lock())
125 result = updateSharedValue();
126 g_sharedValueMutex.unlock();
131 /*! \brief Parameterized test fixture.
133 * Checks that different launch policies work. In further tests of
134 * mutual exclusion, we need to specify std::thread::async, to require
135 * that a thread actually launched. The default policy permits the
136 * std:: implementation to avoid launching a thread, and at least the
137 * behaviour of thread-MPI try_lock also varies with the threading
138 * implementation underlying it. */
139 class DifferentTasksTest : public ::testing::TestWithParam<TaskType>
147 //! Check the results
151 EXPECT_NO_THROW(result = futureResult_.get()) << "Future should not contain an exception";
152 EXPECT_EQ(1, result) << "Task should have run";
153 EXPECT_EQ(1, g_sharedValue) << "Shared value should be updated";
155 //! Contains the result the task returns.
156 std::future<int> futureResult_;
159 TEST_P(DifferentTasksTest, StdAsyncWorksWithDefaultPolicy)
161 auto task = GetParam();
162 EXPECT_NO_THROW(futureResult_ = std::async(task)) << "Async should succeed";
166 TEST_P(DifferentTasksTest, StdAsyncWorksWithAsyncLaunchPolicy)
168 auto task = GetParam();
169 EXPECT_NO_THROW(futureResult_ = std::async(std::launch::async, task)) << "Async should succeed";
173 TEST_P(DifferentTasksTest, StdAsyncWorksWithDeferredLaunchPolicy)
175 auto task = GetParam();
176 EXPECT_NO_THROW(futureResult_ = std::async(std::launch::deferred, task)) << "Async should succeed";
180 // Test that the different launch policies work with the different tasks
181 INSTANTIATE_TEST_CASE_P(WithAndWithoutMutex, DifferentTasksTest, ::testing::Values(updateSharedValue, updateSharedValueWithLock, updateSharedValueWithTryLock));
183 TEST(MutexTaskTest, MutualExclusionWorksWithLock)
186 std::future<int> result;
188 // Hold the mutex, launch a lock attempt on another
189 // thread, check that the shared value isn't changed, then
190 // release the mutex by leaving the scope, after which the
191 // other thread's lock can get the mutex.
192 Lock guard(g_sharedValueMutex);
193 result = std::async(std::launch::async, updateSharedValueWithLock);
194 EXPECT_EQ(0, g_sharedValue) << "Task should not have run yet";
196 EXPECT_EQ(1, result.get()) << "Task should have run";
197 EXPECT_EQ(1, g_sharedValue) << "Shared value should be updated";
200 TEST(MutexTaskTest, MutualExclusionWorksWithTryLockOnOtherThread)
204 // Hold the mutex, launch a try_lock attempt on another
205 // thread, check that the shared value isn't changed, then
206 // make sure the try_lock attempt has returned, double check
207 // that the shared value isn't changed, and release the mutex
208 // by leaving the scope.
209 Lock guard(g_sharedValueMutex);
210 auto result = std::async(std::launch::async, updateSharedValueWithTryLock);
211 EXPECT_EQ(0, g_sharedValue) << "Data race detected";
212 EXPECT_EQ(-1, result.get()) << "The try_lock should fail";
213 EXPECT_EQ(0, g_sharedValue) << "Task should not have run";
215 EXPECT_EQ(0, g_sharedValue) << "Mutex release can't affect the protected value";
218 TEST(MutexTaskTest, MutualExclusionWorksWithTryLockOnSameThread)
221 int finalSharedValue = GMX_NATIVE_WINDOWS ? 1 : 0;
223 // Hold the mutex and launch a try_lock attempt on this
224 // thread. Behaviour then varies with the implementation
225 // underlying thread-MPI.
226 Lock guard(g_sharedValueMutex);
227 int result = updateSharedValueWithTryLock();
228 if (GMX_NATIVE_WINDOWS)
230 EXPECT_EQ(1, result) << "The try_lock should succeed";
231 EXPECT_EQ(finalSharedValue, g_sharedValue) << "Task should have run";
235 EXPECT_EQ(-1, result) << "The try_lock should fail";
236 EXPECT_EQ(finalSharedValue, g_sharedValue) << "Task should not have run";
239 EXPECT_EQ(finalSharedValue, g_sharedValue) << "Mutex release can't affect the protected value";