1 /*
2 * Copyright (C) 2016 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
17 #ifndef _ATOMIC_H_
18 #define _ATOMIC_H_
19
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23
24 #include <stdint.h>
25 #include <stdbool.h>
26 #include <cpu/inc/atomic.h>
27 #include <cpu/inc/barrier.h>
28
29 /* almost all platforms support byte and 32-bit operations of this sort. please do not add other sizes here */
30 uint32_t atomicXchgByte(volatile uint8_t *byte, uint32_t newVal);
31 uint32_t atomicXchg32bits(volatile uint32_t *word, uint32_t newVal);
32 bool atomicCmpXchgByte(volatile uint8_t *byte, uint32_t prevVal, uint32_t newVal);
33 bool atomicCmpXchg32bits(volatile uint32_t *word, uint32_t prevVal, uint32_t newVal);
34
35 //returns old value
36 uint32_t atomicAddByte(volatile uint8_t *byte, uint32_t addend);
37 uint32_t atomicAdd32bits(volatile uint32_t *word, uint32_t addend);
38
39 //writes with barriers
atomicReadByte(volatile uint8_t * byte)40 static inline uint32_t atomicReadByte(volatile uint8_t *byte)
41 {
42 mem_reorder_barrier();
43 return *byte;
44 }
45
atomicRead32bits(volatile uint32_t * word)46 static inline uint32_t atomicRead32bits(volatile uint32_t *word)
47 {
48 mem_reorder_barrier();
49 return *word;
50 }
51
atomicWriteByte(volatile uint8_t * byte,uint32_t val)52 static inline void atomicWriteByte(volatile uint8_t *byte, uint32_t val)
53 {
54 *byte = val;
55 mem_reorder_barrier();
56 }
57
atomicWrite32bits(volatile uint32_t * word,uint32_t val)58 static inline void atomicWrite32bits(volatile uint32_t *word, uint32_t val)
59 {
60 *word = val;
61 mem_reorder_barrier();
62 }
63
64 #ifdef __cplusplus
65 }
66 #endif
67
68 #endif
69