1/* Copyright (C) 2006 Free Software Foundation, Inc. 2 3 This file is part of the GNU OpenMP Library (libgomp). 4 5 Libgomp is free software; you can redistribute it and/or modify it 6 under the terms of the GNU Lesser General Public License as published by 7 the Free Software Foundation; either version 2.1 of the License, or 8 (at your option) any later version. 9 10 Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY 11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 12 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for 13 more details. 14 15 You should have received a copy of the GNU Lesser General Public License 16 along with libgomp; see the file COPYING.LIB. If not, write to the 17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 18 MA 02110-1301, USA. */ 19 20/* As a special exception, if you link this library with other files, some 21 of which are compiled with GCC, to produce an executable, this library 22 does not by itself cause the resulting executable to be covered by the 23 GNU General Public License. This exception does not however invalidate 24 any other reasons why the executable file might be covered by the GNU 25 General Public License. */ 26 27/* This is the POSIX95 implementation of the public OpenMP locking primitives. 28 29 Because OpenMP uses different entry points for normal and recursive 30 locks, and pthreads uses only one entry point, a system may be able 31 to do better and streamline the locking as well as reduce the size 32 of the types exported. */ 33 34#include "libgomp.h" 35 36 37void 38omp_init_lock (omp_lock_t *lock) 39{ 40 pthread_mutex_init (lock, NULL); 41} 42 43void 44omp_destroy_lock (omp_lock_t *lock) 45{ 46 pthread_mutex_destroy (lock); 47} 48 49void 50omp_set_lock (omp_lock_t *lock) 51{ 52 pthread_mutex_lock (lock); 53} 54 55void 56omp_unset_lock (omp_lock_t *lock) 57{ 58 pthread_mutex_unlock (lock); 59} 60 61int 62omp_test_lock (omp_lock_t *lock) 63{ 64 return pthread_mutex_trylock (lock) == 0; 65} 66 67void 68omp_init_nest_lock (omp_nest_lock_t *lock) 69{ 70 pthread_mutex_init (&lock->lock, NULL); 71 lock->owner = (pthread_t) 0; 72 lock->count = 0; 73} 74 75void 76omp_destroy_nest_lock (omp_nest_lock_t *lock) 77{ 78 pthread_mutex_destroy (&lock->lock); 79} 80 81void 82omp_set_nest_lock (omp_nest_lock_t *lock) 83{ 84 pthread_t me = pthread_self (); 85 86 if (lock->owner != me) 87 { 88 pthread_mutex_lock (&lock->lock); 89 lock->owner = me; 90 } 91 92 lock->count++; 93} 94 95void 96omp_unset_nest_lock (omp_nest_lock_t *lock) 97{ 98 lock->count--; 99 100 if (lock->count == 0) 101 { 102 lock->owner = (pthread_t) 0; 103 pthread_mutex_unlock (&lock->lock); 104 } 105} 106 107int 108omp_test_nest_lock (omp_nest_lock_t *lock) 109{ 110 pthread_t me = pthread_self (); 111 112 if (lock->owner != me) 113 { 114 if (pthread_mutex_trylock (&lock->lock) != 0) 115 return 0; 116 lock->owner = me; 117 } 118 119 return ++lock->count; 120} 121 122ialias (omp_init_lock) 123ialias (omp_init_nest_lock) 124ialias (omp_destroy_lock) 125ialias (omp_destroy_nest_lock) 126ialias (omp_set_lock) 127ialias (omp_set_nest_lock) 128ialias (omp_unset_lock) 129ialias (omp_unset_nest_lock) 130ialias (omp_test_lock) 131ialias (omp_test_nest_lock) 132