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 #include <pthread.h>
17 #include <cstdlib>
18 #define MC_ON
19 #include "unix/libpandabase/futex/fmutex.cpp"
20
21 // The tests checks a recursive mutex
22
23 pthread_t pthread_self(void);
24 // Copy of mutex storage, after complete implementation should totally replace mutex::current_tid
25 thread_local pthread_t current_tid;
26
27 static struct fmutex g_x;
28 static int g_shared;
29
Threadn(void *arg)30 static void *Threadn(void *arg)
31 {
32 intptr_t index = reinterpret_cast<intptr_t>(arg);
33
34 bool ret;
35 MutexLock(&g_x, false);
36 MutexLock(&g_x, false);
37 g_shared = index;
38 int r = g_shared;
39 ASSERT(r == index);
40 MutexUnlock(&g_x);
41 MutexUnlock(&g_x);
42 return nullptr;
43 }
44
main()45 int main()
46 {
47 constexpr int N = 2;
48 MutexInit(&g_x);
49 g_x.recursive_mutex_ = true;
50 pthread_t t[N];
51
52 for (long i = 0u; i < N; i++) {
53 pthread_create(&t[i], nullptr, Threadn, reinterpret_cast<void *>(i));
54 }
55
56 for (int i = 0u; i < N; i++) {
57 pthread_join(t[i], nullptr);
58 }
59
60 MutexDestroy(&g_x);
61 return 0;
62 }
63