1
1
// RUN: %clang_tsan %s -o %t
2
2
// RUN: %run %t 2>&1 | FileCheck %s --implicit-check-not='ThreadSanitizer'
3
3
4
+ #include <Availability.h>
4
5
#include <os/lock.h>
5
6
#include <pthread.h>
6
7
#include <stdio.h>
7
8
8
9
long global_variable ;
9
10
os_unfair_lock lock = OS_UNFAIR_LOCK_INIT ;
11
+ char flags_available = 0 ;
10
12
11
13
void * Thread (void * a ) {
12
14
os_unfair_lock_lock (& lock );
@@ -15,6 +17,22 @@ void *Thread(void *a) {
15
17
return NULL ;
16
18
}
17
19
20
+ void * ThreadWithFlags (void * a ) {
21
+ #if defined(__MAC_15_0 ) || defined(__IPHONE_18_0 ) || defined(__TVOS_18_0 ) || \
22
+ defined(__VISIONOS_2_0 ) || defined(__WATCHOS_11_0 )
23
+ # pragma clang diagnostic push
24
+ # pragma clang diagnostic ignored "-Wunguarded-availability-new"
25
+ os_unfair_lock_lock_with_flags (& lock , OS_UNFAIR_LOCK_FLAG_ADAPTIVE_SPIN );
26
+ flags_available = 1 ;
27
+ # pragma clang diagnostic pop
28
+ #else
29
+ os_unfair_lock_lock (& lock );
30
+ #endif
31
+ global_variable ++ ;
32
+ os_unfair_lock_unlock (& lock );
33
+ return NULL ;
34
+ }
35
+
18
36
int main () {
19
37
pthread_t t1 , t2 ;
20
38
global_variable = 0 ;
@@ -23,6 +41,16 @@ int main() {
23
41
pthread_join (t1 , NULL );
24
42
pthread_join (t2 , NULL );
25
43
fprintf (stderr , "global_variable = %ld\n" , global_variable );
44
+
45
+ // CHECK: global_variable = 2
46
+
47
+ pthread_create (& t1 , NULL , ThreadWithFlags , NULL );
48
+ pthread_create (& t2 , NULL , ThreadWithFlags , NULL );
49
+ pthread_join (t1 , NULL );
50
+ pthread_join (t2 , NULL );
51
+ fprintf (stderr ,
52
+ "global_variable = %ld, os_unfair_lock_lock_with_flags %savailable\n" ,
53
+ global_variable , flags_available ? "" : "un" );
26
54
}
27
55
28
- // CHECK: global_variable = 2
56
+ // CHECK: global_variable = 4
0 commit comments