|
11 | 11 | #include <rthw.h>
|
12 | 12 | #include <stdint.h>
|
13 | 13 | #include <stdbool.h>
|
| 14 | +#include <rtthread.h> |
14 | 15 |
|
15 | 16 | /*
|
16 | 17 | * override gcc builtin atomic function for std::atomic<int64_t>, std::atomic<uint64_t>
|
@@ -69,6 +70,84 @@ bool __atomic_compare_exchange_8(volatile void *ptr, volatile void *expected, ui
|
69 | 70 | return exchanged;
|
70 | 71 | }
|
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 | + */ |
| 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 | + */ |
| 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 | + */ |
| 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 | +*/ |
| 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 | + |
72 | 151 | #define __atomic_fetch_op_8(OPNAME, OP) \
|
73 | 152 | uint64_t __atomic_fetch_##OPNAME##_8(volatile void *ptr, uint64_t val, int memorder) {\
|
74 | 153 | volatile uint64_t* val_ptr = (volatile uint64_t*)ptr;\
|
|
0 commit comments