更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
los_mux.h
浏览该文件的文档.
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
44extern "C" {
45#endif /* __cplusplus */
46#endif /* __cplusplus */
47/*! \enum */
48enum {
49 LOS_MUX_PRIO_NONE = 0, ///< 线程的优先级和调度不会受到互斥锁影响,先来后到,普通排队.
50 LOS_MUX_PRIO_INHERIT = 1, ///< 当高优先级的等待低优先级的线程释放锁时,低优先级的线程以高优先级线程的优先级运行。
51 ///< 当线程解锁互斥量时,线程的优先级自动被将到它原来的优先级
52 LOS_MUX_PRIO_PROTECT = 2 ///< 详见: OsMuxPendOp 中的注解,详细说明了LOS_MUX_PRIO_PROTECT的含义
53};
54/*! \enum */
55enum {
56 LOS_MUX_NORMAL = 0, ///< 非递归锁 只有[0.1]两个状态,不做任何特殊的错误检,不进行deadlock detection(死锁检测)
57 LOS_MUX_RECURSIVE = 1, ///< 递归锁 允许同一线程在互斥量解锁前对该互斥量进行多次加锁。递归互斥量维护锁的计数,在解锁次数和加锁次数不相同的情况下,不会释放锁,别的线程就无法加锁此互斥量。
58 LOS_MUX_ERRORCHECK = 2, ///< 进行错误检查,如果一个线程企图对一个已经锁住的mutex进行relock或对未加锁的unlock,将返回一个错误。
59 LOS_MUX_DEFAULT = LOS_MUX_RECURSIVE //鸿蒙系统默认使用递归锁
60};
61/*! \struct LosMuxAttr */
62typedef struct { //互斥锁的属性
63 UINT8 protocol; ///< 协议
64 UINT8 prioceiling; ///< 优先级上限
65 UINT8 type; ///< 类型属性
66 UINT8 reserved; ///< 保留字段
68
69/**
70 * @ingroup los_mux
71 * Mutex object.
72 */
73typedef struct OsMux { //互斥锁结构体
74 UINT32 magic; /**< magic number | 魔法数字*/
75 LosMuxAttr attr; /**< Mutex attribute | 互斥锁属性*/
76 LOS_DL_LIST holdList; /**< The task holding the lock change | 当有任务拿到本锁时,通过holdList节点把锁挂到该任务的锁链表上 */
77 LOS_DL_LIST muxList; /**< Mutex linked list | 等这个锁的任务链表,上面挂的都是任务,注意和holdList的区别. */
78 VOID *owner; /**< The current thread that is locking a mutex | 当前拥有这把锁的任务 */
79 UINT16 muxCount; /**< Times of locking a mutex | 锁定互斥体的次数,递归锁允许多次 */
81
84extern UINT32 LOS_MuxAttrGetType(const LosMuxAttr *attr, INT32 *outType);
85extern UINT32 LOS_MuxAttrSetType(LosMuxAttr *attr, INT32 type);
86extern UINT32 LOS_MuxAttrGetProtocol(const LosMuxAttr *attr, INT32 *protocol);
87extern UINT32 LOS_MuxAttrSetProtocol(LosMuxAttr *attr, INT32 protocol);
88extern UINT32 LOS_MuxAttrGetPrioceiling(const LosMuxAttr *attr, INT32 *prioceiling);
89extern UINT32 LOS_MuxAttrSetPrioceiling(LosMuxAttr *attr, INT32 prioceiling);
90extern UINT32 LOS_MuxSetPrioceiling(LosMux *mutex, INT32 prioceiling, INT32 *oldPrioceiling);
91extern UINT32 LOS_MuxGetPrioceiling(const LosMux *mutex, INT32 *prioceiling);
92extern BOOL LOS_MuxIsValid(const LosMux *mutex);
93
94/**
95 * @ingroup los_mux
96 * @brief Init a mutex.
97 *
98 * @par Description:
99 * This API is used to Init a mutex. A mutex handle is assigned to muxHandle when the mutex is init successfully.
100 * Return LOS_OK on creating successful, return specific error code otherwise.
101 * @attention
102 * <ul>
103 * <li>The total number of mutexes is pre-configured. If there are no available mutexes, the mutex creation fails.</li>
104 * </ul>
105 *
106 * @param mutex [IN] Handle pointer of the successfully init mutex.
107 * @param attr [IN] The mutex attribute.
108 *
109 * @retval #LOS_EINVAL The mutex pointer is NULL.
110 * @retval #LOS_OK The mutex is successfully created.
111 * @par Dependency:
112 * <ul><li>los_mux.h: the header file that contains the API declaration.</li></ul>
113 * @see LOS_MuxDestroy
114 */
115extern UINT32 LOS_MuxInit(LosMux *mutex, const LosMuxAttr *attr);
116
117/**
118 * @ingroup los_mux
119 * @brief Destroy a mutex.
120 *
121 * @par Description:
122 * This API is used to delete a specified mutex. Return LOS_OK on deleting successfully, return specific error code
123 * otherwise.
124 * @attention
125 * <ul>
126 * <li>The specific mutex should be created firstly.</li>
127 * <li>The mutex can be deleted successfully only if no other tasks pend on it.</li>
128 * </ul>
129 *
130 * @param mutex [IN] Handle of the mutex to be deleted.
131 *
132 * @retval #LOS_EINVAL The mutex pointer is NULL.
133 * @retval #LOS_EBUSY Tasks pended on this mutex.
134 * @retval #LOS_EBADF The lock has been destroyed or broken.
135 * @retval #LOS_OK The mutex is successfully deleted.
136 * @par Dependency:
137 * <ul><li>los_mux.h: the header file that contains the API declaration.</li></ul>
138 * @see LOS_MuxInit
139 */
140extern UINT32 LOS_MuxDestroy(LosMux *mutex);
141
142/**
143 * @ingroup los_mux
144 * @brief Wait to lock a mutex.
145 *
146 * @par Description:
147 * This API is used to wait for a specified period of time to lock a mutex.
148 * @attention
149 * <ul>
150 * <li>The specific mutex should be created firstly.</li>
151 * <li>The function fails if the mutex that is waited on is already locked by another thread when the task scheduling
152 * is disabled.</li>
153 * <li>Do not wait on a mutex during an interrupt.</li>
154 * <li>The priority inheritance protocol is supported. If a higher-priority thread is waiting on a mutex, it changes
155 * the priority of the thread that owns the mutex to avoid priority inversion.</li>
156 * <li>A recursive mutex can be locked more than once by the same thread.</li>
157 * <li>Do not call this API in software timer callback. </li>
158 * </ul>
159 *
160 * @param mutex [IN] Handle of the mutex to be waited on.
161 * @param timeout [IN] Waiting time. The value range is [0, LOS_WAIT_FOREVER](unit: Tick).
162 *
163 * @retval #LOS_EINVAL The mutex pointer is NULL, The timeout is zero or Lock status error.
164 * @retval #LOS_EINTR The mutex is being locked during an interrupt.
165 * @retval #LOS_EBUSY Tasks pended on this mutex.
166 * @retval #LOS_EBADF The lock has been destroyed or broken.
167 * @retval #LOS_EDEADLK Mutex error check failed or System locked task scheduling.
168 * @retval #LOS_ETIMEDOUT The mutex waiting times out.
169 * @retval #LOS_OK The mutex is successfully locked.
170 * @par Dependency:
171 * <ul><li>los_mux.h: the header file that contains the API declaration.</li></ul>
172 * @see LOS_MuxInit | LOS_MuxUnlock
173 */
174extern UINT32 LOS_MuxLock(LosMux *mutex, UINT32 timeout);
175
176/**
177 * @ingroup los_mux
178 * @brief Try wait to lock a mutex.
179 *
180 * @par Description:
181 * This API is used to wait for a specified period of time to lock a mutex.
182 * @attention
183 * <ul>
184 * <li>The specific mutex should be created firstly.</li>
185 * <li>The function fails if the mutex that is waited on is already locked by another thread when the task scheduling
186 * is disabled.</li>
187 * <li>Do not wait on a mutex during an interrupt.</li>
188 * <li>The priority inheritance protocol is supported. If a higher-priority thread is waiting on a mutex, it changes
189 * the priority of the thread that owns the mutex to avoid priority inversion.</li>
190 * <li>A recursive mutex can be locked more than once by the same thread.</li>
191 * <li>Do not call this API in software timer callback. </li>
192 * </ul>
193 *
194 * @param mutex [IN] Handle of the mutex to be waited on.
195 *
196 * @retval #LOS_EINVAL The mutex pointer is NULL or Lock status error.
197 * @retval #LOS_EINTR The mutex is being locked during an interrupt.
198 * @retval #LOS_EBUSY Tasks pended on this mutex.
199 * @retval #LOS_EBADF The lock has been destroyed or broken.
200 * @retval #LOS_EDEADLK Mutex error check failed or System locked task scheduling.
201 * @retval #LOS_ETIMEDOUT The mutex waiting times out.
202 * @retval #LOS_OK The mutex is successfully locked.
203 * @par Dependency:
204 * <ul><li>los_mux.h: the header file that contains the API declaration.</li></ul>
205 * @see LOS_MuxInit | LOS_MuxUnlock
206 */
207extern UINT32 LOS_MuxTrylock(LosMux *mutex);
208
209/**
210 * @ingroup los_mux
211 * @brief Release a mutex.
212 *
213 * @par Description:
214 * This API is used to release a specified mutex.
215 * @attention
216 * <ul>
217 * <li>The specific mutex should be created firstly.</li>
218 * <li>Do not release a mutex during an interrupt.</li>
219 * <li>If a recursive mutex is locked for many times, it must be unlocked for the same times to be released.</li>
220 * </ul>
221 *
222 * @param mutex [IN] Handle of the mutex to be released.
223 *
224 * @retval #LOS_EINVAL The mutex pointer is NULL, The timeout is zero or Lock status error.
225 * @retval #LOS_EINTR The mutex is being locked during an interrupt.
226 * @retval #LOS_EBUSY Tasks pended on this mutex.
227 * @retval #LOS_EBADF The lock has been destroyed or broken.
228 * @retval #LOS_EDEADLK Mutex error check failed or System locked task scheduling.
229 * @retval #LOS_ETIMEDOUT The mutex waiting times out.
230 * @retval #LOS_OK The mutex is successfully locked.
231 * @par Dependency:
232 * <ul><li>los_mux.h: the header file that contains the API declaration.</li></ul>
233 * @see LOS_MuxInit | LOS_MuxLock
234 */
235extern UINT32 LOS_MuxUnlock(LosMux *mutex);
236
237#ifdef __cplusplus
238#if __cplusplus
239}
240#endif
241#endif /* __cplusplus */
242
243#endif /* _LOS_MUX_H */
UINT32 LOS_MuxTrylock(LosMux *mutex)
Try wait to lock a mutex.
Definition: los_mux.c:464
UINT32 LOS_MuxInit(LosMux *mutex, const LosMuxAttr *attr)
Init a mutex.
Definition: los_mux.c:262
UINT32 LOS_MuxUnlock(LosMux *mutex)
Release a mutex.
Definition: los_mux.c:559
struct OsMux LosMux
UINT32 LOS_MuxDestroy(LosMux *mutex)
Destroy a mutex.
Definition: los_mux.c:289
UINT32 LOS_MuxLock(LosMux *mutex, UINT32 timeout)
Wait to lock a mutex.
Definition: los_mux.c:437
UINT32 LOS_MuxSetPrioceiling(LosMux *mutex, INT32 prioceiling, INT32 *oldPrioceiling)
设置互斥锁的优先级的上限,老优先级由oldPrioceiling带走
Definition: los_mux.c:200
UINT32 LOS_MuxAttrGetProtocol(const LosMuxAttr *attr, INT32 *protocol)
获取互斥锁的类型属性
Definition: los_mux.c:146
UINT32 LOS_MuxAttrGetPrioceiling(const LosMuxAttr *attr, INT32 *prioceiling)
获取互斥锁属性优先级
Definition: los_mux.c:174
BOOL LOS_MuxIsValid(const LosMux *mutex)
互斥锁是否有效
Definition: los_mux.c:239
UINT32 LOS_MuxAttrDestroy(LosMuxAttr *attr)
????? 销毁互斥属 ,这里啥也没干呀
Definition: los_mux.c:109
UINT32 LOS_MuxGetPrioceiling(const LosMux *mutex, INT32 *prioceiling)
获取互斥锁的优先级的上限
Definition: los_mux.c:229
@ LOS_MUX_ERRORCHECK
进行错误检查,如果一个线程企图对一个已经锁住的mutex进行relock或对未加锁的unlock,将返回一个错误。
Definition: los_mux.h:58
@ LOS_MUX_RECURSIVE
递归锁 允许同一线程在互斥量解锁前对该互斥量进行多次加锁。递归互斥量维护锁的计数,在解锁次数和加锁次数不相同的情况下,不会释放锁,别的线程就无法加锁此互斥量。
Definition: los_mux.h:57
@ LOS_MUX_DEFAULT
Definition: los_mux.h:59
@ LOS_MUX_NORMAL
非递归锁 只有[0.1]两个状态,不做任何特殊的错误检,不进行deadlock detection(死锁检测)
Definition: los_mux.h:56
UINT32 LOS_MuxAttrGetType(const LosMuxAttr *attr, INT32 *outType)
获取互斥锁的类型属性,由outType接走,不送!
Definition: los_mux.c:118
UINT32 LOS_MuxAttrInit(LosMuxAttr *attr)
互斥属性初始化
Definition: los_mux.c:97
UINT32 LOS_MuxAttrSetProtocol(LosMuxAttr *attr, INT32 protocol)
设置互斥锁属性的协议
Definition: los_mux.c:157
@ LOS_MUX_PRIO_INHERIT
Definition: los_mux.h:50
@ LOS_MUX_PRIO_PROTECT
详见: OsMuxPendOp 中的注解,详细说明了LOS_MUX_PRIO_PROTECT的含义
Definition: los_mux.h:52
@ LOS_MUX_PRIO_NONE
线程的优先级和调度不会受到互斥锁影响,先来后到,普通排队.
Definition: los_mux.h:49
UINT32 LOS_MuxAttrSetPrioceiling(LosMuxAttr *attr, INT32 prioceiling)
设置互斥锁属性的优先级的上限
Definition: los_mux.c:187
UINT32 LOS_MuxAttrSetType(LosMuxAttr *attr, INT32 type)
设置互斥锁的类型属性
Definition: los_mux.c:136
unsigned short UINT16
Definition: los_typedef.h:56
signed int INT32
Definition: los_typedef.h:60
unsigned char UINT8
Definition: los_typedef.h:55
unsigned int UINT32
Definition: los_typedef.h:57
size_t BOOL
Definition: los_typedef.h:88
UINT8 protocol
协议
Definition: los_mux.h:63
UINT8 type
类型属性
Definition: los_mux.h:65
UINT8 reserved
保留字段
Definition: los_mux.h:66
UINT8 prioceiling
优先级上限
Definition: los_mux.h:64
Definition: los_mux.h:73
VOID * owner
Definition: los_mux.h:78
UINT32 magic
Definition: los_mux.h:74
LOS_DL_LIST muxList
Definition: los_mux.h:77
UINT16 muxCount
Definition: los_mux.h:79
LOS_DL_LIST holdList
Definition: los_mux.h:76
LosMuxAttr attr
Definition: los_mux.h:75