/* ----------------------------------------------------------------------------
 * Copyright (c) Huawei Technologies Co., Ltd. 2013-2020. All rights reserved.
 * Description: Semaphore HeadFile
 * Author: Huawei LiteOS Team
 * Create: 2013-01-01
 * 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.
 * --------------------------------------------------------------------------- */
/*这段代码实现了对信号量操作的基本功能，包括创建、删除、请求和释放信号量，并对可能出现的错误情况进行了定义和说明。*/
/**
 * @defgroup los_sem Semaphore
 * @ingroup kernel
 */

#ifndef _LOS_SEM_H
#define _LOS_SEM_H

#include "los_base.h"
#include "los_err.h"
#include "los_list.h"
#include "los_task.h"

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

/**
 * @ingroup los_sem
 * Max count of counting semaphores
 */
#define LOS_SEM_COUNT_MAX  0xFFFE/*计数信号量的最大值*/

/**
 * @ingroup los_sem   该宏定义属于信号量模块。
 * Semaphore error code: The memory is insufficient. 内存不足
 *
 * Value: 0x02000700.    以十六进制表示
 *
 * Solution: Allocate more memory. 解决该错误的方法为分配更多的内存。
 */
#define LOS_ERRNO_SEM_NO_MEMORY  LOS_ERRNO_OS_ERROR(LOS_MOD_SEM, 0x00)/*定义一个信号量（Semaphore）的错误码，表示内存不足

/**
 * @ingroup los_sem
 * Semaphore error code: Invalid parameter.
 *
 * Value: 0x02000701.
 *
 * Solution: Change the passed-in invalid parameter value to a valid value.
 */
#define LOS_ERRNO_SEM_INVALID      LOS_ERRNO_OS_ERROR(LOS_MOD_SEM, 0x01)

/**
 * @ingroup los_sem    该宏定义属于信号量模块。
 * Semaphore error code: Null pointer.   该错误码的含义为参数无效。
 *
 * Value: 0x02000702.   以十六进制表示
 *
 * Solution: Change the passed-in null pointer to a valid non-null pointer. 解决该错误的方法：将传入的无效参数值更改为有效值
 */
#define LOS_ERRNO_SEM_PTR_NULL     LOS_ERRNO_OS_ERROR(LOS_MOD_SEM, 0x02)/*这段代码定义了一个信号量模块的错误码，表示参数无效


/**
 * @ingroup los_sem
 * Semaphore error code: No semaphore control structure is available.
 *
 * Value: 0x02000703.
 *
 * Solution: Perform corresponding operations based on the requirements in the code context.解决该错误的方法：根据代码上下文的要求执行相应的操作。
 */
#define LOS_ERRNO_SEM_ALL_BUSY     LOS_ERRNO_OS_ERROR(LOS_MOD_SEM, 0x03)/*定义了一个信号量模块的错误码，表示没有可用的信号量控制结构*/

/**
 * @ingroup los_sem
 * Semaphore error code: Invalid parameter that specifies the timeout interval.
 *
 * Value: 0x02000704.
 *
 * Solution: Change the passed-in parameter value to a valid nonzero value.解决该错误的方法：将传入的参数值更改为有效的非零值
 */
#define LOS_ERRNO_SEM_UNAVAILABLE  LOS_ERRNO_OS_ERROR(LOS_MOD_SEM, 0x04)/*定义了一个信号量模块的错误码，表示指定的超时时间参数无效*/

/**
 * @ingroup los_sem
 * Semaphore error code: The API is called during an interrupt, which is forbidden.
 *
 * Value: 0x02000705.
 *
 * Solution: Do not call the API during an interrupt.解决该错误的方法a:不要在中断期间调用该API
 */
#define LOS_ERRNO_SEM_PEND_INTERR  LOS_ERRNO_OS_ERROR(LOS_MOD_SEM, 0x05)/*定义了一个信号量模块的错误码，表示在中断期间调用了该API，这是被禁止的*/

/**
 * @ingroup los_sem
 * Semaphore error code: The task is unable to request a semaphore because task scheduling is locked.
 *
 * Value: 0x02000706.
 *
 * Solution: Do not call LOS_SemPend when task scheduling is locked.解决该错误的方法：在任务调度被锁定时不要调用LOS_SemPend函数
 */
#define LOS_ERRNO_SEM_PEND_IN_LOCK LOS_ERRNO_OS_ERROR(LOS_MOD_SEM, 0x06)/*定义了一个信号量模块的错误码，表示由于任务调度被锁定，任务无法请求信号量*/

/**
 * @ingroup los_sem
 * Semaphore error code: The request for a semaphore times out.
 *
 * Value: 0x02000707.
 *
 * Solution: Change the passed-in parameter value to the value within the valid range.解决该错误的方法：将传入的参数值更改为有效范围内的值。
 */
#define LOS_ERRNO_SEM_TIMEOUT      LOS_ERRNO_OS_ERROR(LOS_MOD_SEM, 0x07)/*定义了一个信号量模块的错误码，表示请求信号量超时*/

/**
 * @ingroup los_sem
 * Semaphore error code: The times of semaphore release exceed the maximum times permitted.
 *
 * Value: 0x02000708.
 *
 * Solution: Perform corresponding operations based on the requirements in the code context.解决该错误的方法：根据代码上下文的要求执行相应的操作。
 */
#define LOS_ERRNO_SEM_OVERFLOW     LOS_ERRNO_OS_ERROR(LOS_MOD_SEM, 0x08)/*定义了一个信号量模块的错误码，表示信号量释放的次数超过了允许的最大次数

/**
 * @ingroup los_sem
 * Semaphore error code: The queue of the tasks that are waiting on the semaphore control
 *                       structure is not null.
 *
 * Value: 0x02000709.
 *
 * Solution: Delete the semaphore after awaking all tasks that are waiting on the semaphore.解决该错误的方法，即在唤醒等待信号量的所有任务后删除信号量
 */
#define LOS_ERRNO_SEM_PENDED       LOS_ERRNO_OS_ERROR(LOS_MOD_SEM, 0x09)/*定义了一个信号量模块的错误码，表示等待信号量的任务队列不为空*/

/**
 * @ingroup los_sem
 * Semaphore error code: The API is called in system-level callback, which is forbidden.
 *            old usage: The API is called in software timer callback, which is forbidden (LOS_ERRNO_SEM_PEND_SWTERR).
 *
 * Value: 0x0200070A.
 *
 * Solution: Do not call the API in the system-level callback.解决该错误的方法，即不要在系统级回调中调用该API。
 * @deprecated This error code is obsolete since LiteOS 5.0.0.该错误码自LiteOS 5.0.0版本起已经过时。
 */
#define LOS_ERRNO_SEM_PEND_IN_SYSTEM_TASK       LOS_ERRNO_OS_ERROR(LOS_MOD_SEM, 0x0A)/*定义了一个信号量模块的错误码，表示在系统级回调中调用了该API，这是被禁止的*/

/**
 * @ingroup los_sem
 * @brief Create a semaphore.
 *
 * @par Description:
 * This API is used to create a semaphore control structure according to the initial number of available semaphores
 * specified by count and return the ID of this semaphore control structure.
 * @attention
 * None.
 *
 * @param count       [IN] Initial number of available semaphores. The value range is [0, LOS_SEM_COUNT_MAX).
 * @param semHandle   [OUT] ID of the semaphore control structure that is initialized.
 *
 * @retval #LOS_ERRNO_SEM_PTR_NULL  The passed-in semHandle value is NULL.
 * @retval #LOS_ERRNO_SEM_OVERFLOW  The passed-in count value is greater than the maximum number of available
 * semaphores.
 * @retval #LOS_ERRNO_SEM_ALL_BUSY  No semaphore control structure is available.
 * @retval #LOS_OK   The semaphore is successfully created.
 * @par Dependency:
 * <ul><li>los_sem.h: the header file that contains the API declaration.</li></ul>
 * @see LOS_SemDelete | LOS_BinarySemCreate
 * @since Huawei LiteOS V100R001C00
 */
extern UINT32 LOS_SemCreate(UINT16 count, UINT32 *semHandle);/*这段代码声明了一个函数LOS_SemCreate，用于创建信号量。该函数根据传入的初始可用信号量数量，
创建一个信号量控制结构，并返回该信号量控制结构的ID。函数的返回值包括多种情况，如传入的参数为空指针、传入的初始可用信号量数量超过最大数量、没有可用的信号量控制结构等。*/

/**
 * @ingroup los_sem
 * @brief Create a binary semaphore.
 *
 * @par Description:
 * This API is used to create a binary semaphore control structure according to the initial number of
 * available semaphores specified by count and return the ID of this semaphore control structure.
 * @attention
 * None.
 *
 * @param count        [IN] Initial number of available semaphores. The value range is [0, 1].
 * @param semHandle    [OUT] ID of the semaphore control structure that is initialized.
 *
 * @retval #LOS_ERRNO_SEM_PTR_NULL     The passed-in semHandle value is NULL.
 * @retval #LOS_ERRNO_SEM_OVERFLOW     The passed-in count value is greater than the maximum number of
 *                                     available semaphores.
 * @retval #LOS_ERRNO_SEM_ALL_BUSY     No semaphore control structure is available.
 * @retval #LOS_OK   The semaphore is successfully created.
 * @par Dependency:
 * <ul><li>los_sem.h: the header file that contains the API declaration.</li></ul>
 * @see LOS_SemCreate | LOS_SemDelete
 * @since Huawei LiteOS V100R001C00
 */
extern UINT32 LOS_BinarySemCreate(UINT16 count, UINT32 *semHandle);/*声明了一个函数原型 LOS_BinarySemCreate，用于创建一个二进制信号量。*/

/**
 * @ingroup los_sem
 * @brief Delete a semaphore.
 *
 * @par Description:
 * This API is used to delete a semaphore control structure that has an ID specified by semHandle.
 * @attention
 * The specified sem id must be created first before deleting it.
 *
 * @param semHandle   [IN] ID of the semaphore control structure to be deleted. The ID of the semaphore
 *                           control structure is obtained from semaphore creation.
 *
 * @retval #LOS_ERRNO_SEM_INVALID  The passed-in semHandle value is invalid.
 * @retval #LOS_ERRNO_SEM_PENDED   The queue of the tasks that are waiting on the semaphore control structure is
 * not null.
 * @retval #LOS_OK   The semaphore control structure is successfully deleted.
 * @par Dependency:
 * <ul><li>los_sem.h: the header file that contains the API declaration.</li></ul>
 * @see LOS_SemCreate | LOS_BinarySemCreate
 * @since Huawei LiteOS V100R001C00
 */
extern UINT32 LOS_SemDelete(UINT32 semHandle);/*这段代码定义了一个函数LOS_SemDelete，用于删除一个信号量的控制结构*/

/**
 * @ingroup los_sem
 * @brief Request a semaphore.
 *
 * @par Description:
 * This API is used to request a semaphore based on the semaphore control structure ID specified by semHandle and the
 * parameter that specifies the timeout period.
 * @attention
 * <ul>
 * <li>Do not pend sem during an interrupt.</li>
 * <li>Do not pend sem in a system task, such as idle or swtmr task.</li>
 * <li>The specified sem id must be created first. </li>
 * <li>Do not recommend to use this API in software timer callback. </li>
 * </ul>
 *
 * @param semHandle [IN] ID of the semaphore control structure to be requested. The ID of the semaphore control
 *                       structure is obtained from semaphore creation.
 * @param timeout   [IN] Timeout interval for waiting on the semaphore. The value range is [0, 0xFFFFFFFF].
 *                       If the value is set to 0, the semaphore is not waited on. If the value is set to 0xFFFFFFFF,
 *                       the semaphore is waited on forever(unit: Tick).
 *
 * @retval #LOS_ERRNO_SEM_INVALID             The passed-in semHandle value is invalid.
 * @retval #LOS_ERRNO_SEM_UNAVAILABLE         There is no available semaphore resource.
 * @retval #LOS_ERRNO_SEM_PEND_INTERR         The API is called during an interrupt, which is forbidden.
 * @retval #LOS_ERRNO_SEM_PEND_IN_LOCK        The task is unable to request a semaphore because task scheduling
 *                                            is locked.
 * @retval #LOS_ERRNO_SEM_PEND_IN_SYSTEM_TASK The API is called in a system task, such as idle task or software
 *                                            timer, which is forbidden.
 * @retval #LOS_ERRNO_SEM_TIMEOUT             The request for the semaphore times out.
 * @retval #LOS_OK   The semaphore request succeeds.
 * @par Dependency:
 * <ul><li>los_sem.h: the header file that contains the API declaration.</li></ul>
 * @see LOS_SemPost | LOS_SemCreate | LOS_BinarySemCreate
 * @since Huawei LiteOS V100R001C00
 */
extern UINT32 LOS_SemPend(UINT32 semHandle, UINT32 timeout);/*该函数用于请求一个信号量
通过semHandle参数指定要请求的信号量控制结构的ID，该ID是在信号量创建时获得的。
timeout参数指定等待信号量的超时时间，如果timeout值为0，表示不会等待信号量。如果timeout值为0xFFFFFFFF，表示会一直等待信号量。超时时间单位为Tick。
函数返回值为操作结果：
若返回值为LOS_ERRNO_SEM_INVALID，表示传入的semHandle值无效。
若返回值为LOS_ERRNO_SEM_UNAVAILABLE，表示没有可用的信号量资源。
若返回值为LOS_ERRNO_SEM_PEND_INTERR，表示在中断 [Something went wrong, please try again later.]




*/


/**
 * @ingroup los_sem
 * @brief Release a semaphore.
 *
 * @par Description:
 * This API is used to release a semaphore that has a semaphore control structure ID specified by semHandle.
 * @attention
 * The specified sem id must be created first.
 *
 * @param semHandle   [IN] ID of the semaphore control structure to be released.The ID of the semaphore control
 *                         structure is obtained from semaphore creation.
 *
 * @retval #LOS_ERRNO_SEM_INVALID      The passed-in semHandle value is invalid.
 * @retval #LOS_ERRNO_SEM_OVERFLOW     The times of semaphore release exceed the maximum times permitted.
 * @retval #LOS_OK                     The semaphore is successfully released.
 * @par Dependency:
 * <ul><li>los_sem.h: the header file that contains the API declaration.</li></ul>
 * @see LOS_SemPend | LOS_SemCreate | LOS_BinarySemCreate
 * @since Huawei LiteOS V100R001C00
 */
extern UINT32 LOS_SemPost(UINT32 semHandle);/*这段代码定义了一个函数LOS_SemPost，用于释放一个信号量*/
/*LOS_SemPost用于释放由semHandle指定的信号量控制结构。
若释放前没有先创建指定的信号量，则会返回LOS_ERRNO_SEM_INVALID。
若信号量释放的次数超过允许的最大次数，则会返回LOS_ERRNO_SEM_OVERFLOW。
若成功释放信号量，则会返回LOS_OK。
此函数依赖于LOS_SemPend、LOS_SemCreate和LOS_BinarySemCreate函数。*/

#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cplusplus */
#endif /* __cplusplus */

#endif /* _LOS_SEM_H */
