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 a Linux specific implementation of the public OpenMP locking 26 primitives. This implementation uses atomic instructions and the futex 27 syscall. */ 28 29 #include <string.h> 30 #include <unistd.h> 31 #include <sys/syscall.h> 32 #include "wait.h" 33 34 35 /* The internal gomp_mutex_t and the external non-recursive omp_lock_t 36 have the same form. Re-use it. */ 37 38 void 39 gomp_init_lock_30 (omp_lock_t *lock) 40 { 41 gomp_mutex_init (lock); 42 } 43 44 void 45 gomp_destroy_lock_30 (omp_lock_t *lock) 46 { 47 gomp_mutex_destroy (lock); 48 } 49 50 void 51 gomp_set_lock_30 (omp_lock_t *lock) 52 { 53 gomp_mutex_lock (lock); 54 } 55 56 void 57 gomp_unset_lock_30 (omp_lock_t *lock) 58 { 59 gomp_mutex_unlock (lock); 60 } 61 62 int 63 gomp_test_lock_30 (omp_lock_t *lock) 64 { 65 return __sync_bool_compare_and_swap (lock, 0, 1); 66 } 67 68 void 69 gomp_init_nest_lock_30 (omp_nest_lock_t *lock) 70 { 71 memset (lock, '\0', sizeof (*lock)); 72 } 73 74 void 75 gomp_destroy_nest_lock_30 (omp_nest_lock_t *lock) 76 { 77 } 78 79 void 80 gomp_set_nest_lock_30 (omp_nest_lock_t *lock) 81 { 82 void *me = gomp_icv (true); 83 84 if (lock->owner != me) 85 { 86 gomp_mutex_lock (&lock->lock); 87 lock->owner = me; 88 } 89 90 lock->count++; 91 } 92 93 void 94 gomp_unset_nest_lock_30 (omp_nest_lock_t *lock) 95 { 96 if (--lock->count == 0) 97 { 98 lock->owner = NULL; 99 gomp_mutex_unlock (&lock->lock); 100 } 101 } 102 103 int 104 gomp_test_nest_lock_30 (omp_nest_lock_t *lock) 105 { 106 void *me = gomp_icv (true); 107 108 if (lock->owner == me) 109 return ++lock->count; 110 111 if (__sync_bool_compare_and_swap (&lock->lock, 0, 1)) 112 { 113 lock->owner = me; 114 lock->count = 1; 115 return 1; 116 } 117 118 return 0; 119 } 120 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) 130 131 /* The external recursive omp_nest_lock_25_t form requires additional work. */ 132 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 136 that directly. 137 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. 140 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 145 since it's handy. */ 146 147 # if !defined (HAVE_TLS) 148 static inline int gomp_tid (void) 149 { 150 return syscall (SYS_gettid); 151 } 152 # elif !defined(__LP64__) 153 static inline int gomp_tid (void) 154 { 155 return (int) gomp_thread (); 156 } 157 # else 158 static __thread int tid_cache; 159 static inline int gomp_tid (void) 160 { 161 int tid = tid_cache; 162 if (__builtin_expect (tid == 0, 0)) 163 tid_cache = tid = syscall (SYS_gettid); 164 return tid; 165 } 166 # endif 167 168 169 void 170 gomp_init_nest_lock_25 (omp_nest_lock_25_t *lock) 171 { 172 memset (lock, 0, sizeof (lock)); 173 } 174 175 void 176 gomp_destroy_nest_lock_25 (omp_nest_lock_25_t *lock) 177 { 178 } 179 180 void 181 gomp_set_nest_lock_25 (omp_nest_lock_25_t *lock) 182 { 183 int otid, tid = gomp_tid (); 184 185 while (1) 186 { 187 otid = __sync_val_compare_and_swap (&lock->owner, 0, tid); 188 if (otid == 0) 189 { 190 lock->count = 1; 191 return; 192 } 193 if (otid == tid) 194 { 195 lock->count++; 196 return; 197 } 198 199 do_wait (&lock->owner, otid); 200 } 201 } 202 203 void 204 gomp_unset_nest_lock_25 (omp_nest_lock_25_t *lock) 205 { 206 /* ??? Validate that we own the lock here. */ 207 208 if (--lock->count == 0) 209 { 210 __sync_lock_release (&lock->owner); 211 futex_wake (&lock->owner, 1); 212 } 213 } 214 215 int 216 gomp_test_nest_lock_25 (omp_nest_lock_25_t *lock) 217 { 218 int otid, tid = gomp_tid (); 219 220 otid = __sync_val_compare_and_swap (&lock->owner, 0, tid); 221 if (otid == 0) 222 { 223 lock->count = 1; 224 return 1; 225 } 226 if (otid == tid) 227 return ++lock->count; 228 229 return 0; 230 } 231 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) 242 243 #else 244 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) 255 256 #endif 257