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 37 void 38 omp_init_lock (omp_lock_t *lock) 39 { 40 pthread_mutex_init (lock, NULL); 41 } 42 43 void 44 omp_destroy_lock (omp_lock_t *lock) 45 { 46 pthread_mutex_destroy (lock); 47 } 48 49 void 50 omp_set_lock (omp_lock_t *lock) 51 { 52 pthread_mutex_lock (lock); 53 } 54 55 void 56 omp_unset_lock (omp_lock_t *lock) 57 { 58 pthread_mutex_unlock (lock); 59 } 60 61 int 62 omp_test_lock (omp_lock_t *lock) 63 { 64 return pthread_mutex_trylock (lock) == 0; 65 } 66 67 void 68 omp_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 75 void 76 omp_destroy_nest_lock (omp_nest_lock_t *lock) 77 { 78 pthread_mutex_destroy (&lock->lock); 79 } 80 81 void 82 omp_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 95 void 96 omp_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 107 int 108 omp_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 122 ialias (omp_init_lock) 123 ialias (omp_init_nest_lock) 124 ialias (omp_destroy_lock) 125 ialias (omp_destroy_nest_lock) 126 ialias (omp_set_lock) 127 ialias (omp_set_nest_lock) 128 ialias (omp_unset_lock) 129 ialias (omp_unset_nest_lock) 130 ialias (omp_test_lock) 131 ialias (omp_test_nest_lock) 132