1169695Skan/* Copyright (C) 2005 Free Software Foundation, Inc. 2169695Skan Contributed by Richard Henderson <rth@redhat.com>. 3169695Skan 4169695Skan This file is part of the GNU OpenMP Library (libgomp). 5169695Skan 6169695Skan Libgomp is free software; you can redistribute it and/or modify it 7169695Skan under the terms of the GNU Lesser General Public License as published by 8169695Skan the Free Software Foundation; either version 2.1 of the License, or 9169695Skan (at your option) any later version. 10169695Skan 11169695Skan Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY 12169695Skan WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 13169695Skan FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for 14169695Skan more details. 15169695Skan 16169695Skan You should have received a copy of the GNU Lesser General Public License 17169695Skan along with libgomp; see the file COPYING.LIB. If not, write to the 18169695Skan Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 19169695Skan MA 02110-1301, USA. */ 20169695Skan 21169695Skan/* As a special exception, if you link this library with other files, some 22169695Skan of which are compiled with GCC, to produce an executable, this library 23169695Skan does not by itself cause the resulting executable to be covered by the 24169695Skan GNU General Public License. This exception does not however invalidate 25169695Skan any other reasons why the executable file might be covered by the GNU 26169695Skan General Public License. */ 27169695Skan 28169695Skan/* This is a Linux specific implementation of the public OpenMP locking 29169695Skan primitives. This implementation uses atomic instructions and the futex 30169695Skan syscall. */ 31169695Skan 32169695Skan#include "libgomp.h" 33169695Skan#include <string.h> 34169695Skan#include <unistd.h> 35169695Skan#include <sys/syscall.h> 36169695Skan#include "futex.h" 37169695Skan 38169695Skan 39169695Skan/* The internal gomp_mutex_t and the external non-recursive omp_lock_t 40169695Skan have the same form. Re-use it. */ 41169695Skan 42169695Skanvoid 43169695Skanomp_init_lock (omp_lock_t *lock) 44169695Skan{ 45169695Skan gomp_mutex_init (lock); 46169695Skan} 47169695Skan 48169695Skanvoid 49169695Skanomp_destroy_lock (omp_lock_t *lock) 50169695Skan{ 51169695Skan gomp_mutex_destroy (lock); 52169695Skan} 53169695Skan 54169695Skanvoid 55169695Skanomp_set_lock (omp_lock_t *lock) 56169695Skan{ 57169695Skan gomp_mutex_lock (lock); 58169695Skan} 59169695Skan 60169695Skanvoid 61169695Skanomp_unset_lock (omp_lock_t *lock) 62169695Skan{ 63169695Skan gomp_mutex_unlock (lock); 64169695Skan} 65169695Skan 66169695Skanint 67169695Skanomp_test_lock (omp_lock_t *lock) 68169695Skan{ 69169695Skan return __sync_bool_compare_and_swap (lock, 0, 1); 70169695Skan} 71169695Skan 72169695Skan/* The external recursive omp_nest_lock_t form requires additional work. */ 73169695Skan 74169695Skan/* We need an integer to uniquely identify this thread. Most generally 75169695Skan this is the thread's TID, which ideally we'd get this straight from 76169695Skan the TLS block where glibc keeps it. Unfortunately, we can't get at 77169695Skan that directly. 78169695Skan 79169695Skan If we don't support (or have disabled) TLS, one function call is as 80169695Skan good (or bad) as any other. Use the syscall all the time. 81169695Skan 82169695Skan On an ILP32 system (defined here as not LP64), we can make do with 83169695Skan any thread-local pointer. Ideally we'd use the TLS base address, 84169695Skan since that requires the least amount of arithmetic, but that's not 85169695Skan always available directly. Make do with the gomp_thread pointer 86169695Skan since it's handy. */ 87169695Skan 88169695Skan#if !defined (HAVE_TLS) 89169695Skanstatic inline int gomp_tid (void) 90169695Skan{ 91169695Skan return syscall (SYS_gettid); 92169695Skan} 93169695Skan#elif !defined(__LP64__) 94169695Skanstatic inline int gomp_tid (void) 95169695Skan{ 96169695Skan return (int) gomp_thread (); 97169695Skan} 98169695Skan#else 99169695Skanstatic __thread int tid_cache; 100169695Skanstatic inline int gomp_tid (void) 101169695Skan{ 102169695Skan int tid = tid_cache; 103169695Skan if (__builtin_expect (tid == 0, 0)) 104169695Skan tid_cache = tid = syscall (SYS_gettid); 105169695Skan return tid; 106169695Skan} 107169695Skan#endif 108169695Skan 109169695Skan 110169695Skanvoid 111169695Skanomp_init_nest_lock (omp_nest_lock_t *lock) 112169695Skan{ 113169695Skan memset (lock, 0, sizeof (lock)); 114169695Skan} 115169695Skan 116169695Skanvoid 117169695Skanomp_destroy_nest_lock (omp_nest_lock_t *lock) 118169695Skan{ 119169695Skan} 120169695Skan 121169695Skanvoid 122169695Skanomp_set_nest_lock (omp_nest_lock_t *lock) 123169695Skan{ 124169695Skan int otid, tid = gomp_tid (); 125169695Skan 126169695Skan while (1) 127169695Skan { 128169695Skan otid = __sync_val_compare_and_swap (&lock->owner, 0, tid); 129169695Skan if (otid == 0) 130169695Skan { 131169695Skan lock->count = 1; 132169695Skan return; 133169695Skan } 134169695Skan if (otid == tid) 135169695Skan { 136169695Skan lock->count++; 137169695Skan return; 138169695Skan } 139169695Skan 140169695Skan futex_wait (&lock->owner, otid); 141169695Skan } 142169695Skan} 143169695Skan 144169695Skanvoid 145169695Skanomp_unset_nest_lock (omp_nest_lock_t *lock) 146169695Skan{ 147169695Skan /* ??? Validate that we own the lock here. */ 148169695Skan 149169695Skan if (--lock->count == 0) 150169695Skan { 151169695Skan __sync_lock_release (&lock->owner); 152169695Skan futex_wake (&lock->owner, 1); 153169695Skan } 154169695Skan} 155169695Skan 156169695Skanint 157169695Skanomp_test_nest_lock (omp_nest_lock_t *lock) 158169695Skan{ 159169695Skan int otid, tid = gomp_tid (); 160169695Skan 161169695Skan otid = __sync_val_compare_and_swap (&lock->owner, 0, tid); 162169695Skan if (otid == 0) 163169695Skan { 164169695Skan lock->count = 1; 165169695Skan return 1; 166169695Skan } 167169695Skan if (otid == tid) 168169695Skan return ++lock->count; 169169695Skan 170169695Skan return 0; 171169695Skan} 172169695Skan 173169695Skanialias (omp_init_lock) 174169695Skanialias (omp_init_nest_lock) 175169695Skanialias (omp_destroy_lock) 176169695Skanialias (omp_destroy_nest_lock) 177169695Skanialias (omp_set_lock) 178169695Skanialias (omp_set_nest_lock) 179169695Skanialias (omp_unset_lock) 180169695Skanialias (omp_unset_nest_lock) 181169695Skanialias (omp_test_lock) 182169695Skanialias (omp_test_nest_lock) 183