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