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 a Linux specific implementation of the public OpenMP locking
26 primitives. This implementation uses atomic instructions and the futex
31 #include <sys/syscall.h>
35 /* The internal gomp_mutex_t and the external non-recursive omp_lock_t
36 have the same form. Re-use it. */
39 gomp_init_lock_30 (omp_lock_t *lock)
41 gomp_mutex_init (lock);
45 gomp_destroy_lock_30 (omp_lock_t *lock)
47 gomp_mutex_destroy (lock);
51 gomp_set_lock_30 (omp_lock_t *lock)
53 gomp_mutex_lock (lock);
57 gomp_unset_lock_30 (omp_lock_t *lock)
59 gomp_mutex_unlock (lock);
63 gomp_test_lock_30 (omp_lock_t *lock)
65 return __sync_bool_compare_and_swap (lock, 0, 1);
69 gomp_init_nest_lock_30 (omp_nest_lock_t *lock)
71 memset (lock, '\0', sizeof (*lock));
75 gomp_destroy_nest_lock_30 (omp_nest_lock_t *lock)
80 gomp_set_nest_lock_30 (omp_nest_lock_t *lock)
82 void *me = gomp_icv (true);
84 if (lock->owner != me)
86 gomp_mutex_lock (&lock->lock);
94 gomp_unset_nest_lock_30 (omp_nest_lock_t *lock)
96 if (--lock->count == 0)
99 gomp_mutex_unlock (&lock->lock);
104 gomp_test_nest_lock_30 (omp_nest_lock_t *lock)
106 void *me = gomp_icv (true);
108 if (lock->owner == me)
109 return ++lock->count;
111 if (__sync_bool_compare_and_swap (&lock->lock, 0, 1))
121 #ifdef LIBGOMP_GNU_SYMBOL_VERSIONING
122 /* gomp_mutex_* can be safely locked in one thread and
123 unlocked in another thread, so the OpenMP 2.5 and OpenMP 3.0
124 non-nested locks can be the same. */
125 strong_alias (gomp_init_lock_30, gomp_init_lock_25)
126 strong_alias (gomp_destroy_lock_30, gomp_destroy_lock_25)
127 strong_alias (gomp_set_lock_30, gomp_set_lock_25)
128 strong_alias (gomp_unset_lock_30, gomp_unset_lock_25)
129 strong_alias (gomp_test_lock_30, gomp_test_lock_25)
131 /* The external recursive omp_nest_lock_25_t form requires additional work. */
133 /* We need an integer to uniquely identify this thread. Most generally
134 this is the thread's TID, which ideally we'd get this straight from
135 the TLS block where glibc keeps it. Unfortunately, we can't get at
138 If we don't support (or have disabled) TLS, one function call is as
139 good (or bad) as any other. Use the syscall all the time.
141 On an ILP32 system (defined here as not LP64), we can make do with
142 any thread-local pointer. Ideally we'd use the TLS base address,
143 since that requires the least amount of arithmetic, but that's not
144 always available directly. Make do with the gomp_thread pointer
147 # if !defined (HAVE_TLS)
148 static inline int gomp_tid (void)
150 return syscall (SYS_gettid);
152 # elif !defined(__LP64__)
153 static inline int gomp_tid (void)
155 return (int) gomp_thread ();
158 static __thread int tid_cache;
159 static inline int gomp_tid (void)
162 if (__builtin_expect (tid == 0, 0))
163 tid_cache = tid = syscall (SYS_gettid);
170 gomp_init_nest_lock_25 (omp_nest_lock_25_t *lock)
172 memset (lock, 0, sizeof (lock));
176 gomp_destroy_nest_lock_25 (omp_nest_lock_25_t *lock)
181 gomp_set_nest_lock_25 (omp_nest_lock_25_t *lock)
183 int otid, tid = gomp_tid ();
187 otid = __sync_val_compare_and_swap (&lock->owner, 0, tid);
199 do_wait (&lock->owner, otid);
204 gomp_unset_nest_lock_25 (omp_nest_lock_25_t *lock)
206 /* ??? Validate that we own the lock here. */
208 if (--lock->count == 0)
210 __sync_lock_release (&lock->owner);
211 futex_wake (&lock->owner, 1);
216 gomp_test_nest_lock_25 (omp_nest_lock_25_t *lock)
218 int otid, tid = gomp_tid ();
220 otid = __sync_val_compare_and_swap (&lock->owner, 0, tid);
227 return ++lock->count;
232 omp_lock_symver (omp_init_lock)
233 omp_lock_symver (omp_destroy_lock)
234 omp_lock_symver (omp_set_lock)
235 omp_lock_symver (omp_unset_lock)
236 omp_lock_symver (omp_test_lock)
237 omp_lock_symver (omp_init_nest_lock)
238 omp_lock_symver (omp_destroy_nest_lock)
239 omp_lock_symver (omp_set_nest_lock)
240 omp_lock_symver (omp_unset_nest_lock)
241 omp_lock_symver (omp_test_nest_lock)
245 ialias (omp_init_lock)
246 ialias (omp_init_nest_lock)
247 ialias (omp_destroy_lock)
248 ialias (omp_destroy_nest_lock)
249 ialias (omp_set_lock)
250 ialias (omp_set_nest_lock)
251 ialias (omp_unset_lock)
252 ialias (omp_unset_nest_lock)
253 ialias (omp_test_lock)
254 ialias (omp_test_nest_lock)