1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
|
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "apr.h"
#include "apr_atomic.h"
#include "apr_thread_mutex.h"
#include "apr_private.h"
#include <stdlib.h>
#if (defined(SOLARIS2) && SOLARIS2 >= 10)
#include <atomic.h>
#endif
#if defined(__GNUC__) && defined(__STRICT_ANSI__) && !defined(USE_GENERIC_ATOMICS)
/* force use of generic atomics if building e.g. with -std=c89, which
* doesn't allow inline asm */
#define USE_GENERIC_ATOMICS
#endif
#if (defined(__i386__) || defined(__x86_64__)) \
&& defined(__GNUC__) && !defined(USE_GENERIC_ATOMICS)
APR_DECLARE(apr_uint32_t) apr_atomic_cas32(volatile apr_uint32_t *mem,
apr_uint32_t with,
apr_uint32_t cmp)
{
apr_uint32_t prev;
asm volatile ("lock; cmpxchgl %1, %2"
: "=a" (prev)
: "r" (with), "m" (*(mem)), "0"(cmp)
: "memory", "cc");
return prev;
}
#define APR_OVERRIDE_ATOMIC_CAS32
static apr_uint32_t inline intel_atomic_add32(volatile apr_uint32_t *mem,
apr_uint32_t val)
{
asm volatile ("lock; xaddl %0,%1"
: "=r"(val), "=m"(*mem) /* outputs */
: "0"(val), "m"(*mem) /* inputs */
: "memory", "cc");
return val;
}
APR_DECLARE(apr_uint32_t) apr_atomic_add32(volatile apr_uint32_t *mem,
apr_uint32_t val)
{
return intel_atomic_add32(mem, val);
}
#define APR_OVERRIDE_ATOMIC_ADD32
APR_DECLARE(void) apr_atomic_sub32(volatile apr_uint32_t *mem, apr_uint32_t val)
{
asm volatile ("lock; subl %1, %0"
:
: "m" (*(mem)), "r" (val)
: "memory", "cc");
}
#define APR_OVERRIDE_ATOMIC_SUB32
APR_DECLARE(int) apr_atomic_dec32(volatile apr_uint32_t *mem)
{
unsigned char prev;
asm volatile ("lock; decl %1;\n\t"
"setnz %%al"
: "=a" (prev)
: "m" (*(mem))
: "memory", "cc");
return prev;
}
#define APR_OVERRIDE_ATOMIC_DEC32
APR_DECLARE(apr_uint32_t) apr_atomic_inc32(volatile apr_uint32_t *mem)
{
return intel_atomic_add32(mem, 1);
}
#define APR_OVERRIDE_ATOMIC_INC32
APR_DECLARE(void) apr_atomic_set32(volatile apr_uint32_t *mem, apr_uint32_t val)
{
*mem = val;
}
#define APR_OVERRIDE_ATOMIC_SET32
APR_DECLARE(apr_uint32_t) apr_atomic_xchg32(volatile apr_uint32_t *mem, apr_uint32_t val)
{
apr_uint32_t prev = val;
asm volatile ("lock; xchgl %0, %1"
: "=r" (prev)
: "m" (*(mem)), "0"(prev)
: "memory");
return prev;
}
#define APR_OVERRIDE_ATOMIC_XCHG32
/*#define apr_atomic_init(pool) APR_SUCCESS*/
#endif /* (__linux__ || __EMX__ || __FreeBSD__) && __i386__ */
#if (defined(__PPC__) || defined(__ppc__)) && defined(__GNUC__) \
&& !defined(USE_GENERIC_ATOMICS)
APR_DECLARE(apr_uint32_t) apr_atomic_cas32(volatile apr_uint32_t *mem,
apr_uint32_t swap,
apr_uint32_t cmp)
{
apr_uint32_t prev;
asm volatile ("0:\n\t" /* retry local label */
"lwarx %0,0,%1\n\t" /* load prev and reserve */
"cmpw %0,%3\n\t" /* does it match cmp? */
"bne- 1f\n\t" /* ...no, bail out */
"stwcx. %2,0,%1\n\t" /* ...yes, conditionally
store swap */
"bne- 0b\n\t" /* start over if we lost
the reservation */
"1:" /* exit local label */
: "=&r"(prev) /* output */
: "b" (mem), "r" (swap), "r"(cmp) /* inputs */
: "memory", "cc"); /* clobbered */
return prev;
}
#define APR_OVERRIDE_ATOMIC_CAS32
APR_DECLARE(apr_uint32_t) apr_atomic_add32(volatile apr_uint32_t *mem,
apr_uint32_t delta)
{
apr_uint32_t prev, temp;
asm volatile ("0:\n\t" /* retry local label */
"lwarx %0,0,%2\n\t" /* load prev and reserve */
"add %1,%0,%3\n\t" /* temp = prev + delta */
"stwcx. %1,0,%2\n\t" /* conditionally store */
"bne- 0b" /* start over if we lost
the reservation */
/*XXX find a cleaner way to define the temp
* it's not an output
*/
: "=&r" (prev), "=&r" (temp) /* output, temp */
: "b" (mem), "r" (delta) /* inputs */
: "memory", "cc"); /* clobbered */
return prev;
}
#define APR_OVERRIDE_ATOMIC_ADD32
#endif /* __PPC__ && __GNUC__ */
#if (defined(SOLARIS2) && SOLARIS2 >= 10) \
&& !defined(USE_GENERIC_ATOMICS)
#if !defined(APR_OVERRIDE_ATOMIC_CAS32)
APR_DECLARE(apr_uint32_t) apr_atomic_cas32(volatile apr_uint32_t *mem,
apr_uint32_t with,
apr_uint32_t cmp)
{
return atomic_cas_32(mem, cmp, with);
}
#define APR_OVERRIDE_ATOMIC_CAS32
#endif /* APR_OVERRIDE_ATOMIC_CAS32 */
#if !defined(APR_OVERRIDE_ATOMIC_DEC32)
APR_DECLARE(apr_uint32_t) apr_atomic_dec32(volatile apr_uint32_t *mem)
{
apr_uint32_t prev = *mem;
atomic_dec_32(mem);
return prev != 1;
}
#define APR_OVERRIDE_ATOMIC_DEC32
#endif /* APR_OVERRIDE_ATOMIC_DEC32 */
#if !defined(APR_OVERRIDE_ATOMIC_INC32)
APR_DECLARE(apr_uint32_t) apr_atomic_inc32(volatile apr_uint32_t *mem)
{
apr_uint32_t prev = *mem;
atomic_inc_32(mem);
return prev;
}
#define APR_OVERRIDE_ATOMIC_INC32
#endif /* APR_OVERRIDE_ATOMIC_INC32 */
#if !defined(APR_OVERRIDE_ATOMIC_SET32)
APR_DECLARE(void) apr_atomic_set32(volatile apr_uint32_t *mem, apr_uint32_t val)
{
*mem = val;
}
#define APR_OVERRIDE_ATOMIC_SET32
#endif /* APR_OVERRIDE_ATOMIC_SET32 */
#if !defined(APR_OVERRIDE_ATOMIC_XCHG32)
APR_DECLARE(apr_uint32_t) apr_atomic_xchg32(volatile apr_uint32_t *mem,
apr_uint32_t val)
{
return atomic_swap_32(mem, val);
}
#define APR_OVERRIDE_ATOMIC_XCHG32
#endif /* APR_OVERRIDE_ATOMIC_XCHG32 */
#endif /* SOLARIS2 && SOLARIS2 >= 10 */
#if !defined(APR_OVERRIDE_ATOMIC_INIT)
#if APR_HAS_THREADS
#define NUM_ATOMIC_HASH 7
/* shift by 2 to get rid of alignment issues */
#define ATOMIC_HASH(x) (unsigned int)(((unsigned long)(x)>>2)%(unsigned int)NUM_ATOMIC_HASH)
static apr_thread_mutex_t **hash_mutex;
#endif /* APR_HAS_THREADS */
apr_status_t apr_atomic_init(apr_pool_t *p)
{
#if APR_HAS_THREADS
int i;
apr_status_t rv;
hash_mutex = apr_palloc(p, sizeof(apr_thread_mutex_t*) * NUM_ATOMIC_HASH);
for (i = 0; i < NUM_ATOMIC_HASH; i++) {
rv = apr_thread_mutex_create(&(hash_mutex[i]),
APR_THREAD_MUTEX_DEFAULT, p);
if (rv != APR_SUCCESS) {
return rv;
}
}
#endif /* APR_HAS_THREADS */
return APR_SUCCESS;
}
#endif /* !defined(APR_OVERRIDE_ATOMIC_INIT) */
/* abort() if 'x' does not evaluate to APR_SUCCESS. */
#define CHECK(x) do { if ((x) != APR_SUCCESS) abort(); } while (0)
#if !defined(APR_OVERRIDE_ATOMIC_ADD32)
#if defined(APR_OVERRIDE_ATOMIC_CAS32)
apr_uint32_t apr_atomic_add32(volatile apr_uint32_t *mem, apr_uint32_t val)
{
apr_uint32_t old_value, new_value;
do {
old_value = *mem;
new_value = old_value + val;
} while (apr_atomic_cas32(mem, new_value, old_value) != old_value);
return old_value;
}
#else
apr_uint32_t apr_atomic_add32(volatile apr_uint32_t *mem, apr_uint32_t val)
{
apr_uint32_t old_value;
#if APR_HAS_THREADS
apr_thread_mutex_t *lock = hash_mutex[ATOMIC_HASH(mem)];
CHECK(apr_thread_mutex_lock(lock));
old_value = *mem;
*mem += val;
CHECK(apr_thread_mutex_unlock(lock));
#else
old_value = *mem;
*mem += val;
#endif /* APR_HAS_THREADS */
return old_value;
}
#endif /* defined(APR_OVERRIDE_ATOMIC_CAS32) */
#endif /* !defined(APR_OVERRIDE_ATOMIC_ADD32) */
#if !defined(APR_OVERRIDE_ATOMIC_SUB32)
#if defined(APR_OVERRIDE_ATOMIC_CAS32)
void apr_atomic_sub32(volatile apr_uint32_t *mem, apr_uint32_t val)
{
apr_uint32_t old_value, new_value;
do {
old_value = *mem;
new_value = old_value - val;
} while (apr_atomic_cas32(mem, new_value, old_value) != old_value);
}
#else
void apr_atomic_sub32(volatile apr_uint32_t *mem, apr_uint32_t val)
{
#if APR_HAS_THREADS
apr_thread_mutex_t *lock = hash_mutex[ATOMIC_HASH(mem)];
CHECK(apr_thread_mutex_lock(lock));
*mem -= val;
CHECK(apr_thread_mutex_unlock(lock));
#else
*mem -= val;
#endif /* APR_HAS_THREADS */
}
#endif /* defined(APR_OVERRIDE_ATOMIC_CAS32) */
#endif /* !defined(APR_OVERRIDE_ATOMIC_SUB32) */
#if !defined(APR_OVERRIDE_ATOMIC_SET32)
void apr_atomic_set32(volatile apr_uint32_t *mem, apr_uint32_t val)
{
#if APR_HAS_THREADS
apr_thread_mutex_t *lock = hash_mutex[ATOMIC_HASH(mem)];
CHECK(apr_thread_mutex_lock(lock));
*mem = val;
CHECK(apr_thread_mutex_unlock(lock));
#else
*mem = val;
#endif /* APR_HAS_THREADS */
}
#endif /* !defined(APR_OVERRIDE_ATOMIC_SET32) */
#if !defined(APR_OVERRIDE_ATOMIC_INC32)
apr_uint32_t apr_atomic_inc32(volatile apr_uint32_t *mem)
{
return apr_atomic_add32(mem, 1);
}
#endif /* !defined(APR_OVERRIDE_ATOMIC_INC32) */
#if !defined(APR_OVERRIDE_ATOMIC_DEC32)
#if defined(APR_OVERRIDE_ATOMIC_CAS32)
int apr_atomic_dec32(volatile apr_uint32_t *mem)
{
apr_uint32_t old_value, new_value;
do {
old_value = *mem;
new_value = old_value - 1;
} while (apr_atomic_cas32(mem, new_value, old_value) != old_value);
return old_value != 1;
}
#else
int apr_atomic_dec32(volatile apr_uint32_t *mem)
{
#if APR_HAS_THREADS
apr_thread_mutex_t *lock = hash_mutex[ATOMIC_HASH(mem)];
apr_uint32_t new;
CHECK(apr_thread_mutex_lock(lock));
(*mem)--;
new = *mem;
CHECK(apr_thread_mutex_unlock(lock));
return new;
#else
(*mem)--;
return *mem;
#endif /* APR_HAS_THREADS */
}
#endif /* defined(APR_OVERRIDE_ATOMIC_CAS32) */
#endif /* !defined(APR_OVERRIDE_ATOMIC_DEC32) */
#if !defined(APR_OVERRIDE_ATOMIC_CAS32)
apr_uint32_t apr_atomic_cas32(volatile apr_uint32_t *mem, apr_uint32_t with,
apr_uint32_t cmp)
{
apr_uint32_t prev;
#if APR_HAS_THREADS
apr_thread_mutex_t *lock = hash_mutex[ATOMIC_HASH(mem)];
CHECK(apr_thread_mutex_lock(lock));
prev = *mem;
if (prev == cmp) {
*mem = with;
}
CHECK(apr_thread_mutex_unlock(lock));
#else
prev = *mem;
if (prev == cmp) {
*mem = with;
}
#endif /* APR_HAS_THREADS */
return prev;
}
#endif /* !defined(APR_OVERRIDE_ATOMIC_CAS32) */
#if !defined(APR_OVERRIDE_ATOMIC_XCHG32)
#if defined(APR_OVERRIDE_ATOMIC_CAS32)
apr_uint32_t apr_atomic_xchg32(volatile apr_uint32_t *mem, apr_uint32_t val)
{
apr_uint32_t prev;
do {
prev = *mem;
} while (apr_atomic_cas32(mem, val, prev) != prev);
return prev;
}
#else
apr_uint32_t apr_atomic_xchg32(volatile apr_uint32_t *mem, apr_uint32_t val)
{
apr_uint32_t prev;
#if APR_HAS_THREADS
apr_thread_mutex_t *lock = hash_mutex[ATOMIC_HASH(mem)];
CHECK(apr_thread_mutex_lock(lock));
prev = *mem;
*mem = val;
CHECK(apr_thread_mutex_unlock(lock));
#else
prev = *mem;
*mem = val;
#endif /* APR_HAS_THREADS */
return prev;
}
#endif /* defined(APR_OVERRIDE_ATOMIC_CAS32) */
#endif /* !defined(APR_OVERRIDE_ATOMIC_XCHG32) */
#if !defined(APR_OVERRIDE_ATOMIC_CASPTR)
void *apr_atomic_casptr(volatile void **mem, void *with, const void *cmp)
{
void *prev;
#if APR_HAS_THREADS
apr_thread_mutex_t *lock = hash_mutex[ATOMIC_HASH(mem)];
CHECK(apr_thread_mutex_lock(lock));
prev = *(void **)mem;
if (prev == cmp) {
*mem = with;
}
CHECK(apr_thread_mutex_unlock(lock));
#else
prev = *(void **)mem;
if (prev == cmp) {
*mem = with;
}
#endif /* APR_HAS_THREADS */
return prev;
}
#endif /* !defined(APR_OVERRIDE_ATOMIC_CASPTR) */
#if !defined(APR_OVERRIDE_ATOMIC_READ32)
APR_DECLARE(apr_uint32_t) apr_atomic_read32(volatile apr_uint32_t *mem)
{
return *mem;
}
#endif
|