Skip to content

Commit cb6d451

Browse files
atwwwwmysterywolf
authored andcommitted
Add GCC generalized atomic operation function
1 parent 99dafb1 commit cb6d451

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

Diff for: components/libc/cplusplus/cpp11/atomic_8.c

+79
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,84 @@ bool __atomic_compare_exchange_8(volatile void *ptr, volatile void *expected, ui
6970
return exchanged;
7071
}
7172

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+
72151
#define __atomic_fetch_op_8(OPNAME, OP) \
73152
uint64_t __atomic_fetch_##OPNAME##_8(volatile void *ptr, uint64_t val, int memorder) {\
74153
volatile uint64_t* val_ptr = (volatile uint64_t*)ptr;\

0 commit comments

Comments
 (0)