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