4fbc0ad947d27d44995105fdae49afd3823b2d4e
[alexxy/gromacs.git] / src / gromacs / utility / tests / mutex.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35 /*! \internal \file
36  * \brief Tests for gmx::Mutex
37  *
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.
41  *
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
45  * documentation.
46  *
47  * \author Mark Abraham <mark.j.abraham@gmail.com>
48  * \ingroup module_utility
49  */
50
51 #include "gmxpre.h"
52
53 #include "gromacs/utility/mutex.h"
54
55 #include "config.h"
56
57 #include <future>
58 #include <gtest/gtest.h>
59
60
61 namespace gmx
62 {
63 namespace test
64 {
65 namespace
66 {
67
68 //! Convenience definition.
69 using Lock = gmx::lock_guard<Mutex>;
70
71 TEST(MutexBasicTest, CanBeMade)
72 {
73     Mutex m;
74 }
75
76 TEST(MutexBasicTest, CanBeLocked)
77 {
78     Mutex m;
79     ASSERT_NO_THROW(m.lock());
80     m.unlock();
81 }
82
83 TEST(MutexBasicTest, CanBeTryLocked)
84 {
85     Mutex m;
86     ASSERT_TRUE(m.try_lock());
87     m.unlock();
88 }
89
90 TEST(MutexBasicTest, CanBeUsedInLockGuard)
91 {
92     Mutex m;
93     Lock  g(m);
94 }
95
96 //! A shared value for a mutex to protect
97 static int   g_sharedValue;
98 //! A mutex to protect a shared value
99 static Mutex g_sharedValueMutex;
100
101 //! Function type for asynchronous tasks.
102 using TaskType = std::function<int(void)>;
103
104 //! A task that just does work.
105 int updateSharedValue()
106 {
107     return ++g_sharedValue;
108 }
109
110 //! A task that does work after it gets the mutex.
111 int updateSharedValueWithLock()
112 {
113     Lock guard(g_sharedValueMutex);
114     return updateSharedValue();
115 }
116
117 //! A task that does work only if it can get the mutex immediately.
118 int updateSharedValueWithTryLock()
119 {
120     // Special return value to signal when work was not done because
121     // the lock was not acquired.
122     int result = -1;
123     if (g_sharedValueMutex.try_lock())
124     {
125         result = updateSharedValue();
126         g_sharedValueMutex.unlock();
127     }
128     return result;
129 }
130
131 /*! \brief Parameterized test fixture.
132  *
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>
140 {
141     public:
142         //! Constructor
143         DifferentTasksTest()
144         {
145             g_sharedValue = 0;
146         }
147         //! Check the results
148         void checkResults()
149         {
150             int result = 0;
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";
154         }
155         //! Contains the result the task returns.
156         std::future<int> futureResult_;
157 };
158
159 TEST_P(DifferentTasksTest, StdAsyncWorksWithDefaultPolicy)
160 {
161     auto task = GetParam();
162     EXPECT_NO_THROW(futureResult_ = std::async(task)) << "Async should succeed";
163     checkResults();
164 }
165
166 TEST_P(DifferentTasksTest, StdAsyncWorksWithAsyncLaunchPolicy)
167 {
168     auto task = GetParam();
169     EXPECT_NO_THROW(futureResult_ = std::async(std::launch::async, task)) << "Async should succeed";
170     checkResults();
171 }
172
173 TEST_P(DifferentTasksTest, StdAsyncWorksWithDeferredLaunchPolicy)
174 {
175     auto task = GetParam();
176     EXPECT_NO_THROW(futureResult_ = std::async(std::launch::deferred, task)) << "Async should succeed";
177     checkResults();
178 }
179
180 // Test that the different launch policies work with the different tasks
181 INSTANTIATE_TEST_CASE_P(WithAndWithoutMutex, DifferentTasksTest, ::testing::Values(updateSharedValue, updateSharedValueWithLock, updateSharedValueWithTryLock));
182
183 TEST(MutexTaskTest, MutualExclusionWorksWithLock)
184 {
185     g_sharedValue = 0;
186     std::future<int> result;
187     {
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";
195     }
196     EXPECT_EQ(1, result.get()) << "Task should have run";
197     EXPECT_EQ(1, g_sharedValue) << "Shared value should be updated";
198 }
199
200 TEST(MutexTaskTest, MutualExclusionWorksWithTryLockOnOtherThread)
201 {
202     g_sharedValue = 0;
203     {
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";
214     }
215     EXPECT_EQ(0, g_sharedValue) << "Mutex release can't affect the protected value";
216 }
217
218 TEST(MutexTaskTest, MutualExclusionWorksWithTryLockOnSameThread)
219 {
220     g_sharedValue = 0;
221     int finalSharedValue = GMX_NATIVE_WINDOWS ? 1 : 0;
222     {
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)
229         {
230             EXPECT_EQ(1, result) << "The try_lock should succeed";
231             EXPECT_EQ(finalSharedValue, g_sharedValue) << "Task should have run";
232         }
233         else
234         {
235             EXPECT_EQ(-1, result) << "The try_lock should fail";
236             EXPECT_EQ(finalSharedValue, g_sharedValue) << "Task should not have run";
237         }
238     }
239     EXPECT_EQ(finalSharedValue, g_sharedValue) << "Mutex release can't affect the protected value";
240 }
241
242 } // namespace
243 } // namespace
244 } // namespace