]>
Commit | Line | Data |
---|---|---|
fd6481cf | 1 | /* Copyright (C) 2006, 2008 Free Software Foundation, Inc. |
c1c80391 | 2 | |
3 | This file is part of the GNU OpenMP Library (libgomp). | |
4 | ||
5 | Libgomp is free software; you can redistribute it and/or modify it | |
6 | under the terms of the GNU Lesser General Public License as published by | |
7 | the Free Software Foundation; either version 2.1 of the License, or | |
8 | (at your option) any later version. | |
9 | ||
10 | Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY | |
11 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | |
12 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for | |
13 | more details. | |
14 | ||
15 | You should have received a copy of the GNU Lesser General Public License | |
16 | along with libgomp; see the file COPYING.LIB. If not, write to the | |
17 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | |
18 | MA 02110-1301, USA. */ | |
19 | ||
20 | /* As a special exception, if you link this library with other files, some | |
21 | of which are compiled with GCC, to produce an executable, this library | |
22 | does not by itself cause the resulting executable to be covered by the | |
23 | GNU General Public License. This exception does not however invalidate | |
24 | any other reasons why the executable file might be covered by the GNU | |
25 | General Public License. */ | |
26 | ||
27 | /* This is the POSIX95 implementation of the public OpenMP locking primitives. | |
28 | ||
29 | Because OpenMP uses different entry points for normal and recursive | |
30 | locks, and pthreads uses only one entry point, a system may be able | |
31 | to do better and streamline the locking as well as reduce the size | |
32 | of the types exported. */ | |
33 | ||
34 | #include "libgomp.h" | |
35 | ||
fd6481cf | 36 | #ifdef HAVE_BROKEN_POSIX_SEMAPHORES |
37 | void | |
38 | gomp_init_lock_30 (omp_lock_t *lock) | |
39 | { | |
40 | pthread_mutex_init (lock, NULL); | |
41 | } | |
42 | ||
43 | void | |
44 | gomp_destroy_lock_30 (omp_lock_t *lock) | |
45 | { | |
46 | pthread_mutex_destroy (lock); | |
47 | } | |
48 | ||
49 | void | |
50 | gomp_set_lock_30 (omp_lock_t *lock) | |
51 | { | |
52 | pthread_mutex_lock (lock); | |
53 | } | |
54 | ||
55 | void | |
56 | gomp_unset_lock_30 (omp_lock_t *lock) | |
57 | { | |
58 | pthread_mutex_unlock (lock); | |
59 | } | |
60 | ||
61 | int | |
62 | gomp_test_lock_30 (omp_lock_t *lock) | |
63 | { | |
64 | return pthread_mutex_trylock (lock) == 0; | |
65 | } | |
66 | ||
67 | void | |
68 | gomp_init_nest_lock_30 (omp_nest_lock_t *lock) | |
69 | { | |
70 | pthread_mutex_init (&lock->lock, NULL); | |
71 | lock->owner = NULL; | |
72 | lock->count = 0; | |
73 | } | |
74 | ||
75 | void | |
76 | gomp_destroy_nest_lock_30 (omp_nest_lock_t *lock) | |
77 | { | |
78 | pthread_mutex_destroy (&lock->lock); | |
79 | } | |
c1c80391 | 80 | |
81 | void | |
fd6481cf | 82 | gomp_set_nest_lock_30 (omp_nest_lock_t *lock) |
83 | { | |
84 | void *me = gomp_icv (true); | |
85 | ||
86 | if (lock->owner != me) | |
87 | { | |
88 | pthread_mutex_lock (&lock->lock); | |
89 | lock->owner = me; | |
90 | } | |
91 | ||
92 | lock->count++; | |
93 | } | |
94 | ||
95 | void | |
96 | gomp_unset_nest_lock_30 (omp_nest_lock_t *lock) | |
97 | { | |
98 | lock->count--; | |
99 | ||
100 | if (lock->count == 0) | |
101 | { | |
102 | lock->owner = NULL; | |
103 | pthread_mutex_unlock (&lock->lock); | |
104 | } | |
105 | } | |
106 | ||
107 | int | |
108 | gomp_test_nest_lock_30 (omp_nest_lock_t *lock) | |
109 | { | |
110 | void *me = gomp_icv (true); | |
111 | ||
112 | if (lock->owner != me) | |
113 | { | |
114 | if (pthread_mutex_trylock (&lock->lock) != 0) | |
115 | return 0; | |
116 | lock->owner = me; | |
117 | } | |
118 | ||
119 | return ++lock->count; | |
120 | } | |
121 | ||
122 | #else | |
123 | ||
124 | void | |
125 | gomp_init_lock_30 (omp_lock_t *lock) | |
126 | { | |
127 | sem_init (lock, 0, 1); | |
128 | } | |
129 | ||
130 | void | |
131 | gomp_destroy_lock_30 (omp_lock_t *lock) | |
132 | { | |
133 | sem_destroy (lock); | |
134 | } | |
135 | ||
136 | void | |
137 | gomp_set_lock_30 (omp_lock_t *lock) | |
138 | { | |
139 | while (sem_wait (lock) != 0) | |
140 | ; | |
141 | } | |
142 | ||
143 | void | |
144 | gomp_unset_lock_30 (omp_lock_t *lock) | |
145 | { | |
146 | sem_post (lock); | |
147 | } | |
148 | ||
149 | int | |
150 | gomp_test_lock_30 (omp_lock_t *lock) | |
151 | { | |
152 | return sem_trywait (lock) == 0; | |
153 | } | |
154 | ||
155 | void | |
156 | gomp_init_nest_lock_30 (omp_nest_lock_t *lock) | |
157 | { | |
158 | sem_init (&lock->lock, 0, 1); | |
159 | lock->count = 0; | |
160 | lock->owner = NULL; | |
161 | } | |
162 | ||
163 | void | |
164 | gomp_destroy_nest_lock_30 (omp_nest_lock_t *lock) | |
165 | { | |
166 | sem_destroy (&lock->lock); | |
167 | } | |
168 | ||
169 | void | |
170 | gomp_set_nest_lock_30 (omp_nest_lock_t *lock) | |
171 | { | |
172 | void *me = gomp_icv (true); | |
173 | ||
174 | if (lock->owner != me) | |
175 | { | |
176 | while (sem_wait (&lock->lock) != 0) | |
177 | ; | |
178 | lock->owner = me; | |
179 | } | |
180 | lock->count++; | |
181 | } | |
182 | ||
183 | void | |
184 | gomp_unset_nest_lock_30 (omp_nest_lock_t *lock) | |
185 | { | |
186 | if (--lock->count == 0) | |
187 | { | |
188 | lock->owner = NULL; | |
189 | sem_post (&lock->lock); | |
190 | } | |
191 | } | |
192 | ||
193 | int | |
194 | gomp_test_nest_lock_30 (omp_nest_lock_t *lock) | |
195 | { | |
196 | void *me = gomp_icv (true); | |
197 | ||
198 | if (lock->owner != me) | |
199 | { | |
200 | if (sem_trywait (&lock->lock) != 0) | |
201 | return 0; | |
202 | lock->owner = me; | |
203 | } | |
204 | ||
205 | return ++lock->count; | |
206 | } | |
207 | #endif | |
208 | ||
209 | #ifdef LIBGOMP_GNU_SYMBOL_VERSIONING | |
210 | void | |
211 | gomp_init_lock_25 (omp_lock_25_t *lock) | |
c1c80391 | 212 | { |
213 | pthread_mutex_init (lock, NULL); | |
214 | } | |
215 | ||
216 | void | |
fd6481cf | 217 | gomp_destroy_lock_25 (omp_lock_25_t *lock) |
c1c80391 | 218 | { |
219 | pthread_mutex_destroy (lock); | |
220 | } | |
221 | ||
222 | void | |
fd6481cf | 223 | gomp_set_lock_25 (omp_lock_25_t *lock) |
c1c80391 | 224 | { |
225 | pthread_mutex_lock (lock); | |
226 | } | |
227 | ||
228 | void | |
fd6481cf | 229 | gomp_unset_lock_25 (omp_lock_25_t *lock) |
c1c80391 | 230 | { |
231 | pthread_mutex_unlock (lock); | |
232 | } | |
233 | ||
234 | int | |
fd6481cf | 235 | gomp_test_lock_25 (omp_lock_25_t *lock) |
c1c80391 | 236 | { |
237 | return pthread_mutex_trylock (lock) == 0; | |
238 | } | |
239 | ||
240 | void | |
fd6481cf | 241 | gomp_init_nest_lock_25 (omp_nest_lock_25_t *lock) |
c1c80391 | 242 | { |
243 | pthread_mutex_init (&lock->lock, NULL); | |
244 | lock->owner = (pthread_t) 0; | |
245 | lock->count = 0; | |
246 | } | |
247 | ||
248 | void | |
fd6481cf | 249 | gomp_destroy_nest_lock_25 (omp_nest_lock_25_t *lock) |
c1c80391 | 250 | { |
251 | pthread_mutex_destroy (&lock->lock); | |
252 | } | |
253 | ||
254 | void | |
fd6481cf | 255 | gomp_set_nest_lock_25 (omp_nest_lock_25_t *lock) |
c1c80391 | 256 | { |
257 | pthread_t me = pthread_self (); | |
258 | ||
259 | if (lock->owner != me) | |
260 | { | |
261 | pthread_mutex_lock (&lock->lock); | |
262 | lock->owner = me; | |
263 | } | |
264 | ||
265 | lock->count++; | |
266 | } | |
267 | ||
268 | void | |
fd6481cf | 269 | gomp_unset_nest_lock_25 (omp_nest_lock_25_t *lock) |
c1c80391 | 270 | { |
271 | lock->count--; | |
272 | ||
273 | if (lock->count == 0) | |
274 | { | |
275 | lock->owner = (pthread_t) 0; | |
276 | pthread_mutex_unlock (&lock->lock); | |
277 | } | |
278 | } | |
279 | ||
280 | int | |
fd6481cf | 281 | gomp_test_nest_lock_25 (omp_nest_lock_25_t *lock) |
c1c80391 | 282 | { |
283 | pthread_t me = pthread_self (); | |
284 | ||
285 | if (lock->owner != me) | |
286 | { | |
287 | if (pthread_mutex_trylock (&lock->lock) != 0) | |
288 | return 0; | |
289 | lock->owner = me; | |
290 | } | |
291 | ||
292 | return ++lock->count; | |
293 | } | |
294 | ||
fd6481cf | 295 | omp_lock_symver (omp_init_lock) |
296 | omp_lock_symver (omp_destroy_lock) | |
297 | omp_lock_symver (omp_set_lock) | |
298 | omp_lock_symver (omp_unset_lock) | |
299 | omp_lock_symver (omp_test_lock) | |
300 | omp_lock_symver (omp_init_nest_lock) | |
301 | omp_lock_symver (omp_destroy_nest_lock) | |
302 | omp_lock_symver (omp_set_nest_lock) | |
303 | omp_lock_symver (omp_unset_nest_lock) | |
304 | omp_lock_symver (omp_test_nest_lock) | |
305 | ||
306 | #else | |
307 | ||
c1c80391 | 308 | ialias (omp_init_lock) |
309 | ialias (omp_init_nest_lock) | |
310 | ialias (omp_destroy_lock) | |
311 | ialias (omp_destroy_nest_lock) | |
312 | ialias (omp_set_lock) | |
313 | ialias (omp_set_nest_lock) | |
314 | ialias (omp_unset_lock) | |
315 | ialias (omp_unset_nest_lock) | |
316 | ialias (omp_test_lock) | |
317 | ialias (omp_test_nest_lock) | |
fd6481cf | 318 | |
319 | #endif |