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