11bd4fe43Sopenharmony_ci/**
21bd4fe43Sopenharmony_ci * @file hi_atomic.h
31bd4fe43Sopenharmony_ci *
41bd4fe43Sopenharmony_ci * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
51bd4fe43Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
61bd4fe43Sopenharmony_ci * you may not use this file except in compliance with the License.
71bd4fe43Sopenharmony_ci * You may obtain a copy of the License at
81bd4fe43Sopenharmony_ci *
91bd4fe43Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
101bd4fe43Sopenharmony_ci *
111bd4fe43Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
121bd4fe43Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
131bd4fe43Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
141bd4fe43Sopenharmony_ci * See the License for the specific language governing permissions and
151bd4fe43Sopenharmony_ci * limitations under the License.
161bd4fe43Sopenharmony_ci */
171bd4fe43Sopenharmony_ci
181bd4fe43Sopenharmony_ci/**
191bd4fe43Sopenharmony_ci * @defgroup iot_atomic  Atomic Operation
201bd4fe43Sopenharmony_ci * @ingroup osa
211bd4fe43Sopenharmony_ci */
221bd4fe43Sopenharmony_ci#ifndef __HI_ATOMIC_H__
231bd4fe43Sopenharmony_ci#define __HI_ATOMIC_H__
241bd4fe43Sopenharmony_ci#include <hi_types.h>
251bd4fe43Sopenharmony_ci#include <los_hwi.h>
261bd4fe43Sopenharmony_ci
271bd4fe43Sopenharmony_ci#ifdef __cplusplus
281bd4fe43Sopenharmony_ci#if __cplusplus
291bd4fe43Sopenharmony_ciextern "C" {
301bd4fe43Sopenharmony_ci#endif
311bd4fe43Sopenharmony_ci#endif
321bd4fe43Sopenharmony_ci
331bd4fe43Sopenharmony_citypedef struct {
341bd4fe43Sopenharmony_ci    volatile hi_s32 counter;
351bd4fe43Sopenharmony_ci} hi_atomic;
361bd4fe43Sopenharmony_ci
371bd4fe43Sopenharmony_ci#define hi_atomic_init(i)            { (i) }
381bd4fe43Sopenharmony_ci#define hi_atomic_read(v)            ((v)->counter)
391bd4fe43Sopenharmony_ci#define hi_atomic_set(v, i)          (((v)->counter) = (i))
401bd4fe43Sopenharmony_ci
411bd4fe43Sopenharmony_ci#define hi_atomic_inc(v)             hi_atomic_add_return(1, v)
421bd4fe43Sopenharmony_ci#define hi_atomic_dec(v)             hi_atomic_sub_return(1, v)
431bd4fe43Sopenharmony_ci
441bd4fe43Sopenharmony_ci#define hi_atomic_inc_return(v)      (hi_atomic_add_return(1, v))
451bd4fe43Sopenharmony_ci#define hi_atomic_dec_return(v)      (hi_atomic_sub_return(1, v))
461bd4fe43Sopenharmony_ci
471bd4fe43Sopenharmony_ci/**
481bd4fe43Sopenharmony_ci * @ingroup  iot_atomic
491bd4fe43Sopenharmony_ci * If the atomic operation is performed, the operation result is returned.
501bd4fe43Sopenharmony_ciCNcomment:原子加操作,返回操作结果CNend
511bd4fe43Sopenharmony_ci */
521bd4fe43Sopenharmony_ci#define hi_atomic_add_return_op(i, v)   (hi_atomic_add_return(i, v))
531bd4fe43Sopenharmony_ci
541bd4fe43Sopenharmony_ci/**
551bd4fe43Sopenharmony_ci * @ingroup  iot_atomic
561bd4fe43Sopenharmony_ci * The operation result is returned when the atomic subtraction operation is performed.
571bd4fe43Sopenharmony_ciCNcomment:原子减操作,返回操作结果CNend
581bd4fe43Sopenharmony_ci */
591bd4fe43Sopenharmony_ci#define hi_atomic_sub_return_op(i, v)   (hi_atomic_sub_return(i, v))
601bd4fe43Sopenharmony_ci
611bd4fe43Sopenharmony_ci/**
621bd4fe43Sopenharmony_ci * @ingroup  iot_atomic
631bd4fe43Sopenharmony_ci * The specified bit in the atomic setting variable is 1.
641bd4fe43Sopenharmony_ciCNcomment:原子设置变量中指定bit位为1CNend
651bd4fe43Sopenharmony_ci */
661bd4fe43Sopenharmony_ci#define hi_atomic_bit_set_op(bit, v)    (hi_atomic_bit_set(bit, v))
671bd4fe43Sopenharmony_ci/**
681bd4fe43Sopenharmony_ci * @ingroup  iot_atomic
691bd4fe43Sopenharmony_ci * The specified bit in the atomic setting variable is 0.
701bd4fe43Sopenharmony_ciCNcomment:原子设置变量中指定bit位为0CNend
711bd4fe43Sopenharmony_ci */
721bd4fe43Sopenharmony_ci#define hi_atomic_bit_clear_op(bit, v)  (hi_atomic_bit_clear(bit, v))
731bd4fe43Sopenharmony_ci
741bd4fe43Sopenharmony_ci/*
751bd4fe43Sopenharmony_ci* If the atomic operation is performed, the operation result is returned.
761bd4fe43Sopenharmony_ciCNcomment:原子加操作,返回操作结果CNend
771bd4fe43Sopenharmony_ci*
781bd4fe43Sopenharmony_ci* 描述:
791bd4fe43Sopenharmony_ci*          If the atomic operation is performed, the operation result is returned.
801bd4fe43Sopenharmony_ciCNcomment:原子加操作,返回操作结果CNend
811bd4fe43Sopenharmony_ci*
821bd4fe43Sopenharmony_ci* param  i     [IN] type #hi_s32, The number of operands added to an atom.CNcomment:与原子相加的操作数CNend
831bd4fe43Sopenharmony_ci* param  v     [IN] type #hi_atomic*,Pointer to the atomic structure address.CNcomment:原子结构地址指针CNend
841bd4fe43Sopenharmony_ci*
851bd4fe43Sopenharmony_ci* retval #hi_s32 Add Operation Result.CNcomment:加操作结果CNend
861bd4fe43Sopenharmony_ci* par 依赖:
871bd4fe43Sopenharmony_ci*           hi_atomic.h:Header file where the interface declaration is located.
881bd4fe43Sopenharmony_ciCNcomment:该接口声明所在的头文件。CNend
891bd4fe43Sopenharmony_ci*/
901bd4fe43Sopenharmony_ci__attribute__((always_inline)) static inline hi_s32 hi_atomic_add_return(hi_s32 i, hi_atomic *v)
911bd4fe43Sopenharmony_ci{
921bd4fe43Sopenharmony_ci    hi_u32 irq_status;
931bd4fe43Sopenharmony_ci
941bd4fe43Sopenharmony_ci    irq_status = LOS_IntLock();
951bd4fe43Sopenharmony_ci    v->counter += i;
961bd4fe43Sopenharmony_ci    (hi_void)LOS_IntRestore(irq_status);
971bd4fe43Sopenharmony_ci    return v->counter;
981bd4fe43Sopenharmony_ci}
991bd4fe43Sopenharmony_ci
1001bd4fe43Sopenharmony_ci/*
1011bd4fe43Sopenharmony_ci* The operation result is returned when the atomic subtraction operation is performed.
1021bd4fe43Sopenharmony_ciCNcomment:原子减操作,返回操作结果CNend
1031bd4fe43Sopenharmony_ci*
1041bd4fe43Sopenharmony_ci* 描述:
1051bd4fe43Sopenharmony_ci*          The operation result is returned when the atomic subtraction operation is performed.
1061bd4fe43Sopenharmony_ciCNcomment:原子减操作,返回操作结果CNend
1071bd4fe43Sopenharmony_ci*
1081bd4fe43Sopenharmony_ci* param  i     [IN] type #hi_s32, The number of operands subtracted from the atom.
1091bd4fe43Sopenharmony_ciCNcomment:被原子相减的操作数CNend
1101bd4fe43Sopenharmony_ci* param  v     [IN] type #hi_atomic*,Pointer to the atomic structure address.CNcomment:原子结构地址指针CNend
1111bd4fe43Sopenharmony_ci*
1121bd4fe43Sopenharmony_ci* retval #hi_s32 Reduce the operation result. CNcomment:减操作结果CNend
1131bd4fe43Sopenharmony_ci* par 依赖:
1141bd4fe43Sopenharmony_ci*           hi_atomic.h:Header file where the interface declaration is located.
1151bd4fe43Sopenharmony_ciCNcomment:该接口声明所在的头文件。CNend
1161bd4fe43Sopenharmony_ci*/
1171bd4fe43Sopenharmony_ci__attribute__((always_inline)) static inline hi_s32 hi_atomic_sub_return(hi_s32 i, hi_atomic *v)
1181bd4fe43Sopenharmony_ci{
1191bd4fe43Sopenharmony_ci    hi_u32 irq_status;
1201bd4fe43Sopenharmony_ci
1211bd4fe43Sopenharmony_ci    irq_status = LOS_IntLock();
1221bd4fe43Sopenharmony_ci    v->counter = v->counter - i;
1231bd4fe43Sopenharmony_ci    (hi_void)LOS_IntRestore(irq_status);
1241bd4fe43Sopenharmony_ci
1251bd4fe43Sopenharmony_ci    return v->counter;
1261bd4fe43Sopenharmony_ci}
1271bd4fe43Sopenharmony_ci
1281bd4fe43Sopenharmony_ci/*
1291bd4fe43Sopenharmony_ci* The specified bit in the atomic setting variable is 1.CNcomment:原子设置变量中指定bit位为1CNend
1301bd4fe43Sopenharmony_ci*
1311bd4fe43Sopenharmony_ci* 描述:
1321bd4fe43Sopenharmony_ci*          The specified bit in the atomic setting variable is 1.CNcomment:原子设置变量中指定bit位为1CNend
1331bd4fe43Sopenharmony_ci*
1341bd4fe43Sopenharmony_ci* param  bit     [IN] type #hi_s32, Position of the bit that is set to 1. The value range is 0-31.
1351bd4fe43Sopenharmony_ciCNcomment:被置1的bit位置,范围0-31.CNend
1361bd4fe43Sopenharmony_ci* param  value   [IN] type #hi_u32*,Address pointer of the set variable.CNcomment:置位变量的地址指针CNend
1371bd4fe43Sopenharmony_ci*
1381bd4fe43Sopenharmony_ci* retval #None
1391bd4fe43Sopenharmony_ci* par 依赖:
1401bd4fe43Sopenharmony_ci*           hi_atomic.h:Header file where the interface declaration is located.
1411bd4fe43Sopenharmony_ciCNcomment:该接口声明所在的头文件。CNend
1421bd4fe43Sopenharmony_ci*/
1431bd4fe43Sopenharmony_ci__attribute__((always_inline)) static inline hi_void hi_atomic_bit_set(hi_s32 bit, volatile hi_u32 *value)
1441bd4fe43Sopenharmony_ci{
1451bd4fe43Sopenharmony_ci    hi_u32 irq_status;
1461bd4fe43Sopenharmony_ci    irq_status = LOS_IntLock();
1471bd4fe43Sopenharmony_ci
1481bd4fe43Sopenharmony_ci    *value |= (1 << bit);
1491bd4fe43Sopenharmony_ci
1501bd4fe43Sopenharmony_ci    (hi_void)LOS_IntRestore(irq_status);
1511bd4fe43Sopenharmony_ci}
1521bd4fe43Sopenharmony_ci
1531bd4fe43Sopenharmony_ci/*
1541bd4fe43Sopenharmony_ci* The specified bit in the atomic setting variable is 0.CNcomment:原子设置变量中指定bit位为0CNend
1551bd4fe43Sopenharmony_ci*
1561bd4fe43Sopenharmony_ci* 描述:
1571bd4fe43Sopenharmony_ci*          The specified bit in the atomic setting variable is 0.CNcomment:原子设置变量中指定bit位为0CNend
1581bd4fe43Sopenharmony_ci*
1591bd4fe43Sopenharmony_ci* param  bit     [IN] type #hi_s32, Position of the bit that is set to 0. The value range is 0-31.
1601bd4fe43Sopenharmony_ciCNcomment:被置0的bit位置,范围0-31.CNend
1611bd4fe43Sopenharmony_ci* param  value   [IN] type #hi_u32*,Address pointer of the set variable.CNcomment:置位变量的地址指针CNend
1621bd4fe43Sopenharmony_ci*
1631bd4fe43Sopenharmony_ci* retval #None
1641bd4fe43Sopenharmony_ci* par 依赖:
1651bd4fe43Sopenharmony_ci*           hi_atomic.h:Header file where the interface declaration is located.
1661bd4fe43Sopenharmony_ciCNcomment:该接口声明所在的头文件。CNend
1671bd4fe43Sopenharmony_ci*/
1681bd4fe43Sopenharmony_ci__attribute__((always_inline)) static inline hi_void hi_atomic_bit_clear(hi_s32 bit, volatile hi_u32 *value)
1691bd4fe43Sopenharmony_ci{
1701bd4fe43Sopenharmony_ci    hi_u32 irq_status;
1711bd4fe43Sopenharmony_ci    hi_u32 mask;
1721bd4fe43Sopenharmony_ci
1731bd4fe43Sopenharmony_ci    irq_status = LOS_IntLock();
1741bd4fe43Sopenharmony_ci    mask = 1 << bit;
1751bd4fe43Sopenharmony_ci    *value = (*value) & (~mask);
1761bd4fe43Sopenharmony_ci
1771bd4fe43Sopenharmony_ci    (hi_void)LOS_IntRestore(irq_status);
1781bd4fe43Sopenharmony_ci}
1791bd4fe43Sopenharmony_ci
1801bd4fe43Sopenharmony_ci#ifdef __cplusplus
1811bd4fe43Sopenharmony_ci#if __cplusplus
1821bd4fe43Sopenharmony_ci}
1831bd4fe43Sopenharmony_ci#endif
1841bd4fe43Sopenharmony_ci#endif
1851bd4fe43Sopenharmony_ci
1861bd4fe43Sopenharmony_ci#endif /* end of hi_atomic.h */
1871bd4fe43Sopenharmony_ci
188