00001 /* Copyright (C) 2006, 2008, 2009 Free Software Foundation, Inc. 00002 00003 This file is part of the GNU OpenMP Library (libgomp). 00004 00005 Libgomp is free software; you can redistribute it and/or modify it 00006 under the terms of the GNU General Public License as published by 00007 the Free Software Foundation; either version 3, or (at your option) 00008 any later version. 00009 00010 Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY 00011 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 00012 FOR A PARTICULAR PURPOSE. See the GNU General Public License for 00013 more details. 00014 00015 Under Section 7 of GPL version 3, you are granted additional 00016 permissions described in the GCC Runtime Library Exception, version 00017 3.1, as published by the Free Software Foundation. 00018 00019 You should have received a copy of the GNU General Public License and 00020 a copy of the GCC Runtime Library Exception along with this program; 00021 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00022 <http://www.gnu.org/licenses/>. */ 00023 00024 /* This is the POSIX95 implementation of the public OpenMP locking primitives. 00025 00026 Because OpenMP uses different entry points for normal and recursive 00027 locks, and pthreads uses only one entry point, a system may be able 00028 to do better and streamline the locking as well as reduce the size 00029 of the types exported. */ 00030 00031 #include "libgomp.h" 00032 00033 #ifdef HAVE_BROKEN_POSIX_SEMAPHORES 00034 void 00035 gomp_init_lock_30 (omp_lock_t *lock) 00036 { 00037 pthread_mutex_init (lock, NULL); 00038 } 00039 00040 void 00041 gomp_destroy_lock_30 (omp_lock_t *lock) 00042 { 00043 pthread_mutex_destroy (lock); 00044 } 00045 00046 void 00047 gomp_set_lock_30 (omp_lock_t *lock) 00048 { 00049 pthread_mutex_lock (lock); 00050 } 00051 00052 void 00053 gomp_unset_lock_30 (omp_lock_t *lock) 00054 { 00055 pthread_mutex_unlock (lock); 00056 } 00057 00058 int 00059 gomp_test_lock_30 (omp_lock_t *lock) 00060 { 00061 return pthread_mutex_trylock (lock) == 0; 00062 } 00063 00064 void 00065 gomp_init_nest_lock_30 (omp_nest_lock_t *lock) 00066 { 00067 pthread_mutex_init (&lock->lock, NULL); 00068 lock->owner = NULL; 00069 lock->count = 0; 00070 } 00071 00072 void 00073 gomp_destroy_nest_lock_30 (omp_nest_lock_t *lock) 00074 { 00075 pthread_mutex_destroy (&lock->lock); 00076 } 00077 00078 void 00079 gomp_set_nest_lock_30 (omp_nest_lock_t *lock) 00080 { 00081 void *me = gomp_icv (true); 00082 00083 if (lock->owner != me) 00084 { 00085 pthread_mutex_lock (&lock->lock); 00086 lock->owner = me; 00087 } 00088 00089 lock->count++; 00090 } 00091 00092 void 00093 gomp_unset_nest_lock_30 (omp_nest_lock_t *lock) 00094 { 00095 lock->count--; 00096 00097 if (lock->count == 0) 00098 { 00099 lock->owner = NULL; 00100 pthread_mutex_unlock (&lock->lock); 00101 } 00102 } 00103 00104 int 00105 gomp_test_nest_lock_30 (omp_nest_lock_t *lock) 00106 { 00107 void *me = gomp_icv (true); 00108 00109 if (lock->owner != me) 00110 { 00111 if (pthread_mutex_trylock (&lock->lock) != 0) 00112 return 0; 00113 lock->owner = me; 00114 } 00115 00116 return ++lock->count; 00117 } 00118 00119 #else 00120 00121 void 00122 gomp_init_lock_30 (omp_lock_t *lock) 00123 { 00124 sem_init (lock, 0, 1); 00125 } 00126 00127 void 00128 gomp_destroy_lock_30 (omp_lock_t *lock) 00129 { 00130 sem_destroy (lock); 00131 } 00132 00133 void 00134 gomp_set_lock_30 (omp_lock_t *lock) 00135 { 00136 while (sem_wait (lock) != 0) 00137 ; 00138 } 00139 00140 void 00141 gomp_unset_lock_30 (omp_lock_t *lock) 00142 { 00143 sem_post (lock); 00144 } 00145 00146 int 00147 gomp_test_lock_30 (omp_lock_t *lock) 00148 { 00149 return sem_trywait (lock) == 0; 00150 } 00151 00152 void 00153 gomp_init_nest_lock_30 (omp_nest_lock_t *lock) 00154 { 00155 sem_init (&lock->lock, 0, 1); 00156 lock->count = 0; 00157 lock->owner = NULL; 00158 } 00159 00160 void 00161 gomp_destroy_nest_lock_30 (omp_nest_lock_t *lock) 00162 { 00163 sem_destroy (&lock->lock); 00164 } 00165 00166 void 00167 gomp_set_nest_lock_30 (omp_nest_lock_t *lock) 00168 { 00169 void *me = gomp_icv (true); 00170 00171 if (lock->owner != me) 00172 { 00173 while (sem_wait (&lock->lock) != 0) 00174 ; 00175 lock->owner = me; 00176 } 00177 lock->count++; 00178 } 00179 00180 void 00181 gomp_unset_nest_lock_30 (omp_nest_lock_t *lock) 00182 { 00183 if (--lock->count == 0) 00184 { 00185 lock->owner = NULL; 00186 sem_post (&lock->lock); 00187 } 00188 } 00189 00190 int 00191 gomp_test_nest_lock_30 (omp_nest_lock_t *lock) 00192 { 00193 void *me = gomp_icv (true); 00194 00195 if (lock->owner != me) 00196 { 00197 if (sem_trywait (&lock->lock) != 0) 00198 return 0; 00199 lock->owner = me; 00200 } 00201 00202 return ++lock->count; 00203 } 00204 #endif 00205 00206 #ifdef LIBGOMP_GNU_SYMBOL_VERSIONING 00207 void 00208 gomp_init_lock_25 (omp_lock_25_t *lock) 00209 { 00210 pthread_mutex_init (lock, NULL); 00211 } 00212 00213 void 00214 gomp_destroy_lock_25 (omp_lock_25_t *lock) 00215 { 00216 pthread_mutex_destroy (lock); 00217 } 00218 00219 void 00220 gomp_set_lock_25 (omp_lock_25_t *lock) 00221 { 00222 pthread_mutex_lock (lock); 00223 } 00224 00225 void 00226 gomp_unset_lock_25 (omp_lock_25_t *lock) 00227 { 00228 pthread_mutex_unlock (lock); 00229 } 00230 00231 int 00232 gomp_test_lock_25 (omp_lock_25_t *lock) 00233 { 00234 return pthread_mutex_trylock (lock) == 0; 00235 } 00236 00237 void 00238 gomp_init_nest_lock_25 (omp_nest_lock_25_t *lock) 00239 { 00240 pthread_mutex_init (&lock->lock, NULL); 00241 lock->owner = (pthread_t) 0; 00242 lock->count = 0; 00243 } 00244 00245 void 00246 gomp_destroy_nest_lock_25 (omp_nest_lock_25_t *lock) 00247 { 00248 pthread_mutex_destroy (&lock->lock); 00249 } 00250 00251 void 00252 gomp_set_nest_lock_25 (omp_nest_lock_25_t *lock) 00253 { 00254 pthread_t me = pthread_self (); 00255 00256 if (lock->owner != me) 00257 { 00258 pthread_mutex_lock (&lock->lock); 00259 lock->owner = me; 00260 } 00261 00262 lock->count++; 00263 } 00264 00265 void 00266 gomp_unset_nest_lock_25 (omp_nest_lock_25_t *lock) 00267 { 00268 lock->count--; 00269 00270 if (lock->count == 0) 00271 { 00272 lock->owner = (pthread_t) 0; 00273 pthread_mutex_unlock (&lock->lock); 00274 } 00275 } 00276 00277 int 00278 gomp_test_nest_lock_25 (omp_nest_lock_25_t *lock) 00279 { 00280 pthread_t me = pthread_self (); 00281 00282 if (lock->owner != me) 00283 { 00284 if (pthread_mutex_trylock (&lock->lock) != 0) 00285 return 0; 00286 lock->owner = me; 00287 } 00288 00289 return ++lock->count; 00290 } 00291 00292 omp_lock_symver (omp_init_lock) 00293 omp_lock_symver (omp_destroy_lock) 00294 omp_lock_symver (omp_set_lock) 00295 omp_lock_symver (omp_unset_lock) 00296 omp_lock_symver (omp_test_lock) 00297 omp_lock_symver (omp_init_nest_lock) 00298 omp_lock_symver (omp_destroy_nest_lock) 00299 omp_lock_symver (omp_set_nest_lock) 00300 omp_lock_symver (omp_unset_nest_lock) 00301 omp_lock_symver (omp_test_nest_lock) 00302 00303 #else 00304 00305 ialias (omp_init_lock) 00306 ialias (omp_init_nest_lock) 00307 ialias (omp_destroy_lock) 00308 ialias (omp_destroy_nest_lock) 00309 ialias (omp_set_lock) 00310 ialias (omp_set_nest_lock) 00311 ialias (omp_unset_lock) 00312 ialias (omp_unset_nest_lock) 00313 ialias (omp_test_lock) 00314 ialias (omp_test_nest_lock) 00315 00316 #endif