source: trunk/sys/libgomp/lock.c @ 227

Last change on this file since 227 was 1, checked in by alain, 8 years ago

First import

File size: 5.9 KB
Line 
1/* Copyright (C) 2005, 2008, 2009 Free Software Foundation, Inc.
2   Contributed by Richard Henderson <rth@redhat.com>.
3
4   This file is part of the GNU OpenMP Library (libgomp).
5
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)
9   any later version.
10
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
14   more details.
15
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.
19
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/>.  */
24
25/* This is the default PTHREADS implementation of the public OpenMP
26   locking primitives.
27
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.  */
32
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.  */
36#ifndef __osf__
37#define _XOPEN_SOURCE 500
38#endif
39
40#include <gomp/libgomp.h>
41
42#ifdef HAVE_BROKEN_POSIX_SEMAPHORES
43void
44gomp_init_lock_30 (omp_lock_t *lock)
45{
46  pthread_mutex_init (lock, NULL);
47}
48
49void
50gomp_destroy_lock_30 (omp_lock_t *lock)
51{
52  pthread_mutex_destroy (lock);
53}
54
55void
56gomp_set_lock_30 (omp_lock_t *lock)
57{
58  pthread_mutex_lock (lock);
59}
60
61void
62gomp_unset_lock_30 (omp_lock_t *lock)
63{
64  pthread_mutex_unlock (lock);
65}
66
67int
68gomp_test_lock_30 (omp_lock_t *lock)
69{
70  return pthread_mutex_trylock (lock) == 0;
71}
72
73void
74gomp_init_nest_lock_30 (omp_nest_lock_t *lock)
75{
76  pthread_mutex_init (&lock->lock, NULL);
77  lock->count = 0;
78  lock->owner = NULL;
79}
80
81void
82gomp_destroy_nest_lock_30 (omp_nest_lock_t *lock)
83{
84  pthread_mutex_destroy (&lock->lock);
85}
86
87void
88gomp_set_nest_lock_30 (omp_nest_lock_t *lock)
89{
90  void *me = gomp_icv (true);
91
92  if (lock->owner != me)
93    {
94      pthread_mutex_lock (&lock->lock);
95      lock->owner = me;
96    }
97  lock->count++;
98}
99
100void
101gomp_unset_nest_lock_30 (omp_nest_lock_t *lock)
102{
103  if (--lock->count == 0)
104    {
105      lock->owner = NULL;
106      pthread_mutex_unlock (&lock->lock);
107    }
108}
109
110int
111gomp_test_nest_lock_30 (omp_nest_lock_t *lock)
112{
113  void *me = gomp_icv (true);
114
115  if (lock->owner != me)
116    {
117      if (pthread_mutex_trylock (&lock->lock) != 0)
118        return 0;
119      lock->owner = me;
120    }
121
122  return ++lock->count;
123}
124
125#else
126
127void
128gomp_init_lock_30 (omp_lock_t *lock)
129{
130  sem_init (lock, 0, 1);
131}
132
133void
134gomp_destroy_lock_30 (omp_lock_t *lock)
135{
136  sem_destroy (lock);
137}
138
139void
140gomp_set_lock_30 (omp_lock_t *lock)
141{
142  while (sem_wait (lock) != 0)
143    ;
144}
145
146void
147gomp_unset_lock_30 (omp_lock_t *lock)
148{
149  sem_post (lock);
150}
151
152int
153gomp_test_lock_30 (omp_lock_t *lock)
154{
155  return sem_trywait (lock) == 0;
156}
157
158void
159gomp_init_nest_lock_30 (omp_nest_lock_t *lock)
160{
161  sem_init (&lock->lock, 0, 1);
162  lock->count = 0;
163  lock->owner = NULL;
164}
165
166void
167gomp_destroy_nest_lock_30 (omp_nest_lock_t *lock)
168{
169  sem_destroy (&lock->lock);
170}
171
172void
173gomp_set_nest_lock_30 (omp_nest_lock_t *lock)
174{
175  void *me = gomp_icv (true);
176
177  if (lock->owner != me)
178    {
179      while (sem_wait (&lock->lock) != 0)
180        ;
181      lock->owner = me;
182    }
183  lock->count++;
184}
185
186void
187gomp_unset_nest_lock_30 (omp_nest_lock_t *lock)
188{
189  if (--lock->count == 0)
190    {
191      lock->owner = NULL;
192      sem_post (&lock->lock);
193    }
194}
195
196int
197gomp_test_nest_lock_30 (omp_nest_lock_t *lock)
198{
199  void *me = gomp_icv (true);
200
201  if (lock->owner != me)
202    {
203      if (sem_trywait (&lock->lock) != 0)
204        return 0;
205      lock->owner = me;
206    }
207
208  return ++lock->count;
209}
210#endif
211
212#ifdef LIBGOMP_GNU_SYMBOL_VERSIONING
213void
214gomp_init_lock_25 (omp_lock_25_t *lock)
215{
216  pthread_mutex_init (lock, NULL);
217}
218
219void
220gomp_destroy_lock_25 (omp_lock_25_t *lock)
221{
222  pthread_mutex_destroy (lock);
223}
224
225void
226gomp_set_lock_25 (omp_lock_25_t *lock)
227{
228  pthread_mutex_lock (lock);
229}
230
231void
232gomp_unset_lock_25 (omp_lock_25_t *lock)
233{
234  pthread_mutex_unlock (lock);
235}
236
237int
238gomp_test_lock_25 (omp_lock_25_t *lock)
239{
240  return pthread_mutex_trylock (lock) == 0;
241}
242
243void
244gomp_init_nest_lock_25 (omp_nest_lock_25_t *lock)
245{
246  pthread_mutexattr_t attr;
247
248  pthread_mutexattr_init (&attr);
249  pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
250  pthread_mutex_init (&lock->lock, &attr);
251  lock->count = 0;
252  pthread_mutexattr_destroy (&attr);
253}
254
255void
256gomp_destroy_nest_lock_25 (omp_nest_lock_25_t *lock)
257{
258  pthread_mutex_destroy (&lock->lock);
259}
260
261void
262gomp_set_nest_lock_25 (omp_nest_lock_25_t *lock)
263{
264  pthread_mutex_lock (&lock->lock);
265  lock->count++;
266}
267
268void
269gomp_unset_nest_lock_25 (omp_nest_lock_25_t *lock)
270{
271  lock->count--;
272  pthread_mutex_unlock (&lock->lock);
273}
274
275int
276gomp_test_nest_lock_25 (omp_nest_lock_25_t *lock)
277{
278  if (pthread_mutex_trylock (&lock->lock) == 0)
279    return ++lock->count;
280  return 0;
281}
282
283omp_lock_symver (omp_init_lock)
284omp_lock_symver (omp_destroy_lock)
285omp_lock_symver (omp_set_lock)
286omp_lock_symver (omp_unset_lock)
287omp_lock_symver (omp_test_lock)
288omp_lock_symver (omp_init_nest_lock)
289omp_lock_symver (omp_destroy_nest_lock)
290omp_lock_symver (omp_set_nest_lock)
291omp_lock_symver (omp_unset_nest_lock)
292omp_lock_symver (omp_test_nest_lock)
293
294#else
295
296ialias (omp_init_lock)
297ialias (omp_init_nest_lock)
298ialias (omp_destroy_lock)
299ialias (omp_destroy_nest_lock)
300ialias (omp_set_lock)
301ialias (omp_set_nest_lock)
302ialias (omp_unset_lock)
303ialias (omp_unset_nest_lock)
304ialias (omp_test_lock)
305ialias (omp_test_nest_lock)
306
307#endif
Note: See TracBrowser for help on using the repository browser.