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