PR libstdc++/85812 fix memory leak in std::make_exception_ptr
[official-gcc.git] / libgomp / config / posix / lock.c
blobed81a9e03b91e351caec2c58e1bbd1c9d3dc39ba
1 /* Copyright (C) 2005-2018 Free Software Foundation, Inc.
2 Contributed by Richard Henderson <rth@redhat.com>.
4 This file is part of the GNU Offloading and Multi Processing Library
5 (libgomp).
7 Libgomp is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 more details.
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
26 /* This is the default PTHREADS implementation of the public OpenMP
27 locking primitives.
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. */
34 /* We need UNIX98/XPG5 extensions to get recursive locks. Request XPG6 since
35 Solaris requires this for C99 and later. */
36 #define _XOPEN_SOURCE 600
38 #include "libgomp.h"
40 #ifdef HAVE_BROKEN_POSIX_SEMAPHORES
41 void
42 gomp_init_lock_30 (omp_lock_t *lock)
44 pthread_mutex_init (lock, NULL);
47 void
48 gomp_destroy_lock_30 (omp_lock_t *lock)
50 pthread_mutex_destroy (lock);
53 void
54 gomp_set_lock_30 (omp_lock_t *lock)
56 pthread_mutex_lock (lock);
59 void
60 gomp_unset_lock_30 (omp_lock_t *lock)
62 pthread_mutex_unlock (lock);
65 int
66 gomp_test_lock_30 (omp_lock_t *lock)
68 return pthread_mutex_trylock (lock) == 0;
71 void
72 gomp_init_nest_lock_30 (omp_nest_lock_t *lock)
74 pthread_mutex_init (&lock->lock, NULL);
75 lock->count = 0;
76 lock->owner = NULL;
79 void
80 gomp_destroy_nest_lock_30 (omp_nest_lock_t *lock)
82 pthread_mutex_destroy (&lock->lock);
85 void
86 gomp_set_nest_lock_30 (omp_nest_lock_t *lock)
88 void *me = gomp_icv (true);
90 if (lock->owner != me)
92 pthread_mutex_lock (&lock->lock);
93 lock->owner = me;
95 lock->count++;
98 void
99 gomp_unset_nest_lock_30 (omp_nest_lock_t *lock)
101 if (--lock->count == 0)
103 lock->owner = NULL;
104 pthread_mutex_unlock (&lock->lock);
109 gomp_test_nest_lock_30 (omp_nest_lock_t *lock)
111 void *me = gomp_icv (true);
113 if (lock->owner != me)
115 if (pthread_mutex_trylock (&lock->lock) != 0)
116 return 0;
117 lock->owner = me;
120 return ++lock->count;
123 #else
125 void
126 gomp_init_lock_30 (omp_lock_t *lock)
128 sem_init (lock, 0, 1);
131 void
132 gomp_destroy_lock_30 (omp_lock_t *lock)
134 sem_destroy (lock);
137 void
138 gomp_set_lock_30 (omp_lock_t *lock)
140 while (sem_wait (lock) != 0)
144 void
145 gomp_unset_lock_30 (omp_lock_t *lock)
147 sem_post (lock);
151 gomp_test_lock_30 (omp_lock_t *lock)
153 return sem_trywait (lock) == 0;
156 void
157 gomp_init_nest_lock_30 (omp_nest_lock_t *lock)
159 sem_init (&lock->lock, 0, 1);
160 lock->count = 0;
161 lock->owner = NULL;
164 void
165 gomp_destroy_nest_lock_30 (omp_nest_lock_t *lock)
167 sem_destroy (&lock->lock);
170 void
171 gomp_set_nest_lock_30 (omp_nest_lock_t *lock)
173 void *me = gomp_icv (true);
175 if (lock->owner != me)
177 while (sem_wait (&lock->lock) != 0)
179 lock->owner = me;
181 lock->count++;
184 void
185 gomp_unset_nest_lock_30 (omp_nest_lock_t *lock)
187 if (--lock->count == 0)
189 lock->owner = NULL;
190 sem_post (&lock->lock);
195 gomp_test_nest_lock_30 (omp_nest_lock_t *lock)
197 void *me = gomp_icv (true);
199 if (lock->owner != me)
201 if (sem_trywait (&lock->lock) != 0)
202 return 0;
203 lock->owner = me;
206 return ++lock->count;
208 #endif
210 #ifdef LIBGOMP_GNU_SYMBOL_VERSIONING
211 void
212 gomp_init_lock_25 (omp_lock_25_t *lock)
214 pthread_mutex_init (lock, NULL);
217 void
218 gomp_destroy_lock_25 (omp_lock_25_t *lock)
220 pthread_mutex_destroy (lock);
223 void
224 gomp_set_lock_25 (omp_lock_25_t *lock)
226 pthread_mutex_lock (lock);
229 void
230 gomp_unset_lock_25 (omp_lock_25_t *lock)
232 pthread_mutex_unlock (lock);
236 gomp_test_lock_25 (omp_lock_25_t *lock)
238 return pthread_mutex_trylock (lock) == 0;
241 void
242 gomp_init_nest_lock_25 (omp_nest_lock_25_t *lock)
244 pthread_mutexattr_t attr;
246 pthread_mutexattr_init (&attr);
247 pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
248 pthread_mutex_init (&lock->lock, &attr);
249 lock->count = 0;
250 pthread_mutexattr_destroy (&attr);
253 void
254 gomp_destroy_nest_lock_25 (omp_nest_lock_25_t *lock)
256 pthread_mutex_destroy (&lock->lock);
259 void
260 gomp_set_nest_lock_25 (omp_nest_lock_25_t *lock)
262 pthread_mutex_lock (&lock->lock);
263 lock->count++;
266 void
267 gomp_unset_nest_lock_25 (omp_nest_lock_25_t *lock)
269 lock->count--;
270 pthread_mutex_unlock (&lock->lock);
274 gomp_test_nest_lock_25 (omp_nest_lock_25_t *lock)
276 if (pthread_mutex_trylock (&lock->lock) == 0)
277 return ++lock->count;
278 return 0;
281 omp_lock_symver (omp_init_lock)
282 omp_lock_symver (omp_destroy_lock)
283 omp_lock_symver (omp_set_lock)
284 omp_lock_symver (omp_unset_lock)
285 omp_lock_symver (omp_test_lock)
286 omp_lock_symver (omp_init_nest_lock)
287 omp_lock_symver (omp_destroy_nest_lock)
288 omp_lock_symver (omp_set_nest_lock)
289 omp_lock_symver (omp_unset_nest_lock)
290 omp_lock_symver (omp_test_nest_lock)
292 #else
294 ialias (omp_init_lock)
295 ialias (omp_init_nest_lock)
296 ialias (omp_destroy_lock)
297 ialias (omp_destroy_nest_lock)
298 ialias (omp_set_lock)
299 ialias (omp_set_nest_lock)
300 ialias (omp_unset_lock)
301 ialias (omp_unset_nest_lock)
302 ialias (omp_test_lock)
303 ialias (omp_test_nest_lock)
305 #endif