xref: /llvm-project/openmp/runtime/test/misc_bugs/cancellation_for_sections.c (revision 7d2cfa1fd5c5878acd3e8b3f44a6f892a3793f93)
1 // RUN: %libomp-compile && env OMP_CANCELLATION=true %libomp-run
2 // Clang had a bug until version 4.0.1 which resulted in a hang.
3 // UNSUPPORTED: clang-3, clang-4.0.0
4 
5 // Regression test for a bug in cancellation to cover effect of `#pragma omp cancel`
6 // in a loop construct, on sections construct.
7 // Pass condition: Cancellation status from `for` does not persist
8 // to `sections`.
9 
10 #include <stdio.h>
11 #include <omp.h>
12 
13 int result[2] = {0, 0};
14 
cq416850_for_sections()15 void cq416850_for_sections() {
16 
17     unsigned i;
18      // 1) loop
19     #pragma omp for
20     for (i = 0; i < 1; i++) {
21         result[0] = 1;
22         #pragma omp cancel for
23         result[0] = 2;
24     }
25 
26 //        printf("thread %d: result[0] = %d, result[1] = %d \n",  omp_get_thread_num(), result[0], result[1]);
27 
28 
29     // 2) sections
30     #pragma omp sections
31     {
32         #pragma omp section
33         {
34             result[1] = 1;
35             #pragma omp cancellation point sections
36             result[1] = 2;
37         }
38     }
39 }
40 
main(void)41 int main(void) {
42     if(!omp_get_cancellation()) {
43         printf("Cancellation not enabled!\n");
44         return 2;
45     }
46 
47     #pragma omp parallel num_threads(4)
48     {
49         cq416850_for_sections();
50     }
51 
52     if (result[0] != 1 || result[1] != 2) {
53         printf("Incorrect values. "
54                "result[0] = %d (expected 1), "
55                "result[1] = %d (expected 2).\n",
56                result[0], result[1]);
57         printf("FAILED\n");
58         return 1;
59     }
60 
61     printf("PASSED\n");
62     return 0;
63 }
64