Skip to content

Commit 5f863b7

Browse files
committed
Add GCC generalized atomic operation function
1 parent 73850e2 commit 5f863b7

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

components/libc/cplusplus/cpp11/atomic_8.c

+81
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <rthw.h>
1212
#include <stdint.h>
1313
#include <stdbool.h>
14+
#include <rtthread.h>
1415

1516
/*
1617
* override gcc builtin atomic function for std::atomic<int64_t>, std::atomic<uint64_t>
@@ -69,6 +70,86 @@ bool __atomic_compare_exchange_8(volatile void *ptr, volatile void *expected, ui
6970
return exchanged;
7071
}
7172

73+
/* decleare some */
74+
75+
/**
76+
* @param size is the length of the value to load.
77+
*
78+
* @param mem is the source memory to load the value from.
79+
*
80+
* @param _return is the pointer to the space where the loaded value will be stored.
81+
*/
82+
void __atomic_load(size_t size, void *mem, void *_return, int model)
83+
{
84+
rt_base_t level;
85+
level = rt_hw_interrupt_disable();
86+
rt_memcpy(_return, mem, size);
87+
rt_hw_interrupt_enable(level);
88+
}
89+
90+
/**
91+
* @param size is the length of the value to store.
92+
*
93+
* @param mem is the destination memory space to store the value.
94+
*
95+
* @param val is the pointer to the value to store.
96+
*/
97+
void __atomic_store(size_t size, void *mem, void *val, int model)
98+
{
99+
rt_base_t level;
100+
level = rt_hw_interrupt_disable();
101+
rt_memcpy(mem, val, size);
102+
rt_hw_interrupt_enable(level);
103+
}
104+
105+
/**
106+
* @param size is the length of value to exchange.
107+
*
108+
* @param mem is the destination space to exchange.
109+
*
110+
* @param val is the pointer of value to exchange.
111+
*
112+
* @param _return gives back the the value before exchanging.
113+
*/
114+
void __atomic_exchange(size_t size, void *mem, void *val, void *_return, int model)
115+
{
116+
rt_base_t level;
117+
level = rt_hw_interrupt_disable();
118+
rt_memcpy(_return, mem, size);
119+
rt_memcpy(mem, val, size);
120+
rt_hw_interrupt_enable(level);
121+
}
122+
123+
/**
124+
* @param size is the length of value to operate.
125+
*
126+
* @param obj is the destination value space to operate.
127+
*
128+
* @param expected is the value to be compared with obj.
129+
*
130+
* @param desired is the value pointer to be written into obj, under the condition that *expected equals *obj.
131+
*
132+
* @return true if succeed in writing *desired into *obj; false if not.
133+
*/
134+
bool __atomic_compare_exchange(size_t size, void *obj, void *expected, void *desired, int success, int failure)
135+
{
136+
rt_base_t level;
137+
volatile bool exchanged = false;
138+
level = rt_hw_interrupt_disable();
139+
if (rt_memcmp(obj, expected, size) == 0)
140+
{
141+
rt_memcpy(obj, desired, size);
142+
exchanged = true;
143+
}
144+
else
145+
{
146+
rt_memcpy(expected, obj, size);
147+
exchanged = false;
148+
}
149+
rt_hw_interrupt_enable(level);
150+
return exchanged;
151+
}
152+
72153
#define __atomic_fetch_op_8(OPNAME, OP) \
73154
uint64_t __atomic_fetch_##OPNAME##_8(volatile void *ptr, uint64_t val, int memorder) {\
74155
volatile uint64_t* val_ptr = (volatile uint64_t*)ptr;\

0 commit comments

Comments
 (0)