138494Sobrien/* Copyright (C) 2006 Free Software Foundation, Inc. 2310490Scy 338494Sobrien This file is part of the GNU OpenMP Library (libgomp). 438494Sobrien 538494Sobrien Libgomp is free software; you can redistribute it and/or modify it 638494Sobrien under the terms of the GNU Lesser General Public License as published by 738494Sobrien the Free Software Foundation; either version 2.1 of the License, or 838494Sobrien (at your option) any later version. 938494Sobrien 1038494Sobrien Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY 1138494Sobrien WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 1238494Sobrien FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for 1338494Sobrien more details. 1438494Sobrien 1538494Sobrien You should have received a copy of the GNU Lesser General Public License 1638494Sobrien along with libgomp; see the file COPYING.LIB. If not, write to the 1738494Sobrien Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 1838494Sobrien MA 02110-1301, USA. */ 19310490Scy 2038494Sobrien/* As a special exception, if you link this library with other files, some 2138494Sobrien of which are compiled with GCC, to produce an executable, this library 2238494Sobrien does not by itself cause the resulting executable to be covered by the 2338494Sobrien GNU General Public License. This exception does not however invalidate 2438494Sobrien any other reasons why the executable file might be covered by the GNU 2538494Sobrien General Public License. */ 2638494Sobrien 2738494Sobrien/* This is the POSIX95 implementation of the public OpenMP locking primitives. 2838494Sobrien 2938494Sobrien Because OpenMP uses different entry points for normal and recursive 3038494Sobrien locks, and pthreads uses only one entry point, a system may be able 3138494Sobrien to do better and streamline the locking as well as reduce the size 3238494Sobrien of the types exported. */ 3338494Sobrien 3438494Sobrien#include "libgomp.h" 3538494Sobrien 36174313Sobrien 3738494Sobrienvoid 3838494Sobrienomp_init_lock (omp_lock_t *lock) 3938494Sobrien{ 4038494Sobrien pthread_mutex_init (lock, NULL); 4138494Sobrien} 4238494Sobrien 4338494Sobrienvoid 4438494Sobrienomp_destroy_lock (omp_lock_t *lock) 4538494Sobrien{ 4638494Sobrien pthread_mutex_destroy (lock); 4738494Sobrien} 4841145Sobrien 4941145Sobrienvoid 5041145Sobrienomp_set_lock (omp_lock_t *lock) 5141145Sobrien{ 5241145Sobrien pthread_mutex_lock (lock); 5338494Sobrien} 5438494Sobrien 5538494Sobrienvoid 5638494Sobrienomp_unset_lock (omp_lock_t *lock) 5738494Sobrien{ 5838494Sobrien pthread_mutex_unlock (lock); 5938494Sobrien} 6038494Sobrien 6138494Sobrienint 6238494Sobrienomp_test_lock (omp_lock_t *lock) 6338494Sobrien{ 6438494Sobrien return pthread_mutex_trylock (lock) == 0; 6538494Sobrien} 6638494Sobrien 6738494Sobrienvoid 6838494Sobrienomp_init_nest_lock (omp_nest_lock_t *lock) 6938494Sobrien{ 7038494Sobrien pthread_mutex_init (&lock->lock, NULL); 7138494Sobrien lock->owner = (pthread_t) 0; 7238494Sobrien lock->count = 0; 7338494Sobrien} 7438494Sobrien 7538494Sobrienvoid 7638494Sobrienomp_destroy_nest_lock (omp_nest_lock_t *lock) 7738494Sobrien{ 7838494Sobrien pthread_mutex_destroy (&lock->lock); 7938494Sobrien} 8038494Sobrien 8138494Sobrienvoid 8238494Sobrienomp_set_nest_lock (omp_nest_lock_t *lock) 8338494Sobrien{ 8438494Sobrien pthread_t me = pthread_self (); 8538494Sobrien 8638494Sobrien if (lock->owner != me) 8738494Sobrien { 8838494Sobrien pthread_mutex_lock (&lock->lock); 8938494Sobrien lock->owner = me; 9038494Sobrien } 9138494Sobrien 92310490Scy lock->count++; 9338494Sobrien} 9438494Sobrien 9538494Sobrienvoid 9638494Sobrienomp_unset_nest_lock (omp_nest_lock_t *lock) 9738494Sobrien{ 9838494Sobrien lock->count--; 9938494Sobrien 10038494Sobrien if (lock->count == 0) 10138494Sobrien { 10238494Sobrien lock->owner = (pthread_t) 0; 10338494Sobrien pthread_mutex_unlock (&lock->lock); 10438494Sobrien } 10538494Sobrien} 10638494Sobrien 10738494Sobrienint 10838494Sobrienomp_test_nest_lock (omp_nest_lock_t *lock) 10938494Sobrien{ 11038494Sobrien pthread_t me = pthread_self (); 11138494Sobrien 11238494Sobrien if (lock->owner != me) 11338494Sobrien { 11438494Sobrien if (pthread_mutex_trylock (&lock->lock) != 0) 11541145Sobrien return 0; 11638494Sobrien lock->owner = me; 11738494Sobrien } 11838494Sobrien 11938494Sobrien return ++lock->count; 12038494Sobrien} 12138494Sobrien 12238494Sobrienialias (omp_init_lock) 12338494Sobrienialias (omp_init_nest_lock) 12438494Sobrienialias (omp_destroy_lock) 12538494Sobrienialias (omp_destroy_nest_lock) 12638494Sobrienialias (omp_set_lock) 12738494Sobrienialias (omp_set_nest_lock) 12838494Sobrienialias (omp_unset_lock) 12938494Sobrienialias (omp_unset_nest_lock) 13038494Sobrienialias (omp_test_lock) 13138494Sobrienialias (omp_test_nest_lock) 13238494Sobrien