1 /* Copyright (C) 2005-2013 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)
67 return __atomic_compare_exchange_n (lock, &oldval, 1, false,
68 MEMMODEL_ACQUIRE, MEMMODEL_RELAXED);
72 gomp_init_nest_lock_30 (omp_nest_lock_t *lock)
74 memset (lock, '\0', sizeof (*lock));
78 gomp_destroy_nest_lock_30 (omp_nest_lock_t *lock)
83 gomp_set_nest_lock_30 (omp_nest_lock_t *lock)
85 void *me = gomp_icv (true);
87 if (lock->owner != me)
89 gomp_mutex_lock (&lock->lock);
97 gomp_unset_nest_lock_30 (omp_nest_lock_t *lock)
99 if (--lock->count == 0)
102 gomp_mutex_unlock (&lock->lock);
107 gomp_test_nest_lock_30 (omp_nest_lock_t *lock)
109 void *me = gomp_icv (true);
112 if (lock->owner == me)
113 return ++lock->count;
116 if (__atomic_compare_exchange_n (&lock->lock, &oldval, 1, false,
117 MEMMODEL_ACQUIRE, MEMMODEL_RELAXED))
127 #ifdef LIBGOMP_GNU_SYMBOL_VERSIONING
128 /* gomp_mutex_* can be safely locked in one thread and
129 unlocked in another thread, so the OpenMP 2.5 and OpenMP 3.0
130 non-nested locks can be the same. */
131 strong_alias (gomp_init_lock_30, gomp_init_lock_25)
132 strong_alias (gomp_destroy_lock_30, gomp_destroy_lock_25)
133 strong_alias (gomp_set_lock_30, gomp_set_lock_25)
134 strong_alias (gomp_unset_lock_30, gomp_unset_lock_25)
135 strong_alias (gomp_test_lock_30, gomp_test_lock_25)
137 /* The external recursive omp_nest_lock_25_t form requires additional work. */
139 /* We need an integer to uniquely identify this thread. Most generally
140 this is the thread's TID, which ideally we'd get this straight from
141 the TLS block where glibc keeps it. Unfortunately, we can't get at
144 If we don't support (or have disabled) TLS, one function call is as
145 good (or bad) as any other. Use the syscall all the time.
147 On an ILP32 system (defined here as not LP64), we can make do with
148 any thread-local pointer. Ideally we'd use the TLS base address,
149 since that requires the least amount of arithmetic, but that's not
150 always available directly. Make do with the gomp_thread pointer
153 # if !defined (HAVE_TLS)
154 static inline int gomp_tid (void)
156 return syscall (SYS_gettid);
158 # elif !defined(__LP64__)
159 static inline int gomp_tid (void)
161 return (int) gomp_thread ();
164 static __thread int tid_cache;
165 static inline int gomp_tid (void)
168 if (__builtin_expect (tid == 0, 0))
169 tid_cache = tid = syscall (SYS_gettid);
176 gomp_init_nest_lock_25 (omp_nest_lock_25_t *lock)
178 memset (lock, 0, sizeof (*lock));
182 gomp_destroy_nest_lock_25 (omp_nest_lock_25_t *lock)
187 gomp_set_nest_lock_25 (omp_nest_lock_25_t *lock)
189 int otid, tid = gomp_tid ();
194 if (__atomic_compare_exchange_n (&lock->owner, &otid, tid, false,
195 MEMMODEL_ACQUIRE, MEMMODEL_RELAXED))
206 do_wait (&lock->owner, otid);
211 gomp_unset_nest_lock_25 (omp_nest_lock_25_t *lock)
213 /* ??? Validate that we own the lock here. */
215 if (--lock->count == 0)
217 __atomic_store_n (&lock->owner, 0, MEMMODEL_RELEASE);
218 futex_wake (&lock->owner, 1);
223 gomp_test_nest_lock_25 (omp_nest_lock_25_t *lock)
225 int otid, tid = gomp_tid ();
228 if (__atomic_compare_exchange_n (&lock->owner, &otid, tid, false,
229 MEMMODEL_ACQUIRE, MEMMODEL_RELAXED))
235 return ++lock->count;
240 omp_lock_symver (omp_init_lock)
241 omp_lock_symver (omp_destroy_lock)
242 omp_lock_symver (omp_set_lock)
243 omp_lock_symver (omp_unset_lock)
244 omp_lock_symver (omp_test_lock)
245 omp_lock_symver (omp_init_nest_lock)
246 omp_lock_symver (omp_destroy_nest_lock)
247 omp_lock_symver (omp_set_nest_lock)
248 omp_lock_symver (omp_unset_nest_lock)
249 omp_lock_symver (omp_test_nest_lock)
253 ialias (omp_init_lock)
254 ialias (omp_init_nest_lock)
255 ialias (omp_destroy_lock)
256 ialias (omp_destroy_nest_lock)
257 ialias (omp_set_lock)
258 ialias (omp_set_nest_lock)
259 ialias (omp_unset_lock)
260 ialias (omp_unset_nest_lock)
261 ialias (omp_test_lock)
262 ialias (omp_test_nest_lock)