1 /**
2 * @file hi_atomic.h
3 *
4 * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 /**
19 * @defgroup iot_atomic Atomic Operation
20 * @ingroup osa
21 */
22 #ifndef __HI_ATOMIC_H__
23 #define __HI_ATOMIC_H__
24 #include <hi_types.h>
25 #include <los_hwi.h>
26
27 #ifdef __cplusplus
28 #if __cplusplus
29 extern "C" {
30 #endif
31 #endif
32
33 typedef struct {
34 volatile hi_s32 counter;
35 } hi_atomic;
36
37 #define hi_atomic_init(i) { (i) }
38 #define hi_atomic_read(v) ((v)->counter)
39 #define hi_atomic_set(v, i) (((v)->counter) = (i))
40
41 #define hi_atomic_inc(v) hi_atomic_add_return(1, v)
42 #define hi_atomic_dec(v) hi_atomic_sub_return(1, v)
43
44 #define hi_atomic_inc_return(v) (hi_atomic_add_return(1, v))
45 #define hi_atomic_dec_return(v) (hi_atomic_sub_return(1, v))
46
47 /**
48 * @ingroup iot_atomic
49 * If the atomic operation is performed, the operation result is returned.
50 CNcomment:原子加操作,返回操作结果CNend
51 */
52 #define hi_atomic_add_return_op(i, v) (hi_atomic_add_return(i, v))
53
54 /**
55 * @ingroup iot_atomic
56 * The operation result is returned when the atomic subtraction operation is performed.
57 CNcomment:原子减操作,返回操作结果CNend
58 */
59 #define hi_atomic_sub_return_op(i, v) (hi_atomic_sub_return(i, v))
60
61 /**
62 * @ingroup iot_atomic
63 * The specified bit in the atomic setting variable is 1.
64 CNcomment:原子设置变量中指定bit位为1CNend
65 */
66 #define hi_atomic_bit_set_op(bit, v) (hi_atomic_bit_set(bit, v))
67 /**
68 * @ingroup iot_atomic
69 * The specified bit in the atomic setting variable is 0.
70 CNcomment:原子设置变量中指定bit位为0CNend
71 */
72 #define hi_atomic_bit_clear_op(bit, v) (hi_atomic_bit_clear(bit, v))
73
74 /*
75 * If the atomic operation is performed, the operation result is returned.
76 CNcomment:原子加操作,返回操作结果CNend
77 *
78 * 描述:
79 * If the atomic operation is performed, the operation result is returned.
80 CNcomment:原子加操作,返回操作结果CNend
81 *
82 * param i [IN] type #hi_s32, The number of operands added to an atom.CNcomment:与原子相加的操作数CNend
83 * param v [IN] type #hi_atomic*,Pointer to the atomic structure address.CNcomment:原子结构地址指针CNend
84 *
85 * retval #hi_s32 Add Operation Result.CNcomment:加操作结果CNend
86 * par 依赖:
87 * hi_atomic.h:Header file where the interface declaration is located.
88 CNcomment:该接口声明所在的头文件。CNend
89 */
hi_atomic_add_return(hi_s32 i,hi_atomic * v)90 __attribute__((always_inline)) static inline hi_s32 hi_atomic_add_return(hi_s32 i, hi_atomic *v)
91 {
92 hi_u32 irq_status;
93
94 irq_status = LOS_IntLock();
95 v->counter += i;
96 (hi_void)LOS_IntRestore(irq_status);
97 return v->counter;
98 }
99
100 /*
101 * The operation result is returned when the atomic subtraction operation is performed.
102 CNcomment:原子减操作,返回操作结果CNend
103 *
104 * 描述:
105 * The operation result is returned when the atomic subtraction operation is performed.
106 CNcomment:原子减操作,返回操作结果CNend
107 *
108 * param i [IN] type #hi_s32, The number of operands subtracted from the atom.
109 CNcomment:被原子相减的操作数CNend
110 * param v [IN] type #hi_atomic*,Pointer to the atomic structure address.CNcomment:原子结构地址指针CNend
111 *
112 * retval #hi_s32 Reduce the operation result. CNcomment:减操作结果CNend
113 * par 依赖:
114 * hi_atomic.h:Header file where the interface declaration is located.
115 CNcomment:该接口声明所在的头文件。CNend
116 */
hi_atomic_sub_return(hi_s32 i,hi_atomic * v)117 __attribute__((always_inline)) static inline hi_s32 hi_atomic_sub_return(hi_s32 i, hi_atomic *v)
118 {
119 hi_u32 irq_status;
120
121 irq_status = LOS_IntLock();
122 v->counter = v->counter - i;
123 (hi_void)LOS_IntRestore(irq_status);
124
125 return v->counter;
126 }
127
128 /*
129 * The specified bit in the atomic setting variable is 1.CNcomment:原子设置变量中指定bit位为1CNend
130 *
131 * 描述:
132 * The specified bit in the atomic setting variable is 1.CNcomment:原子设置变量中指定bit位为1CNend
133 *
134 * param bit [IN] type #hi_s32, Position of the bit that is set to 1. The value range is 0-31.
135 CNcomment:被置1的bit位置,范围0-31.CNend
136 * param value [IN] type #hi_u32*,Address pointer of the set variable.CNcomment:置位变量的地址指针CNend
137 *
138 * retval #None
139 * par 依赖:
140 * hi_atomic.h:Header file where the interface declaration is located.
141 CNcomment:该接口声明所在的头文件。CNend
142 */
hi_atomic_bit_set(hi_s32 bit,volatile hi_u32 * value)143 __attribute__((always_inline)) static inline hi_void hi_atomic_bit_set(hi_s32 bit, volatile hi_u32 *value)
144 {
145 hi_u32 irq_status;
146 irq_status = LOS_IntLock();
147
148 *value |= (1 << bit);
149
150 (hi_void)LOS_IntRestore(irq_status);
151 }
152
153 /*
154 * The specified bit in the atomic setting variable is 0.CNcomment:原子设置变量中指定bit位为0CNend
155 *
156 * 描述:
157 * The specified bit in the atomic setting variable is 0.CNcomment:原子设置变量中指定bit位为0CNend
158 *
159 * param bit [IN] type #hi_s32, Position of the bit that is set to 0. The value range is 0-31.
160 CNcomment:被置0的bit位置,范围0-31.CNend
161 * param value [IN] type #hi_u32*,Address pointer of the set variable.CNcomment:置位变量的地址指针CNend
162 *
163 * retval #None
164 * par 依赖:
165 * hi_atomic.h:Header file where the interface declaration is located.
166 CNcomment:该接口声明所在的头文件。CNend
167 */
hi_atomic_bit_clear(hi_s32 bit,volatile hi_u32 * value)168 __attribute__((always_inline)) static inline hi_void hi_atomic_bit_clear(hi_s32 bit, volatile hi_u32 *value)
169 {
170 hi_u32 irq_status;
171 hi_u32 mask;
172
173 irq_status = LOS_IntLock();
174 mask = 1 << bit;
175 *value = (*value) & (~mask);
176
177 (hi_void)LOS_IntRestore(irq_status);
178 }
179
180 #ifdef __cplusplus
181 #if __cplusplus
182 }
183 #endif
184 #endif
185
186 #endif /* end of hi_atomic.h */
187
188