[memprof] Upgrade a unit test to MemProf Version 3 (#117063)
[llvm-project.git] / openmp / runtime / test / worksharing / sections / omp_sections_nowait.c
blobcaff254f4330b89ee78be79c602b9355768519bc
1 // RUN: %libomp-compile-and-run
2 #include <stdio.h>
3 #include "omp_testsuite.h"
5 /*
6 * This test will hang if the nowait is not working properly
8 * It relies on a thread skipping to the second sections construct to
9 * release the threads in the first sections construct
11 * Also, since scheduling of sections is implementation defined, it is
12 * necessary to have all four sections in the second sections construct
13 * release the threads since we can't guarantee which section a single thread
14 * will execute.
16 volatile int release;
17 volatile int count;
19 void wait_for_release_then_increment(int rank)
21 fprintf(stderr, "Thread nr %d enters first section"
22 " and waits.\n", rank);
23 while (release == 0);
24 #pragma omp atomic
25 count++;
28 void release_and_increment(int rank)
30 fprintf(stderr, "Thread nr %d sets release to 1\n", rank);
31 release = 1;
32 #pragma omp flush(release)
33 #pragma omp atomic
34 count++;
37 int test_omp_sections_nowait()
39 release = 0;
40 count = 0;
42 #pragma omp parallel num_threads(4)
44 int rank;
45 rank = omp_get_thread_num ();
46 #pragma omp sections nowait
48 #pragma omp section
50 wait_for_release_then_increment(rank);
52 #pragma omp section
54 wait_for_release_then_increment(rank);
56 #pragma omp section
58 wait_for_release_then_increment(rank);
60 #pragma omp section
62 fprintf(stderr, "Thread nr %d enters first sections and goes "
63 "immediately to next sections construct to release.\n", rank);
64 #pragma omp atomic
65 count++;
68 /* Begin of second sections environment */
69 #pragma omp sections
71 #pragma omp section
73 release_and_increment(rank);
75 #pragma omp section
77 release_and_increment(rank);
79 #pragma omp section
81 release_and_increment(rank);
83 #pragma omp section
85 release_and_increment(rank);
89 // Check to make sure all eight sections were executed
90 return (count==8);
93 int main()
95 int i;
96 int num_failed=0;
98 for(i = 0; i < REPETITIONS; i++) {
99 if(!test_omp_sections_nowait()) {
100 num_failed++;
103 return num_failed;