xref: /openbsd-src/gnu/gcc/libgomp/config/posix/sem.c (revision 404b540a9034ac75a6199ad1a32d1bbc7a0d4210)
1*404b540aSrobert /* Copyright (C) 2005, 2006 Free Software Foundation, Inc.
2*404b540aSrobert    Contributed by Richard Henderson <rth@redhat.com>.
3*404b540aSrobert 
4*404b540aSrobert    This file is part of the GNU OpenMP Library (libgomp).
5*404b540aSrobert 
6*404b540aSrobert    Libgomp is free software; you can redistribute it and/or modify it
7*404b540aSrobert    under the terms of the GNU Lesser General Public License as published by
8*404b540aSrobert    the Free Software Foundation; either version 2.1 of the License, or
9*404b540aSrobert    (at your option) any later version.
10*404b540aSrobert 
11*404b540aSrobert    Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
12*404b540aSrobert    WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13*404b540aSrobert    FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
14*404b540aSrobert    more details.
15*404b540aSrobert 
16*404b540aSrobert    You should have received a copy of the GNU Lesser General Public License
17*404b540aSrobert    along with libgomp; see the file COPYING.LIB.  If not, write to the
18*404b540aSrobert    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19*404b540aSrobert    MA 02110-1301, USA.  */
20*404b540aSrobert 
21*404b540aSrobert /* As a special exception, if you link this library with other files, some
22*404b540aSrobert    of which are compiled with GCC, to produce an executable, this library
23*404b540aSrobert    does not by itself cause the resulting executable to be covered by the
24*404b540aSrobert    GNU General Public License.  This exception does not however invalidate
25*404b540aSrobert    any other reasons why the executable file might be covered by the GNU
26*404b540aSrobert    General Public License.  */
27*404b540aSrobert 
28*404b540aSrobert /* This is the default POSIX 1003.1b implementation of a semaphore
29*404b540aSrobert    synchronization mechanism for libgomp.  This type is private to
30*404b540aSrobert    the library.
31*404b540aSrobert 
32*404b540aSrobert    This is a bit heavy weight for what we need, in that we're not
33*404b540aSrobert    interested in sem_wait as a cancelation point, but it's not too
34*404b540aSrobert    bad for a default.  */
35*404b540aSrobert 
36*404b540aSrobert #include "libgomp.h"
37*404b540aSrobert 
38*404b540aSrobert #ifdef HAVE_BROKEN_POSIX_SEMAPHORES
39*404b540aSrobert #include <stdlib.h>
40*404b540aSrobert 
gomp_sem_init(gomp_sem_t * sem,int value)41*404b540aSrobert void gomp_sem_init (gomp_sem_t *sem, int value)
42*404b540aSrobert {
43*404b540aSrobert   int ret;
44*404b540aSrobert 
45*404b540aSrobert   ret = pthread_mutex_init (&sem->mutex, NULL);
46*404b540aSrobert   if (ret)
47*404b540aSrobert     return;
48*404b540aSrobert 
49*404b540aSrobert   ret = pthread_cond_init (&sem->cond, NULL);
50*404b540aSrobert   if (ret)
51*404b540aSrobert     return;
52*404b540aSrobert 
53*404b540aSrobert   sem->value = value;
54*404b540aSrobert }
55*404b540aSrobert 
gomp_sem_wait(gomp_sem_t * sem)56*404b540aSrobert void gomp_sem_wait (gomp_sem_t *sem)
57*404b540aSrobert {
58*404b540aSrobert   int ret;
59*404b540aSrobert 
60*404b540aSrobert   ret = pthread_mutex_lock (&sem->mutex);
61*404b540aSrobert   if (ret)
62*404b540aSrobert     return;
63*404b540aSrobert 
64*404b540aSrobert   if (sem->value > 0)
65*404b540aSrobert     {
66*404b540aSrobert       sem->value--;
67*404b540aSrobert       ret = pthread_mutex_unlock (&sem->mutex);
68*404b540aSrobert       return;
69*404b540aSrobert     }
70*404b540aSrobert 
71*404b540aSrobert   while (sem->value <= 0)
72*404b540aSrobert     {
73*404b540aSrobert       ret = pthread_cond_wait (&sem->cond, &sem->mutex);
74*404b540aSrobert       if (ret)
75*404b540aSrobert 	{
76*404b540aSrobert 	  pthread_mutex_unlock (&sem->mutex);
77*404b540aSrobert 	  return;
78*404b540aSrobert 	}
79*404b540aSrobert     }
80*404b540aSrobert 
81*404b540aSrobert   sem->value--;
82*404b540aSrobert   ret = pthread_mutex_unlock (&sem->mutex);
83*404b540aSrobert   return;
84*404b540aSrobert }
85*404b540aSrobert 
gomp_sem_post(gomp_sem_t * sem)86*404b540aSrobert void gomp_sem_post (gomp_sem_t *sem)
87*404b540aSrobert {
88*404b540aSrobert   int ret;
89*404b540aSrobert 
90*404b540aSrobert   ret = pthread_mutex_lock (&sem->mutex);
91*404b540aSrobert   if (ret)
92*404b540aSrobert     return;
93*404b540aSrobert 
94*404b540aSrobert   sem->value++;
95*404b540aSrobert 
96*404b540aSrobert   ret = pthread_mutex_unlock (&sem->mutex);
97*404b540aSrobert   if (ret)
98*404b540aSrobert     return;
99*404b540aSrobert 
100*404b540aSrobert   ret = pthread_cond_signal (&sem->cond);
101*404b540aSrobert 
102*404b540aSrobert   return;
103*404b540aSrobert }
104*404b540aSrobert 
gomp_sem_destroy(gomp_sem_t * sem)105*404b540aSrobert void gomp_sem_destroy (gomp_sem_t *sem)
106*404b540aSrobert {
107*404b540aSrobert   int ret;
108*404b540aSrobert 
109*404b540aSrobert   ret = pthread_mutex_destroy (&sem->mutex);
110*404b540aSrobert   if (ret)
111*404b540aSrobert     return;
112*404b540aSrobert 
113*404b540aSrobert   ret = pthread_cond_destroy (&sem->cond);
114*404b540aSrobert 
115*404b540aSrobert   return;
116*404b540aSrobert }
117*404b540aSrobert #else /* HAVE_BROKEN_POSIX_SEMAPHORES  */
118*404b540aSrobert void
gomp_sem_wait(gomp_sem_t * sem)119*404b540aSrobert gomp_sem_wait (gomp_sem_t *sem)
120*404b540aSrobert {
121*404b540aSrobert   /* With POSIX, the wait can be canceled by signals.  We don't want that.
122*404b540aSrobert      It is expected that the return value here is -1 and errno is EINTR.  */
123*404b540aSrobert   while (sem_wait (sem) != 0)
124*404b540aSrobert     continue;
125*404b540aSrobert }
126*404b540aSrobert #endif
127