Skip to content

Commit 116d667

Browse files
committed
Add GCC generalized atomic operation function
1 parent 73850e2 commit 116d667

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

components/libc/cplusplus/cpp11/atomic_8.c

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,84 @@ bool __atomic_compare_exchange_8(volatile void *ptr, volatile void *expected, ui
6969
return exchanged;
7070
}
7171

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

0 commit comments

Comments
 (0)