1 /**
2  * @file os_semaphore.h
3  * @author ALLWINNERTECH IOT WLAN Team
4  */
5 
6 /*
7  * Copyright (C) 2017 ALLWINNERTECH TECHNOLOGY CO., LTD. All rights reserved.
8  *
9  *  Redistribution and use in source and binary forms, with or without
10  *  modification, are permitted provided that the following conditions
11  *  are met:
12  *    1. Redistributions of source code must retain the above copyright
13  *       notice, this list of conditions and the following disclaimer.
14  *    2. Redistributions in binary form must reproduce the above copyright
15  *       notice, this list of conditions and the following disclaimer in the
16  *       documentation and/or other materials provided with the
17  *       distribution.
18  *    3. Neither the name of ALLWINNERTECH TECHNOLOGY CO., LTD. nor the names of
19  *       its contributors may be used to endorse or promote products derived
20  *       from this software without specific prior written permission.
21  *
22  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #ifndef _KERNEL_OS_FREERTOS_OS_SEMAPHORE_H_
36 #define _KERNEL_OS_FREERTOS_OS_SEMAPHORE_H_
37 
38 #include "_os_common.h"
39 #include "semphr.h"
40 
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44 
45 /**
46  * @brief Semaphore object definition
47  */
48 typedef struct OS_Semaphore {
49     SemaphoreHandle_t   handle;
50 } OS_Semaphore_t;
51 
52 OS_Status OS_SemaphoreCreate(OS_Semaphore_t *sem, uint32_t initCount, uint32_t maxCount);
53 OS_Status OS_SemaphoreCreateBinary(OS_Semaphore_t *sem);
54 OS_Status OS_SemaphoreDelete(OS_Semaphore_t *sem);
55 OS_Status OS_SemaphoreWait(OS_Semaphore_t *sem, OS_Time_t waitMS);
56 OS_Status OS_SemaphoreRelease(OS_Semaphore_t *sem);
57 OS_Status OS_SemaphoreReset(OS_Semaphore_t *sem);
58 
59 
60 /**
61  * @brief Check whether the semaphore object is valid or not
62  * @param[in] sem Pointer to the semaphore object
63  * @return 1 on valid, 0 on invalid
64  */
OS_SemaphoreIsValid(OS_Semaphore_t * sem)65 static __always_inline int OS_SemaphoreIsValid(OS_Semaphore_t *sem)
66 {
67     return (sem->handle != OS_INVALID_HANDLE);
68 }
69 
70 /**
71  * @brief Set the semaphore object to invalid state
72  * @param[in] sem Pointer to the semaphore object
73  * @return None
74  */
OS_SemaphoreSetInvalid(OS_Semaphore_t * sem)75 static __always_inline void OS_SemaphoreSetInvalid(OS_Semaphore_t *sem)
76 {
77     sem->handle = OS_INVALID_HANDLE;
78 }
79 
80 #ifdef __cplusplus
81 }
82 #endif
83 
84 #endif /* _KERNEL_OS_FREERTOS_OS_SEMAPHORE_H_ */
85