• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3  * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this list of
9  *    conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12  *    of conditions and the following disclaimer in the documentation and/or other materials
13  *    provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16  *    to endorse or promote products derived from this software without specific prior written
17  *    permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /**
33  * @defgroup los_mux Mutex
34  * @ingroup kernel
35  */
36 
37 #ifndef _LOS_MUX_H
38 #define _LOS_MUX_H
39 
40 #include "los_base.h"
41 
42 #ifdef __cplusplus
43 #if __cplusplus
44 extern "C" {
45 #endif /* __cplusplus */
46 #endif /* __cplusplus */
47 
48 enum {
49     LOS_MUX_PRIO_NONE = 0,
50     LOS_MUX_PRIO_INHERIT = 1,
51     LOS_MUX_PRIO_PROTECT = 2
52 };
53 
54 enum {
55     LOS_MUX_NORMAL = 0,
56     LOS_MUX_RECURSIVE = 1,
57     LOS_MUX_ERRORCHECK = 2,
58     LOS_MUX_DEFAULT = LOS_MUX_RECURSIVE
59 };
60 
61 typedef struct {
62     UINT8 protocol;
63     UINT8 prioceiling;
64     UINT8 type;
65     UINT8 reserved;
66 } LosMuxAttr;
67 
68 /**
69  * @ingroup los_mux
70  * Mutex object.
71  */
72 typedef struct OsMux {
73     UINT32 magic;        /**< magic number */
74     LosMuxAttr attr;     /**< Mutex attribute */
75     LOS_DL_LIST holdList; /**< The task holding the lock change */
76     LOS_DL_LIST muxList; /**< Mutex linked list */
77     VOID *owner;         /**< The current thread that is locking a mutex */
78     UINT16 muxCount;     /**< Times of locking a mutex */
79 } LosMux;
80 
81 extern UINT32 LOS_MuxAttrInit(LosMuxAttr *attr);
82 extern UINT32 LOS_MuxAttrDestroy(LosMuxAttr *attr);
83 extern UINT32 LOS_MuxAttrGetType(const LosMuxAttr *attr, INT32 *outType);
84 extern UINT32 LOS_MuxAttrSetType(LosMuxAttr *attr, INT32 type);
85 extern UINT32 LOS_MuxAttrGetProtocol(const LosMuxAttr *attr, INT32 *protocol);
86 extern UINT32 LOS_MuxAttrSetProtocol(LosMuxAttr *attr, INT32 protocol);
87 extern UINT32 LOS_MuxAttrGetPrioceiling(const LosMuxAttr *attr, INT32 *prioceiling);
88 extern UINT32 LOS_MuxAttrSetPrioceiling(LosMuxAttr *attr, INT32 prioceiling);
89 extern UINT32 LOS_MuxSetPrioceiling(LosMux *mutex, INT32 prioceiling, INT32 *oldPrioceiling);
90 extern UINT32 LOS_MuxGetPrioceiling(const LosMux *mutex, INT32 *prioceiling);
91 extern BOOL LOS_MuxIsValid(const LosMux *mutex);
92 
93 /**
94  * @ingroup los_mux
95  * @brief Init a mutex.
96  *
97  * @par Description:
98  * This API is used to Init a mutex. A mutex handle is assigned to muxHandle when the mutex is init successfully.
99  * Return LOS_OK on creating successful, return specific error code otherwise.
100  * @attention
101  * <ul>
102  * <li>The total number of mutexes is pre-configured. If there are no available mutexes, the mutex creation fails.</li>
103  * </ul>
104  *
105  * @param mutex             [IN] Handle pointer of the successfully init mutex.
106  * @param attr              [IN] The mutex attribute.
107  *
108  * @retval #LOS_EINVAL      The mutex pointer is NULL.
109  * @retval #LOS_OK          The mutex is successfully created.
110  * @par Dependency:
111  * <ul><li>los_mux.h: the header file that contains the API declaration.</li></ul>
112  * @see LOS_MuxDestroy
113  */
114 extern UINT32 LOS_MuxInit(LosMux *mutex, const LosMuxAttr *attr);
115 
116 /**
117  * @ingroup los_mux
118  * @brief Destroy a mutex.
119  *
120  * @par Description:
121  * This API is used to delete a specified mutex. Return LOS_OK on deleting successfully, return specific error code
122  * otherwise.
123  * @attention
124  * <ul>
125  * <li>The specific mutex should be created firstly.</li>
126  * <li>The mutex can be deleted successfully only if no other tasks pend on it.</li>
127  * </ul>
128  *
129  * @param mutex         [IN] Handle of the mutex to be deleted.
130  *
131  * @retval #LOS_EINVAL  The mutex pointer is NULL.
132  * @retval #LOS_EBUSY   Tasks pended on this mutex.
133  * @retval #LOS_EBADF   The lock has been destroyed or broken.
134  * @retval #LOS_OK      The mutex is successfully deleted.
135  * @par Dependency:
136  * <ul><li>los_mux.h: the header file that contains the API declaration.</li></ul>
137  * @see LOS_MuxInit
138  */
139 extern UINT32 LOS_MuxDestroy(LosMux *mutex);
140 
141 /**
142  * @ingroup los_mux
143  * @brief Wait to lock a mutex.
144  *
145  * @par Description:
146  * This API is used to wait for a specified period of time to lock a mutex.
147  * @attention
148  * <ul>
149  * <li>The specific mutex should be created firstly.</li>
150  * <li>The function fails if the mutex that is waited on is already locked by another thread when the task scheduling
151  * is disabled.</li>
152  * <li>Do not wait on a mutex during an interrupt.</li>
153  * <li>The priority inheritance protocol is supported. If a higher-priority thread is waiting on a mutex, it changes
154  * the priority of the thread that owns the mutex to avoid priority inversion.</li>
155  * <li>A recursive mutex can be locked more than once by the same thread.</li>
156  * <li>Do not call this API in software timer callback. </li>
157  * </ul>
158  *
159  * @param mutex           [IN] Handle of the mutex to be waited on.
160  * @param timeout         [IN] Waiting time. The value range is [0, LOS_WAIT_FOREVER](unit: Tick).
161  *
162  * @retval #LOS_EINVAL    The mutex pointer is NULL, The timeout is zero or Lock status error.
163  * @retval #LOS_EINTR     The mutex is being locked during an interrupt.
164  * @retval #LOS_EBUSY     Tasks pended on this mutex.
165  * @retval #LOS_EBADF     The lock has been destroyed or broken.
166  * @retval #LOS_EDEADLK   Mutex error check failed or System locked task scheduling.
167  * @retval #LOS_ETIMEDOUT The mutex waiting times out.
168  * @retval #LOS_OK                           The mutex is successfully locked.
169  * @par Dependency:
170  * <ul><li>los_mux.h: the header file that contains the API declaration.</li></ul>
171  * @see LOS_MuxInit | LOS_MuxUnlock
172  */
173 extern UINT32 LOS_MuxLock(LosMux *mutex, UINT32 timeout);
174 
175 /**
176  * @ingroup los_mux
177  * @brief Try wait to lock a mutex.
178  *
179  * @par Description:
180  * This API is used to wait for a specified period of time to lock a mutex.
181  * @attention
182  * <ul>
183  * <li>The specific mutex should be created firstly.</li>
184  * <li>The function fails if the mutex that is waited on is already locked by another thread when the task scheduling
185  * is disabled.</li>
186  * <li>Do not wait on a mutex during an interrupt.</li>
187  * <li>The priority inheritance protocol is supported. If a higher-priority thread is waiting on a mutex, it changes
188  * the priority of the thread that owns the mutex to avoid priority inversion.</li>
189  * <li>A recursive mutex can be locked more than once by the same thread.</li>
190  * <li>Do not call this API in software timer callback. </li>
191  * </ul>
192  *
193  * @param mutex           [IN] Handle of the mutex to be waited on.
194  *
195  * @retval #LOS_EINVAL    The mutex pointer is NULL or Lock status error.
196  * @retval #LOS_EINTR     The mutex is being locked during an interrupt.
197  * @retval #LOS_EBUSY     Tasks pended on this mutex.
198  * @retval #LOS_EBADF     The lock has been destroyed or broken.
199  * @retval #LOS_EDEADLK   Mutex error check failed or System locked task scheduling.
200  * @retval #LOS_ETIMEDOUT The mutex waiting times out.
201  * @retval #LOS_OK                           The mutex is successfully locked.
202  * @par Dependency:
203  * <ul><li>los_mux.h: the header file that contains the API declaration.</li></ul>
204  * @see LOS_MuxInit | LOS_MuxUnlock
205  */
206 extern UINT32 LOS_MuxTrylock(LosMux *mutex);
207 
208 /**
209  * @ingroup los_mux
210  * @brief Release a mutex.
211  *
212  * @par Description:
213  * This API is used to release a specified mutex.
214  * @attention
215  * <ul>
216  * <li>The specific mutex should be created firstly.</li>
217  * <li>Do not release a mutex during an interrupt.</li>
218  * <li>If a recursive mutex is locked for many times, it must be unlocked for the same times to be released.</li>
219  * </ul>
220  *
221  * @param mutex        [IN] Handle of the mutex to be released.
222  *
223  * @retval #LOS_EINVAL    The mutex pointer is NULL, The timeout is zero or Lock status error.
224  * @retval #LOS_EINTR     The mutex is being locked during an interrupt.
225  * @retval #LOS_EBUSY     Tasks pended on this mutex.
226  * @retval #LOS_EBADF     The lock has been destroyed or broken.
227  * @retval #LOS_EDEADLK   Mutex error check failed or System locked task scheduling.
228  * @retval #LOS_ETIMEDOUT The mutex waiting times out.
229  * @retval #LOS_OK                           The mutex is successfully locked.
230  * @par Dependency:
231  * <ul><li>los_mux.h: the header file that contains the API declaration.</li></ul>
232  * @see LOS_MuxInit | LOS_MuxLock
233  */
234 extern UINT32 LOS_MuxUnlock(LosMux *mutex);
235 
236 #ifdef __cplusplus
237 #if __cplusplus
238 }
239 #endif
240 #endif /* __cplusplus */
241 
242 #endif /* _LOS_MUX_H */
243