xref: /llvm-project/openmp/runtime/test/lock/omp_init_lock.c (revision 42016791101782a0f5e64e1ea9ea282e27a8132a)
1 // RUN: %libomp-compile-and-run
2 #include "omp_testsuite.h"
3 #include <stdio.h>
4 
5 // This should be slightly less than KMP_I_LOCK_CHUNK, which is 1024
6 #define LOCKS_PER_ITER 1000
7 #define ITERATIONS (REPETITIONS + 1)
8 
9 // This tests concurrently using locks on one thread while initializing new
10 // ones on another thread.  This exercises the global lock pool.
test_omp_init_lock()11 int test_omp_init_lock() {
12   int i;
13   omp_lock_t lcks[ITERATIONS * LOCKS_PER_ITER];
14 #pragma omp parallel for schedule(static) num_threads(NUM_TASKS)
15   for (i = 0; i < ITERATIONS; i++) {
16     int j;
17     omp_lock_t *my_lcks = &lcks[i * LOCKS_PER_ITER];
18     for (j = 0; j < LOCKS_PER_ITER; j++) {
19       omp_init_lock(&my_lcks[j]);
20     }
21     for (j = 0; j < LOCKS_PER_ITER * 100; j++) {
22       omp_set_lock(&my_lcks[j % LOCKS_PER_ITER]);
23       omp_unset_lock(&my_lcks[j % LOCKS_PER_ITER]);
24     }
25   }
26   // Wait until all repetitions are done.  The test is exercising growth of
27   // the global lock pool, which does not shrink when no locks are allocated.
28   {
29     int j;
30     for (j = 0; j < ITERATIONS * LOCKS_PER_ITER; j++) {
31       omp_destroy_lock(&lcks[j]);
32     }
33   }
34 
35   return 0;
36 }
37 
main()38 int main() {
39   // No use repeating this test, since it's exercising a private global pool
40   // which is not reset between test iterations.
41   return test_omp_init_lock();
42 }
43