los_arch_atomic.h 5.5 KB
Newer Older
C
Caoruihong 已提交
1
/*
M
mamingshuai 已提交
2 3
 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
C
Caoruihong 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 *    conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 *    of conditions and the following disclaimer in the documentation and/or other materials
 *    provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 *    to endorse or promote products derived from this software without specific prior written
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

32 33
#ifndef _LOS_ARCH_ATOMIC_H
#define _LOS_ARCH_ATOMIC_H
C
Caoruihong 已提交
34 35 36 37 38 39 40 41 42 43

#include "los_compiler.h"

#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cplusplus */
#endif /* __cplusplus */

/**
44
 * @ingroup  los_arch_atomic
C
Caoruihong 已提交
45 46 47 48 49 50 51 52
 * @brief Atomic exchange for 32-bit variable.
 *
 * @par Description:
 * This API is used to implement the atomic exchange for 32-bit variable and return the previous value of the atomic variable.
 * @attention
 * <ul>The pointer v must not be NULL.</ul>
 *
 * @param  v       [IN] The variable pointer.
53
 * @param  val     [IN] The exchange value.
C
Caoruihong 已提交
54 55 56
 *
 * @retval #INT32       The previous value of the atomic variable
 * @par Dependency:
57
 * <ul><li>los_arch_atomic.h: the header file that contains the API declaration.</li></ul>
C
Caoruihong 已提交
58 59
 * @see
 */
L
LiteOS2021 已提交
60
STATIC INLINE INT32 ArchAtomicXchg32bits(volatile INT32 *v, INT32 val)
C
Caoruihong 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
{
    INT32 prevVal = 0;
    UINT32 status = 0;

    do {
        __asm__ __volatile__("ldrex   %0, [%3]\n"
                             "strex   %1, %4, [%3]"
                             : "=&r"(prevVal), "=&r"(status), "+m"(*v)
                             : "r"(v), "r"(val)
                             : "cc");
    } while (__builtin_expect(status != 0, 0));

    return prevVal;
}

/**
77
 * @ingroup  los_arch_atomic
C
Caoruihong 已提交
78 79 80
 * @brief Atomic auto-decrement.
 *
 * @par Description:
81
 * This API is used to implement the atomic auto-decrement and return the result of auto-decrement.
C
Caoruihong 已提交
82 83 84 85 86 87 88 89 90 91
 * @attention
 * <ul>
 * <li>The pointer v must not be NULL.</li>
 * <li>The value which v point to must not be INT_MIN to avoid overflow after reducing 1.</li>
 * </ul>
 *
 * @param  v      [IN] The addSelf variable pointer.
 *
 * @retval #INT32  The return value of variable auto-decrement.
 * @par Dependency:
92
 * <ul><li>los_arch_atomic.h: the header file that contains the API declaration.</li></ul>
C
Caoruihong 已提交
93 94
 * @see
 */
L
LiteOS2021 已提交
95
STATIC INLINE INT32 ArchAtomicDecRet(volatile INT32 *v)
C
Caoruihong 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
{
    INT32 val = 0;
    UINT32 status = 0;

    do {
        __asm__ __volatile__("ldrex   %0, [%3]\n"
                             "sub   %0, %0, #1\n"
                             "strex   %1, %0, [%3]"
                             : "=&r"(val), "=&r"(status), "+m"(*v)
                             : "r"(v)
                             : "cc");
    } while (__builtin_expect(status != 0, 0));

    return val;
}

/**
113
 * @ingroup  los_arch_atomic
C
Caoruihong 已提交
114 115 116 117 118 119 120 121 122
 * @brief Atomic exchange for 32-bit variable with compare.
 *
 * @par Description:
 * This API is used to implement the atomic exchange for 32-bit variable, if the value of variable is equal to oldVal.
 * @attention
 * <ul>The pointer v must not be NULL.</ul>
 *
 * @param  v       [IN] The variable pointer.
 * @param  val     [IN] The new value.
123
 * @param  oldVal  [IN] The old value.
C
Caoruihong 已提交
124 125 126 127
 *
 * @retval TRUE  The previous value of the atomic variable is not equal to oldVal.
 * @retval FALSE The previous value of the atomic variable is equal to oldVal.
 * @par Dependency:
128
 * <ul><li>los_arch_atomic.h: the header file that contains the API declaration.</li></ul>
C
Caoruihong 已提交
129 130
 * @see
 */
L
LiteOS2021 已提交
131
STATIC INLINE BOOL ArchAtomicCmpXchg32bits(volatile INT32 *v, INT32 val, INT32 oldVal)
C
Caoruihong 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
{
    INT32 prevVal = 0;
    UINT32 status = 0;

    do {
        __asm__ __volatile__("1: ldrex %0, %2\n"
                             "    mov %1, #0\n"
                             "    cmp %0, %3\n"
                             "    bne 2f\n"
                             "    strex %1, %4, %2\n"
                             "2:"
                             : "=&r"(prevVal), "=&r"(status), "+Q"(*v)
                             : "r"(oldVal), "r"(val)
                             : "cc");
    } while (__builtin_expect(status != 0, 0));

    return prevVal != oldVal;
}

#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cplusplus */
#endif /* __cplusplus */

157
#endif /* _LOS_ARCH_ATOMIC_H */