]> git.saurik.com Git - apple/libc.git/blob - arm/sys/gcc_atomic.c
Libc-825.24.tar.gz
[apple/libc.git] / arm / sys / gcc_atomic.c
1 /*
2 * Copyright (c) 2008 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #ifndef __clang__
25
26 #include <stdint.h>
27 #include <libkern/OSAtomic.h>
28 #include <arm/arch.h>
29 #include <stdbool.h>
30
31 /*
32 * Implement gcc atomic "builtins" in terms of OSAtomic routines,
33 * since the ARM GCC target does not currently support them
34 */
35
36 int32_t __sync_fetch_and_add_4 (int32_t *ptr, int32_t value, ...)
37 {
38 return OSAtomicAdd32Barrier(value, ptr) - value;
39 }
40
41 int32_t __sync_fetch_and_sub_4 (int32_t *ptr, int32_t value, ...)
42 {
43 return OSAtomicAdd32Barrier(-value, ptr) + value;
44 }
45
46 uint32_t __sync_fetch_and_or_4(uint32_t *ptr, uint32_t value, ...)
47 {
48 return OSAtomicOr32OrigBarrier(value, ptr);
49 }
50
51 uint32_t __sync_fetch_and_and_4(uint32_t *ptr, uint32_t value, ...)
52 {
53 return OSAtomicAnd32OrigBarrier(value, ptr);
54 }
55
56 uint32_t __sync_fetch_and_xor_4(uint32_t *ptr, uint32_t value, ...)
57 {
58 return OSAtomicXor32OrigBarrier(value, ptr);
59 }
60
61 int32_t __sync_add_and_fetch_4 (int32_t *ptr, int32_t value, ...)
62 {
63 return OSAtomicAdd32Barrier(value, ptr);
64 }
65
66 int32_t __sync_sub_and_fetch_4 (int32_t *ptr, int32_t value, ...)
67 {
68 return OSAtomicAdd32Barrier(-value, ptr);
69 }
70
71 uint32_t __sync_or_and_fetch_4 (uint32_t *ptr, int32_t value, ...)
72 {
73 return OSAtomicOr32Barrier(value, ptr);
74 }
75
76 uint32_t __sync_and_and_fetch_4 (uint32_t *ptr, int32_t value, ...)
77 {
78 return OSAtomicAnd32Barrier(value, ptr);
79 }
80
81 uint32_t __sync_xor_and_fetch_4 (uint32_t *ptr, int32_t value, ...)
82 {
83 return OSAtomicXor32Barrier(value, ptr);
84 }
85
86 bool __sync_bool_compare_and_swap_4(int32_t *ptr, int32_t oldval, int32_t newval, ...)
87 {
88 return OSAtomicCompareAndSwap32Barrier(oldval, newval, ptr);
89 }
90
91 int32_t __sync_val_compare_and_swap_4(int32_t *ptr, int32_t oldval, int32_t newval, ...)
92 {
93 int32_t old = *ptr;
94 OSAtomicCompareAndSwap32Barrier(oldval, newval, ptr);
95 return old;
96 }
97
98 int32_t __sync_lock_test_and_set_4(int32_t *ptr, int32_t value, ...)
99 {
100 int32_t old;
101
102 do {
103 old = *ptr;
104 } while (!OSAtomicCompareAndSwap32Barrier(old, value, ptr));
105
106 return old;
107 }
108
109 void __sync_lock_release_4(int32_t *ptr, ...)
110 {
111 *ptr = 0;
112 }
113
114 __private_extern__
115 void __sync_synchronize(void)
116 {
117 OSMemoryBarrier();
118 }
119
120 #endif