1 // RUN: %libomp-compile-and-run
6 #include "omp_my_sleep.h"
9 int task_grabbed
= 0, task_can_proceed
= 0;
10 int task2_grabbed
= 0, task2_can_proceed
= 0;
12 static void wait_on_flag(int *flag
) {
17 #pragma omp atomic read
21 if (secs
== timelimit
) {
22 fprintf(stderr
, "error: timeout in wait_on_flag()\n");
25 } while (flag_value
== 0);
28 static void signal_flag(int *flag
) {
33 int main(int argc
, char** argv
) {
35 // Ensure two threads are running
36 int num_threads
= omp_get_max_threads();
38 omp_set_num_threads(2);
40 #pragma omp parallel shared(a)
43 // Let us be extra safe here
44 if (omp_get_num_threads() > 1) {
45 #pragma omp single nowait
47 // Schedule independent child task that
48 // waits to be flagged after sebsequent taskwait depend()
51 signal_flag(&task_grabbed
);
52 wait_on_flag(&task_can_proceed
);
54 // Let another worker thread grab the task to execute
55 wait_on_flag(&task_grabbed
);
56 // This should be ignored since the task above has
57 // no dependency information
58 #pragma omp task if(0) depend(inout: a)
60 // Signal the independent task to proceed
61 signal_flag(&task_can_proceed
);
63 // Schedule child task with dependencies that taskwait does
65 #pragma omp task depend(inout: b)
67 signal_flag(&task2_grabbed
);
68 wait_on_flag(&task2_can_proceed
);
72 // Let another worker thread grab the task to execute
73 wait_on_flag(&task2_grabbed
);
74 // This should be ignored since the task above has
75 // dependency information on b instead of a
76 #pragma omp task if(0) depend(inout: a)
78 // Signal the task to proceed
79 signal_flag(&task2_can_proceed
);
81 // Generate one child task for taskwait
82 #pragma omp task shared(a) depend(inout: a)
88 #pragma omp task if(0) depend(inout: a)
91 #pragma omp atomic read
95 fprintf(stderr
, "error: dependent task was not executed before "
96 "taskwait finished\n");
99 } // #pragma omp single
100 } // if (num_threads > 1)
101 } // #pragma omp parallel