xref: /llvm-project/openmp/runtime/test/api/kmp_set_defaults_lock_bug.c (revision 7f976d556ac72f5063a06a475c669e1bba868167)
1 // RUN: %libomp-compile-and-run
2 #include <stdio.h>
3 #include "omp_testsuite.h"
4 /* The bug occurs if the lock table is reallocated after
5    kmp_set_defaults() is called.  If the table is reallocated,
6    then the lock will not point to a valid lock object after the
7    kmp_set_defaults() call.*/
8 omp_lock_t lock;
9 
test_kmp_set_defaults_lock_bug()10 int test_kmp_set_defaults_lock_bug()
11 {
12   /* checks that omp_get_num_threads is equal to the number of
13      threads */
14   int nthreads_lib;
15   int nthreads = 0;
16 
17   nthreads_lib = -1;
18 
19   #pragma omp parallel
20   {
21     omp_set_lock(&lock);
22     nthreads++;
23     omp_unset_lock(&lock);
24     #pragma omp single
25     {
26       nthreads_lib = omp_get_num_threads ();
27     }  /* end of single */
28   } /* end of parallel */
29   kmp_set_defaults("OMP_NUM_THREADS");
30   #pragma omp parallel
31   {
32     omp_set_lock(&lock);
33     nthreads++;
34     omp_unset_lock(&lock);
35   } /* end of parallel */
36 
37   return (nthreads == 2*nthreads_lib);
38 }
39 
main()40 int main()
41 {
42   int i;
43   int num_failed=0;
44   omp_init_lock(&lock);
45 
46   for(i = 0; i < REPETITIONS; i++) {
47     if(!test_kmp_set_defaults_lock_bug()) {
48       num_failed++;
49     }
50   }
51   omp_destroy_lock(&lock);
52   return num_failed;
53 }
54