1 /**
2  * @file os_mutex.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_MUTEX_H_
36 #define _KERNEL_OS_FREERTOS_OS_MUTEX_H_
37 
38 #include "_os_common.h"
39 #include "_os_time.h"
40 #include "_os_thread.h"
41 #include "semphr.h"
42 
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46 
47 /**
48  * @brief Mutex object definition
49  */
50 typedef struct OS_Mutex {
51     SemaphoreHandle_t   handle;
52 } OS_Mutex_t;
53 
54 OS_Status OS_MutexCreate(OS_Mutex_t *mutex);
55 OS_Status OS_MutexDelete(OS_Mutex_t *mutex);
56 OS_Status OS_MutexLock(OS_Mutex_t *mutex, OS_Time_t waitMS);
57 OS_Status OS_MutexUnlock(OS_Mutex_t *mutex);
58 
59 OS_Status OS_RecursiveMutexCreate(OS_Mutex_t *mutex);
60 OS_Status OS_RecursiveMutexLock(OS_Mutex_t *mutex, OS_Time_t waitMS);
61 OS_Status OS_RecursiveMutexUnlock(OS_Mutex_t *mutex);
62 
63 /**
64  * @brief Delete the recursive mutex object
65  * @param[in] mutex Pointer to the recursive mutex object
66  * @retval OS_Status, OS_OK on success
67  */
OS_RecursiveMutexDelete(OS_Mutex_t * mutex)68 static __always_inline OS_Status OS_RecursiveMutexDelete(OS_Mutex_t *mutex)
69 {
70     return OS_MutexDelete(mutex);
71 }
72 
73 /**
74  * @brief Check whether the mutex object is valid or not
75  * @param[in] mutex Pointer to the mutex object
76  * @return 1 on valid, 0 on invalid
77  */
OS_MutexIsValid(OS_Mutex_t * mutex)78 static __always_inline int OS_MutexIsValid(OS_Mutex_t *mutex)
79 {
80     return (mutex->handle != OS_INVALID_HANDLE);
81 }
82 
83 /**
84  * @brief Set the mutex object to invalid state
85  * @param[in] mutex Pointer to the mutex object
86  * @return None
87  */
OS_MutexSetInvalid(OS_Mutex_t * mutex)88 static __always_inline void OS_MutexSetInvalid(OS_Mutex_t *mutex)
89 {
90     mutex->handle = OS_INVALID_HANDLE;
91 }
92 
93 /**
94  * @brief Get the mutex object's owner
95  * @note A mutex object's owner is a thread that locks the mutex
96  * @param[in] mutex Pointer to the mutex object
97  * @return The handle of the thread that locks the mutex object.
98  *         NULL when the mutex is not locked by any thread.
99  */
OS_MutexGetOwner(OS_Mutex_t * mutex)100 static __always_inline OS_ThreadHandle_t OS_MutexGetOwner(OS_Mutex_t *mutex)
101 {
102     return (OS_ThreadHandle_t)xSemaphoreGetMutexHolder(mutex->handle);
103 }
104 
105 #ifdef __cplusplus
106 }
107 #endif
108 
109 #endif /* _KERNEL_OS_FREERTOS_OS_MUTEX_H_ */
110