]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libgomp/config/posix/lock.c
1 /* Copyright (C) 2005, 2008, 2009 Free Software Foundation, Inc.
2 Contributed by Richard Henderson <rth@redhat.com>.
4 This file is part of the GNU OpenMP Library (libgomp).
6 Libgomp is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 Under Section 7 of GPL version 3, you are granted additional
17 permissions described in the GCC Runtime Library Exception, version
18 3.1, as published by the Free Software Foundation.
20 You should have received a copy of the GNU General Public License and
21 a copy of the GCC Runtime Library Exception along with this program;
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 <http://www.gnu.org/licenses/>. */
25 /* This is the default PTHREADS implementation of the public OpenMP
28 Because OpenMP uses different entry points for normal and recursive
29 locks, and pthreads uses only one entry point, a system may be able
30 to do better and streamline the locking as well as reduce the size
31 of the types exported. */
33 /* We need Unix98 extensions to get recursive locks. On Tru64 UNIX V4.0F,
34 the declarations are available without _XOPEN_SOURCE, which actually
35 breaks compilation. */
37 #define _XOPEN_SOURCE 500
42 #ifdef HAVE_BROKEN_POSIX_SEMAPHORES
44 gomp_init_lock_30 (omp_lock_t
*lock
)
46 pthread_mutex_init (lock
, NULL
);
50 gomp_destroy_lock_30 (omp_lock_t
*lock
)
52 pthread_mutex_destroy (lock
);
56 gomp_set_lock_30 (omp_lock_t
*lock
)
58 pthread_mutex_lock (lock
);
62 gomp_unset_lock_30 (omp_lock_t
*lock
)
64 pthread_mutex_unlock (lock
);
68 gomp_test_lock_30 (omp_lock_t
*lock
)
70 return pthread_mutex_trylock (lock
) == 0;
74 gomp_init_nest_lock_30 (omp_nest_lock_t
*lock
)
76 pthread_mutex_init (&lock
->lock
, NULL
);
82 gomp_destroy_nest_lock_30 (omp_nest_lock_t
*lock
)
84 pthread_mutex_destroy (&lock
->lock
);
88 gomp_set_nest_lock_30 (omp_nest_lock_t
*lock
)
90 void *me
= gomp_icv (true);
92 if (lock
->owner
!= me
)
94 pthread_mutex_lock (&lock
->lock
);
101 gomp_unset_nest_lock_30 (omp_nest_lock_t
*lock
)
103 if (--lock
->count
== 0)
106 pthread_mutex_unlock (&lock
->lock
);
111 gomp_test_nest_lock_30 (omp_nest_lock_t
*lock
)
113 void *me
= gomp_icv (true);
115 if (lock
->owner
!= me
)
117 if (pthread_mutex_trylock (&lock
->lock
) != 0)
122 return ++lock
->count
;
128 gomp_init_lock_30 (omp_lock_t
*lock
)
130 sem_init (lock
, 0, 1);
134 gomp_destroy_lock_30 (omp_lock_t
*lock
)
140 gomp_set_lock_30 (omp_lock_t
*lock
)
142 while (sem_wait (lock
) != 0)
147 gomp_unset_lock_30 (omp_lock_t
*lock
)
153 gomp_test_lock_30 (omp_lock_t
*lock
)
155 return sem_trywait (lock
) == 0;
159 gomp_init_nest_lock_30 (omp_nest_lock_t
*lock
)
161 sem_init (&lock
->lock
, 0, 1);
167 gomp_destroy_nest_lock_30 (omp_nest_lock_t
*lock
)
169 sem_destroy (&lock
->lock
);
173 gomp_set_nest_lock_30 (omp_nest_lock_t
*lock
)
175 void *me
= gomp_icv (true);
177 if (lock
->owner
!= me
)
179 while (sem_wait (&lock
->lock
) != 0)
187 gomp_unset_nest_lock_30 (omp_nest_lock_t
*lock
)
189 if (--lock
->count
== 0)
192 sem_post (&lock
->lock
);
197 gomp_test_nest_lock_30 (omp_nest_lock_t
*lock
)
199 void *me
= gomp_icv (true);
201 if (lock
->owner
!= me
)
203 if (sem_trywait (&lock
->lock
) != 0)
208 return ++lock
->count
;
212 #ifdef LIBGOMP_GNU_SYMBOL_VERSIONING
214 gomp_init_lock_25 (omp_lock_25_t
*lock
)
216 pthread_mutex_init (lock
, NULL
);
220 gomp_destroy_lock_25 (omp_lock_25_t
*lock
)
222 pthread_mutex_destroy (lock
);
226 gomp_set_lock_25 (omp_lock_25_t
*lock
)
228 pthread_mutex_lock (lock
);
232 gomp_unset_lock_25 (omp_lock_25_t
*lock
)
234 pthread_mutex_unlock (lock
);
238 gomp_test_lock_25 (omp_lock_25_t
*lock
)
240 return pthread_mutex_trylock (lock
) == 0;
244 gomp_init_nest_lock_25 (omp_nest_lock_25_t
*lock
)
246 pthread_mutexattr_t attr
;
248 pthread_mutexattr_init (&attr
);
249 pthread_mutexattr_settype (&attr
, PTHREAD_MUTEX_RECURSIVE
);
250 pthread_mutex_init (&lock
->lock
, &attr
);
252 pthread_mutexattr_destroy (&attr
);
256 gomp_destroy_nest_lock_25 (omp_nest_lock_25_t
*lock
)
258 pthread_mutex_destroy (&lock
->lock
);
262 gomp_set_nest_lock_25 (omp_nest_lock_25_t
*lock
)
264 pthread_mutex_lock (&lock
->lock
);
269 gomp_unset_nest_lock_25 (omp_nest_lock_25_t
*lock
)
272 pthread_mutex_unlock (&lock
->lock
);
276 gomp_test_nest_lock_25 (omp_nest_lock_25_t
*lock
)
278 if (pthread_mutex_trylock (&lock
->lock
) == 0)
279 return ++lock
->count
;
283 omp_lock_symver (omp_init_lock
)
284 omp_lock_symver (omp_destroy_lock
)
285 omp_lock_symver (omp_set_lock
)
286 omp_lock_symver (omp_unset_lock
)
287 omp_lock_symver (omp_test_lock
)
288 omp_lock_symver (omp_init_nest_lock
)
289 omp_lock_symver (omp_destroy_nest_lock
)
290 omp_lock_symver (omp_set_nest_lock
)
291 omp_lock_symver (omp_unset_nest_lock
)
292 omp_lock_symver (omp_test_nest_lock
)
296 ialias (omp_init_lock
)
297 ialias (omp_init_nest_lock
)
298 ialias (omp_destroy_lock
)
299 ialias (omp_destroy_nest_lock
)
300 ialias (omp_set_lock
)
301 ialias (omp_set_nest_lock
)
302 ialias (omp_unset_lock
)
303 ialias (omp_unset_nest_lock
)
304 ialias (omp_test_lock
)
305 ialias (omp_test_nest_lock
)