更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
los_sem.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_sem Semaphore
34 * @ingroup kernel
35 */
36
37#ifndef _LOS_SEM_H
38#define _LOS_SEM_H
39
40#include "los_base.h"
41#include "los_err.h"
42#include "los_list.h"
43#include "los_task.h"
44
45#ifdef __cplusplus
46#if __cplusplus
47extern "C" {
48#endif /* __cplusplus */
49#endif /* __cplusplus */
50
51/**
52 * @ingroup los_sem
53 * Semaphore error code: The memory is insufficient.
54 *
55 * Value: 0x02000700
56 *
57 * Solution: Allocate more memory.
58 */
59#define LOS_ERRNO_SEM_NO_MEMORY LOS_ERRNO_OS_ERROR(LOS_MOD_SEM, 0x00)
60
61/**
62 * @ingroup los_sem
63 * Semaphore error code: Invalid parameter.
64 *
65 * Value: 0x02000701
66 *
67 * Solution: Change the passed-in invalid parameter value to a valid value.
68 */
69#define LOS_ERRNO_SEM_INVALID LOS_ERRNO_OS_ERROR(LOS_MOD_SEM, 0x01)
70
71/**
72 * @ingroup los_sem
73 * Semaphore error code: Null pointer.
74 *
75 * Value: 0x02000702
76 *
77 * Solution: Change the passed-in null pointer to a valid non-null pointer.
78 */
79#define LOS_ERRNO_SEM_PTR_NULL LOS_ERRNO_OS_ERROR(LOS_MOD_SEM, 0x02)
80
81/**
82 * @ingroup los_sem
83 * Semaphore error code: No semaphore control structure is available.
84 *
85 * Value: 0x02000703
86 *
87 * Solution: Perform corresponding operations based on the requirements in the code context.
88 */
89#define LOS_ERRNO_SEM_ALL_BUSY LOS_ERRNO_OS_ERROR(LOS_MOD_SEM, 0x03)
90
91/**
92 * @ingroup los_sem
93 * Semaphore error code: Invalid parameter that specifies the timeout interval.
94 *
95 * Value: 0x02000704
96 *
97 *
98 * Solution: Change the passed-in parameter value to a valid nonzero value.
99 */
100#define LOS_ERRNO_SEM_UNAVAILABLE LOS_ERRNO_OS_ERROR(LOS_MOD_SEM, 0x04)
101
102/**
103 * @ingroup los_sem
104 * Semaphore error code: The API is called during an interrupt, which is forbidden.
105 *
106 * Value: 0x02000705
107 *
108 * Solution: Do not call the API during an interrupt.
109 */
110#define LOS_ERRNO_SEM_PEND_INTERR LOS_ERRNO_OS_ERROR(LOS_MOD_SEM, 0x05)
111
112/**
113 * @ingroup los_sem
114 * Semaphore error code: The task is unable to request a semaphore because task scheduling is locked.
115 *
116 * Value: 0x02000706
117 *
118 * Solution: Do not call LOS_SemPend when task scheduling is locked.
119 */
120#define LOS_ERRNO_SEM_PEND_IN_LOCK LOS_ERRNO_OS_ERROR(LOS_MOD_SEM, 0x06)
121
122/**
123 * @ingroup los_sem
124 * Semaphore error code: The request for a semaphore times out.
125 *
126 * Value: 0x02000707
127 *
128 * Solution: Change the passed-in parameter value to the value within the valid range.
129 */
130#define LOS_ERRNO_SEM_TIMEOUT LOS_ERRNO_OS_ERROR(LOS_MOD_SEM, 0x07)
131
132/**
133 * @ingroup los_sem
134 * Semaphore error code: The times of semaphore release exceed the maximum times permitted.
135 *
136 * Value: 0x02000708
137 *
138 * Solution: Perform corresponding operations based on the requirements in the code context.
139 */
140#define LOS_ERRNO_SEM_OVERFLOW LOS_ERRNO_OS_ERROR(LOS_MOD_SEM, 0x08)
141
142/**
143 * @ingroup los_sem
144 * Semaphore error code: The queue of the tasks that are waiting on the semaphore control structure is not null.
145 *
146 * Value: 0x02000709
147 *
148 * Solution: Delete the semaphore after awaking all tasks that are waiting on the semaphore.
149 */
150#define LOS_ERRNO_SEM_PENDED LOS_ERRNO_OS_ERROR(LOS_MOD_SEM, 0x09)
151
152/**
153* @ingroup los_sem
154* Semaphore error code: The API is called in system-level callback, which is forbidden.
155* old usage: The API is called in software timer callback, which is forbidden. (LOS_ERRNO_SEM_PEND_SWTERR)
156* Value: 0x0200070A
157*
158* Solution: Do not call the API during an interrupt.
159*/
160#define LOS_ERRNO_SEM_PEND_IN_SYSTEM_TASK LOS_ERRNO_OS_ERROR(LOS_MOD_SEM, 0x0A)
161
162/**
163 * @ingroup los_sem
164 * Maximum number of binary semaphores.
165 *
166 */
167#define OS_SEM_BINARY_COUNT_MAX 1
168
169/**
170 * @ingroup los_sem
171 * @brief Create a semaphore.
172 *
173 * @par Description:
174 * This API is used to create a semaphore control structure according to the initial number of available semaphores
175 * specified by count and return the ID of this semaphore control structure.
176 * @attention
177 * <ul>
178 * <li>None.</li>
179 * </ul>
180 *
181 * @param count [IN] Initial number of available semaphores. The value range is [0, OS_SEM_COUNT_MAX].
182 * @param semHandle [OUT] ID of the semaphore control structure that is initialized.
183 *
184 * @retval #LOS_ERRNO_SEM_PTR_NULL The passed-in semHandle value is NULL.
185 * @retval #LOS_ERRNO_SEM_OVERFLOW The passed-in count value is greater than the maximum number of available
186 * semaphores.
187 * @retval #LOS_ERRNO_SEM_ALL_BUSY No semaphore control structure is available.
188 * @retval #LOS_OK The semaphore is successfully created.
189 * @par Dependency:
190 * <ul><li>los_sem.h: the header file that contains the API declaration.</li></ul>
191 * @see LOS_SemDelete
192 */
193extern UINT32 LOS_SemCreate(UINT16 count, UINT32 *semHandle);
194
195/**
196 * @ingroup los_sem
197 * @brief Create a binary semaphore.
198 *
199 * @par Description:
200 * This API is used to create a binary semaphore control structure according to the initial number of
201 * available semaphores specified by count and return the ID of this semaphore control structure.
202 * @attention
203 * <ul>
204 * <li>None.</li>
205 * </ul>
206 *
207 * @param count [IN] Initial number of available semaphores. The value range is [0, 1].
208 * @param semHandle [OUT] ID of the semaphore control structure that is initialized.
209 *
210 * @retval #LOS_ERRNO_SEM_PTR_NULL The passed-in semHandle value is NULL.
211 * @retval #LOS_ERRNO_SEM_OVERFLOW The passed-in count value is greater than the maximum number of
212 * available semaphores.
213 * @retval #LOS_ERRNO_SEM_ALL_BUSY No semaphore control structure is available.
214 * @retval #LOS_OK The semaphore is successfully created.
215 * @par Dependency:
216 * <ul><li>los_sem.h: the header file that contains the API declaration.</li></ul>
217 * @see LOS_SemDelete
218 */
219extern UINT32 LOS_BinarySemCreate(UINT16 count, UINT32 *semHandle);
220
221/**
222 * @ingroup los_sem
223 * @brief Delete a semaphore.
224 *
225 * @par Description:
226 * This API is used to delete a semaphore control structure that has an ID specified by semHandle.
227 * @attention
228 * <ul>
229 * <li>The specified sem id must be created first. </li>
230 * </ul>
231 *
232 * @param semHandle [IN] ID of the semaphore control structure to be deleted. The ID of the semaphore
233 * control structure is obtained from semaphore creation.
234 *
235 * @retval #LOS_ERRNO_SEM_INVALID The passed-in semHandle value is invalid.
236 * @retval #LOS_ERRNO_SEM_PENDED The queue of the tasks that are waiting on the semaphore control structure is
237 * not null.
238 * @retval #LOS_OK The semaphore control structure is successfully deleted.
239 * @par Dependency:
240 * <ul><li>los_sem.h: the header file that contains the API declaration.</li></ul>
241 * @see LOS_SemCreate
242 */
243extern UINT32 LOS_SemDelete(UINT32 semHandle);
244
245/**
246 * @ingroup los_sem
247 * @brief Request a semaphore.
248 *
249 * @par Description:
250 * This API is used to request a semaphore based on the semaphore control structure ID specified by semHandle and the
251 * parameter that specifies the timeout period.
252 * @attention
253 * <ul>
254 * <li>The specified sem id must be created first. </li>
255 * <li>Do not call this API in software timer callback. </li>
256 * </ul>
257 *
258 * @param semHandle [IN] ID of the semaphore control structure to be requested. The ID of the semaphore control
259 * structure is obtained from semaphore creation.
260 * @param timeout [IN] Timeout interval for waiting on the semaphore. The value range is [0, 0xFFFFFFFF].
261 * If the value is set to 0, the semaphore is not waited on. If the value is set to 0xFFFFFFFF,
262 * the semaphore is waited on forever(unit: Tick).
263 *
264 * @retval #LOS_ERRNO_SEM_INVALID The passed-in semHandle value is invalid.
265 * @retval #LOS_ERRNO_SEM_UNAVAILABLE There is no available semaphore resource.
266 * @retval #LOS_ERRNO_SEM_PEND_INTERR The API is called during an interrupt, which is forbidden.
267 * @retval #LOS_ERRNO_SEM_PEND_IN_LOCK The task is unable to request a semaphore because task scheduling is locked.
268 * @retval #LOS_ERRNO_SEM_TIMEOUT The request for the semaphore times out.
269 * @retval #LOS_OK The semaphore request succeeds.
270 * @par Dependency:
271 * <ul><li>los_sem.h: the header file that contains the API declaration.</li></ul>
272 * @see LOS_SemPost | LOS_SemCreate
273 */
274extern UINT32 LOS_SemPend(UINT32 semHandle, UINT32 timeout);
275
276/**
277 * @ingroup los_sem
278 * @brief Release a semaphore.
279 *
280 * @par Description:
281 * This API is used to release a semaphore that has a semaphore control structure ID specified by semHandle.
282 * @attention
283 * <ul>
284 * <li>The specified sem id must be created first. </li>
285 * </ul>
286 *
287 * @param semHandle [IN] ID of the semaphore control structure to be released.The ID of the semaphore control
288 * structure is obtained from semaphore creation.
289 *
290 * @retval #LOS_ERRNO_SEM_INVALID The passed-in semHandle value is invalid.
291 * @retval #LOS_ERRNO_SEM_OVERFLOW The times of semaphore release exceed the maximum times permitted.
292 * @retval #LOS_OK The semaphore is successfully released.
293 * @par Dependency:
294 * <ul><li>los_sem.h: the header file that contains the API declaration.</li></ul>
295 * @see LOS_SemPend | LOS_SemCreate
296 */
297extern UINT32 LOS_SemPost(UINT32 semHandle);
298
299#ifdef __cplusplus
300#if __cplusplus
301}
302#endif /* __cplusplus */
303#endif /* __cplusplus */
304
305#endif /* _LOS_SEM_H */
UINT32 LOS_SemCreate(UINT16 count, UINT32 *semHandle)
Create a semaphore.
Definition: los_sem.c:177
UINT32 LOS_BinarySemCreate(UINT16 count, UINT32 *semHandle)
Create a binary semaphore.
Definition: los_sem.c:182
UINT32 LOS_SemPost(UINT32 semHandle)
Release a semaphore.
Definition: los_sem.c:315
UINT32 LOS_SemPend(UINT32 semHandle, UINT32 timeout)
Request a semaphore.
Definition: los_sem.c:226
UINT32 LOS_SemDelete(UINT32 semHandle)
Delete a semaphore.
Definition: los_sem.c:187
双向链表由内联函数实现 http://weharmonyos.com/openharmony/zh-cn/device-dev/kernel/kernel-small-apx-dll....
unsigned short UINT16
Definition: los_typedef.h:56
unsigned int UINT32
Definition: los_typedef.h:57