]> git.saurik.com Git - apple/libc.git/blame - arm/sys/gcc_atomic.c
Libc-825.24.tar.gz
[apple/libc.git] / arm / sys / gcc_atomic.c
CommitLineData
34e8f829
A
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 */
1f2f436a
A
23
24#ifndef __clang__
25
34e8f829
A
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
ad3c9f2a 36int32_t __sync_fetch_and_add_4 (int32_t *ptr, int32_t value, ...)
34e8f829 37{
1f2f436a 38 return OSAtomicAdd32Barrier(value, ptr) - value;
34e8f829
A
39}
40
ad3c9f2a 41int32_t __sync_fetch_and_sub_4 (int32_t *ptr, int32_t value, ...)
34e8f829 42{
1f2f436a 43 return OSAtomicAdd32Barrier(-value, ptr) + value;
34e8f829
A
44}
45
ad3c9f2a 46uint32_t __sync_fetch_and_or_4(uint32_t *ptr, uint32_t value, ...)
34e8f829 47{
1f2f436a 48 return OSAtomicOr32OrigBarrier(value, ptr);
34e8f829
A
49}
50
ad3c9f2a 51uint32_t __sync_fetch_and_and_4(uint32_t *ptr, uint32_t value, ...)
34e8f829 52{
1f2f436a 53 return OSAtomicAnd32OrigBarrier(value, ptr);
34e8f829
A
54}
55
ad3c9f2a 56uint32_t __sync_fetch_and_xor_4(uint32_t *ptr, uint32_t value, ...)
34e8f829 57{
1f2f436a 58 return OSAtomicXor32OrigBarrier(value, ptr);
34e8f829
A
59}
60
ad3c9f2a 61int32_t __sync_add_and_fetch_4 (int32_t *ptr, int32_t value, ...)
34e8f829 62{
1f2f436a 63 return OSAtomicAdd32Barrier(value, ptr);
34e8f829
A
64}
65
ad3c9f2a 66int32_t __sync_sub_and_fetch_4 (int32_t *ptr, int32_t value, ...)
34e8f829 67{
1f2f436a 68 return OSAtomicAdd32Barrier(-value, ptr);
34e8f829
A
69}
70
ad3c9f2a 71uint32_t __sync_or_and_fetch_4 (uint32_t *ptr, int32_t value, ...)
34e8f829 72{
1f2f436a 73 return OSAtomicOr32Barrier(value, ptr);
34e8f829
A
74}
75
ad3c9f2a 76uint32_t __sync_and_and_fetch_4 (uint32_t *ptr, int32_t value, ...)
34e8f829 77{
1f2f436a 78 return OSAtomicAnd32Barrier(value, ptr);
34e8f829
A
79}
80
ad3c9f2a 81uint32_t __sync_xor_and_fetch_4 (uint32_t *ptr, int32_t value, ...)
34e8f829 82{
1f2f436a 83 return OSAtomicXor32Barrier(value, ptr);
34e8f829
A
84}
85
ad3c9f2a 86bool __sync_bool_compare_and_swap_4(int32_t *ptr, int32_t oldval, int32_t newval, ...)
34e8f829 87{
1f2f436a 88 return OSAtomicCompareAndSwap32Barrier(oldval, newval, ptr);
34e8f829
A
89}
90
ad3c9f2a 91int32_t __sync_val_compare_and_swap_4(int32_t *ptr, int32_t oldval, int32_t newval, ...)
34e8f829
A
92{
93 int32_t old = *ptr;
1f2f436a 94 OSAtomicCompareAndSwap32Barrier(oldval, newval, ptr);
34e8f829
A
95 return old;
96}
97
ad3c9f2a 98int32_t __sync_lock_test_and_set_4(int32_t *ptr, int32_t value, ...)
34e8f829
A
99{
100 int32_t old;
101
102 do {
103 old = *ptr;
1f2f436a 104 } while (!OSAtomicCompareAndSwap32Barrier(old, value, ptr));
34e8f829
A
105
106 return old;
107}
108
ad3c9f2a 109void __sync_lock_release_4(int32_t *ptr, ...)
34e8f829
A
110{
111 *ptr = 0;
112}
1f2f436a 113
ad3c9f2a
A
114__private_extern__
115void __sync_synchronize(void)
116{
117 OSMemoryBarrier();
118}
119
1f2f436a 120#endif