1 /**
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #pragma clang diagnostic ignored "-Wc11-extensions"
17
18 #include <cstdlib>
19
20 #include <gtest/gtest.h>
21 #include <pthread.h>
22
23 #define MC_ON
24 #include "unix/libpandabase/futex/fmutex.cpp"
25
26 pthread_t pthread_self(void);
27 // Copy of mutex storage, after complete implementation should totally replace mutex::current_tid
28 thread_local pthread_t current_tid;
29
30 namespace panda::test {
31 static struct fmutex g_x;
32 static int g_shared;
33
ThreadN(void *arg)34 static void *ThreadN(void *arg)
35 {
36 intptr_t index = reinterpret_cast<intptr_t>(arg);
37
38 bool ret;
39 do {
40 ret = MutexLock(&g_x, true);
41 } while (!ret);
42 EXPECT_EQ(g_x.recursiveCount, 1);
43
44 g_shared = index;
45 int r = g_shared;
46 EXPECT_TRUE(r == index);
47
48 MutexUnlock(&g_x);
49 EXPECT_EQ(g_x.recursiveCount, 0);
50 return nullptr;
51 }
52
HWTEST(FMutexTest, TryLockTest, testing::ext::TestSize.Level0)53 HWTEST(FMutexTest, TryLockTest, testing::ext::TestSize.Level0)
54 {
55 // The tests check mutex try lock
56 constexpr int N = 2;
57
58 MutexInit(&g_x);
59 ASSERT_EQ(g_x.recursiveCount, 0);
60 ASSERT_EQ(g_x.state_and_waiters_, 0);
61 ASSERT_EQ(g_x.exclusive_owner_, 0U);
62 ASSERT_FALSE(g_x.recursive_mutex_);
63
64 pthread_t t[N];
65 for (long i = 0u; i < N; i++) {
66 pthread_create(&t[i], nullptr, ThreadN, reinterpret_cast<void *>(i));
67 }
68
69 for (int i = 0u; i < N; i++) {
70 pthread_join(t[i], nullptr);
71 }
72
73 ASSERT_TRUE(MutexTryLockWithSpinning(&g_x));
74 MutexUnlock(&g_x);
75
76 MutexDestroy(&g_x);
77 ASSERT_FALSE(MutexDoNotCheckOnDeadlock());
78 }
79
80 }
81