los_arch_atomic.h 8.0 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

#include "los_compiler.h"

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

43 44 45 46
STATIC INLINE INT32 ArchAtomicRead(const Atomic *v)
{
    INT32 val;

m0_37218149's avatar
m0_37218149 已提交
47
    __asm__ __volatile__("ldrex %0, [%1]\n"
48 49 50 51 52 53 54 55 56 57 58
                         : "=&r"(val)
                         : "r"(v)
                         : "cc");

    return val;
}

STATIC INLINE VOID ArchAtomicSet(Atomic *v, INT32 setVal)
{
    UINT32 status;

m0_37218149's avatar
m0_37218149 已提交
59 60 61 62 63 64 65
    do {
        __asm__ __volatile__("ldrex %0, [%1]\n"
                             "strex %0, %2, [%1]\n"
                             : "=&r"(status)
                             : "r"(v), "r"(setVal)
                             : "cc");
    } while (__builtin_expect(status != 0, 0));
66 67 68 69 70 71 72 73
}

STATIC INLINE INT32 ArchAtomicAdd(Atomic *v, INT32 addVal)
{
    INT32 val;
    UINT32 status;

    do {
m0_37218149's avatar
m0_37218149 已提交
74 75 76
        __asm__ __volatile__("ldrex %1, [%2]\n"
                             "add %1, %1, %3\n"
                             "strex %0, %1, [%2]"
77 78 79 80 81 82 83 84 85 86 87 88 89 90
                             : "=&r"(status), "=&r"(val)
                             : "r"(v), "r"(addVal)
                             : "cc");
    } while (__builtin_expect(status != 0, 0));

    return val;
}

STATIC INLINE INT32 ArchAtomicSub(Atomic *v, INT32 subVal)
{
    INT32 val;
    UINT32 status;

    do {
m0_37218149's avatar
m0_37218149 已提交
91 92 93
        __asm__ __volatile__("ldrex %1, [%2]\n"
                             "sub %1, %1, %3\n"
                             "strex %0, %1, [%2]"
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
                             : "=&r"(status), "=&r"(val)
                             : "r"(v), "r"(subVal)
                             : "cc");
    } while (__builtin_expect(status != 0, 0));

    return val;
}

STATIC INLINE VOID ArchAtomicInc(Atomic *v)
{
    (VOID)ArchAtomicAdd(v, 1);
}

STATIC INLINE VOID ArchAtomicDec(Atomic *v)
{
    (VOID)ArchAtomicSub(v, 1);
}

STATIC INLINE INT32 ArchAtomicIncRet(Atomic *v)
{
    return ArchAtomicAdd(v, 1);
}

STATIC INLINE INT32 ArchAtomicDecRet(Atomic *v)
{
    return ArchAtomicSub(v, 1);
}

C
Caoruihong 已提交
122
/**
123
 * @ingroup  los_arch_atomic
C
Caoruihong 已提交
124 125 126
 * @brief Atomic exchange for 32-bit variable.
 *
 * @par Description:
127 128
 * This API is used to implement the atomic exchange for 32-bit variable
 * and return the previous value of the atomic variable.
C
Caoruihong 已提交
129 130 131 132
 * @attention
 * <ul>The pointer v must not be NULL.</ul>
 *
 * @param  v       [IN] The variable pointer.
133
 * @param  val     [IN] The exchange value.
C
Caoruihong 已提交
134 135 136
 *
 * @retval #INT32       The previous value of the atomic variable
 * @par Dependency:
137
 * <ul><li>los_arch_atomic.h: the header file that contains the API declaration.</li></ul>
C
Caoruihong 已提交
138 139
 * @see
 */
L
LiteOS2021 已提交
140
STATIC INLINE INT32 ArchAtomicXchg32bits(volatile INT32 *v, INT32 val)
C
Caoruihong 已提交
141 142 143 144 145
{
    INT32 prevVal = 0;
    UINT32 status = 0;

    do {
m0_37218149's avatar
m0_37218149 已提交
146 147
        __asm__ __volatile__("ldrex %0, [%3]\n"
                             "strex %1, %4, [%3]"
C
Caoruihong 已提交
148 149 150 151 152 153 154 155 156
                             : "=&r"(prevVal), "=&r"(status), "+m"(*v)
                             : "r"(v), "r"(val)
                             : "cc");
    } while (__builtin_expect(status != 0, 0));

    return prevVal;
}

/**
157
 * @ingroup  los_arch_atomic
C
Caoruihong 已提交
158 159 160 161 162 163 164 165 166
 * @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.
167
 * @param  oldVal  [IN] The old value.
C
Caoruihong 已提交
168 169 170 171
 *
 * @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:
172
 * <ul><li>los_arch_atomic.h: the header file that contains the API declaration.</li></ul>
C
Caoruihong 已提交
173 174
 * @see
 */
L
LiteOS2021 已提交
175
STATIC INLINE BOOL ArchAtomicCmpXchg32bits(volatile INT32 *v, INT32 val, INT32 oldVal)
C
Caoruihong 已提交
176 177 178 179 180
{
    INT32 prevVal = 0;
    UINT32 status = 0;

    do {
m0_37218149's avatar
m0_37218149 已提交
181 182 183 184 185 186
        __asm__ __volatile__("ldrex %0, %2\n"
                             "mov %1, #0\n"
                             "cmp %0, %3\n"
                             "bne 1f\n"
                             "strex %1, %4, %2\n"
                             "1:"
C
Caoruihong 已提交
187 188 189 190 191 192 193 194
                             : "=&r"(prevVal), "=&r"(status), "+Q"(*v)
                             : "r"(oldVal), "r"(val)
                             : "cc");
    } while (__builtin_expect(status != 0, 0));

    return prevVal != oldVal;
}

195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
STATIC INLINE INT64 ArchAtomic64Read(const Atomic64 *v)
{
    INT64 val;
    UINT32 intSave;

    intSave = LOS_IntLock();
    val = *v;
    LOS_IntRestore(intSave);

    return val;
}

STATIC INLINE VOID ArchAtomic64Set(Atomic64 *v, INT64 setVal)
{
    UINT32 intSave;

    intSave = LOS_IntLock();
    *v = setVal;
    LOS_IntRestore(intSave);
}

STATIC INLINE INT64 ArchAtomic64Add(Atomic64 *v, INT64 addVal)
{
    INT64 val;
    UINT32 intSave;

    intSave = LOS_IntLock();
    *v += addVal;
    val = *v;
    LOS_IntRestore(intSave);

    return val;
}

STATIC INLINE INT64 ArchAtomic64Sub(Atomic64 *v, INT64 subVal)
{
    INT64 val;
    UINT32 intSave;

    intSave = LOS_IntLock();
    *v -= subVal;
    val = *v;
    LOS_IntRestore(intSave);

    return val;
}

STATIC INLINE VOID ArchAtomic64Inc(Atomic64 *v)
{
    (VOID)ArchAtomic64Add(v, 1);
}

STATIC INLINE INT64 ArchAtomic64IncRet(Atomic64 *v)
{
    return ArchAtomic64Add(v, 1);
}

STATIC INLINE VOID ArchAtomic64Dec(Atomic64 *v)
{
    (VOID)ArchAtomic64Sub(v, 1);
}

STATIC INLINE INT64 ArchAtomic64DecRet(Atomic64 *v)
{
    return ArchAtomic64Sub(v, 1);
}

STATIC INLINE INT64 ArchAtomicXchg64bits(Atomic64 *v, INT64 val)
{
    INT64 prevVal;
    UINT32 intSave;

    intSave = LOS_IntLock();
    prevVal = *v;
    *v = val;
    LOS_IntRestore(intSave);

    return prevVal;
}

STATIC INLINE BOOL ArchAtomicCmpXchg64bits(Atomic64 *v, INT64 val, INT64 oldVal)
{
    INT64 prevVal;
    UINT32 intSave;

    intSave = LOS_IntLock();
    prevVal = *v;
    if (prevVal == oldVal) {
        *v = val;
    }
    LOS_IntRestore(intSave);

    return prevVal != oldVal;
}

C
Caoruihong 已提交
290 291 292 293 294 295
#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cplusplus */
#endif /* __cplusplus */

296
#endif /* _LOS_ARCH_ATOMIC_H */