Debian Codegraph @

src/OVAL/probes/SEAP/sexp-atomic.c

raw symbols SCIP
1 /*
2 * Copyright 2009 Red Hat Inc., Durham, North Carolina.
3 * All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * "Daniel Kopecek" <[email protected]>
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdlib.h>
28 #include <stdint.h>
29 #include <stdbool.h>
30
31 #include "_sexp-atomic.h"
32
33 #if defined(HAVE_ATOMIC_BUILTINS)
34 uint16_t SEXP_atomic_dec_u16 (volatile uint16_t *ptr)
35 {
36 return (__sync_sub_and_fetch (ptr, 1));
37 }
38
39 uint16_t SEXP_atomic_inc_u16 (volatile uint16_t *ptr)
40 {
41 return (__sync_fetch_and_add (ptr, 1));
42 }
43
44 bool SEXP_atomic_cas_u16 (volatile uint16_t *ptr, uint16_t old, uint16_t new)
45 {
46 return ((bool) __sync_bool_compare_and_swap (ptr, old, new));
47 }
48
49 uint32_t SEXP_atomic_dec_u32 (volatile uint32_t *ptr)
50 {
51 return (__sync_sub_and_fetch (ptr, 1));
52 }
53
54 uint32_t SEXP_atomic_inc_u32 (volatile uint32_t *ptr)
55 {
56 return (__sync_fetch_and_add (ptr, 1));
57 }
58
59 bool SEXP_atomic_cas_u32 (volatile uint32_t *ptr, uint32_t old, uint32_t new)
60 {
61 return ((bool) __sync_bool_compare_and_swap (ptr, old, new));
62 }
63
64 #ifdef SEXP_ATOMIC_64BITS
65 uint64_t SEXP_atomic_dec_u64 (volatile uint64_t *ptr)
66 {
67 return (__sync_sub_and_fetch (ptr, 1));
68 }
69
70 uint64_t SEXP_atomic_inc_u64 (volatile uint64_t *ptr)
71 {
72 return (__sync_fetch_and_add (ptr, 1));
73 }
74
75 bool SEXP_atomic_cas_u64 (volatile uint64_t *ptr, uint64_t old, uint64_t new)
76 {
77 return ((bool) __sync_bool_compare_and_swap (ptr, old, new));
78 }
79 #endif /* SEXP_ATOMIC_64BITS */
80
81 #else
82 /* Using mutex-based emulation of atomic operations! */
83 # include <pthread.h>
84 # define SEXP_ATOMIC_MTX_CNT 17
85
86 static pthread_once_t __SEXP_atomic_once = PTHREAD_ONCE_INIT;
87 static pthread_mutex_t __SEXP_atomic_mtx[SEXP_ATOMIC_MTX_CNT];
88
89 static void SEXP_atomic_deinit (void)
90 {
91 register int i;
92
93 for (i = SEXP_ATOMIC_MTX_CNT; i > 0; --i)
94 pthread_mutex_destroy (&__SEXP_atomic_mtx[i - 1]);
95
96 return;
97 }
98
99 static void SEXP_atomic_init (void)
100 {
101 register int i;
102
103 for (i = SEXP_ATOMIC_MTX_CNT; i > 0; --i)
104 pthread_mutex_init (&__SEXP_atomic_mtx[i - 1], NULL);
105
106 atexit (SEXP_atomic_deinit);
107
108 return;
109 }
110
111 static inline void SEXP_atomic_once (void)
112 {
113 if (pthread_once (&__SEXP_atomic_once, SEXP_atomic_init) != 0)
114 abort ();
115 }
116
117 static inline void SEXP_atomic_lock (uintptr_t ptr)
118 {
119 if (pthread_mutex_lock (&__SEXP_atomic_mtx[ptr % SEXP_ATOMIC_MTX_CNT]) != 0)
120 abort ();
121 }
122
123 static inline void SEXP_atomic_unlock (uintptr_t ptr)
124 {
125 if (pthread_mutex_unlock (&__SEXP_atomic_mtx[ptr % SEXP_ATOMIC_MTX_CNT]) != 0)
126 abort ();
127 }
128
129 uint16_t SEXP_atomic_dec_u16 (volatile uint16_t *ptr)
130 {
131 uint16_t r;
132
133 SEXP_atomic_once();
134 SEXP_atomic_lock((uintptr_t)ptr);
135 r = *ptr - 1;
136 *ptr = r;
137 SEXP_atomic_unlock((uintptr_t)ptr);
138
139 return (r);
140 }
141
142 uint16_t SEXP_atomic_inc_u16 (volatile uint16_t *ptr)
143 {
144 uint16_t r;
145
146 SEXP_atomic_once();
147 SEXP_atomic_lock((uintptr_t)ptr);
148 r = *ptr;
149 *ptr = r + 1;
150 SEXP_atomic_unlock((uintptr_t)ptr);
151
152 return (r);
153 }
154
155 bool SEXP_atomic_cas_u16 (volatile uint16_t *ptr, uint16_t old, uint16_t new)
156 {
157 bool r;
158
159 SEXP_atomic_once();
160 SEXP_atomic_lock((uintptr_t)ptr);
161 if (*ptr == old) {
162 *ptr = new;
163 r = true;
164 } else
165 r = false;
166 SEXP_atomic_unlock((uintptr_t)ptr);
167
168 return (r);
169 }
170
171 uint32_t SEXP_atomic_dec_u32 (volatile uint32_t *ptr)
172 {
173 uint32_t r;
174
175 SEXP_atomic_once();
176 SEXP_atomic_lock((uintptr_t)ptr);
177 r = *ptr - 1;
178 *ptr = r;
179 SEXP_atomic_unlock((uintptr_t)ptr);
180
181 return (r);
182 }
183
184 uint32_t SEXP_atomic_inc_u32 (volatile uint32_t *ptr)
185 {
186 uint32_t r;
187
188 SEXP_atomic_once();
189 SEXP_atomic_lock((uintptr_t)ptr);
190 r = *ptr;
191 *ptr = r + 1;
192 SEXP_atomic_unlock((uintptr_t)ptr);
193
194 return (r);
195 }
196
197 bool SEXP_atomic_cas_u32 (volatile uint32_t *ptr, uint32_t old, uint32_t new)
198 {
199 bool r;
200
201 SEXP_atomic_once();
202 SEXP_atomic_lock((uintptr_t)ptr);
203 if (*ptr == old) {
204 *ptr = new;
205 r = true;
206 } else
207 r = false;
208 SEXP_atomic_unlock((uintptr_t)ptr);
209
210 return (r);
211 }
212
213 #ifdef SEXP_ATOMIC_64BITS
214 uint64_t SEXP_atomic_dec_u64 (volatile uint64_t *ptr)
215 {
216 uint64_t r;
217
218 SEXP_atomic_once();
219 SEXP_atomic_lock((uintptr_t)ptr);
220 r = *ptr - 1;
221 *ptr = r;
222 SEXP_atomic_unlock((uintptr_t)ptr);
223
224 return (r);
225 }
226
227 uint64_t SEXP_atomic_inc_u64 (volatile uint64_t *ptr)
228 {
229 uint64_t r;
230
231 SEXP_atomic_once();
232 SEXP_atomic_lock((uintptr_t)ptr);
233 r = *ptr;
234 *ptr = r + 1;
235 SEXP_atomic_unlock((uintptr_t)ptr);
236
237 return (r);
238 }
239
240 bool SEXP_atomic_cas_u64 (volatile uint64_t *ptr, uint64_t old, uint64_t new)
241 {
242 bool r;
243
244 SEXP_atomic_once();
245 SEXP_atomic_lock((uintptr_t)ptr);
246 if (*ptr == old) {
247 *ptr = new;
248 r = true;
249 } else
250 r = false;
251 SEXP_atomic_unlock((uintptr_t)ptr);
252
253 return (r);
254 }
255 #endif /* SEXP_ATOMIC_64BITS */
256
257 #endif /* !HAVE_ATOMIC_BUILTINS */