1 /*
2 * Copyright (c) 2008-2014 Travis Geiselbrecht
3 *
4 * Use of this source code is governed by a MIT-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/MIT
7 */
8 #pragma once
9
10 #include <lk/compiler.h>
11 #include <stdbool.h>
12
13 __BEGIN_CDECLS
14
15 /* use built in atomic intrinsics if the architecture doesn't otherwise
16 * override it. */
17 #if !defined(USE_BUILTIN_ATOMICS) || USE_BUILTIN_ATOMICS
atomic_add(volatile int * ptr,int val)18 static inline int atomic_add(volatile int *ptr, int val) {
19 return __atomic_fetch_add(ptr, val, __ATOMIC_RELAXED);
20 }
21
atomic_or(volatile int * ptr,int val)22 static inline int atomic_or(volatile int *ptr, int val) {
23 return __atomic_fetch_or(ptr, val, __ATOMIC_RELAXED);
24 }
25
atomic_and(volatile int * ptr,int val)26 static inline int atomic_and(volatile int *ptr, int val) {
27 return __atomic_fetch_and(ptr, val, __ATOMIC_RELAXED);
28 }
29
atomic_swap(volatile int * ptr,int val)30 static inline int atomic_swap(volatile int *ptr, int val) {
31 return __atomic_exchange_n(ptr, val, __ATOMIC_RELAXED);
32 }
atomic_cmpxchg(volatile int * ptr,int oldval,int newval)33 static inline int atomic_cmpxchg(volatile int *ptr, int oldval, int newval) {
34 // TODO: implement
35 return 0;
36 }
37
38 #else
39 static int atomic_swap(volatile int *ptr, int val);
40 static int atomic_add(volatile int *ptr, int val);
41 static int atomic_and(volatile int *ptr, int val);
42 static int atomic_or(volatile int *ptr, int val);
43
44 /* if an implementation wants to implement it themselves */
45 #include <arch/arch_atomic.h>
46
47 #endif
48
49 __END_CDECLS
50
51