blob: e7c57617d32a4e3a52c9a5b5ab0c6749cdcac430 [file] [log] [blame]
David 'Digit' Turnere31bfae2011-11-15 15:47:02 +01001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#ifndef BIONIC_ATOMIC_GCC_BUILTIN_H
17#define BIONIC_ATOMIC_GCC_BUILTIN_H
18
19/* This header file is used by default if we don't have optimized atomic
20 * routines for a given platform. See bionic_atomic_arm.h and
21 * bionic_atomic_x86.h for examples.
22 */
23
24__ATOMIC_INLINE__ void
25__bionic_memory_barrier(void)
26{
27 __sync_synchronize();
28}
29
30__ATOMIC_INLINE__ int
31__bionic_cmpxchg(int32_t old_value, int32_t new_value, volatile int32_t* ptr)
32{
33 /* We must return 0 on success */
34 return __sync_bool_compare_and_swap(ptr, old_value, new_value) == 0;
35}
36
37__ATOMIC_INLINE__ int32_t
38__bionic_swap(int32_t new_value, volatile int32_t* ptr)
39{
40 int32_t prev;
41 do {
42 prev = *ptr;
43 status = __sync_val_compare_and_swap(ptr, prev, new_value);
44 } while (status == 0);
45 return prev;
46}
47
48__ATOMIC_INLINE__ int32_t
49__bionic_atomic_inc(volatile int32_t* ptr)
50{
51 /* We must return the old value */
52 return __sync_fetch_and_add(ptr, 1);
53}
54
55__ATOMIC_INLINE__ int32_t
56__bionic_atomic_dec(volatile int32_t* ptr)
57{
58 /* We must return the old value */
59 return __sync_fetch_and_add(ptr, -1);
60}
61
62#endif /* BIONIC_ATOMIC_GCC_BUILTIN_H */