]> git.saurik.com Git - apple/libc.git/blob - arm/sys/gcc_atomic.c
Libc-594.9.4.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 #include <stdint.h>
24 #include <libkern/OSAtomic.h>
25 #include <arm/arch.h>
26 #include <stdbool.h>
27
28 /*
29 * Implement gcc atomic "builtins" in terms of OSAtomic routines,
30 * since the ARM GCC target does not currently support them
31 */
32
33 int32_t __sync_fetch_and_add_4 (int32_t *ptr, int32_t value)
34 {
35 return OSAtomicAdd32(value, ptr) - value;
36 }
37
38 int32_t __sync_fetch_and_sub_4 (int32_t *ptr, int32_t value)
39 {
40 return OSAtomicAdd32(-value, ptr) + value;
41 }
42
43 uint32_t __sync_fetch_and_or_4(uint32_t *ptr, uint32_t value)
44 {
45 return OSAtomicOr32Orig(value, ptr);
46 }
47
48 uint32_t __sync_fetch_and_and_4(uint32_t *ptr, uint32_t value)
49 {
50 return OSAtomicAnd32Orig(value, ptr);
51 }
52
53 uint32_t __sync_fetch_and_xor_4(uint32_t *ptr, uint32_t value)
54 {
55 OSAtomicXor32Orig(value, ptr);
56 }
57
58 int32_t __sync_add_and_fetch_4 (int32_t *ptr, int32_t value)
59 {
60 return OSAtomicAdd32(value, ptr);
61 }
62
63 int32_t __sync_sub_and_fetch_4 (int32_t *ptr, int32_t value)
64 {
65 return OSAtomicAdd32(-value, ptr);
66 }
67
68 uint32_t __sync_or_and_fetch_4 (uint32_t *ptr, int32_t value)
69 {
70 return OSAtomicOr32(value, ptr);
71 }
72
73 uint32_t __sync_and_and_fetch_4 (uint32_t *ptr, int32_t value)
74 {
75 return OSAtomicAnd32(value, ptr);
76 }
77
78 uint32_t __sync_xor_and_fetch_4 (uint32_t *ptr, int32_t value)
79 {
80 return OSAtomicXor32(value, ptr);
81 }
82
83 bool __sync_bool_compare_and_swap_4(int32_t *ptr, int32_t oldval, int32_t newval)
84 {
85 return OSAtomicCompareAndSwap32(oldval, newval, ptr);
86 }
87
88 int32_t __sync_val_compare_and_swap_4(int32_t *ptr, int32_t oldval, int32_t newval)
89 {
90 int32_t old = *ptr;
91 OSAtomicCompareAndSwap32(oldval, newval, ptr);
92 return old;
93 }
94
95 int32_t __sync_lock_test_and_set_4(int32_t *ptr, int32_t value)
96 {
97 int32_t old;
98
99 do {
100 old = *ptr;
101 } while (!OSAtomicCompareAndSwap32(old, value, ptr));
102
103 return old;
104 }
105
106 void __sync_lock_release_4(int32_t *ptr)
107 {
108 *ptr = 0;
109 }