1 /*
2 * Copyright (C) 2021 HiSilicon (Shanghai) Technologies CO., LIMITED.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19 #ifndef __OAL_LINUX_SEMAPHORE_H__
20 #define __OAL_LINUX_SEMAPHORE_H__
21
22 /* ****************************************************************************
23 1 其他头文件包含
24 **************************************************************************** */
25 #if (_PRE_OS_VERSION_LINUX == _PRE_OS_VERSION)
26 #include <linux/semaphore.h>
27 #endif
28
29 #if (_PRE_OS_VERSION_LITEOS == _PRE_OS_VERSION)
30 #include <los_sem_pri.h>
31 #include <los_sem.h>
32 #endif
33
34 #ifdef __cplusplus
35 #if __cplusplus
36 extern "C" {
37 #endif
38 #endif
39
40 /* ****************************************************************************
41 2 STRUCT定义
42 **************************************************************************** */
43 #if (_PRE_OS_VERSION_LINUX == _PRE_OS_VERSION)
44 typedef struct semaphore oal_semaphore_stru;
45 #endif
46
47 #if (_PRE_OS_VERSION_LITEOS == _PRE_OS_VERSION)
48 #define SEM_CB_S LosSemCB
49 #define usSemID semID
50 typedef SEM_CB_S oal_semaphore_stru;
51 #endif
52
53 /* ****************************************************************************
54 10 函数声明
55 **************************************************************************** */
56 #if (_PRE_OS_VERSION_LINUX == _PRE_OS_VERSION)
oal_sema_init(oal_semaphore_stru * sem,hi_s32 val)57 static inline hi_void oal_sema_init(oal_semaphore_stru *sem, hi_s32 val)
58 {
59 sema_init(sem, val);
60 }
61
62
oal_up(oal_semaphore_stru * sem)63 static inline hi_void oal_up(oal_semaphore_stru *sem)
64 {
65 up(sem);
66 }
67
oal_down(oal_semaphore_stru * sem)68 static inline hi_void oal_down(oal_semaphore_stru *sem)
69 {
70 down(sem);
71 }
72
oal_down_timeout(oal_semaphore_stru * sem,hi_s32 timeout)73 static inline hi_s32 oal_down_timeout(oal_semaphore_stru *sem, hi_s32 timeout)
74 {
75 return down_timeout(sem, timeout);
76 }
77
oal_down_interruptible(oal_semaphore_stru * sem)78 static inline hi_s32 oal_down_interruptible(oal_semaphore_stru *sem)
79 {
80 return down_interruptible(sem);
81 }
82
oal_down_trylock(oal_semaphore_stru * sem)83 static inline hi_s32 oal_down_trylock(oal_semaphore_stru *sem)
84 {
85 return down_trylock(sem);
86 }
87
oal_sema_destroy(const oal_semaphore_stru * pst_sem)88 static inline hi_s32 oal_sema_destroy(const oal_semaphore_stru *pst_sem)
89 {
90 return 0;
91 }
92 #endif
93
94 #if (_PRE_OS_VERSION_LITEOS == _PRE_OS_VERSION)
oal_sema_init(oal_semaphore_stru * pst_sem,hi_u16 us_val)95 static inline hi_void oal_sema_init(oal_semaphore_stru *pst_sem, hi_u16 us_val)
96 {
97 hi_u32 ul_semhandle;
98 if (pst_sem == HI_NULL) {
99 oal_io_print1("[%s]pst_sem is null!\n", (uintptr_t) __func__);
100 return;
101 }
102
103 if (LOS_OK != LOS_SemCreate(us_val, &ul_semhandle)) {
104 oal_io_print0("LOS_SemCreate failed!\n");
105 return;
106 }
107 *pst_sem = *(GET_SEM(ul_semhandle));
108 }
109
oal_up(const oal_semaphore_stru * pst_sem)110 static inline hi_void oal_up(const oal_semaphore_stru *pst_sem)
111 {
112 if (pst_sem == HI_NULL) {
113 oal_io_print1("[%s]pst_sem is null!\n", (uintptr_t) __func__);
114 return;
115 }
116
117 if (0 != LOS_SemPost(pst_sem->usSemID)) {
118 oal_io_print0("LOS_SemPost failed!\n");
119 return;
120 }
121 }
122
oal_down(const oal_semaphore_stru * pst_sem)123 static inline hi_void oal_down(const oal_semaphore_stru *pst_sem)
124 {
125 if (pst_sem == HI_NULL) {
126 oal_io_print1("[%s]pst_sem is null!\n", __func__);
127 return;
128 }
129 if (0 != LOS_SemPend(pst_sem->usSemID, 0xFFFFFFFF)) {
130 oal_io_print0("LOS_SemPend failed!\n");
131 return;
132 }
133 }
134
oal_down_timeout(const oal_semaphore_stru * pst_sem,hi_u32 ul_timeout)135 static inline hi_s32 oal_down_timeout(const oal_semaphore_stru *pst_sem, hi_u32 ul_timeout)
136 {
137 hi_u32 ul_reval;
138 if (pst_sem == HI_NULL) {
139 oal_io_print1("[%s]pst_sem is null!\n", (uintptr_t) __func__);
140 return -1;
141 }
142
143 ul_reval = LOS_SemPend(pst_sem->usSemID, ul_timeout);
144 if (ul_reval != 0) {
145 return -1;
146 }
147 return 0;
148 }
149
oal_down_interruptible(const oal_semaphore_stru * pst_sem)150 static inline hi_s32 oal_down_interruptible(const oal_semaphore_stru *pst_sem)
151 {
152 hi_u32 ul_reval;
153 if (pst_sem == HI_NULL) {
154 oal_io_print1("[%s]pst_sem is null!\n", __func__);
155 return -1;
156 }
157 ul_reval = LOS_SemPend(pst_sem->usSemID, 0xFFFFFFFF);
158 if (ul_reval != 0) {
159 return -1;
160 }
161 return 0;
162 }
163
oal_down_trylock(const oal_semaphore_stru * pst_sem)164 static inline hi_s32 oal_down_trylock(const oal_semaphore_stru *pst_sem)
165 {
166 hi_u32 ul_reval;
167 if (pst_sem == HI_NULL) {
168 oal_io_print1("[%s]pst_sem is null!\n", __func__);
169 return -1;
170 }
171
172 ul_reval = LOS_SemPend(pst_sem->usSemID, 0);
173 if (ul_reval != 0) {
174 return -1;
175 }
176 return 0;
177 }
178
oal_sema_destroy(const oal_semaphore_stru * pst_sem)179 static inline hi_s32 oal_sema_destroy(const oal_semaphore_stru *pst_sem)
180 {
181 hi_u32 ul_reval;
182 if (pst_sem == HI_NULL) {
183 oal_io_print1("[%s]pst_sem is null!\n", __func__);
184 return -1;
185 }
186
187 ul_reval = LOS_SemDelete(pst_sem->usSemID);
188 if (ul_reval != 0) {
189 return -1;
190 }
191 return 0;
192 }
193 #endif
194
195 #ifdef __cplusplus
196 #if __cplusplus
197 }
198 #endif
199 #endif
200
201 #endif /* end of oal_completion.h */
202