1 /*
2 * Copyright (c) 2006-2021, RT-Thread Development Team
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Change Logs:
7 * Date Author Notes
8 * 2021-04-27 peterfan Add copyright header.
9 */
10
11 #include <rthw.h>
12 #include <stdint.h>
13 #include <stdbool.h>
14 #include <rtthread.h>
15
16 /*
17 * override gcc builtin atomic function for std::atomic<int64_t>, std::atomic<uint64_t>
18 * @see https://gcc.gnu.org/onlinedocs/gcc/_005f_005fatomic-Builtins.html
19 */
__atomic_load_8(volatile void * ptr,int memorder)20 uint64_t __atomic_load_8(volatile void *ptr, int memorder)
21 {
22 volatile uint64_t *val_ptr = (volatile uint64_t *)ptr;
23 rt_base_t level;
24 uint64_t tmp;
25 level = rt_hw_interrupt_disable();
26 tmp = *val_ptr;
27 rt_hw_interrupt_enable(level);
28 return tmp;
29 }
30
__atomic_store_8(volatile void * ptr,uint64_t val,int memorder)31 void __atomic_store_8(volatile void *ptr, uint64_t val, int memorder)
32 {
33 volatile uint64_t *val_ptr = (volatile uint64_t *)ptr;
34 rt_base_t level;
35 level = rt_hw_interrupt_disable();
36 *val_ptr = val;
37 rt_hw_interrupt_enable(level);
38 }
39
__atomic_exchange_8(volatile void * ptr,uint64_t val,int memorder)40 uint64_t __atomic_exchange_8(volatile void *ptr, uint64_t val, int memorder)
41 {
42 volatile uint64_t *val_ptr = (volatile uint64_t *)ptr;
43 rt_base_t level;
44 uint64_t tmp;
45 level = rt_hw_interrupt_disable();
46 tmp = *val_ptr;
47 *val_ptr = val;
48 rt_hw_interrupt_enable(level);
49 return tmp;
50 }
51
__atomic_compare_exchange_8(volatile void * ptr,volatile void * expected,uint64_t desired,bool weak,int success_memorder,int failure_memorder)52 bool __atomic_compare_exchange_8(volatile void *ptr, volatile void *expected, uint64_t desired, bool weak, int success_memorder, int failure_memorder)
53 {
54 volatile uint64_t *val_ptr = (volatile uint64_t *)ptr;
55 volatile uint64_t *expected_ptr = (volatile uint64_t *)expected;
56 rt_base_t level;
57 bool exchanged;
58 level = rt_hw_interrupt_disable();
59 if (*val_ptr == *expected_ptr)
60 {
61 *val_ptr = desired;
62 exchanged = true;
63 }
64 else
65 {
66 *expected_ptr = *val_ptr;
67 exchanged = false;
68 }
69 rt_hw_interrupt_enable(level);
70 return exchanged;
71 }
72
73 /**
74 * @param size is the length of the value to load.
75 *
76 * @param mem is the source memory to load the value from.
77 *
78 * @param _return is the pointer to the space where the loaded value will be stored.
79 */
__atomic_load(size_t size,void * mem,void * _return,int model)80 void __atomic_load(size_t size, void *mem, void *_return, int model)
81 {
82 rt_base_t level;
83 level = rt_hw_interrupt_disable();
84 rt_memcpy(_return, mem, size);
85 rt_hw_interrupt_enable(level);
86 }
87
88 /**
89 * @param size is the length of the value to store.
90 *
91 * @param mem is the destination memory space to store the value.
92 *
93 * @param val is the pointer to the value to store.
94 */
__atomic_store(size_t size,void * mem,void * val,int model)95 void __atomic_store(size_t size, void *mem, void *val, int model)
96 {
97 rt_base_t level;
98 level = rt_hw_interrupt_disable();
99 rt_memcpy(mem, val, size);
100 rt_hw_interrupt_enable(level);
101 }
102
103 /**
104 * @param size is the length of value to exchange.
105 *
106 * @param mem is the destination space to exchange.
107 *
108 * @param val is the pointer of value to exchange.
109 *
110 * @param _return gives back the the value before exchanging.
111 */
__atomic_exchange(size_t size,void * mem,void * val,void * _return,int model)112 void __atomic_exchange(size_t size, void *mem, void *val, void *_return, int model)
113 {
114 rt_base_t level;
115 level = rt_hw_interrupt_disable();
116 rt_memcpy(_return, mem, size);
117 rt_memcpy(mem, val, size);
118 rt_hw_interrupt_enable(level);
119 }
120
121 /**
122 * @param size is the length of value to operate.
123 *
124 * @param obj is the destination value space to operate.
125 *
126 * @param expected is the value to be compared with obj.
127 *
128 * @param desired is the value pointer to be written into obj, under the condition that *expected equals *obj.
129 *
130 * @return true if succeed in writing *desired into *obj; false if not.
131 */
__atomic_compare_exchange(size_t size,void * obj,void * expected,void * desired,int success,int failure)132 bool __atomic_compare_exchange(size_t size, void *obj, void *expected, void *desired, int success, int failure)
133 {
134 rt_base_t level;
135 volatile bool exchanged = false;
136 level = rt_hw_interrupt_disable();
137 if (rt_memcmp(obj, expected, size) == 0)
138 {
139 rt_memcpy(obj, desired, size);
140 exchanged = true;
141 }
142 else
143 {
144 rt_memcpy(expected, obj, size);
145 exchanged = false;
146 }
147 rt_hw_interrupt_enable(level);
148 return exchanged;
149 }
150
151 #define __atomic_fetch_op_8(OPNAME, OP) \
152 uint64_t __atomic_fetch_##OPNAME##_8(volatile void *ptr, uint64_t val, int memorder) {\
153 volatile uint64_t* val_ptr = (volatile uint64_t*)ptr;\
154 rt_base_t level;\
155 uint64_t tmp;\
156 level = rt_hw_interrupt_disable();\
157 tmp = *val_ptr;\
158 *val_ptr OP##= val;\
159 rt_hw_interrupt_enable(level);\
160 return tmp;\
161 }
162
163 __atomic_fetch_op_8(add, +)
164 __atomic_fetch_op_8(sub, -)
165 __atomic_fetch_op_8( and, &)
166 __atomic_fetch_op_8( or, |)
167 __atomic_fetch_op_8(xor, ^)
168