1 /* This testcase is part of GDB, the GNU debugger.
3 Copyright 2017-2022 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 /* Enforce execution order between two threads using a lock. */
27 omp_set_lock_in_order (int num
, omp_lock_t
*lock
)
29 /* Ensure that thread num 0 first sets the lock. */
34 /* Block thread num 1 until it can set the lock. */
38 /* This bit here is guaranteed to be executed first by thread num 0, and
39 once thread num 0 unsets the lock, to be executed by thread num 1. */
43 /* Testcase for checking access to variables in a single / outer scope.
44 Make sure that variables not referred to in the parallel section are
45 accessible from the debugger. */
50 static int s1
= -41, s2
= -42, s3
= -43;
51 int i1
= 11, i2
= 12, i3
= 13;
53 #pragma omp parallel num_threads (2) shared (s1, i1) private (s2, i2)
55 int thread_num
= omp_get_thread_num ();
56 omp_set_lock_in_order (thread_num
, &lock
);
58 s2
= 100 * (thread_num
+ 1) + 2;
62 printf ("single_scope: thread_num=%d, s1=%d, i1=%d, s2=%d, i2=%d\n",
63 thread_num
, s1
, i1
, s2
, i2
);
65 omp_unset_lock (&lock
);
68 printf ("single_scope: s1=%d, s2=%d, s3=%d, i1=%d, i2=%d, i3=%d\n",
69 s1
, s2
, s3
, i1
, i2
, i3
);
72 static int file_scope_var
= 9876;
74 /* Testcase for checking access to variables from parallel region
75 nested within more than one lexical scope. Of particular interest
76 are variables which are not referenced in the parallel section. */
84 int i11
= 11, i12
= 12;
87 int i21
= -21, i22
= 22;
89 #pragma omp parallel num_threads (2) \
94 int thread_num
= omp_get_thread_num ();
95 omp_set_lock_in_order (thread_num
, &lock
);
97 i21
= 100 * (thread_num
+ 1) + 21;
100 printf ("multi_scope: thread_num=%d, i01=%d, i11=%d, i21=%d\n",
101 thread_num
, i01
, i11
, i21
);
103 omp_unset_lock (&lock
);
106 printf ("multi_scope: i01=%d, i02=%d, i11=%d, "
107 "i12=%d, i21=%d, i22=%d\n",
108 i01
, i02
, i11
, i12
, i21
, i22
);
113 /* Nested functions in C is a GNU extension. Some non-GNU compilers
114 define __GNUC__, but they don't support nested functions. So,
115 unfortunately, we can't use that for our test. */
116 #if HAVE_NESTED_FUNCTION_SUPPORT
118 /* Testcase for checking access of variables from within parallel
119 region in a lexically nested function. */
125 int i
= 1, j
= 2, k
= 3;
128 foo (int p
, int q
, int r
)
134 #pragma omp parallel num_threads (2) shared (i, p, x) private (j, q, y)
136 int tn
= omp_get_thread_num ();
137 omp_set_lock_in_order (tn
, &lock
);
143 printf ("nested_func: tn=%d: i=%d, p=%d, x=%d, j=%d, q=%d, y=%d\n",
144 tn
, i
, p
, x
, j
, q
, y
);
146 omp_unset_lock (&lock
);
153 i
= 101; j
= 102; k
= 103;
158 /* Testcase for checking access to variables from within a nested parallel
162 nested_parallel (void)
169 #pragma omp parallel num_threads (2) private (l)
171 int num
= omp_get_thread_num ();
172 omp_set_lock_in_order (num
, &lock
);
174 int nthr
= omp_get_num_threads ();
175 int off
= num
* nthr
;
178 #pragma omp parallel num_threads (2) shared (num)
180 int inner_num
= omp_get_thread_num ();
181 omp_set_lock_in_order (inner_num
, &lock2
);
184 printf ("nested_parallel (inner threads): outer thread num = %d, thread num = %d\n", num
, inner_num
);
186 omp_unset_lock (&lock2
);
189 printf ("nested_parallel (outer threads) %d: k = %d, l = %d\n", num
, k
, l
);
191 omp_unset_lock (&lock
);
196 main (int argc
, char **argv
)
198 omp_init_lock (&lock
);
199 omp_init_lock (&lock2
);
203 #if HAVE_NESTED_FUNCTION_SUPPORT
208 omp_destroy_lock (&lock
);
209 omp_destroy_lock (&lock2
);