7 static int only_touch_stackvar
;
9 /* We should have no error on local and global
10 as these are both thread local variables. */
11 static __thread
int local
;
14 /* We will wrongly share this variable indirectly through a pointer
15 We should have an error for this. */
16 static __thread
int badly_shared_local
;
18 /* ptr_to_badly_shared_local allows to have multiple threads seeing
19 the same thread local storage. This is however really bad sharing
20 as this can cause SEGV or whatever, as when the thread disappears,
21 the badly_shared_local var pointed to can also disappear.
22 By default, the regtest does not test this really bad sharing. */
23 pthread_mutex_t protect_ptr_to_badly_shared_local
= PTHREAD_MUTEX_INITIALIZER
;
24 int *ptr_to_badly_shared_local
;
26 static void local_false_positive(void)
28 local
= local
+ 1; // no error is expected
31 static void global_false_positive(void)
33 global
= global
+ 1; // no error is expected
36 static void badly_shared_local_error_expected(void)
38 *ptr_to_badly_shared_local
= *ptr_to_badly_shared_local
+ 1; // an error is expected
39 // This can cause a SIGSEGV.
42 static void *level2(void *p
)
46 stackvar
= stackvar
+ only_touch_stackvar
;
48 local_false_positive();
49 global_false_positive();
50 if (only_touch_stackvar
!= 0) {
51 badly_shared_local_error_expected();
57 static void *level1(void *p
)
59 pthread_t threads
[NLEVEL2
];
63 pthread_mutex_lock(&protect_ptr_to_badly_shared_local
);
64 if (ptr_to_badly_shared_local
== NULL
)
65 ptr_to_badly_shared_local
= &badly_shared_local
;
66 pthread_mutex_unlock(&protect_ptr_to_badly_shared_local
);
68 for(i
= 0; i
< NLEVEL2
; i
++) {
69 pthread_create(&threads
[curthread
++], NULL
, level2
, NULL
);
72 for(i
= 0; i
< curthread
; i
++)
73 pthread_join(threads
[i
], NULL
);
79 int main(int argc
, char*argv
[])
81 pthread_t threads
[NLEVEL1
];
85 only_touch_stackvar
= argc
> 1;
87 for(i
= 0; i
< NLEVEL1
; i
++) {
88 pthread_create(&threads
[curthread
++], NULL
, level1
, NULL
);
91 fprintf(stderr
, "starting join in main\n");
93 for(i
= 0; i
< curthread
; i
++)
94 pthread_join(threads
[i
], NULL
);
95 fprintf(stderr
, "finished join in main\n");
102 fprintf(stderr
, "starting join in main\n");
105 fprintf(stderr
, "finished join in main\n");