xref: /llvm-project/compiler-rt/test/sanitizer_common/TestCases/Linux/pthread_mutex.cpp (revision dd87c5b322684b072e9df3058b84d09a702a1ea4)
1 // RUN: %clangxx -O1 %s -o %t && %run %t
2 // RUN: %clangxx -O1 -DUSE_GLIBC %s -o %t && %run %t
3 // UNSUPPORTED: android
4 
5 #include <pthread.h>
6 
7 #if !defined(__GLIBC_PREREQ)
8 #define __GLIBC_PREREQ(a, b) 0
9 #endif
10 
11 #if defined(USE_GLIBC) && !__GLIBC_PREREQ(2, 34)
12 // They were removed from GLIBC 2.34
13 extern "C" int __pthread_mutex_lock(pthread_mutex_t *__mutex);
14 extern "C" int __pthread_mutex_unlock(pthread_mutex_t *__mutex);
15 #define LOCK __pthread_mutex_lock
16 #define UNLOCK __pthread_mutex_unlock
17 #else
18 #define LOCK pthread_mutex_lock
19 #define UNLOCK pthread_mutex_unlock
20 #endif
21 
22 pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
23 int x;
24 
Start(void * arg)25 static void *Start(void *arg) {
26   LOCK(&m);
27   ++x;
28   UNLOCK(&m);
29   return nullptr;
30 }
31 
main()32 int main() {
33   pthread_t threads[2] = {};
34   for (pthread_t &t : threads)
35     pthread_create(&t, 0, &Start, 0);
36   for (pthread_t &t : threads)
37     pthread_join(t, 0);
38   return 0;
39 }
40