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