1169695Skan/* Copyright (C) 2005, 2006 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 the default POSIX 1003.1b implementation of a semaphore 29169695Skan synchronization mechanism for libgomp. This type is private to 30169695Skan the library. 31169695Skan 32169695Skan This is a bit heavy weight for what we need, in that we're not 33169695Skan interested in sem_wait as a cancelation point, but it's not too 34169695Skan bad for a default. */ 35169695Skan 36169695Skan#include "libgomp.h" 37169695Skan 38169695Skan#ifdef HAVE_BROKEN_POSIX_SEMAPHORES 39169695Skan#include <stdlib.h> 40169695Skan 41169695Skanvoid gomp_sem_init (gomp_sem_t *sem, int value) 42169695Skan{ 43169695Skan int ret; 44169695Skan 45169695Skan ret = pthread_mutex_init (&sem->mutex, NULL); 46169695Skan if (ret) 47169695Skan return; 48169695Skan 49169695Skan ret = pthread_cond_init (&sem->cond, NULL); 50169695Skan if (ret) 51169695Skan return; 52169695Skan 53169695Skan sem->value = value; 54169695Skan} 55169695Skan 56169695Skanvoid gomp_sem_wait (gomp_sem_t *sem) 57169695Skan{ 58169695Skan int ret; 59169695Skan 60169695Skan ret = pthread_mutex_lock (&sem->mutex); 61169695Skan if (ret) 62169695Skan return; 63169695Skan 64169695Skan if (sem->value > 0) 65169695Skan { 66169695Skan sem->value--; 67169695Skan ret = pthread_mutex_unlock (&sem->mutex); 68169695Skan return; 69169695Skan } 70169695Skan 71169695Skan while (sem->value <= 0) 72169695Skan { 73169695Skan ret = pthread_cond_wait (&sem->cond, &sem->mutex); 74169695Skan if (ret) 75169695Skan { 76169695Skan pthread_mutex_unlock (&sem->mutex); 77169695Skan return; 78169695Skan } 79169695Skan } 80169695Skan 81169695Skan sem->value--; 82169695Skan ret = pthread_mutex_unlock (&sem->mutex); 83169695Skan return; 84169695Skan} 85169695Skan 86169695Skanvoid gomp_sem_post (gomp_sem_t *sem) 87169695Skan{ 88169695Skan int ret; 89169695Skan 90169695Skan ret = pthread_mutex_lock (&sem->mutex); 91169695Skan if (ret) 92169695Skan return; 93169695Skan 94169695Skan sem->value++; 95169695Skan 96169695Skan ret = pthread_mutex_unlock (&sem->mutex); 97169695Skan if (ret) 98169695Skan return; 99169695Skan 100169695Skan ret = pthread_cond_signal (&sem->cond); 101169695Skan 102169695Skan return; 103169695Skan} 104169695Skan 105169695Skanvoid gomp_sem_destroy (gomp_sem_t *sem) 106169695Skan{ 107169695Skan int ret; 108169695Skan 109169695Skan ret = pthread_mutex_destroy (&sem->mutex); 110169695Skan if (ret) 111169695Skan return; 112169695Skan 113169695Skan ret = pthread_cond_destroy (&sem->cond); 114169695Skan 115169695Skan return; 116169695Skan} 117169695Skan#else /* HAVE_BROKEN_POSIX_SEMAPHORES */ 118169695Skanvoid 119169695Skangomp_sem_wait (gomp_sem_t *sem) 120169695Skan{ 121169695Skan /* With POSIX, the wait can be canceled by signals. We don't want that. 122169695Skan It is expected that the return value here is -1 and errno is EINTR. */ 123169695Skan while (sem_wait (sem) != 0) 124169695Skan continue; 125169695Skan} 126169695Skan#endif 127