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
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)
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
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
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
40 #ifdef HAVE_BROKEN_POSIX_SEMAPHORES
42 gomp_init_lock_30 (omp_lock_t
*lock
)
44 pthread_mutex_init (lock
, NULL
);
48 gomp_destroy_lock_30 (omp_lock_t
*lock
)
50 pthread_mutex_destroy (lock
);
54 gomp_set_lock_30 (omp_lock_t
*lock
)
56 pthread_mutex_lock (lock
);
60 gomp_unset_lock_30 (omp_lock_t
*lock
)
62 pthread_mutex_unlock (lock
);
66 gomp_test_lock_30 (omp_lock_t
*lock
)
68 return pthread_mutex_trylock (lock
) == 0;
72 gomp_init_nest_lock_30 (omp_nest_lock_t
*lock
)
74 pthread_mutex_init (&lock
->lock
, NULL
);
80 gomp_destroy_nest_lock_30 (omp_nest_lock_t
*lock
)
82 pthread_mutex_destroy (&lock
->lock
);
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
);
99 gomp_unset_nest_lock_30 (omp_nest_lock_t
*lock
)
101 if (--lock
->count
== 0)
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)
120 return ++lock
->count
;
126 gomp_init_lock_30 (omp_lock_t
*lock
)
128 sem_init (lock
, 0, 1);
132 gomp_destroy_lock_30 (omp_lock_t
*lock
)
138 gomp_set_lock_30 (omp_lock_t
*lock
)
140 while (sem_wait (lock
) != 0)
145 gomp_unset_lock_30 (omp_lock_t
*lock
)
151 gomp_test_lock_30 (omp_lock_t
*lock
)
153 return sem_trywait (lock
) == 0;
157 gomp_init_nest_lock_30 (omp_nest_lock_t
*lock
)
159 sem_init (&lock
->lock
, 0, 1);
165 gomp_destroy_nest_lock_30 (omp_nest_lock_t
*lock
)
167 sem_destroy (&lock
->lock
);
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)
185 gomp_unset_nest_lock_30 (omp_nest_lock_t
*lock
)
187 if (--lock
->count
== 0)
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)
206 return ++lock
->count
;
210 #ifdef LIBGOMP_GNU_SYMBOL_VERSIONING
212 gomp_init_lock_25 (omp_lock_25_t
*lock
)
214 pthread_mutex_init (lock
, NULL
);
218 gomp_destroy_lock_25 (omp_lock_25_t
*lock
)
220 pthread_mutex_destroy (lock
);
224 gomp_set_lock_25 (omp_lock_25_t
*lock
)
226 pthread_mutex_lock (lock
);
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;
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
);
250 pthread_mutexattr_destroy (&attr
);
254 gomp_destroy_nest_lock_25 (omp_nest_lock_25_t
*lock
)
256 pthread_mutex_destroy (&lock
->lock
);
260 gomp_set_nest_lock_25 (omp_nest_lock_25_t
*lock
)
262 pthread_mutex_lock (&lock
->lock
);
267 gomp_unset_nest_lock_25 (omp_nest_lock_25_t
*lock
)
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
;
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
)
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
)