• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification,
5  * are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice, this list of
8  *    conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11  *    of conditions and the following disclaimer in the documentation and/or other materials
12  *    provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15  *    to endorse or promote products derived from this software without specific prior written
16  *    permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #ifndef OSAL_ATOMIC_DEF_H
32 #define OSAL_ATOMIC_DEF_H
33 
34 #include <los_atomic.h>
35 #include <los_hwi.h>
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif /* __cplusplus */
40 
41 #define OsalAtomicReadWrapper(v) LOS_AtomicRead((const Atomic *)&(v)->counter)
42 #define OsalAtomicSetWrapper(v, value) LOS_AtomicSet((Atomic *)&(v)->counter, value)
43 #define OsalAtomicIncWrapper(v) LOS_AtomicInc((Atomic *)&(v)->counter)
44 #define OsalAtomicIncRetWrapper(v) LOS_AtomicIncRet((Atomic *)&(v)->counter)
45 #define OsalAtomicDecWrapper(v) LOS_AtomicDec((Atomic *)&(v)->counter)
46 #define OsalAtomicDecRetWrapper(v) LOS_AtomicDecRet((Atomic *)&(v)->counter)
47 
48 #define OSAL_BITS_PER_LONG 32
49 #define OSAL_BIT_MASK(nr) (1UL << ((nr) % OSAL_BITS_PER_LONG))
50 #define OSAL_BIT_WORD(nr) ((nr) / OSAL_BITS_PER_LONG)
51 #define OSAL_BITOP_WORD(nr) ((nr) / OSAL_BITS_PER_LONG)
52 
OsalTestBitWrapper(unsigned long nr,const volatile unsigned long * addr)53 static inline int32_t OsalTestBitWrapper(unsigned long nr, const volatile unsigned long *addr)
54 {
55     return (1UL & (addr[OSAL_BIT_WORD(nr)] >> (nr & (OSAL_BITS_PER_LONG - 1))));
56 }
57 
OsalTestSetBitWrapper(unsigned long nr,volatile unsigned long * addr)58 static inline int32_t OsalTestSetBitWrapper(unsigned long nr, volatile unsigned long *addr)
59 {
60     uint32_t intSave = LOS_IntLock();
61 
62     const unsigned long mask = OSAL_BIT_MASK(nr);
63     unsigned long *p = ((unsigned long *)addr) + OSAL_BIT_WORD(nr);
64     unsigned long old = *p;
65     *p = old | mask;
66 
67     LOS_IntRestore(intSave);
68     return ((old & mask) != 0);
69 }
70 
OsalTestClearBitWrapper(unsigned long nr,volatile unsigned long * addr)71 static inline int32_t OsalTestClearBitWrapper(unsigned long nr, volatile unsigned long *addr)
72 {
73     uint32_t intSave = LOS_IntLock();
74 
75     const unsigned long mask = OSAL_BIT_MASK(nr);
76     unsigned long *p = ((unsigned long *)addr) + OSAL_BIT_WORD(nr);
77     unsigned long old = *p;
78     *p = old & ~mask;
79 
80     LOS_IntRestore(intSave);
81     return ((old & mask) != 0);
82 }
83 
OsalClearBitWrapper(unsigned long nr,volatile unsigned long * addr)84 static inline void OsalClearBitWrapper(unsigned long nr, volatile unsigned long *addr)
85 {
86     uint32_t intSave = LOS_IntLock();
87 
88     const unsigned long mask = OSAL_BIT_MASK(nr);
89     unsigned long *p = ((unsigned long *)addr) + OSAL_BIT_WORD(nr);
90     *p &= ~mask;
91 
92     LOS_IntRestore(intSave);
93 }
94 
95 
96 #ifdef __cplusplus
97 }
98 #endif /* __cplusplus */
99 
100 #endif /* OSAL_ATOMIC_DEF_H */
101 
102