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