config/posix/sem.c

Go to the documentation of this file.
00001 /* Copyright (C) 2005, 2006, 2009 Free Software Foundation, Inc.
00002    Contributed by Richard Henderson <rth@redhat.com>.
00003 
00004    This file is part of the GNU OpenMP Library (libgomp).
00005 
00006    Libgomp is free software; you can redistribute it and/or modify it
00007    under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 3, or (at your option)
00009    any later version.
00010 
00011    Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
00012    WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00013    FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
00014    more details.
00015 
00016    Under Section 7 of GPL version 3, you are granted additional
00017    permissions described in the GCC Runtime Library Exception, version
00018    3.1, as published by the Free Software Foundation.
00019 
00020    You should have received a copy of the GNU General Public License and
00021    a copy of the GCC Runtime Library Exception along with this program;
00022    see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023    <http://www.gnu.org/licenses/>.  */
00024 
00025 /* This is the default POSIX 1003.1b implementation of a semaphore
00026    synchronization mechanism for libgomp.  This type is private to
00027    the library.
00028 
00029    This is a bit heavy weight for what we need, in that we're not
00030    interested in sem_wait as a cancelation point, but it's not too
00031    bad for a default.  */
00032 
00033 #include "libgomp.h"
00034 
00035 #ifdef HAVE_BROKEN_POSIX_SEMAPHORES
00036 #include <stdlib.h>
00037 
00038 void gomp_sem_init (gomp_sem_t *sem, int value)
00039 {
00040   int ret;
00041 
00042   ret = pthread_mutex_init (&sem->mutex, NULL);
00043   if (ret)
00044     return;
00045 
00046   ret = pthread_cond_init (&sem->cond, NULL);
00047   if (ret)
00048     return;
00049 
00050   sem->value = value;
00051 }
00052 
00053 void gomp_sem_wait (gomp_sem_t *sem)
00054 {
00055   int ret;
00056 
00057   ret = pthread_mutex_lock (&sem->mutex);
00058   if (ret)
00059     return;
00060 
00061   if (sem->value > 0)
00062     {
00063       sem->value--;
00064       ret = pthread_mutex_unlock (&sem->mutex);
00065       return;
00066     }
00067 
00068   while (sem->value <= 0)
00069     {
00070       ret = pthread_cond_wait (&sem->cond, &sem->mutex);
00071       if (ret)
00072     {
00073       pthread_mutex_unlock (&sem->mutex);
00074       return;
00075     }
00076     }
00077 
00078   sem->value--;
00079   ret = pthread_mutex_unlock (&sem->mutex);
00080   return;
00081 }
00082 
00083 void gomp_sem_post (gomp_sem_t *sem)
00084 {
00085   int ret;
00086 
00087   ret = pthread_mutex_lock (&sem->mutex);
00088   if (ret)
00089     return;
00090 
00091   sem->value++;
00092 
00093   ret = pthread_mutex_unlock (&sem->mutex);
00094   if (ret)
00095     return;
00096 
00097   ret = pthread_cond_signal (&sem->cond);
00098 
00099   return;
00100 }
00101 
00102 void gomp_sem_destroy (gomp_sem_t *sem)
00103 {
00104   int ret;
00105 
00106   ret = pthread_mutex_destroy (&sem->mutex);
00107   if (ret)
00108     return;
00109 
00110   ret = pthread_cond_destroy (&sem->cond);
00111 
00112   return;
00113 }
00114 #else /* HAVE_BROKEN_POSIX_SEMAPHORES  */
00115 void
00116 gomp_sem_wait (gomp_sem_t *sem)
00117 {
00118   /* With POSIX, the wait can be canceled by signals.  We don't want that.
00119      It is expected that the return value here is -1 and errno is EINTR.  */
00120   while (sem_wait (sem) != 0)
00121     continue;
00122 }
00123 #endif

Generated on Fri Apr 5 05:38:09 2013 for Libgomp by  doxygen 1.4.7